Json Schema
Support for json schema enforced responses
README: JSON Schema Usage Example
Overview
This Python script demonstrates how to use an OpenAI-powered API to generate employee profiles in JSON format that conform to a predefined schema. It uses guided decoding to ensure the generated profiles are schema-compliant and provides validation using the jsonschema
library.
This example is tailored for testing with CentML’s hosted LLM endpoint, using environment variables for configuration.
Prerequisites
clone the codex repo
Python Environment:
- Ensure you have Python 3.7 or later installed.
- Install the required Python packages:
Environment Variables:
Set the following environment variables before running the script:
How It Works
- Schema Definition:
- The script defines a JSON schema for employee profiles, including fields like name, age, skills, and work_history.
- OpenAI API Interaction:
- The script interacts with the CentML-hosted OpenAI-compatible API to generate JSON profiles.
- The API generates responses based on the schema using guided decoding.
- Validation:
- Each generated JSON profile is validated against the predefined schema using
jsonschema
. - The script ensures that subsequent profiles have unique name and age fields.
- Each generated JSON profile is validated against the predefined schema using
- Environment Variables:
- The script dynamically reads configuration details (API key, hostname, and model name) from environment variables for flexibility.
Usage Instructions
-
Set Environment Variables: Export the required environment variables:
-
Run the Script: Execute the script to generate and validate employee profiles:
-
Output:
- The script will generate and print two valid, schema-compliant JSON profiles.
- Example output:
Both profiles are valid and unique!
Key Features
- Schema Validation: Ensures all generated JSON profiles adhere to the predefined schema.
- Guided Decoding: Uses guided decoding to enforce structure in the generated data.
Troubleshooting
- Missing Environment Variables:
If environment variables are not set, the script defaults to:
- API Key: “no_key”
- Hostname: “llama3-8b.user-1404.gcp.centml.org”
- Model Name: “meta-llama/Meta-Llama-3-8B-Instruct”
- JSON Validation Errors: Ensure the schema is correctly defined and matches the expected structure.
- Network Errors: Confirm that the hostname is reachable and the API key is valid.
Customization
To adapt the script for other schemas:
- Update the
sample_json_schema
object with your desired schema. - Adjust the prompts in the
messages
array accordingly.
For questions or additional support, feel free to reach out!