from opperai import Opper
from pydantic import BaseModel, Field
import os
opper = Opper(http_bearer=os.getenv("OPPER_API_KEY", ""))
# Fictive E-commerce Database Schema
FICTIVE_DB_SCHEMA = """
users(id, username, email, first_name, last_name, created_at, updated_at, is_active)
products(id, name, description, price, category_id, stock_quantity, created_at, updated_at)
categories(id, name, description, parent_category_id)
orders(id, user_id, total_amount, status, created_at, updated_at)
order_items(id, order_id, product_id, quantity, unit_price, total_price)
reviews(id, user_id, product_id, rating, comment, created_at)
addresses(id, user_id, street, city, state, zip_code, country, is_default)
payments(id, order_id, amount, payment_method, status, created_at)
inventory(id, product_id, quantity, warehouse_id, last_updated)
warehouses(id, name, address, city, state, zip_code)
"""
class Query(BaseModel):
thoughts: str = Field(description="Thoughts about the query")
sql_query: str = Field(description="The generated SQL query")
confidence: float = Field(description="Confidence level (0.0 to 1.0) in the generated query")
if __name__ == "__main__":
result = opper.call(
name="generate_sql_query",
instructions="Given a conversation and a database structure, generate an sql query to answer the question",
input={
"conversation": "Find all users who signed up last month",
"db_structure": FICTIVE_DB_SCHEMA,
"comment": "None"
},
output_schema=Query
)
print(f"SQL Query: {result.json_payload['sql_query']}")