Json output

By specifying the prompt to generate responses in JSON format, I was able to successfully receive the output in the desired JSON structure using the following code.

from openai import OpenAI
import json

client = OpenAI(
    base_url="https://api.sambanova.ai/v1", 
    api_key="YOUR_API_KEY"
)

# Define the prompt for JSON output
prompt = '''Provide the information in valid JSON format with no extra text. Ensure it matches this structure exactly:
{
    "name": "string",
    "age": "integer",
    "location": "string"
}'''

query="My name is Omkar. I am from India and 26 years old."

response = client.chat.completions.create(
  model="Meta-Llama-3.1-8B-Instruct",
  messages=[
      {"role": "system", "content": prompt},
      {"role": "user", "content": query}
    ]
)
print("Query: ",query)
json_output = json.loads(response.choices[0].message.content)
print("Response:",json_output)

Output:

Query:  My name is Omkar. I am from India and 26 years old.
Response: {'name': 'Omkar', 'age': 26, 'location': 'India'}

Query:  My name is Jack. I am from USA and 34 years old.
Response: {'name': 'Jack', 'age': 34, 'location': 'USA'}

3 Likes