GPT와 멀티 턴 대화하기

OpenAI API 활용하기

James Chapman

Curriculum Manager, DataCamp

어시스턴트 메시지

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 활용하기

대화 구축하기

![어시스턴트에 "내 이름은 제임스야"라고 알리는 사용자 요청을 보냅니다. "제임스, 안녕하세요!"라고 답변하는 어시스턴트."

OpenAI API 활용하기

대화 구축하기

"제임스, 안녕하세요!"라는 어시스턴트의 응답이 딕셔너리의 메시지 리스트로 이동됩니다.

OpenAI API 활용하기

대화 구축하기

"제임스, 안녕하세요!"라는 어시스턴트의 응답이 딕셔너리의 메시지 리스트로 이동됩니다.

OpenAI API 활용하기

대화 구축하기

딕셔너리의 메시지 리스트 끝에 사용자 응답이 추가되어 "내 이름이 뭐야?"라고 묻고 있습니다.

OpenAI API 활용하기

대화 구축하기

"당신의 이름은 제임스입니다"라고 답변한 어시스턴트.

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...