feat: suporte para provedor OpenRouter
This commit is contained in:
@@ -64,6 +64,7 @@ export const ApiKeySetup: React.FC<ApiKeySetupProps> = ({ onKeySave }) => {
|
||||
case 'anthropic': return 'https://console.anthropic.com/keys';
|
||||
case 'azure': return 'https://portal.azure.com/#view/Microsoft_AAD_IAM/ActiveDirectoryMenuBlade/RegisteredApps';
|
||||
case 'ollama': return 'https://ollama.com/download';
|
||||
case 'openrouter': return 'https://openrouter.ai/keys';
|
||||
default: return '#';
|
||||
}
|
||||
};
|
||||
@@ -75,6 +76,7 @@ export const ApiKeySetup: React.FC<ApiKeySetupProps> = ({ onKeySave }) => {
|
||||
case 'anthropic': return 'Anthropic (Claude)';
|
||||
case 'azure': return 'Azure OpenAI';
|
||||
case 'ollama': return 'Ollama (Local)';
|
||||
case 'openrouter': return 'OpenRouter';
|
||||
default: return 'API';
|
||||
}
|
||||
};
|
||||
@@ -244,8 +246,8 @@ export const ApiKeySetup: React.FC<ApiKeySetupProps> = ({ onKeySave }) => {
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Azure Endpoint */}
|
||||
{provider === 'azure' && (
|
||||
{/* Outros Provedores */}
|
||||
{provider !== 'ollama' && (
|
||||
<div>
|
||||
<label htmlFor="api-key-setup" className="block text-sm font-medium text-slate-700 dark:text-slate-300 mb-2">
|
||||
Chave de API ({getProviderLabel(provider)})
|
||||
|
||||
@@ -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' };
|
||||
}
|
||||
};
|
||||
+8
-2
@@ -1,6 +1,6 @@
|
||||
import { AIProvider } from './providers';
|
||||
|
||||
export type AIProvider = 'gemini' | 'openai' | 'anthropic' | 'azure' | 'ollama';
|
||||
export type AIProvider = 'gemini' | 'openai' | 'anthropic' | 'azure' | 'ollama' | 'openrouter';
|
||||
|
||||
export const OLLAMA_AUTO_DETECT_URLS = [
|
||||
'http://localhost:11434',
|
||||
@@ -57,7 +57,13 @@ export const PROVIDERS: ProviderConfig[] = [
|
||||
name: 'Ollama (Local)',
|
||||
description: 'LLMs rodando localmente na sua VPS',
|
||||
models: [],
|
||||
requiresEndpoint: true,
|
||||
defaultModel: 'llama3.2-vision'
|
||||
},
|
||||
{
|
||||
id: 'openrouter',
|
||||
name: 'OpenRouter',
|
||||
description: 'Acesso a múltiplos modelos através do OpenRouter',
|
||||
models: ['google/gemini-2.5-flash', 'google/gemini-2.5-pro', 'anthropic/claude-3.5-sonnet', 'openai/gpt-4o', 'meta-llama/llama-3.1-70b-instruct'],
|
||||
defaultModel: 'google/gemini-2.5-flash'
|
||||
}
|
||||
];
|
||||
Reference in New Issue
Block a user