สร้างแชทบอทสำหรับให้เหตุผลเพื่อช่วยเขียนโค้ด
มารวมทุกสิ่งที่เรียนมาเพื่อสร้างแชทบอทสำหรับให้เหตุผลที่ช่วยเขียนโค้ดกันเถอะ!
คุณได้รับข้อความจากผู้ใช้สองข้อความ ได้แก่ ข้อความแรกที่ขอโค้ด Python สำหรับงานหนึ่งๆ และข้อความต่อเนื่องที่ขอให้เขียนโค้ดด้วยไลบรารีที่กำหนด
เนื่องจาก V4-Pro ใช้การให้เหตุผลเป็นค่าเริ่มต้นอยู่แล้ว สิ่งที่ต้องทำมีเพียงวนลูปการสนทนาและเพิ่มฟิลด์ที่ถูกต้องเข้าไปในประวัติการสนทนา
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
การใช้งาน DeepSeek ใน Python
คำแนะนำการฝึกหัด
- วนลูปคำถามของผู้ใช้
- ส่งคำถามของผู้ใช้แต่ละข้อ
qไปยังโมเดลdeepseek-ai/DeepSeek-V4-Pro-0813 - เพิ่มคำตอบสุดท้ายของผู้ช่วย (คือ
.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")