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

DaVinci Assistant API 介紹

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

您正在檢視此內容的舊版本。請檢視目前版本

比較目前 檢視版本歷史記錄

« 上一頁 版本 13 下一步 »

Assistant API 介紹

使用 gradio 進行 preview 測試

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

  1. 取得 User API key

  2. 取得 Assistant ID,對

    image-20240628-074722.pngimage-20240628-075641.png

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

image-20240628-074811.png

image-20240628-075118.png

image-20240628-075134.png

image-20240628-075157.png

使用教學

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

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

    1. ASSISTANT_ID="YOUR ASSISTANT ID"
      API_KEY="YOUR API KEY"
      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://stag.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"
    }
    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
        RUN_STAUS=`curl -s --location --request GET "${RUN_URL}/$RUN_ID" \
    --header 'OpenAI-Beta: assistants=v2' \
    --header 'Content-Type: application/json' \
    --header "${AUTH_HEADER}" \ | jq .status | tr -d '"'`;
        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"
  6. 如何發送 user msg 到 Assistant

    1. create message → get message ID

  7. 如何取得 Assistant 的回應

    1. create run → run ID

    2. check run status → if run status == done

    3. list message →

  8. 當 Assistant 要求要執行 plugin 時,你要

    1. 怎樣知道 Assistant 要執行 plugin 以及什麼 plugin,要代入什麼參數?

    2. 打 run_plugin_api 來執行

    3. 將 plugin 結果傳回 Assistant

    4. 補充:file 時,先用 DocChat

API_KEY="your api key"
ASS_ID="your ass id"

> bash sendMessage.sh

cur dhdsh
cur dghsdeh

  • 無標籤