Complete Example Project
// complete-example.ts
import { AIModel, ConfigManager, ProviderRegistry } from './ai-model-lib';
// 1. Setup configuration
const config = ConfigManager.getInstance();
config.setDefaultConfig({
temperature: 0.7,
maxTokens: 1000,
debug: process.env.NODE_ENV === 'development'
});
// 2. Register custom provider if needed
ProviderRegistry.registerProvider('local-ai', {
name: 'Local AI',
baseURL: 'http://localhost:8080/v1',
defaultModel: 'local-model'
});
// 3. Create AI instance
const ai = config.createModel({
apiKey: process.env.OPENROUTER_API_KEY!,
provider: 'openrouter',
systemPrompt: 'You are a helpful and concise assistant.'
});
// 4. Use in application
async function chatWithUser(message: string): Promise<string> {
try {
const response = await ai.sendTextMessage(message);
// Log usage for monitoring
console.log(`Tokens used: ${response.usage.totalTokens}`);
return response.content;
} catch (error) {
console.error('AI service error:', error.message);
return 'Sorry, I encountered an error. Please try again.';
}
}
// 5. Example usage
async function main() {
const answer1 = await chatWithUser('What is the weather like?');
console.log('AI:', answer1);
const answer2 = await chatWithUser('How about tomorrow?');
console.log('AI:', answer2);
}
main();Source captured: 2026-10-11