feat: add Ollama local provider support

- Added Ollama (local) as AI provider option
- Configure VPS endpoint for Ollama connection
- Auto-detect available models from Ollama server
- Support for vision-capable models (llama3.2-vision, etc)
This commit is contained in:
2026-04-04 19:46:14 +00:00
parent 97eb42c243
commit a395f0d696
5 changed files with 213 additions and 77 deletions

View File

@@ -347,7 +347,67 @@ export const analyzeCertificate = async (options: AnalyzeOptions): Promise<Repor
return analyzeWithAnthropic(file, apiKey, model);
case 'azure':
return analyzeWithAzure(file, apiKey, endpoint!, model);
case 'ollama':
return analyzeWithOllama(file, endpoint!, model);
default:
throw new Error(`Provedor não suportado: ${provider}`);
}
};
export const analyzeWithOllama = async (file: File, endpoint: string, model: string = 'llama3.2-vision'): Promise<ReportData> => {
if (!endpoint) {
throw new Error("O endpoint do Ollama é necessário. Configure o endereço da sua VPS.");
}
const base64Data = await new Promise<string>((resolve) => {
const reader = new FileReader();
reader.onloadend = () => resolve((reader.result as string).split(',')[1]);
reader.readAsDataURL(file);
});
const url = `${endpoint}/api/chat`;
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
model,
messages: [
{
role: 'user',
content: [
{
type: 'image',
data: base64Data
},
{
type: 'text',
text: PROMPT_BASE + "\n\nRetorne apenas JSON válido sem formatação markdown."
}
]
}
],
format: 'json',
options: {
temperature: 0.1,
num_predict: 4096
}
})
});
if (!response.ok) {
const error = await response.text();
throw new Error(`Erro do Ollama: ${error}`);
}
const data = await response.json();
const content = data.message?.content;
if (!content) {
throw new Error("Resposta vazia do Ollama");
}
return cleanAndParseJson(content) as ReportData;
};

View File

@@ -22,6 +22,8 @@ export const testApiKey = async (provider: AIProvider, apiKey: string, endpoint?
return await testAnthropic(apiKey);
case 'azure':
return await testAzure(apiKey, endpoint);
case 'ollama':
return await testOllama(endpoint);
default:
return { success: false, error: 'Provedor não suportado' };
}
@@ -33,6 +35,44 @@ export const testApiKey = async (provider: AIProvider, apiKey: string, endpoint?
}
};
const testOllama = async (endpoint?: string): Promise<TestResult> => {
if (!endpoint) {
return { success: false, error: 'Endereço do Ollama é obrigatório (ex: http://192.168.1.100:11434)' };
}
try {
const response = await fetch(`${endpoint}/api/tags`);
if (!response.ok) {
return { success: false, error: 'Não foi possível conectar ao Ollama. Verifique o endereço.' };
}
const data = await response.json();
const models = data.models?.map((m: any) => ({
id: m.name,
name: m.name
})) || [];
const visionModels = models.filter((m: ModelInfo) =>
m.id.includes('vision') ||
m.id.includes('llama3') ||
m.id.includes('qwen2') ||
m.id.includes('moondream')
);
if (visionModels.length > 0) {
return { success: true, models: visionModels };
}
return {
success: true,
models: models.length > 0 ? models : [{ id: 'llama3.2', name: 'Llama 3.2 (Padrão)' }]
};
} catch (error: any) {
return { success: false, error: 'Não foi possível conectar ao Ollama. Verifique o endereço e certifique-se que o Ollama está rodando.' };
}
};
const testGemini = async (apiKey: string): Promise<TestResult> => {
const { GoogleGenAI } = await import('@google/genai');