import os
from openai import OpenAI
from pydantic import BaseModel
from typing import List
# Define your data model
class Entities(BaseModel):
attributes: List[str]
colors: List[str]
animals: List[str]
requesty_api_key = "YOUR_REQUESTY_API_KEY" # Safely load your API key
# Initialize OpenAI client with Requesty router
client = OpenAI(
api_key=requesty_api_key,
base_url="https://router.requesty.ai/v1",
)
# Request a JSON response
response = client.chat.completions.create(
model="openai/gpt-4o", # Works with any supported model
messages=[
{
"role": "system",
"content": "Extract entities from the input text and return them in JSON format with the following structure: {\"attributes\": [...], \"colors\": [...], \"animals\": [...]}"
},
{
"role": "user",
"content": "The quick brown fox jumps over the lazy dog with piercing blue eyes",
},
],
response_format={"type": "json_object"}
)
# Parse with Pydantic
content = response.choices[0].message.content
extracted = Entities.model_validate_json(content)
print(f"Attributes: {extracted.attributes}")
print(f"Colors: {extracted.colors}")
print(f"Animals: {extracted.animals}")