การสร้างประวัติการสนทนา
แพลตฟอร์มสอนคณิตศาสตร์ออนไลน์แห่งหนึ่งชื่อ Easy as Pi ได้ว่าจ้างให้ช่วยพัฒนาระบบ AI ติวเตอร์ คุณเห็นทันทีว่าสามารถสร้างแอปพลิเคชันนี้ด้วย DeepSeek ในโหมดแชทได้ จึงเริ่มออกแบบ proof-of-concept (POC) เบื้องต้นเพื่อให้ผู้มีส่วนได้ส่วนเสียหลักของบริษัทพิจารณา
ในขั้นแรก จะสาธิตวิธีเก็บการตอบกลับของโมเดลต่อข้อความของนักเรียนลงในประวัติข้อความ ซึ่งจะช่วยให้เกิดการสนทนาต่อเนื่องได้อย่างสมบูรณ์
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
การใช้งาน DeepSeek ใน Python
คำแนะนำการฝึกหัด
- ส่ง
messagesไปยังโมเดลในคำขอแชท - ดึงข้อความของ assistant จาก
responseแปลงเป็น message dictionary แล้ว append เข้าไปในmessages
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
client = OpenAI(api_key="", base_url="https://api.together.xyz/v1")
messages = [
{"role": "system", "content": "You are a helpful math tutor that generates concise, one-sentence responses."},
{"role": "user", "content": "Explain what pi is."}
]
# Send the chat messages to the model
response = client.chat.completions.create(
model="deepseek-ai/DeepSeek-V4-Pro",
messages=____,
max_tokens=500
)
# Extract the assistant message from the response
assistant_dict = {"role": "____", "content": ____}
# Add assistant_dict to the messages dictionary
messages.____(assistant_dict)
print(messages)