Multi-turn chat completions with GPT

Working with the OpenAI API

James Chapman

Curriculum Manager, DataCamp

Chat completions for single-turn tasks

response = client.chat.completions.create(
  model="gpt-4o-mini",
  messages=[{"role": "system",
             "content": "You are a data science tutor."},
            {"role": "user",
             "content": "What is the difference between mutable and immutable objects?"}]
)
  • System: controls assistant's behavior
  • User: instruct the assistant
  • Assistant: response to user instruction
Working with the OpenAI API

Providing examples

 

  • Steer model in the right direction
  • Nothing surfaced to the end-user
  • Example: Data Science Tutor Application
    • Provide examples of data science questions and answers

A data science owl.

Working with the OpenAI API

Providing examples

response = client.chat.completions.create(
  model="gpt-4o-mini",
  messages=[{"role": "system",
             "content": "You are a data science 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?"}] )
Working with the OpenAI API

The response

print(response.choices[0].message.content)
Mutable objects are objects whose values can change after they are created. Examples of
mutable objects in Python include lists, sets and dictionaries. Immutable objects are
objects whose values cannot change after they are created. Examples of immutable objects
in Python include strings, numbers and tuples.
Working with the OpenAI API

Storing responses

 

  • Create conversation history
  • Create back-and-forth conversations

 

A multi-turn conversation.

Working with the OpenAI API

Building a conversation

Sending a user request telling the assistant that my name is James. The assistant responses with "Hi James!"

Working with the OpenAI API

Building a conversation

The "Hi James!" response from the assistant is moved into the messages list of dictionaries.

Working with the OpenAI API

Building a conversation

The "Hi James!" response from the assistant is moved into the messages list of dictionaries.

Working with the OpenAI API

Building a conversation

A user response is added to the end of the message list of dictionaries asking "What is my name?"

Working with the OpenAI API

Building a conversation

The assistant replies with "Your name is James."

Working with the OpenAI API

Coding a conversation

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")
Working with the OpenAI API

Conversation with an 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.
Working with the OpenAI API

Let's practice!

Working with the OpenAI API

Preparing Video For Download...