feat: suporte para provedor OpenRouter
This commit is contained in:
@@ -349,6 +349,8 @@ export const analyzeCertificate = async (options: AnalyzeOptions): Promise<Repor
|
||||
return analyzeWithAzure(file, apiKey, endpoint!, model);
|
||||
case 'ollama':
|
||||
return analyzeWithOllama(file, endpoint!, model);
|
||||
case 'openrouter':
|
||||
return analyzeWithOpenRouter(file, apiKey, model);
|
||||
default:
|
||||
throw new Error(`Provedor não suportado: ${provider}`);
|
||||
}
|
||||
@@ -421,5 +423,58 @@ export const analyzeWithOllama = async (file: File, endpoint: string, model: str
|
||||
throw new Error("Resposta vazia do Ollama/OpenWebUI");
|
||||
}
|
||||
|
||||
return cleanAndParseJson(content) as ReportData;
|
||||
};
|
||||
|
||||
export const analyzeWithOpenRouter = async (file: File, apiKey: string, model: string = 'google/gemini-2.5-flash'): Promise<ReportData> => {
|
||||
if (!apiKey) {
|
||||
throw new Error("A chave de API do OpenRouter não foi fornecida.");
|
||||
}
|
||||
|
||||
const base64Data = await new Promise<string>((resolve) => {
|
||||
const reader = new FileReader();
|
||||
reader.onloadend = () => resolve((reader.result as string).split(',')[1]);
|
||||
reader.readAsDataURL(file);
|
||||
});
|
||||
|
||||
const response = await fetch('https://openrouter.ai/api/v1/chat/completions', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Authorization': `Bearer ${apiKey}`,
|
||||
'Content-Type': 'application/json',
|
||||
'HTTP-Referer': 'https://steelcheck.local',
|
||||
'X-Title': 'SteelCheck',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
model,
|
||||
messages: [
|
||||
{
|
||||
role: 'user',
|
||||
content: [
|
||||
{ type: 'text', text: PROMPT_BASE + "\n\nRetorne apenas JSON válido sem formatação markdown." },
|
||||
{
|
||||
type: 'image_url',
|
||||
image_url: { url: `data:${file.type};base64,${base64Data}` }
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
temperature: 0.1,
|
||||
response_format: { type: 'json_object' }
|
||||
})
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const error = await response.json();
|
||||
throw new Error(`Erro do OpenRouter: ${error.error?.message || 'Erro desconhecido'}`);
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
const content = data.choices[0]?.message?.content;
|
||||
|
||||
if (!content) {
|
||||
throw new Error("Resposta vazia do OpenRouter");
|
||||
}
|
||||
|
||||
return cleanAndParseJson(content) as ReportData;
|
||||
};
|
||||
@@ -26,6 +26,8 @@ export const testApiKey = async (provider: AIProvider, apiKey: string, endpoint?
|
||||
return await testAzure(apiKey, endpoint);
|
||||
case 'ollama':
|
||||
return await testOllama(endpoint);
|
||||
case 'openrouter':
|
||||
return await testOpenRouter(apiKey);
|
||||
default:
|
||||
return { success: false, error: 'Provedor não suportado' };
|
||||
}
|
||||
@@ -229,4 +231,30 @@ const testAzure = async (apiKey: string, endpoint: string): Promise<TestResult>
|
||||
} catch (error: any) {
|
||||
return { success: false, error: error.message || 'Erro ao conectar com Azure' };
|
||||
}
|
||||
};
|
||||
|
||||
const testOpenRouter = async (apiKey: string): Promise<TestResult> => {
|
||||
try {
|
||||
const response = await fetch('https://openrouter.ai/api/v1/models', {
|
||||
headers: { 'Authorization': `Bearer ${apiKey}` }
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const error = await response.json();
|
||||
return { success: false, error: error.error?.message || 'Chave de API inválida' };
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
models: [
|
||||
{ id: 'google/gemini-2.5-flash', name: 'Gemini 2.5 Flash' },
|
||||
{ id: 'google/gemini-2.5-pro', name: 'Gemini 2.5 Pro' },
|
||||
{ id: 'anthropic/claude-3.5-sonnet', name: 'Claude 3.5 Sonnet' },
|
||||
{ id: 'openai/gpt-4o', name: 'GPT-4o' },
|
||||
{ id: 'meta-llama/llama-3.1-70b-instruct', name: 'Llama 3.1 70B' }
|
||||
]
|
||||
};
|
||||
} catch (error: any) {
|
||||
return { success: false, error: error.message || 'Erro ao conectar com OpenRouter' };
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user