Precise Prompting
from openai import OpenAI
api_key = "sk-proj-..."
client = OpenAI(api_key=api_key)
prompt = """
What are 5 interesting facts about the moon?
Output in JSON format without any other text:
{
"facts": [
]
}
"""
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{
"role": "user",
"content": prompt,
},
],
response_format={"type": "json_object"},
)
output = response.choices[0].message.content
print(output)
>>> Output (a JSON string object)
{
"facts": [
"The Moon is about ...",
"The Moon has no ...",
"The Moon is slowly ...",
"The Moon has a ...",
"A day on the ..."
]
}
Leverage Powerful Tools
from openai import OpenAI
from typing import List
from pydantic import BaseModel
api_key = "sk-proj-..."
client = OpenAI(api_key=api_key)
prompt = """
What are 5 interesting facts about the moon?
"""
class MoonFacts(BaseModel):
facts: List[str]
response = client.beta.chat.completions.parse(
model="gpt-4o-mini",
messages=[{"role": "user", "content": prompt}],
response_format=MoonFacts,
)
print(response.choices[0].message.parsed)
>>> Output (a pydantic model)
facts: [
"The Moon is about ...",
"The Moon has no ...",
"The Moon is slowly ...",
"The Moon has a ...",
"A day on the ..."
]
https://www.linkedin.com/posts/johnidouglas_llms-ai-machinelearning-activity-7312804788553842688-m7io/