呼叫外部 API
既然你已經有結構清楚的函式定義,接下來要改進端點請求。你會使用 Chat Completions 端點,並傳入 system 訊息,讓 AI 助理知道目前的情境是貨幣兌換,且需要根據使用者輸入抽取對應的貨幣代碼。
在本練習中,已預先載入 get_exchange_rate() 函式。get_exchange_rate() 會呼叫 ExchangeRate API,輸入一個貨幣代碼,並回傳包含指定匯率資訊的回應。同時也預先載入 print_response() 函式供你印出輸出。
本練習屬於課程
使用 OpenAI API 開發 AI 系統
練習說明
- 呼叫 Chat Completions 端點,並確保
system取得處理提示的指示。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
client = OpenAI(api_key="")
# Call the Chat Completions endpoint
response = ____(
model="gpt-4o-mini",
messages=[
____,
{"role": ____, "content": "I'd like to know the current exchange rates for the Euro."}],
tools=function_definition)
print_response(response)