เริ่มต้นใช้งานเริ่มต้นใช้งานได้ฟรี

สร้างแชทบอทให้เหตุผลสำหรับงานเขียนโค้ด

มารวมทุกอย่างที่เรียนมาเพื่อสร้างแชทบอทที่ใช้การให้เหตุผลช่วยเขียนโค้ดกัน!

คุณได้รับข้อความจากผู้ใช้สองข้อความ ข้อความแรกขอให้เขียนโค้ด Python สำหรับงานหนึ่ง และข้อความที่สองขอให้เขียนโค้ดนั้นใหม่โดยใช้ไลบรารีที่ระบุ

V4-Pro ใช้การให้เหตุผลเป็นค่าเริ่มต้นอยู่แล้ว สิ่งที่ต้องทำคือวนลูปการสนทนาและเพิ่มฟิลด์ที่ถูกต้องเข้าไปในประวัติการสนทนา

แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร

การใช้งาน DeepSeek ใน Python

ดูคอร์ส

คำแนะนำการฝึกหัด

  • วนลูปคำถามของผู้ใช้
  • ส่งคำถามแต่ละข้อ q ไปยังโมเดล deepseek-ai/DeepSeek-V4.1-Flash
  • เพิ่มคำตอบสุดท้ายของ 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")
แก้ไขและรันโค้ด