การสร้างประวัติการสนทนา
แพลตฟอร์มเรียนคณิตศาสตร์ออนไลน์แห่งหนึ่งชื่อ Easy as Pi ได้ว่าจ้างให้ช่วยพัฒนาระบบติวเตอร์ AI คุณมองเห็นทันทีว่าสามารถสร้างแอปพลิเคชันนี้โดยใช้ OpenAI API และเริ่มออกแบบ proof-of-concept (POC) เบื้องต้นเพื่อนำเสนอต่อผู้มีส่วนได้ส่วนเสียหลักของบริษัท
ขั้นแรก จะสาธิตวิธีจัดเก็บการตอบกลับต่อข้อความของนักเรียนไว้ในประวัติข้อความ ซึ่งจะช่วยให้รองรับการสนทนาได้อย่างต่อเนื่อง
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
การทำงานกับ OpenAI API
คำแนะนำการฝึกหัด
- ส่ง
messagesไปยังโมเดลในคำขอแบบ chat - ดึงข้อความของ assistant จาก
responseแปลงเป็น message dictionary แล้ว append เข้าไปในmessages
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
client = OpenAI(api_key="")
messages = [
{"role": "system", "content": "You are a helpful math tutor that speaks concisely."},
{"role": "user", "content": "Explain what pi is."}
]
# Send the chat messages to the model
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=____,
max_completion_tokens=100
)
# Extract the assistant message from the response
assistant_dict = {"role": "____", "content": ____}
# Add assistant_dict to the messages dictionary
messages.____(assistant_dict)
print(messages)