|
|
| async function ollamaAvailable() { |
| try { |
| const response = await fetch(`${APP_CONFIG.ollama.baseUrl}/api/tags`, { |
| method: "GET" |
| }); |
| return response.ok; |
| } catch (e) { |
| return false; |
| } |
| } |
|
|
| async function callAI(prompt) { |
| const available = await ollamaAvailable(); |
|
|
| |
| if (!available) { |
| return { |
| status: "offline", |
| text: "Ollama n'est pas détecté sur http://localhost:11434\n\n1. Téléchargez Ollama: https://ollama.com/download\n2. Installez et lancez l'application\n3. Dans un terminal, exécutez: ollama serve\n4. Configurez l'URL dans les paramètres\n\nAucune clé API n'est requise - tout fonctionne localement", |
| type: 'text' |
| }; |
| } |
| |
| try { |
| const response = await fetch(`${APP_CONFIG.ollama.baseUrl}/api/generate`, { |
| method: "POST", |
| headers: { |
| "Content-Type": "application/json", |
| "Authorization": `Bearer ${APP_CONFIG.ollama.apiKey}` |
| }, |
| body: JSON.stringify({ |
| model: APP_CONFIG.ollama.defaultModel, |
| prompt: prompt, |
| stream: false |
| }) |
| }); |
|
|
| if (!response.ok) { |
| throw new Error(`Ollama API error: ${response.status}`); |
| } |
|
|
| const data = await response.json(); |
| return { |
| status: "online", |
| text: data.response, |
| type: 'text' |
| }; |
| } catch (error) { |
| console.error('Ollama API call failed:', error); |
| throw error; |
| } |
| } |
| async function generateImage(prompt) { |
| if (APP_CONFIG.aiMode === 'demo') { |
| return { |
| url: 'http://static.photos/technology/640x360/42', |
| type: 'image' |
| }; |
| } |
|
|
| try { |
| const response = await fetch(`${APP_CONFIG.ollama.baseUrl}/api/generate`, { |
| method: 'POST', |
| headers: { |
| 'Content-Type': 'application/json', |
| 'Authorization': `Bearer ${APP_CONFIG.ollama.apiKey}` |
| }, |
| body: JSON.stringify({ |
| model: 'stable-diffusion', |
| prompt: prompt, |
| stream: false |
| }) |
| }); |
|
|
| if (!response.ok) { |
| throw new Error(`Ollama image generation error: ${response.status}`); |
| } |
|
|
| const data = await response.json(); |
| return { |
| url: data.response, |
| type: 'image', |
| model: data.model |
| }; |
| } catch (error) { |
| console.error('Image generation failed:', error); |
| throw error; |
| } |
| } |
|
|