使用外部 API 呼叫處理回應
為了讓你的匯率換算應用程式更好地與其他系統整合,你想加入一些檢查來確保模型找到合適的答案。你會先檢查回應是否是透過 tool_calls 產生的。若是,接著檢查產生結果所使用的函式是否為 get_exchange_rate。如果符合,便載入從使用者提示中擷取的貨幣代碼,並以該代碼作為引數呼叫 get_exchange_rate()。最後,若該呼叫產生了回應,就回傳此回應。
在本練習中,response、json 函式庫,以及 get_exchange_rate() 函式都已預先載入。
本練習屬於課程
使用 OpenAI API 開發 AI 系統
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Check that the response has been produced using function calling
if ____:
# Extract the function
function_call = ____
print(function_call)
else:
print("I am sorry, but I could not understand your request.")