開始使用免費開始

指定特定函式

你收到幾則顧客評論需要分析,並被要求從每則評論中擷取產品名稱、款式(variant)以及顧客情緒。為了確保模型只擷取這些特定資訊,你決定使用 function calling,並明確指定模型要使用的函式。請使用 Chat Completions 端點、搭配 function calling 與 tool_choice 來擷取這些資訊。

在本練習中,messagesfunction_definition 已經預先載入。

本練習屬於課程

使用 OpenAI API 開發 AI 系統

檢視課程

練習說明

  • 將你的函式定義加入 tools。
  • 將回應設定為呼叫 extract_review_info 函式。
  • 列印回應。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

client = OpenAI(api_key="")

response= client.chat.completions.create(
    model=model,
    messages=messages,
    # Add the function definition
    ____,
    # Specify the function to be called for the response
    tool_choice=____
)

# Print the response
print(____)
編輯並執行程式碼