已比較的版本

索引鍵

  • 此行已新增。
  • 此行已移除。
  • 格式已變更。

...

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

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

    1. 程式碼區塊
      languagebash
      export ASSISTANT_ID="YOUR ASSISTANT ID"
      export API_KEY="YOUR API KEY"
      export IMAGE_URL="YOUR IMAGE URL HEHE"
      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. 程式碼區塊
    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": [
        {
            "type": "image_url",
            "image_url": {
                "url": "$IMAGE_URL"
            }
        }
      ]
    }
    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 ""
    echo "davinci bot: "$RESPONSE_MSG
  5. 即可看到結果如下

    程式碼區塊
    davinci bot: "response from assistant"

Python

...

Text or Image as Input

程式碼區塊
languagepy
import json
from openai import OpenAI
from datetime import datetime

ASSISTANT_API = 'https://prod.dvcbot.net/api/assts/v1'
API_KEY = ''
client = OpenAI(
    base_url=ASSISTANT_API,
    api_key=API_KEY,
)
ASSISTANT_ID = ''

# 定義多個訊息
messages = [
    {"type": "text", "text": "tell me about the image"},
    {"type": "image_url", "image_url": {"url": "https://xxx.xxx.xxx.jpg"}},
    {"type": "text", "text": "What do you think about this image?"}
]

# 建立 thread
thread = client.beta.threads.create(messages=[])

# 連續發送訊息
for message in messages:
    client.beta.threads.messages.create(thread_id=thread.id, role='user', content=[message])

# 執行 assistant
run = client.beta.threads.runs.create_and_poll(thread_id=thread.id, assistant_id=ASSISTANT_ID, additional_instructions=f"\nThe current time is: {datetime.now()}")

while run.status == 'requires_action' and run.required_action:
    outputs = []
    for call in run.required_action.submit_tool_outputs.tool_calls:
        resp = 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 = client.beta.threads.runs.submit_tool_outputs_and_poll(run_id=run.id, thread_id=thread.id, tool_outputs=outputs)

if run.status == 'failed' and run.last_error:
    print(run.last_error.model_dump_json())

msgs = client.beta.threads.messages.list(thread_id=thread.id, order='desc')
client.beta.threads.delete(thread_id=thread.id)
print(msgs.data[0].content[0].text.value)

Voice mode

(Coming Soon)