MediaTek DaVinci Assistant API 介紹
MediaTek DaVinci Assistant API 介紹
MediaTek DaVinci 推出 Assistant API,讓您在達哥平台上開發的 Assistant 可以串接進各式各樣的環境當中,進而達到達哥 Assistant 可以在不同環境、裝置服務您的需求。
如何分享製作好的 Assistant
當我們在達哥上建立好 Assistant 後,我們有兩種方式將 Assistant 分享給其他使用者使用:
Share 該 Assistant 給對方
提供該 Assistant 的『Assistant ID 』與『發自 API Key 』給對方
若想收回該 Assistant 的服務時,僅在自己的 API Key 面板上刪除該 API Key 即可
使用 https://www.gradio.app/playground?demo=Hello_World&code=IyBQbGVhc2UgdXBkYXRlIHlvdXIgYXNzaXN0YW50IGlkIGFuZCBhcGkga2V5IGhlcmU6CkFQSV9LRVkgPSAiIgpBU1NJU1RBTlRfSUQgPSAiIgpBU1NJU1RBTlRfQVBJID0gImh0dHBzOi8vcHJvZC5kdmNib3QubmV0L2FwaS9hc3N0cy92MSIKCmltcG9ydCBtaWNyb3BpcDsgYXdhaXQgbWljcm9waXAuaW5zdGFsbCgnb3BlbmFpPT0xLjM5LjAnKTsgZnJvbSBweW9kaWRlLmh0dHAgaW1wb3J0IHB5ZmV0Y2g7IGltcG9ydCBodHRweDsgaW1wb3J0IGdyYWRpbyBhcyBncgpmcm9tIG9wZW5haSBpbXBvcnQgQXN5bmNPcGVuQUkKZnJvbSBkYXRldGltZSBpbXBvcnQgZGF0ZXRpbWUKaW1wb3J0IGpzb24KCmNsYXNzIFRyYW5zcG9ydChodHRweC5Bc3luY0Jhc2VUcmFuc3BvcnQpOgogICAgYXN5bmMgZGVmIGhhbmRsZV9hc3luY19yZXF1ZXN0KHNlbGYsIHJlcXVlc3Q6IGh0dHB4LlJlcXVlc3QpOgogICAgICAgIHJlc3AgPSBhd2FpdCBweWZldGNoKHN0cihyZXF1ZXN0LnVybCksIG1ldGhvZD1yZXF1ZXN0Lm1ldGhvZCwgaGVhZGVycz1kaWN0KHJlcXVlc3QuaGVhZGVycy5pdGVtcygpKSwgYm9keT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yLm1vZGVsX2R1bXBfanNvbigpCiAgICAgICAgbXNncyA9IGF3YWl0IGNsaWVudC5iZXRhLnRocmVhZHMubWVzc2FnZXMubGlzdCh0aHJlYWRfaWQ9dGhyZWFkLmlkLCBvcmRlcj0nZGVzYycpCiAgICAgICAgYXdhaXQgY2xpZW50LmJldGEudGhyZWFkcy5kZWxldGUodGhyZWFkX2lkPXRocmVhZC5pZCkKICAgICAgICByZXR1cm4gbXNncy5kYXRhWzBdLmNvbnRlbnRbMF0udGV4dC52YWx1ZQogICAgZGVtbyA9IGdyLkNoYXRJbnRlcmZhY2Uoc2VuZF9tZXNzYWdlKQogICAgZGVtby5sYXVuY2goKQo= 進行 preview 測試
當我們在達哥平台上創建完 Assistant 時,若想進行 preview 測試,我們提供與 gradio 串接的 sample code。以下為教學步驟:
取得 User API key:
點選達哥面板左下角的
Settings
按鈕點選
Assistant API Key
後,點選+ API Key
按鈕新增複製
API Key
取得 Assistant ID:
選取欲 preview 的 Assistant,點選
Setting
按鈕選取
Advanced
tab,複製Assistant ID
Demo
Text:
Image:
替換掉對應的
API_KEY
與ASSISTANT_ID
在輸入框輸入 image url 即可
如果您想要使用本機影像,您可以使用下列 Python 程式碼將它轉換成 base64,以便將其傳遞至 API。 或者您可以使用線上工具將影像檔轉成 base64。
import base64 from mimetypes import guess_type # Function to encode a local image into data URL def local_image_to_data_url(image_path): # Guess the MIME type of the image based on the file extension mime_type, _ = guess_type(image_path) if mime_type is None: mime_type = 'application/octet-stream' # Default MIME type if none is found # Read and encode the image file with open(image_path, "rb") as image_file: base64_encoded_data = base64.b64encode(image_file.read()).decode('utf-8') # Construct the data URL return f"data:{mime_type};base64,{base64_encoded_data}" # Example usage image_path = '<path_to_image>' data_url = local_image_to_data_url(image_path) print("Data URL:", data_url)
得到 base64 字串後,在輸入框輸入以下格式即可
"data:image/jpeg;base64,<your_image_data>"
Gradio Sample code (for backup),如果上面 Gradio 連結打不開,可以把這段 paste 進 Gradio playground
# Please update your assistant id and api key here:
API_KEY = ""
ASSISTANT_ID = ""
ASSISTANT_API = "https://prod.dvcbot.net/api/assts/v1"
import micropip; await micropip.install('openai==1.39.0'); from pyodide.http import pyfetch; import httpx; import gradio as gr
from openai import AsyncOpenAI
from datetime import datetime
import json
class Transport(httpx.AsyncBaseTransport):
async def handle_async_request(self, request: httpx.Request):
resp = await pyfetch(str(request.url), method=request.method, headers=dict(request.headers.items()), body=json.dumps(json.loads(request.content), ensure_ascii=False).encode() if request.method != 'GET' and request.method != 'DELETE' else None)
return httpx.Response(resp.status, headers=resp.headers, stream=httpx.ByteStream(await resp.bytes()))
client = AsyncOpenAI(base_url=ASSISTANT_API, api_key=API_KEY, http_client=httpx.AsyncClient(transport=Transport()))
if __name__ == "__main__":
async def send_message(message, history):
thread = await client.beta.threads.create(messages=[{"role": "user" if i == 0 else "assistant", "content": c} for p in history for i, c in enumerate(p)])
await client.beta.threads.messages.create(thread_id=thread.id, role='user', content=message)
run = await client.beta.threads.runs.create_and_poll(thread_id=thread.id, assistant_id=ASSISTANT_ID, additional_instructions=f"\nThe current time is: {datetime.now()}", timeout=2.0)
while run.status == 'requires_action' and run.required_action:
outputs = []
for call in run.required_action.submit_tool_outputs.tool_calls:
resp = await client._client.post(ASSISTANT_API+'/pluginapi', params={"tid": thread.id, "aid": ASSISTANT_ID, "pid": call.function.name}, headers={"Authorization": "Bearer " + API_KEY}, json=json.loads(call.function.arguments))
outputs.append({"tool_call_id": call.id, "output": resp.text[:8000]})
run = await client.beta.threads.runs.submit_tool_outputs_and_poll(run_id=run.id, thread_id=thread.id, tool_outputs=outputs, timeout=2.0)
if run.status == 'failed' and run.last_error:
return run.last_error.model_dump_json()
msgs = await client.beta.threads.messages.list(thread_id=thread.id, order='desc')
await client.beta.threads.delete(thread_id=thread.id)
return msgs.data[0].content[0].text.value
demo = gr.ChatInterface(send_message)
demo.launch()
MediaTek DaVinci Assistant API 使用教學
Curl
常用 API curl 範例
請注意,如果不是用 sandbox 進行 PoC,而是自行部屬,請將所有 api 使用中的 url “https://prod.dvcbot.net“ 修改成自行部屬的 url。
create thread
create msg
create run (non streaming)
call plugin api
submit tool outputs to run
Wrap up: use curl to write a script
將上面常用的 curl api 組合在一起,即可用 bash 完成一個陽春版的達哥對話腳本:
要先拿到 Assistant ID & User API key
export
API_KEY
、ASSISTANT_ID
以及你的INPUT_MSG
執行以下腳本 (請先確保環境有安裝 jq 套件)
mac:
brew install jq
ubuntu:
apt-get install jq
cent os:
yum install jq
即可看到結果如下
Image
要先拿到 Assistant ID & User API key
export
API_KEY
、ASSISTANT_ID
以及你的INPUT_MSG
IMAGE_URL
格式參考上方 Gradio image 範例
執行以下腳本 (請先確保環境有安裝 jq 套件)
mac:
brew install jq
ubuntu:
apt-get install jq
cent os:
yum install jq
即可看到結果如下
Python
Text or image as Input
Text & image as input (Streaming)
Voice mode
(Coming Soon)