const API_URL = 'http://localhost:8000'; const API_KEY = 'demo-key-1'; class AIAPIClient { constructor(apiUrl, apiKey) { this.apiUrl = apiUrl; this.apiKey = apiKey; } async request(endpoint, options = {}) { const url = `${this.apiUrl}${endpoint}`; const headers = { 'Authorization': `Bearer ${this.apiKey}`, 'Content-Type': 'application/json', ...options.headers, }; const response = await fetch(url, { ...options, headers, }); if (!response.ok) { const error = await response.json(); throw new Error(`API Error: ${error.message || response.statusText}`); } return response.json(); } async healthCheck() { return this.request('/health', { method: 'GET' }); } async verifyApiKey() { return this.request('/auth/verify', { method: 'POST' }); } async chat(conversation, model = null, options = {}) { return this.request('/ai/chat', { method: 'POST', body: JSON.stringify({ conversation, model, options, }), }); } async simpleQuery(query, model = null) { const params = new URLSearchParams({ q: query }); if (model) params.append('model', model); return this.request(`/ai/query?${params}`, { method: 'GET' }); } async ragQuery(query, topK = 5, model = null, useRetrieval = true, filters = null) { return this.request('/rag/query', { method: 'POST', body: JSON.stringify({ query, top_k: topK, model, use_retrieval: useRetrieval, filters, }), }); } async generateImage(prompt, options = {}) { return this.request('/image/generate', { method: 'POST', body: JSON.stringify({ prompt, ...options, }), }); } async synthesizeVoice(text, voice = 'alloy', format = 'mp3', speed = 1.0) { return this.request('/voice/synthesize', { method: 'POST', body: JSON.stringify({ text, voice, format, speed, }), }); } async transcribeAudio(audioBase64, model = null, language = null) { return this.request('/voice/transcribe', { method: 'POST', body: JSON.stringify({ audio_base64: audioBase64, model, language, }), }); } async uploadDocument(filename, contentBase64, metadata = {}) { return this.request('/upload', { method: 'POST', body: JSON.stringify({ filename, content_base64: contentBase64, metadata, }), }); } async getDocumentSources(docId) { return this.request(`/docs/${docId}/sources`, { method: 'GET' }); } async getMetrics() { return this.request('/metrics', { method: 'GET' }); } async getAvailableModels() { return this.request('/rag/models', { method: 'GET' }); } } async function main() { const client = new AIAPIClient(API_URL, API_KEY); try { console.log('=== AI API Client Examples ===\n'); console.log('1. Health Check'); const health = await client.healthCheck(); console.log(JSON.stringify(health, null, 2)); console.log('\n'); console.log('2. Simple Query'); const queryResult = await client.simpleQuery('What is artificial intelligence?'); console.log(JSON.stringify(queryResult, null, 2)); console.log('\n'); console.log('3. Chat Conversation'); const chatResult = await client.chat([ { role: 'user', content: 'Tell me a fun fact about space' } ], null, { temperature: 0.8, max_tokens: 150 }); console.log(JSON.stringify(chatResult, null, 2)); console.log('\n'); console.log('4. RAG Query'); const ragResult = await client.ragQuery( 'What are the main features?', 5, null, true ); console.log(JSON.stringify(ragResult, null, 2)); console.log('\n'); console.log('5. Image Generation'); const imageResult = await client.generateImage( 'A futuristic cityscape at night', { size: '1024x1024', n: 1 } ); console.log('Image generated:', imageResult.images[0].url.substring(0, 100) + '...'); console.log('\n'); console.log('6. Voice Synthesis'); const voiceResult = await client.synthesizeVoice( 'Welcome to the AI API service.', 'alloy', 'mp3' ); console.log('Audio generated:', voiceResult.audio_url.substring(0, 100) + '...'); console.log('\n'); console.log('7. Document Upload'); const docContent = Buffer.from('This is a sample document.').toString('base64'); const uploadResult = await client.uploadDocument( 'sample.txt', docContent, { title: 'Sample', category: 'test' } ); console.log(JSON.stringify(uploadResult, null, 2)); console.log('\n'); console.log('8. Get Metrics'); const metrics = await client.getMetrics(); console.log(JSON.stringify(metrics, null, 2)); console.log('\n'); } catch (error) { console.error('Error:', error.message); } } if (typeof window === 'undefined') { main(); } if (typeof module !== 'undefined' && module.exports) { module.exports = AIAPIClient; }