...
Share 該 Assistant 給對方
提供該 Assistant 的『Assistant ID 』與『發自 API Key 』給對方
若想收回該 Assistant 的服務時,僅在自己的 API Key 面板上刪除該 API Key 即可
使用 https://www.gradio.app/
...
進行 preview 測試
當我們在達哥平台上創建完 Assistant 時,若想進行 preview 測試,我們提供與 gradio 串接的 sample code。以下為教學步驟: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:
Image:
替換掉對應的
API_KEY
與ASSISTANT_ID
在輸入框輸入 image url 即可
如果您想要使用本機影像,您可以使用下列 Python 程式碼將它轉換成 base64,以便將其傳遞至 API。 或者您可以使用線上工具將影像檔轉成 base64。
程式碼區塊 language py 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)
...
程式碼區塊 |
---|
"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。
程式碼區塊 | ||
---|---|---|
| ||
// 原本第 25 行呼叫 plugin-api 的代碼
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))
//做以下修改帶入客製化參數
my_customized_params = """{"msg":"this is a customized params for demo"}"""
resp = await client._client.post(ASSISTANT_API+'/pluginapi', params={"tid": thread.id, "aid": ASSISTANT_ID, "pid": call.function.name, "params": my_customized_params}, headers={"Authorization": "Bearer " + API_KEY}, json=json.loads(call.function.arguments)) |
在 plugin 中就可以以 “CUSTOMIZED_PARAMS” 這個變數來取得傳入的客製化參數。
以 python plugin 為例可以參考 Python Plugin 說明。
MediaTek DaVinci Assistant API 使用教學
...