開始使用免費開始

擷取回應

你在一間剛推出新智慧型手機的公司工作。行銷團隊從各大線上平臺蒐集了顧客評論,想分析這些回饋,以了解顧客情緒,以及手機最常被提到的功能。為了加速流程,你已使用 OpenAI API 結合 function calling,從這些評論中擷取結構化資料。接下來你需要撰寫一個函式,清理輸出,並只回傳函式回傳內容所組成的字典。

get_response() 函式、messages 變數(包含評論),以及 function_definition(包含用來從評論擷取情緒與產品功能的函式)都已經預先載入。請注意,messagesfunction_definition 都可以作為引數傳入 get_response(),以從 chat completions 端點取得回應。

本練習屬於課程

使用 OpenAI API 開發 AI 系統

檢視課程

練習說明

  • 定義一個函式,回傳一個字典,內容為回應中 arguments 底下的輸出資料。
  • 列印該字典。

動手互動練習

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

client = OpenAI(api_key="")

response = get_response(messages, function_definition)

# Define the function to extract the data dictionary
def extract_dictionary(____):
  return ____

# Print the data dictionary
____
編輯並執行程式碼