Simple Example
This example demonstrates the core features of the Go-LLMs library using a mock provider, making it easy to understand the basic functionality without requiring any API keys.
Overview
The Simple example showcases:
- Creating and using a mock LLM provider
- Defining schemas for structured data validation
- Generating structured outputs with schema validation
- Processing raw LLM responses into structured data
- Streaming responses
- Enhancing prompts with schema information
- Working with typed Go structs for structured data
Features Demonstrated
- Basic Text Generation - Simple text generation with a prompt
- Schema Definition - Creating JSON Schema compatible schemas for validation
- Structured Generation - Generating structured data with schema validation
- Response Processing - Extracting and validating JSON data from raw text responses
- Response Streaming - Streaming tokens as they're generated
- Prompt Enhancement - Enriching prompts with schema information
- Typed Data Handling - Working with Go structs for structured data
Running the Example
To run the example:
make example EXAMPLE=simple
./bin/simple
Note: This example uses a mock provider, so no API keys are required.
Structured Data Example
The example demonstrates structured data validation using a Person schema:
// Person defines a struct for our schema
type Person struct {
Name string `json:"name" validate:"required" description:"Person's name"`
Age int `json:"age" validate:"min=0,max=120" description:"Age in years"`
Email string `json:"email" validate:"required,email" description:"Email address"`
}
The schema includes:
- Required fields (name, email)
- Integer validation with min/max values
- Format validation for email
Key Components
- MockProvider - Simulates LLM provider responses
- Schema - Defines the structure and validation rules for data
- Validator - Validates structured outputs against schemas
- StructuredProcessor - Processes raw responses into structured data
- PromptEnhancer - Enriches prompts with schema information
Learning Path
This example serves as an introduction to the library. After understanding this example, you can explore:
- Anthropic Example - Working with a real LLM provider
- Multi Example - Using multiple providers together
- Agent Example - Building an agent with tools for interaction
- Other specialized examples in the examples directory