Harnessing Structure in Conversations: Schema-Driven ChatGPT Interactions

Shardul Lingwal
4 min readOct 7, 2023

🌟 Introduction 🌟

In the dynamic realm of conversational AI, OpenAI’s ChatGPT has emerged as a powerful tool for natural language generation. While its free-form responses mimic human conversation, there are scenarios where a structured, schema-driven approach becomes essential. This article takes a closer look at the innovative concept of schema-driven interactions with ChatGPT, offering a detailed exploration of its application using a real-world example — predicting a cricket team for the Men’s World Cup 2023.

📋 Understanding Schema-Driven Interactions 📋

The traditional strength of ChatGPT lies in its ability to emulate human-like responses, complete with creativity and variability. However, when the need arises for information to conform to a specific structure, schema-driven interactions become invaluable. Now, users can guide ChatGPT to produce responses in a predefined format, aligning more closely with specific requirements.

💻 The Code in Action 💻

ChatGPT Prompt without pre-defined Schema

import openai
import json

openai.api_key = "<open_ai api key>" # replace with your open_ai api key

def normal_gpt_response():
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": "Predict a 15-player team for Indian Cricket Team for Men's World Cup 2023"}]
)

print(response.get("choices", [{}])[0].get("message", {}).get("content", ""))

normal_gpt_response()
- Initiates a standard ChatGPT conversation without specific schema instructions.
- Generates a free-form list of players for the cricket team.

This is a simple code that asks ChatGPT for a response, based on the prompt provided. Though every time the response would include the list of 15 players, but there was no structure that was being followed. Let’s look at some of the variations we encountered for the exact same prompt.

1. Sometimes the list would be `comma-separated` and sometimes it would be separated by `new line`.
2. Sometimes the list would be `numbered`, and sometimes it would not.
3. Sometimes the response would have `some extra text`, eg. "Here's a possible prediction for the 15-player Indian Cricket Team for Men's World Cup 2023:" along with the list of players.

ChatGPT Prompt with pre-defined Schema

import openai
import json

openai.api_key = "<open_ai api key>" # replace with your open_ai api key

def schema_gpt_response():
schema: dict = { # the response will be a dict with 4 keys, value of each is a list of strings
"type": "object",
"properties": {
"batsmen": {
"type": "array",
"items": {"type": "string"},
"description": "Lists the Batsmen in the team.",
},
"allrounders": {
"type": "array",
"items": {"type": "string"},
"description": "Lists the All-rounders in the team.",
},
"bowlers": {
"type": "array",
"items": {"type": "string"},
"description": "Lists the Bowlers in the team.",
},
"others": {
"type": "array",
"items": {"type": "string"},
"description": "Lists the other players in the team."
}
}
}
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": "Predict a 15-player team for Indian Cricket Team for Men's World Cup 2023"}],
functions=[{ "name": "print_resp", "parameters": schema }],
)
response_schema = response.get("choices", [()])[0].get("message", {}).get("function_call", {}).get("arguments", {})
print(response_schema)

schema_gpt_response()
- Introduces a schema describing the desired structure of the response.
- Utilizes the schema in the conversation, guiding ChatGPT to provide a structured response.
- Outputs a neatly categorized list of players as per the defined schema.

This code for the same prompt asks the ChatGPT to provide the response in a certain structure, following the schema provided in the request. Here, we need to focus on the functions parameter of the request (which tells the request that we need to call a function, hence we need the response in a particular format). Let’s dive a bit deeper into it.
1. name: name of the function that is supposed to be called passing this response.
2. parameters: schema of the response.
Since the request doesn’t call the function directly and it’s supposed to be called explicitly, we can pass anything (even if the function doesn’t exist).

Now let’s take a look at the response once,

{
"batsmen": [
"Rohit Sharma",
"Virat Kohli",
"KL Rahul",
"Shreyas Iyer",
"Shubman Gill"
],
"allrounders": [
"Hardik Pandya",
"Ravindra Jadeja"
],
"bowlers": [
"Jasprit Bumrah",
"Mohammed Shami",
"Bhuvneshwar Kumar",
"Yuzvendra Chahal"
],
"others": [
"Rishabh Pant",
"Ravichandran Ashwin",
"Deepak Chahar",
"Suryakumar Yadav"
]
}

🚀 Unlocking Potential with Precision 🚀

The variations in responses from normal_gpt_response() showcase not just the adaptability but also the inherent creativity of ChatGPT. Whether it's presenting a comma-separated list, a neatly organized newline-separated format, or incorporating additional text, ChatGPT crafts responses that resonate with diverse user preferences.
In this journey, we’ve glimpsed the potential of schema-driven responses to redefine how we interact with AI. From shaping financial reports to envisioning the future of content creation, the implications are profound. This capability not only meets current needs but foreshadows a future where AI models seamlessly adapt to user-defined structures, paving the way for a new era of personalized and refined interactions.

🎉 Conclusion 🎉

As we conclude, let’s celebrate the present and look forward to the exciting developments that lie ahead in the dynamic and ever-evolving landscape of conversational AI. The schema is set, and the conversation is just beginning. Here’s to a future where our interactions with AI are not just intelligent but precisely tailored to our needs and preferences. Cheers to the next chapter in the evolution of conversational AI! 🎊

--

--