- Utilize the paid ChatGPT API for more control over output and to maintain a given JSON schema
- Learn about prompt engineering and system prompts to improve ChatGPT's output quality
- Simplify schema input/output with Pydantic for structured data support
Blurb for Social Media:
Discover how to unlock the full potential of ChatGPT with the paid API, prompt engineering, and Pydantic for structured data support. Gain more control over output and learn to maintain a given JSON schema. #ChatGPT #AI #Pydantic
Post Categories:
1. Artificial Intelligence
2. API Development
3. Data Schema
SEO Keywords:
1. ChatGPT API
2. Prompt Engineering
3. Pydantic Schema
{
"summary": [
"ChatGPT's potential is underutilized without the paid ChatGPT API",
"System prompts and prompt engineering are key to maximizing ChatGPT's capabilities",
"Structured data support in ChatGPT allows for more control over output and input"
],
"blurb": "Unlock the full potential of ChatGPT with system prompts and structured data support. Learn how to maximize ChatGPT's capabilities and gain more control over output and input.",
"categories": ["AI and Machine Learning", "Technology", "Programming"],
"description": "Returns an answer to a question the user asked.",
"properties": {
"answer": {
"description": "Answer to the user's question.",
"title": "Answer",
"type": "integer"
},
"ones_name": {
"description": "Name of the ones digit of the answer.",
"title": "Ones Name",
"type": "string"
}
},
"required": ["answer", "ones_name"],
"title": "answer_question",
"type": "object"
}
{
"tools": [
{
"name": "answer_question",
"description": "Returns an answer to a question the user asked.",
"parameters": {
"properties": {
"answer": {
"description": "Answer to the user's question.",
"type": "integer"
},
"ones_name": {
"description": "Name of the ones digit of the answer.",
"type": "string"
}
},
"required": ["answer", "ones_name"],
"type": "object"
}
}
],
"tool_choice": {
"type": "function",
"function": {
"name": "answer_question"
}
}
}
from simpleaichat import AIChat
ai = AIChat(console=False,
save_messages=False,
model="gpt-3.5-turbo",
params={"temperature":0.0}# for consistent demo output
)
response_structured = ai(
"How many miles is it from San Francisco to Los Angeles?",
output_schema=answer_question
)
{
"answer": 382,
"ones_name": "two"
}
classanswer_code_question(BaseModel):
"""Returns an answer to a coding question the user asked."""
code:str= Field(description="Code the user requested, without code comments.")
response_structured = ai(
"Write a Python function to detect whether a string is a palindrome, as efficiently as possible.",
output_schema=answer_code_question
)
{
"code": "def is_palindrome(s):n return s == s[::-1]"
}
from simpleaichat.utils import fd
classanswer_code_question(BaseModel):
"""Returns an answer to a coding question the user asked."""
code:str= fd("Code the user requested, without code comments.")
optimized_code:str= fd("Algorithmically optimized code from the previous response.")
response_structured = ai(
"Write a Python function to detect whether a string is a palindrome, as efficiently as possible.",
output_schema=answer_code_question,
)
{
"code": "def is_palindrome(s):n return s == s[::-1]",
"optimized_code": "def is_palindrome(s):n left = 0n right = len(s) - 1n while left < right:n if s[left] != s[right]:n return Falsen left += 1n right -= 1n return True"
}
from typing import Literal
classget_current_weather(BaseModel):
location:str= fd("The city and state, e.g. San Francisco, CA")
unit: Literal["celsius","fahrenheit"]=None
classcalculate_equation(BaseModel):
"""Returns an answer to a math equation the user asked."""
value_a:int
value_b:int
op: Literal["+","-","*","/"]= fd(
"The operator to perform between value_a and value_b."
dialogue:list[Chat]= fd("Dialogue between the characters", min_length=5)
system_prompt ="""You are a world-famous comedian. Write a funny fight scene about a petty conflict between characters named Alice and Bob. The script should broadly be about the subject(s) the user provides. You will receive a $500 tip for every joke you include in the script."""
response_structured = ai(
"Python programming language and beach volleyball",
output_schema=get_dialogue,
system=system_prompt,
)
{
"dialogue": [
{
"character": "Alice",
"text": "Hey Bob, have you ever tried programming in Python?"
},
{
"character": "Bob",
"text": "Yeah, I have. It's like playing beach volleyball with a snake!"
},
{
"character": "Alice",
"text": "What do you mean?"
},
{
"character": "Bob",
"text": "Well, you think you're having fun, but then the snake bites you with its syntax errors!"
},
{
"character": "Alice",
"text": "Haha, that's true. But once you get the hang of it, it's like spiking the ball with precision!"
},
{
"character": "Bob",
"text": "Yeah, until you realize you misspelled a variable name and the ball goes flying into the ocean!"
},
{
"character": "Alice",
"text": "Oh come on, Bob. It's not that bad. Python is a powerful language."
},
{
"character": "Bob",
"text": "Powerful, yes. But sometimes it feels like trying to dig a hole in the sand with a spoon!"
},
{
"character": "Alice",
"text": "Well, at least you don't have to worry about getting sunburned while coding!"
},
{
"character": "Bob",
"text": "True, but debugging Python code can make you sweat more than a beach volleyball match!"
},
{
"character": "Alice",
"text": "Haha, you're right. It's a love-hate relationship with Python, just like beach volleyball!"
}
]
}
from typing import Union
classBackground(BaseModel):
"""A setup to the background for the user."""
background:str= fd("Background for the user's question", min_length=30)
classThought(BaseModel):
"""A thought about the user's question."""
thought:str= fd("Text of the thought.")
helpful:bool= fd("Whether the thought is helpful to solving the user's question.")
flawed:bool= fd("Whether the thought is flawed or misleading.")
classAnswer(BaseModel):
"""The answer to the user's question"""
answer:str= fd("Text of the answer.")
score:int= fd(
"Score from 1 to 10 on how correct the previous answer is",
min_value=1,
max_value=10,
)
classreason_question(BaseModel):
"""Returns a detailed reasoning to the user's question."""
"Reasonings to solve the users questions.", min_length=5
)
system_prompt ="""
You are the most intelligent person in the world.
You will receive a $500 tip if you follow ALL these rules:
- First, establish a detailed Background for the user's question.
- Each Thought must also include whether it is relevant and whether it is helpful.
- Answers must be scored accurately and honestly.
- Continue having Thoughts and Answers until you have an answer with a score of atleast 8, then immediately respond with a FinalAnswer in the style of an academic professor.
"""
response_structured = ai(
"23 shirts take 1 hour to dry outside, how long do 44 shirts take?",
output_schema=reason_question,
system=system_prompt.strip(),
)
{
"reasonings": [
{
"background": "The user is asking about the drying time for shirts when hung outside. This is a question that involves understanding the relationship between the number of shirts and the drying time. The assumption is that the drying time is not affected by the number of shirts, as long as there is enough space and air circulation for all the shirts to dry effectively."
},
{
"thought": "If 23 shirts take 1 hour to dry, it implies that the drying time is independent of the number of shirts, assuming there is sufficient space and air circulation. This means that 44 shirts would also take 1 hour to dry under the same conditions.",
"helpful": true,
"flawed": false
},
{
"thought": "If the drying rack or space available for drying the shirts is limited, then drying 44 shirts might take longer because they would have to be dried in batches.",
"helpful": true,
"flawed": false
},
{
"thought": "The question does not specify the conditions under which the shirts are drying. Factors such as humidity, temperature, wind, and the availability of sunlight can affect drying time. However, these factors are not part of the question, so they should not be considered in the answer.",
"helpful": true,
"flawed": false
},
{
"thought": "The question might be interpreted as a direct proportionality problem, where twice the number of shirts would take twice as long to dry. However, this interpretation is flawed because it assumes that the drying time is directly proportional to the number of shirts, which is not necessarily the case.",
"helpful": true,
"flawed": true
},
{
"answer": "44 shirts would take the same amount of time to dry as 23 shirts, which is 1 hour, assuming that all shirts have enough space and air circulation to dry effectively.",
"score": 9
}
]
}
response_structured = ai(
"Kayley has three brothers. Each of her brothers has two sisters. How many sisters does Kayley have?",
output_schema=reason_question,
system=system_prompt.strip(),
)
{
"reasonings": [
{
"background": "This is a classic example of a logic puzzle that is often used to test one's ability to process information and avoid being misled by assumptions. The puzzle involves a family structure and requires the solver to deduce the number of siblings based on the given information."
},
{
"thought": "The question might be trying to trick the reader into thinking that each brother has different sisters.",
"helpful": true,
"flawed": false
},
{
"thought": "If Kayley has three brothers and each brother has two sisters, it might seem like there are six sisters in total.",
"helpful": false,
"flawed": true
},
{
"thought": "Since the brothers are Kayley's siblings, they must share the same sisters.",
"helpful": true,
"flawed": false
},
{
"thought": "The puzzle is designed to test comprehension and logical reasoning, not mathematical computation.",
"helpful": true,
"flawed": false
},
{
"answer": "Kayley has one sister.",
"score": 10
}
]
}
Your response must follow this JSON Schema:
{
"description": "Returns an answer to a question the user asked.",
"properties": {
"answer": {
"description": "Answer to the user's question.",
"type": "integer"
},
"ones_name": {
"description": "Name of the ones digit of the answer.",