การให้บริบทผ่านบทสนทนาตัวอย่าง
สมมติว่ามีบริการจัดส่งชื่อ MyPersonalDelivery ที่เสนอตัวเลือกการจัดส่งหลากหลายรูปแบบสำหรับสินค้าประเภทต่าง ๆ คุณต้องการสร้างแชทบอตบริการลูกค้าเพื่อช่วยเหลือลูกค้าในทุกเรื่องที่ต้องการ โดยจะกำหนด context_question และ context_answer เกี่ยวกับสินค้าที่บริษัทจัดส่ง ผ่านบทสนทนาก่อนหน้า แล้วทดสอบว่าโมเดลรับรู้บริบทนี้ได้หรือไม่ผ่าน user prompt ใหม่
แพ็กเกจ OpenAI รวมถึงสตริง context_question และ context_answer ถูกโหลดไว้ให้แล้ว
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
Prompt Engineering กับ OpenAI API
คำแนะนำการฝึกหัด
- กำหนด
system_promptที่ระบุวัตถุประสงค์ของแชทบอต และแนะนำให้ตอบคำถามด้วยน้ำเสียงที่สุภาพ - นำ
system_prompt,context_questionและcontext_answerมาจัดโครงสร้างบทสนทนา เพื่อให้แชทบอตใช้เป็นบริบทในการตอบคำถามของผู้ใช้รายใหม่
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
client = OpenAI(api_key="")
# Define the system prompt
system_prompt = "____"
context_question = "What types of items can be delivered using MyPersonalDelivery?"
context_answer = "We deliver everything from everyday essentials such as groceries, medications, and documents to larger items like electronics, clothing, and furniture. However, please note that we currently do not offer delivery for hazardous materials or extremely fragile items requiring special handling."
# Add the context to the model
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "____", "content": ____},
{"role": "____", "content": ____},
{"role": "____", "content": ____ },
{"role": "user", "content": "Do you deliver furniture?"}])
response = response.choices[0].message.content
print(response)