開始使用免費開始

加入 assistant 訊息

聊天模型非常適合用來建立對話式應用,不過如果提供部分對話內容作為基礎,效果會更好。

請改進這個地理家教應用,將以下範例學生提問與理想模型回應加入 messages:

  • 範例問題:Give me a quick summary of Portugal.
  • 範例答案:Portugal is a country in Europe that borders Spain. The capital city is Lisboa.

本練習屬於課程

Working with the OpenAI API

檢視課程

練習說明

  • 將提供的範例問答以 user-assistant 配對的形式加入要傳給模型的 messages。
    • 範例問題:Give me a quick summary of Portugal.
    • 範例答案:Portugal is a country in Europe that borders Spain. The capital city is Lisboa.

動手互動練習

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

client = OpenAI(api_key="")

response = client.chat.completions.create(
    model="gpt-4o-mini",
    # Add a user and assistant message for in-context learning
    messages=[
        {"role": "system", "content": "You are a helpful Geography tutor that generates concise summaries for different countries."},
        ____,
        ____,
        {"role": "user", "content": "Give me a quick summary of Greece."}
    ]
)

print(response.choices[0].message.content)
編輯並執行程式碼