/
MediaTek DaVinci Assistant API 介紹

MediaTek DaVinci Assistant API 介紹

 

MediaTek DaVinci Assistant API 介紹

MediaTek DaVinci 推出 Assistant API,讓您在達哥平台上開發的 Assistant 可以串接進各式各樣的環境當中,進而達到達哥 Assistant 可以在不同環境、裝置服務您的需求。

如何分享製作好的 Assistant

當我們在達哥上建立好 Assistant 後,我們有兩種方式將 Assistant 分享給其他使用者使用:

  1. Share 該 Assistant 給對方

  2. 提供該 Assistant 的『Assistant ID 』與『發自 API Key 』給對方

    1. 若想收回該 Assistant 的服務時,僅在自己的 API Key 面板上刪除該 API Key 即可

使用 https://www.gradio.app/ 進行 preview 測試

 

當我們在達哥平台上創建完 Assistant 時,若想進行 preview 測試,我們提供與 gradio 串接的 sample code (參考下面的 Gradio Sample code )。以下為教學步驟:

  1. 取得 User API key:

    1. 點選達哥面板左下角的 Settings按鈕

      image-20241202-014659.png
    2. 點選 Assistant API Key 後,點選 + API Key 按鈕新增

      image-20241202-014352.png
    3. 複製 API Key

  2. 取得 Assistant ID

    1. 選取欲 preview 的 Assistant,點選 Setting 按鈕

    2. 選取 Advanced tab,複製 Assistant ID

  3. Demo

    1. Text:

      1. 點選https://www.gradio.app/playground?demo=Hello_World&code=IyBQbGVhc2UgdXBkYXRlIHlvdXIgYXNzaXN0YW50IGlkIGFuZCBhcGkga2V5IGhlcmU6CkFQSV9LRVkgPSAiIgpBU1NJU1RBTlRfSUQgPSAiIgpBU1NJU1RBTlRfQVBJID0gImh0dHBzOi8vcHJvZC5kdmNib3QubmV0L2FwaS9hc3N0cy92MSIKCmltcG9ydCBtaWNyb3BpcDsgYXdhaXQgbWljcm9waXAuaW5zdGFsbCgnb3BlbmFpPT0xLjM5LjAnKTsgZnJvbSBweW9kaWRlLmh0dHAgaW1wb3J0IHB5ZmV0Y2g7IGltcG9ydCBodHRweDsgaW1wb3J0IGdyYWRpbyBhcyBncgpmcm9tIG9wZW5haSBpbXBvcnQgQXN5bmNPcGVuQUkKZnJvbSBkYXRldG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0IjogcmVzcC50ZXh0Wzo4MDAwXX0pCiAgICAgICAgICAgIHJ1biA9IGF3YWl0IGNsaWVudC5iZXRhLnRocmVhZHMucnVucy5zdWJtaXRfdG9vbF9vdXRwdXRzX2FuZF9wb2xsKHJ1bl9pZD1ydW4uaWQsIHRocmVhZF9pZD10aHJlYWQuaWQsIHRvb2xfb3V0cHV0cz1vdXRwdXRzLCB0aW1lb3V0PTIuMCkKICAgICAgICBpZiBydW4uc3RhdHVzID09ICdmYWlsZWQnIGFuZCBydW4ubGFzdF9lcnJvcjoKICAgICAgICAgICAgcmV0dXJuIHJ1bi5sYXN0X2Vycm9yLm1vZGVsX2R1bXBfanNvbigpCiAgICAgICAgbXNncyA9IGF3YWl0IGNsaWVudC5iZXRhLnRocmVhZHMubWVzc2FnZXMubGlzdCh0aHJlYWRfaWQ9dGhyZWFkLmlkLCBvcmRlcj0nZGVzYycpCiAgICAgICAgYXdhaXQgY2xpZW50LmJldGEudGhyZWFkcy5kZWxldGUodGhyZWFkX2lkPXRocmVhZC5pZCkKICAgICAgICByZXR1cm4gbXNncy5kYXRhWzBdLmNvbnRlbnRbMF0udGV4dC52YWx1ZQogICAgZGVtbyA9IGdyLkNoYXRJbnRlcmZhY2Uoc2VuZF9tZXNzYWdlKQogICAgZGVtby5sYXVuY2goKQo=

      2. 替換掉對應的 API_KEYASSISTANT_ID

         

      3. 在輸入框輸入即可

         

    2. Image:

      1. 點選https://www.gradio.app/playground?demo=Hello_World&code=IyBQbGVhc2UgdXBkYXRlIHlvdXIgYXNzaXN0YW50IGlkIGFuZCBhcGkga2V5IGhlcmU6CkFQSV9LRVkgPSAiIgpBU1NJU1RBTlRfSUQgPSAiIgpBU1NJU1RBTlRfQVBJID0gImh0dHBzOi8vcHJvZC5kdmNib3QubmV0L2FwaS9hc3N0cy92MSIKCmltcG9ydCBtaWNyb3BpcDsgYXdhaXQgbWljcm9waXAuaW5zdGFsbCgnb3BlbmFpPT0xLjM5LjAnKTsgZnJvbSBweW9k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iYWlkIjogQVNTSVNUQU5UX0lELCAicGlkIjogY2FsbC5mdW5jdGlvbi5uYW1lfSwgaGVhZGVycz17IkF1dGhvcml6YXRpb24iOiAiQmVhcmVyICIgKyBBUElfS0VZfSwganNvbj1qc29uLmxvYWRzKGNhbGwuZnVuY3Rpb24uYXJndW1lbnRzKSkKICAgICAgICAgICAgICAgIG91dHB1dHMuYXBwZW5kKHsidG9vbF9jYWxsX2lkIjogY2FsbC5pZCwgIm91dHB1dCI6IHJlc3AudGV4dFs6ODAwMF19KQogICAgICAgICAgICBydW4gPSBhd2FpdCBjbGllbnQuYmV0YS50aHJlYWRzLnJ1bnMuc3VibWl0X3Rvb2xfb3V0cHV0c19hbmRfcG9sbChydW5faWQ9cnVuLmlkLCB0aHJlYWRfaWQ9dGhyZWFkLmlkLCB0b29sX291dHB1dHM9b3V0cHV0cywgdGltZW91dD0yLjApCiAgICAgICAgaWYgcnVuLnN0YXR1cyA9PSAnZmFpbGVkJyBhbmQgcnVuLmxhc3RfZXJyb3I6CiAgICAgICAgICAgIHJldHVybiBydW4ubGFzdF9lcnJvci5tb2RlbF9kdW1wX2pzb24oKQogICAgICAgIG1zZ3MgPSBhd2FpdCBjbGllbnQuYmV0YS50aHJlYWRzLm1lc3NhZ2VzLmxpc3QodGhyZWFkX2lkPXRocmVhZC5pZCwgb3JkZXI9J2Rlc2MnKQogICAgICAgIGF3YWl0IGNsaWVudC5iZXRhLnRocmVhZHMuZGVsZXRlKHRocmVhZF9pZD10aHJlYWQuaWQpCiAgICAgICAgcmV0dXJuIG1zZ3MuZGF0YVswXS5jb250ZW50WzBdLnRleHQudmFsdWUKICAgIGRlbW8gPSBnci5DaGF0SW50ZXJmYWNlKHNlbmRfbWVzc2FnZSkKICAgIGRlbW8ubGF1bmNoKCkK

      2. 替換掉對應的 API_KEYASSISTANT_ID

         

      3. 在輸入框輸入 image url 即可

         

        1. 如果您想要使用本機影像,您可以使用下列 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

# 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()

 

CUSTOMIZED_PARAMS 使用,傳遞客製化參數進 plugin

如果你有需要透過 plugin-api 傳客製化參數的需求,可以將第 25 行呼叫 plugin-api 的代碼做以下變動。

customized_params 必須是一個合法的 json。

在 plugin 中就可以以 “CUSTOMIZED_PARAMS” 這個變數來取得傳入的客製化參數。
以 python plugin 為例可以參考 Python Plugin 說明。

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 完成一個陽春版的達哥對話腳本:

  1. 要先拿到 Assistant ID & User API key

  2. export API_KEYASSISTANT_ID 以及你的 INPUT_MSG

  3. 執行以下腳本 (請先確保環境有安裝 jq 套件)

    1. mac: brew install jq

    2. ubuntu: apt-get install jq

    3. cent os: yum install jq

  4. 即可看到結果如下

Image

  1. 要先拿到 Assistant ID & User API key

  2. export API_KEYASSISTANT_ID 以及你的 INPUT_MSG

    1. IMAGE_URL 格式參考上方 Gradio image 範例

  3. 執行以下腳本 (請先確保環境有安裝 jq 套件)

    1. mac: brew install jq

    2. ubuntu: apt-get install jq

    3. cent os: yum install jq

  4. 即可看到結果如下

Python

Text or image as Input

Text & image as input (Streaming)

Voice mode

(Coming Soon)

Related pages