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:
@@ -4,7 +4,7 @@ import { PROVIDERS, type AIProvider } from '../types/providers';
|
||||
import { testApiKey, type ModelInfo } from '../services/apiTestService';
|
||||
|
||||
interface ApiKeySetupProps {
|
||||
onKeySave: (key: string, provider: AIProvider, model: string) => void;
|
||||
onKeySave: (key: string, provider: AIProvider, model: string, endpoint?: string) => void;
|
||||
}
|
||||
|
||||
const isValidApiKey = (key: string): boolean => {
|
||||
@@ -63,6 +63,7 @@ export const ApiKeySetup: React.FC<ApiKeySetupProps> = ({ onKeySave }) => {
|
||||
case 'openai': return 'https://platform.openai.com/api-keys';
|
||||
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';
|
||||
default: return '#';
|
||||
}
|
||||
};
|
||||
@@ -73,6 +74,7 @@ export const ApiKeySetup: React.FC<ApiKeySetupProps> = ({ onKeySave }) => {
|
||||
case 'openai': return 'OpenAI';
|
||||
case 'anthropic': return 'Anthropic (Claude)';
|
||||
case 'azure': return 'Azure OpenAI';
|
||||
case 'ollama': return 'Ollama (Local)';
|
||||
default: return 'API';
|
||||
}
|
||||
};
|
||||
@@ -115,7 +117,11 @@ export const ApiKeySetup: React.FC<ApiKeySetupProps> = ({ onKeySave }) => {
|
||||
};
|
||||
|
||||
const handleSave = () => {
|
||||
if (localApiKey.trim() && isValidApiKey(localApiKey)) {
|
||||
if (provider === 'ollama') {
|
||||
if (endpoint.trim()) {
|
||||
onKeySave('', provider, model, endpoint.trim());
|
||||
}
|
||||
} else if (localApiKey.trim() && isValidApiKey(localApiKey)) {
|
||||
onKeySave(localApiKey.trim(), provider, model);
|
||||
}
|
||||
};
|
||||
@@ -157,79 +163,86 @@ export const ApiKeySetup: React.FC<ApiKeySetupProps> = ({ onKeySave }) => {
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Azure Endpoint (only show if Azure is selected) */}
|
||||
{provider === 'azure' && (
|
||||
{/* Azure/Ollama Endpoint */}
|
||||
{(provider === 'azure' || provider === 'ollama') && (
|
||||
<div>
|
||||
<label htmlFor="azure-endpoint" className="block text-sm font-medium text-slate-700 dark:text-slate-300 mb-2">
|
||||
Endpoint do Azure
|
||||
<label htmlFor="provider-endpoint" className="block text-sm font-medium text-slate-700 dark:text-slate-300 mb-2">
|
||||
{provider === 'ollama' ? 'Endereço do Ollama (VPS)' : 'Endpoint do Azure'}
|
||||
</label>
|
||||
<input
|
||||
type="url"
|
||||
id="azure-endpoint"
|
||||
id="provider-endpoint"
|
||||
className="block w-full rounded-xl border-slate-300 dark:border-slate-600 bg-white/50 dark:bg-slate-700/50 py-3 px-4 text-slate-900 dark:text-slate-100 shadow-sm focus:border-blue-500 focus:ring-blue-500 sm:text-sm"
|
||||
placeholder="https://seu-resource.openai.azure.com"
|
||||
placeholder={provider === 'ollama' ? 'http://192.168.1.100:11434' : 'https://seu-resource.openai.azure.com'}
|
||||
value={endpoint}
|
||||
onChange={(e) => setEndpoint(e.target.value)}
|
||||
/>
|
||||
{provider === 'ollama' && (
|
||||
<p className="mt-1 text-xs text-slate-500">
|
||||
Informe o IP público da sua VPS e a porta (padrão: 11434)
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* API Key Input */}
|
||||
<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)})
|
||||
</label>
|
||||
<div className="relative">
|
||||
<div className="pointer-events-none absolute inset-y-0 left-0 flex items-center pl-3">
|
||||
<KeyIcon className="h-5 w-5 text-slate-400" />
|
||||
{/* API Key Input (not needed for Ollama) */}
|
||||
{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)})
|
||||
</label>
|
||||
<div className="relative">
|
||||
<div className="pointer-events-none absolute inset-y-0 left-0 flex items-center pl-3">
|
||||
<KeyIcon className="h-5 w-5 text-slate-400" />
|
||||
</div>
|
||||
<input
|
||||
type="password"
|
||||
id="api-key-setup"
|
||||
className={`block w-full rounded-xl border-slate-300 dark:border-slate-600 bg-white/50 dark:bg-slate-700/50 py-3 pl-10 pr-24 text-slate-900 dark:text-slate-100 shadow-sm focus:border-blue-500 focus:ring-blue-500 sm:text-sm transition-all
|
||||
${isValid === false ? 'border-red-500 focus:border-red-500 focus:ring-red-500' : ''}
|
||||
${testStatus === 'success' ? 'border-green-500 focus:border-green-500 focus:ring-green-500' : ''}`}
|
||||
placeholder="Cole sua chave aqui"
|
||||
value={localApiKey}
|
||||
onChange={handleChange}
|
||||
onKeyDown={(e) => e.key === 'Enter' && handleTestApi()}
|
||||
autoComplete="off"
|
||||
/>
|
||||
<div className="absolute inset-y-0 right-0 flex items-center pr-2">
|
||||
<button
|
||||
type="button"
|
||||
onClick={handleTestApi}
|
||||
disabled={isTesting || !localApiKey.trim()}
|
||||
className={`flex items-center gap-1 px-2 py-1 rounded-lg text-xs font-medium transition-all
|
||||
${testStatus === 'success'
|
||||
? 'bg-green-100 text-green-700 dark:bg-green-900/30 dark:text-green-400'
|
||||
: testStatus === 'error'
|
||||
? 'bg-red-100 text-red-700 dark:bg-red-900/30 dark:text-red-400'
|
||||
: 'bg-slate-200 dark:bg-slate-600 text-slate-600 dark:text-slate-300 hover:bg-blue-500 hover:text-white'
|
||||
} disabled:opacity-50 disabled:cursor-not-allowed`}
|
||||
>
|
||||
{isTesting ? (
|
||||
<span className="w-3 h-3 border-2 border-current border-t-transparent rounded-full animate-spin"></span>
|
||||
) : testStatus === 'success' ? (
|
||||
<svg className="w-3 h-3" fill="none" viewBox="0 0 24 24" stroke="currentColor">
|
||||
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M5 13l4 4L19 7" />
|
||||
</svg>
|
||||
) : (
|
||||
<TestTubeIcon className="w-3 h-3" />
|
||||
)}
|
||||
<span>{isTesting ? 'Testando...' : testStatus === 'success' ? 'OK' : 'Testar'}</span>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<input
|
||||
type="password"
|
||||
id="api-key-setup"
|
||||
className={`block w-full rounded-xl border-slate-300 dark:border-slate-600 bg-white/50 dark:bg-slate-700/50 py-3 pl-10 pr-24 text-slate-900 dark:text-slate-100 shadow-sm focus:border-blue-500 focus:ring-blue-500 sm:text-sm transition-all
|
||||
${isValid === false ? 'border-red-500 focus:border-red-500 focus:ring-red-500' : ''}
|
||||
${testStatus === 'success' ? 'border-green-500 focus:border-green-500 focus:ring-green-500' : ''}`}
|
||||
placeholder="Cole sua chave aqui"
|
||||
value={localApiKey}
|
||||
onChange={handleChange}
|
||||
onKeyDown={(e) => e.key === 'Enter' && handleTestApi()}
|
||||
autoComplete="off"
|
||||
/>
|
||||
<div className="absolute inset-y-0 right-0 flex items-center pr-2">
|
||||
<button
|
||||
type="button"
|
||||
onClick={handleTestApi}
|
||||
disabled={isTesting || !localApiKey.trim()}
|
||||
className={`flex items-center gap-1 px-2 py-1 rounded-lg text-xs font-medium transition-all
|
||||
${testStatus === 'success'
|
||||
? 'bg-green-100 text-green-700 dark:bg-green-900/30 dark:text-green-400'
|
||||
: testStatus === 'error'
|
||||
? 'bg-red-100 text-red-700 dark:bg-red-900/30 dark:text-red-400'
|
||||
: 'bg-slate-200 dark:bg-slate-600 text-slate-600 dark:text-slate-300 hover:bg-blue-500 hover:text-white'
|
||||
} disabled:opacity-50 disabled:cursor-not-allowed`}
|
||||
>
|
||||
{isTesting ? (
|
||||
<span className="w-3 h-3 border-2 border-current border-t-transparent rounded-full animate-spin"></span>
|
||||
) : testStatus === 'success' ? (
|
||||
<svg className="w-3 h-3" fill="none" viewBox="0 0 24 24" stroke="currentColor">
|
||||
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M5 13l4 4L19 7" />
|
||||
</svg>
|
||||
) : (
|
||||
<TestTubeIcon className="w-3 h-3" />
|
||||
)}
|
||||
<span>{isTesting ? 'Testando...' : testStatus === 'success' ? 'OK' : 'Testar'}</span>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
{error && (
|
||||
<p className="mt-2 text-xs text-red-600 dark:text-red-400" role="alert">
|
||||
{error}
|
||||
{error && (
|
||||
<p className="mt-2 text-xs text-red-600 dark:text-red-400" role="alert">
|
||||
{error}
|
||||
</p>
|
||||
)}
|
||||
<p className="mt-2 text-xs text-slate-500 dark:text-slate-400">
|
||||
Não tem uma chave? <a href={getProviderLink(provider)} target="_blank" rel="noopener noreferrer" className="font-semibold text-blue-600 hover:text-blue-700 dark:text-blue-400 hover:underline">Obtenha aqui</a>.
|
||||
</p>
|
||||
)}
|
||||
<p className="mt-2 text-xs text-slate-500 dark:text-slate-400">
|
||||
Não tem uma chave? <a href={getProviderLink(provider)} target="_blank" rel="noopener noreferrer" className="font-semibold text-blue-600 hover:text-blue-700 dark:text-blue-400 hover:underline">Obtenha aqui</a>.
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Model Selector */}
|
||||
<div>
|
||||
@@ -268,7 +281,7 @@ export const ApiKeySetup: React.FC<ApiKeySetupProps> = ({ onKeySave }) => {
|
||||
|
||||
<button
|
||||
onClick={handleSave}
|
||||
disabled={!localApiKey.trim() || isValid === false}
|
||||
disabled={provider !== 'ollama' && (!localApiKey.trim() || isValid === false)}
|
||||
className="w-full flex justify-center items-center gap-2 bg-gradient-to-r from-blue-600 to-indigo-600 hover:from-blue-700 hover:to-indigo-700 text-white font-bold py-3.5 px-4 rounded-xl shadow-lg hover:shadow-blue-500/30 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500 disabled:opacity-50 disabled:cursor-not-allowed transition-all duration-300 transform active:scale-[0.98]"
|
||||
>
|
||||
<SaveIcon className="h-5 w-5" />
|
||||
|
||||
Reference in New Issue
Block a user