สร้าง Reasoning Chatbot สำหรับการเขียนโค้ด
มารวมทุกอย่างเข้าด้วยกันเพื่อสร้าง Reasoning Chatbot สำหรับช่วยเขียนโค้ดกัน!
มีข้อความจากผู้ใช้ 2 ข้อความให้พร้อมแล้ว ได้แก่ ข้อความแรกขอให้เขียนโค้ด Python สำหรับงานหนึ่งๆ และข้อความติดตามที่ขอให้เขียนโค้ดโดยใช้ไลบรารีเฉพาะเจาะจง
เนื่องจาก V4-Pro ใช้การให้เหตุผลเป็นค่าเริ่มต้น สิ่งที่ต้องทำคือวนลูปการสนทนาและเพิ่มฟิลด์ที่ถูกต้องลงในประวัติการสนทนา
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
การใช้งาน DeepSeek ใน Python
คำแนะนำการฝึกหัด
- วนลูปตามคำถามของผู้ใช้
- ส่งคำถามแต่ละข้อ
qไปยังโมเดลdeepseek-ai/DeepSeek-V4-Pro - เพิ่มคำตอบสุดท้ายของ Assistant (ค่า
.contentไม่ใช่ส่วนการให้เหตุผล) ลงในmessagesเพื่อให้ประวัติการสนทนากระชับ
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
client = OpenAI(api_key="", base_url="https://api.together.xyz/v1")
messages = []
user_msgs = ["Write some Python code to generate a list of numbers from 1-10.", "Update the code to use the NumPy library."]
# Loop over the user questions
for q in ____:
print("User: ", q)
user_dict = {"role": "user", "content": q}
messages.append(user_dict)
# Create the API request — V4-Pro reasons by default
response = client.chat.completions.create(
model="deepseek-ai/____",
messages=____,
max_tokens=500
)
# Append only the final answer to the conversation history
assistant_dict = {"role": "assistant", "content": response.choices[0].message.____}
messages.append(assistant_dict)
print("Assistant: ", response.choices[0].message.content, "\n")