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 即可
使用 Gradio 進行 preview 測試
當我們在達哥平台上創建完 Assistant 時,若想進行 preview 測試,我們提供與 gradio 串接的 sample code (參考下面的 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:
替換掉對應的
API_KEY
與ASSISTANT_ID
在輸入框輸入即可
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
# 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 完成一個陽春版的達哥對話腳本:
要先拿到 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)