การสนทนาแบบหลายรอบกับ GPT

การทำงานกับ OpenAI API

James Chapman

Curriculum Manager, DataCamp

ข้อความจาก assistant

response = client.chat.completions.create(
  model="gpt-4o-mini",
  messages=[{"role": "system",
             "content": "You are a Python programming tutor who speaks concisely."},

{"role": "user", "content": "How do you define a Python list?"},
{"role": "assistant", "content": "Lists are defined by enclosing a comma-separated sequence of objects inside square brackets [ ]."},
{"role": "user", "content": "What is the difference between mutable and immutable objects?"}] )
การทำงานกับ OpenAI API

สร้างการสนทนา

ส่งคำขอของผู้ใช้แจ้งให้ assistant ทราบว่าชื่อ James แล้ว assistant ตอบว่า "Hi James!"

การทำงานกับ OpenAI API

สร้างการสนทนา

การตอบกลับ "Hi James!" จาก assistant ถูกย้ายเข้าไปใน messages list of dictionaries

การทำงานกับ OpenAI API

สร้างการสนทนา

การตอบกลับ "Hi James!" จาก assistant ถูกย้ายเข้าไปใน messages list of dictionaries

การทำงานกับ OpenAI API

สร้างการสนทนา

เพิ่มข้อความจากผู้ใช้ต่อท้าย message list of dictionaries โดยถามว่า "What is my name?"

การทำงานกับ OpenAI API

สร้างการสนทนา

assistant ตอบว่า "Your name is James."

การทำงานกับ OpenAI API

เขียนโค้ดการสนทนา

messages = [{"role": "system",
             "content": "You are a data science tutor who provides short, simple explanations."}]


user_qs = ["Why is Python so popular?", "Summarize this in one sentence."]
for q in user_qs:
print("User: ", q)
user_dict = {"role": "user", "content": q}
messages.append(user_dict)
response = client.chat.completions.create( model="gpt-4o-mini", messages=messages )
assistant_dict = {"role": "assistant", "content": response.choices[0].message.content} messages.append(assistant_dict)
print("Assistant: ", response.choices[0].message.content, "\n")
การทำงานกับ OpenAI API

สนทนากับ AI

User:  Why is Python so popular?
Assistant:  Python is popular for many reasons, including its simplicity,
versatility, and wide range of available libraries. It has a relatively
easy-to-learn syntax that makes it accessible to beginners and experts alike. It
can be used for a variety of tasks, such as data analysis, web development,
scientific computing, and machine learning. Additionally, Python has an active
community of developers who contribute to its development and share their
knowledge through online resources and forums.

User:  Summarize this in one sentence.
Assistant:  Python is popular due to its simplicity, versatility, wide range of
libraries, and active community of developers.
การทำงานกับ OpenAI API

มาฝึกกันเถอะ!

การทำงานกับ OpenAI API

Preparing Video For Download...