開始使用免費開始

加入 assistant 訊息

Chat 模式很適合打造對話式應用程式,而且你可以先提供部分對話內容,進一步提升模型的效果。

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

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

本練習屬於課程

使用 Python 操作 DeepSeek

檢視課程

練習說明

  • 將提供的範例問答,作為一組 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="", base_url="https://api.together.xyz/v1")

response = client.chat.completions.create(
    model="deepseek-ai/DeepSeek-V4.1-Flash",
    # Add a user and assistant message for in-context learning
    messages=[
        {"role": "system", "content": "You are a helpful Geography tutor that generates concise, one-sentence summaries for different countries."},
        ____,
        ____,
        {"role": "user", "content": "Give me a quick summary of Greece."}
    ]
)

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