feat(docs): add structured output example and details

This commit is contained in:
2025-09-04 14:49:01 +02:00
parent 5da20536af
commit 18769c134d
10 changed files with 647 additions and 22 deletions

View File

@@ -12,6 +12,7 @@ A professional, type-safe TypeScript package that provides a unified interface f
- 🔧 **Configurable**: Extensive configuration options for each provider
- 📦 **Zero Dependencies**: Lightweight with minimal external dependencies
- 🌐 **Local Support**: OpenWebUI integration for local/private AI models
- 🎨 **Structured Output**: Define custom response types for type-safe AI outputs
## 🚀 Quick Start
@@ -83,6 +84,69 @@ const openwebui = createOpenWebUIProvider({ apiKey: 'your-key', baseUrl: 'http:/
const provider = createProvider('claude', { apiKey: 'your-key' });
```
## 🎨 Structured Response Types
Define custom response types for type-safe, structured AI outputs:
```typescript
import { createResponseType, validateResponseType } from 'simple-ai-provider';
// Define your response type
interface UserProfile {
name: string;
age: number;
email: string;
preferences: {
theme: 'light' | 'dark';
notifications: boolean;
};
}
const userProfileType = createResponseType<UserProfile>(
`{
name: string;
age: number;
email: string;
preferences: {
theme: 'light' | 'dark';
notifications: boolean;
};
}`,
'A user profile with personal information and preferences',
{
name: 'John Doe',
age: 30,
email: 'john@example.com',
preferences: { theme: 'dark', notifications: true }
}
);
// Use with any provider
const response = await claude.complete({
messages: [
{ role: 'user', content: 'Generate a user profile for a software developer' }
],
responseType: userProfileType,
maxTokens: 500
});
// Validate and get typed response
const validation = validateResponseType(response.content, userProfileType);
if (validation.isValid) {
const userProfile = validation.data as UserProfile;
console.log(`Name: ${userProfile.name}`);
console.log(`Theme: ${userProfile.preferences.theme}`);
}
```
### Key Benefits
- **Type Safety**: Get fully typed responses from AI providers
- **Automatic Prompting**: System prompts are automatically generated
- **Validation**: Built-in response validation and parsing
- **Consistency**: Ensures AI outputs match your expected format
- **Developer Experience**: IntelliSense and compile-time type checking
## 📝 Environment Variables
Set up your API keys: