import { useCallback, useEffect, useState } from 'react'; import { CTAButton, DropdownSelect, FormGroup, FormInput, FormLabel, H3, Input, Spacer, Stack, useBoolean, } from 'ohmy-ui'; import { LoadingIndicator } from '@/ui/App'; interface SelectOption { label: string; value: string; } export default function Settings({ onDone = () => {}, submitButtonText = 'Save' }) { const [llmConfig, setLLMConfig] = useState<{ apiKey: string; model: SelectOption; models: { openai: SelectOption[]; ollama: SelectOption[]; anthropic: SelectOption[]; }; provider: SelectOption; providers: SelectOption[]; }>(); const [vectorDBConfig, setVectorDBConfig] = useState<{ url: string; apiKey: string; provider: SelectOption; options: SelectOption[]; }>(); const { value: isSaving, setTrue: startSaving, setFalse: stopSaving, } = useBoolean(false); const saveConfig = (event: React.FormEvent) => { event.preventDefault(); const newVectorConfig = { provider: vectorDBConfig?.provider.value, url: event.target.vectorDBUrl.value, apiKey: event.target.vectorDBApiKey.value, }; const newLLMConfig = { provider: llmConfig?.provider.value, model: llmConfig?.model.value, apiKey: event.target.llmApiKey.value, }; startSaving(); fetch('http://0.0.0.0:8000/settings', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ llm: newLLMConfig, vectorDB: newVectorConfig, }), }) .then(() => { onDone(); }) .finally(() => stopSaving()); }; const handleVectorDBChange = useCallback((newVectorDBProvider: SelectOption) => { setVectorDBConfig((config) => { if (config?.provider !== newVectorDBProvider) { return { ...config, provider: newVectorDBProvider, url: '', apiKey: '', }; } return config; }); }, []); const handleLLMProviderChange = useCallback((newLLMProvider: SelectOption) => { setLLMConfig((config) => { if (config?.provider !== newLLMProvider) { return { ...config, provider: newLLMProvider, model: config?.models[newLLMProvider.value][0], apiKey: '', }; } return config; }); }, []); const handleLLMModelChange = useCallback((newLLMModel: SelectOption) => { setLLMConfig((config) => { if (config?.model !== newLLMModel) { return { ...config, model: newLLMModel, }; } return config; }); }, []); useEffect(() => { const fetchConfig = async () => { const response = await fetch('http://0.0.0.0:8000/settings'); const settings = await response.json(); if (!settings.llm.model) { settings.llm.model = settings.llm.models[settings.llm.provider.value][0]; } setLLMConfig(settings.llm); setVectorDBConfig(settings.vectorDB); }; fetchConfig(); }, []); return (
LLM provider: LLM model: Vector DB provider: {submitButtonText} {isSaving && }
) }