跳至橫幅的結尾
前往橫幅的開頭

MediaTek DaVinci Assistant API 介紹

跳至中繼資料的結尾
前往中繼資料的開頭

You are viewing an old version of this content. View the current version.

比較目前 View Version History

« 上一頁 版本 31 下一步 »

MediaTek DaVinci Assistant API 介紹

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

使用 gradio 進行 preview 測試

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

  1. 取得 User API key:

    1. 點選達哥面板左下角 More Actions 按鈕,選取 Settings

      image-20240628-074811.png
    2. 進到 Settings 後,點選 + API Key 按鈕新增

      image-20240628-075118.png
    3. 複製 API Key

      image-20240628-075157.png
  2. 取得 Assistant ID

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

      image-20240628-075641.png
    2. 選取 Advanced tab,複製 Assistant ID

      image-20240628-075916.png
  3. 替換掉 sample code 的 API_KEYASSISTANT_ID,之後即可直接在輸入框開始對話

    1. 點選此連結

    2. 替換掉對應的 API_KEYASSISTANT_ID

      image-20240628-080723.png

    3. 在輸入框輸入即可

      image-20240628-080750.png

MediaTek DaVinci Assistant API 使用教學

Curl

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

  2. expot API KEY,ASSISTANT_ID 以及你的 input

    1. export ASSISTANT_ID="YOUR ASSISTANT ID"
      export API_KEY="YOUR API KEY"
      export INPUT_MSG="YOUR MESSAGE TO ASSISTANT"
  3. 執行以下腳本 (請先確保環境有安裝 jq 套件)

    1. mac: brew install jq

    2. ubuntu: apt-get install jq

    3. cent os: yum install jq

  4. BASE_URL="https://prod.dvcbot.net/api/assts/v1"
    
    # create thread
    AUTH_HEADER="Authorization: Bearer ${API_KEY}"
    THREAD_URL="${BASE_URL}/threads"
    THREAD_ID=`curl -s --location "${THREAD_URL}" \
    --header 'OpenAI-Beta: assistants=v2' \
    --header 'Content-Type: application/json' \
    --header "${AUTH_HEADER}" \
    --data '{}' | jq .id | tr -d '"'`
    
    # add msg to thread
    CREATE_MSG_DATA=$(< <(cat <<EOF
    {
      "role": "user",
      "content": "$INPUT_MSG"
    }
    EOF
    ))
    MSG_URL="${BASE_URL}/threads/${THREAD_ID}/messages"
    curl -s --location "${MSG_URL}" \
    --header 'OpenAI-Beta: assistants=v2' \
    --header 'Content-Type: application/json' \
    --header "${AUTH_HEADER}" \
    --data "${CREATE_MSG_DATA}" > /dev/null
    
    # run the assistant within thread
    CREATE_RUN_DATA=$(< <(cat <<EOF
    {
      "assistant_id": "$ASSISTANT_ID",
      "additional_instructions": "The current time is: `date '+%Y-%m-%d %H:%M:%S'`"
    }
    EOF
    ))
    
    RUN_URL="${BASE_URL}/threads/${THREAD_ID}/runs"
    RUN_ID=`curl -s --location "${RUN_URL}" \
    --header 'OpenAI-Beta: assistants=v2' \
    --header 'Content-Type: application/json' \
    --header "${AUTH_HEADER}" \
    --data "${CREATE_RUN_DATA}" | jq .id | tr -d '"'`
    
    # get run result
    RUN_STAUS=""
    while [[ $RUN_STAUS != "completed" ]]
    do
        RESP=`curl -s --location --request GET "${RUN_URL}/$RUN_ID" \
    --header 'OpenAI-Beta: assistants=v2' \
    --header 'Content-Type: application/json' \
    --header "${AUTH_HEADER}"`
    
        RUN_STAUS=`echo $RESP| jq .status | tr -d '"'`;
        REQUIRED_ACTION=`echo $RESP| jq .required_action`
    
        while [[ $RUN_STAUS = "requires_action" ]] && [[ ! -z "$REQUIRED_ACTION" ]]
        do
            TOOL_OUTPUTS='[]'
            LEN=$( echo "$REQUIRED_ACTION" | jq '.submit_tool_outputs.tool_calls | length' )
            for (( i=0; i<$LEN; i++ ))
            do
                FUNC_NAME=`echo "$REQUIRED_ACTION" | jq ".submit_tool_outputs.tool_calls[$i].function.name" | tr -d '"'`
    
                ARGS=`echo "$REQUIRED_ACTION" | jq ".submit_tool_outputs.tool_calls[$i].function.arguments"`
                ARGS=${ARGS//\\\"/\"}
                ARGS=${ARGS#"\""}
                ARGS=${ARGS%"\""}
    
                PLUGINAPI_URL="${BASE_URL}/pluginapi?tid=${THREAD_ID}&aid=${ASSISTANT_ID}&pid=${FUNC_NAME}"
                OUTPUT=`curl -s --location "${PLUGINAPI_URL}" \
    --header 'OpenAI-Beta: assistants=v2' \
    --header 'Content-Type: application/json' \
    --header "${AUTH_HEADER}" \
    --data "${ARGS}"`
                OUTPUT="${OUTPUT:0:8000}"
                OUTPUT=${OUTPUT//\"/\\\"}
                CALL_ID=`echo "$REQUIRED_ACTION" | jq ".submit_tool_outputs.tool_calls[$i].id" | tr -d '"'`
                TOOL_OUTPUT=$(< <(cat <<EOF
    {
      "tool_call_id": "$CALL_ID",
      "output": "$OUTPUT"
    }
    EOF
    ))
                TOOL_OUTPUTS=$(jq --argjson obj "$TOOL_OUTPUT" '. += [$obj]' <<< "$TOOL_OUTPUTS")
            done
    
            SUBMIT_TOOL_OUTPUT_RUN_RUL="${BASE_URL}/threads/${THREAD_ID}/runs/${RUN_ID}/submit_tool_outputs"
    
            TOOL_OUTPUTS_DATA=$(< <(cat <<EOF
    {
      "tool_outputs": $TOOL_OUTPUTS
    }
    EOF
    ))
    
            curl -s --location "${SUBMIT_TOOL_OUTPUT_RUN_RUL}" \
    --header 'OpenAI-Beta: assistants=v2' \
    --header 'Content-Type: application/json' \
    --header "${AUTH_HEADER}" \
    --data "${TOOL_OUTPUTS_DATA}" > /dev/null
    
            RESP=`curl -s --location --request GET "${RUN_URL}/$RUN_ID" \
    --header 'OpenAI-Beta: assistants=v2' \
    --header 'Content-Type: application/json' \
    --header "${AUTH_HEADER}"`
            RUN_STAUS=`echo $RESP| jq .status | tr -d '"'`;
            sleep 1
        done
        sleep 1
    done
    
    #list msg
    RESPONSE_MSG=`curl -s --location --request GET "${MSG_URL}" \
    --header 'OpenAI-Beta: assistants=v2' \
    --header 'Content-Type: application/json' \
    --header "${AUTH_HEADER}" | jq .data[0].content[].text.value`
    
    echo "you: "$INPUT_MSG
    echo ""
    echo "davinci bot: "$RESPONSE_MSG
  5. 即可看到結果如下

    you: "your message here"
    davinci bot: "response from assistant"

Python

將提供可開關 Streaming 的 Asst API,並支援 text, image 的 input 與 text output 或是 audio 的 input/output 的 sample code

  • 無標籤