diff --git a/backend/danswer/llm/factory.py b/backend/danswer/llm/factory.py index 9f0f70f92e8..0920be752bf 100644 --- a/backend/danswer/llm/factory.py +++ b/backend/danswer/llm/factory.py @@ -71,6 +71,7 @@ def _create_llm(model: str) -> LLM: api_base=llm_provider.api_base, api_version=llm_provider.api_version, custom_config=llm_provider.custom_config, + temperature=temperature_override, additional_headers=additional_headers, long_term_logger=long_term_logger, ) @@ -128,11 +129,13 @@ def get_llm( api_base: str | None = None, api_version: str | None = None, custom_config: dict[str, str] | None = None, - temperature: float = GEN_AI_TEMPERATURE, + temperature: float | None = None, timeout: int = QA_TIMEOUT, additional_headers: dict[str, str] | None = None, long_term_logger: LongTermLogger | None = None, ) -> LLM: + if temperature is None: + temperature = GEN_AI_TEMPERATURE return DefaultMultiLLM( model_provider=provider, model_name=model, diff --git a/web/src/app/admin/configuration/llm/interfaces.ts b/web/src/app/admin/configuration/llm/interfaces.ts index 54c12d1e8fa..b47fcc6ce2a 100644 --- a/web/src/app/admin/configuration/llm/interfaces.ts +++ b/web/src/app/admin/configuration/llm/interfaces.ts @@ -89,3 +89,6 @@ export const getProviderIcon = (providerName: string, modelName?: string) => { return CPUIcon; } }; + +export const isAnthropic = (provider: string, modelName: string) => + provider === "anthropic" || modelName.toLowerCase().includes("claude"); diff --git a/web/src/app/chat/ChatPage.tsx b/web/src/app/chat/ChatPage.tsx index 11a4dd0f265..e433ad2ea7c 100644 --- a/web/src/app/chat/ChatPage.tsx +++ b/web/src/app/chat/ChatPage.tsx @@ -411,7 +411,7 @@ export function ChatPage({ // reset LLM overrides (based on chat session!) llmOverrideManager.updateModelOverrideForChatSession(selectedChatSession); - llmOverrideManager.setTemperature(null); + llmOverrideManager.updateTemperature(null); // remove uploaded files setCurrentMessageFiles([]); diff --git a/web/src/app/chat/RegenerateOption.tsx b/web/src/app/chat/RegenerateOption.tsx index dd92bee0dbc..48ac766aadd 100644 --- a/web/src/app/chat/RegenerateOption.tsx +++ b/web/src/app/chat/RegenerateOption.tsx @@ -14,7 +14,6 @@ import { destructureValue, getFinalLLM, structureValue } from "@/lib/llm/utils"; import { useState } from "react"; import { Hoverable } from "@/components/Hoverable"; import { Popover } from "@/components/popover/Popover"; -import { StarFeedback } from "@/components/icons/icons"; import { IconType } from "react-icons"; import { FiRefreshCw } from "react-icons/fi"; diff --git a/web/src/app/chat/modal/configuration/LlmTab.tsx b/web/src/app/chat/modal/configuration/LlmTab.tsx index 4e51a21933e..46db83e4e0f 100644 --- a/web/src/app/chat/modal/configuration/LlmTab.tsx +++ b/web/src/app/chat/modal/configuration/LlmTab.tsx @@ -35,25 +35,9 @@ export const LlmTab = forwardRef( checkPersonaRequiresImageGeneration(currentAssistant); const { llmProviders } = useChatContext(); - const { setLlmOverride, temperature, setTemperature } = llmOverrideManager; + const { setLlmOverride, temperature, updateTemperature } = + llmOverrideManager; const [isTemperatureExpanded, setIsTemperatureExpanded] = useState(false); - const [localTemperature, setLocalTemperature] = useState( - temperature || 0 - ); - const debouncedSetTemperature = useCallback( - (value: number) => { - const debouncedFunction = debounce((value: number) => { - setTemperature(value); - }, 300); - return debouncedFunction(value); - }, - [setTemperature] - ); - - const handleTemperatureChange = (value: number) => { - setLocalTemperature(value); - debouncedSetTemperature(value); - }; return (
@@ -108,26 +92,26 @@ export const LlmTab = forwardRef( - handleTemperatureChange(parseFloat(e.target.value)) + updateTemperature(parseFloat(e.target.value)) } className="w-full p-2 border border-border rounded-md" min="0" max="2" step="0.01" - value={localTemperature} + value={temperature || 0} />
- {localTemperature} + {temperature}
diff --git a/web/src/components/chat_search/AssistantSelector.tsx b/web/src/components/chat_search/AssistantSelector.tsx index c0579663597..f4d8c9bb1f4 100644 --- a/web/src/components/chat_search/AssistantSelector.tsx +++ b/web/src/components/chat_search/AssistantSelector.tsx @@ -46,7 +46,7 @@ const AssistantSelector = ({ liveAssistant: Persona; onAssistantChange: (assistant: Persona) => void; chatSessionId?: string; - llmOverrideManager?: LlmOverrideManager; + llmOverrideManager: LlmOverrideManager; isMobile: boolean; }) => { const { finalAssistants } = useAssistants(); @@ -54,11 +54,9 @@ const AssistantSelector = ({ const dropdownRef = useRef(null); const { llmProviders } = useChatContext(); const { user } = useUser(); + const [assistants, setAssistants] = useState(finalAssistants); const [isTemperatureExpanded, setIsTemperatureExpanded] = useState(false); - const [localTemperature, setLocalTemperature] = useState( - llmOverrideManager?.temperature || 0 - ); // Initialize selectedTab from localStorage const [selectedTab, setSelectedTab] = useState(() => { @@ -92,21 +90,6 @@ const AssistantSelector = ({ } }; - const debouncedSetTemperature = useCallback( - (value: number) => { - const debouncedFunction = debounce((value: number) => { - llmOverrideManager?.setTemperature(value); - }, 300); - return debouncedFunction(value); - }, - [llmOverrideManager] - ); - - const handleTemperatureChange = (value: number) => { - setLocalTemperature(value); - debouncedSetTemperature(value); - }; - // Handle tab change and update localStorage const handleTabChange = (index: number) => { setSelectedTab(index); @@ -119,7 +102,7 @@ const AssistantSelector = ({ const [_, currentLlm] = getFinalLLM( llmProviders, liveAssistant, - llmOverrideManager?.llmOverride ?? null + llmOverrideManager.llmOverride ?? null ); const requiresImageGeneration = @@ -204,11 +187,10 @@ const AssistantSelector = ({ llmProviders={llmProviders} currentLlm={currentLlm} userDefault={userDefaultModel} - includeUserDefault={true} onSelect={(value: string | null) => { if (value == null) return; const { modelName, name, provider } = destructureValue(value); - llmOverrideManager?.setLlmOverride({ + llmOverrideManager.setLlmOverride({ name, provider, modelName, @@ -216,7 +198,6 @@ const AssistantSelector = ({ if (chatSessionId) { updateModelOverrideForChatSession(chatSessionId, value); } - setIsOpen(false); }} />
@@ -243,26 +224,31 @@ const AssistantSelector = ({ - handleTemperatureChange(parseFloat(e.target.value)) + llmOverrideManager.updateTemperature( + parseFloat(e.target.value) + ) } className="w-full p-2 border border-border rounded-md" min="0" max="2" step="0.01" - value={localTemperature} + value={llmOverrideManager.temperature?.toString() || "0"} />
- {localTemperature} + {llmOverrideManager.temperature}
diff --git a/web/src/components/llm/LLMList.tsx b/web/src/components/llm/LLMList.tsx index ff15a295a4e..036ce106d85 100644 --- a/web/src/components/llm/LLMList.tsx +++ b/web/src/components/llm/LLMList.tsx @@ -19,7 +19,6 @@ interface LlmListProps { scrollable?: boolean; hideProviderIcon?: boolean; requiresImageGeneration?: boolean; - includeUserDefault?: boolean; currentAssistant?: Persona; } @@ -31,7 +30,6 @@ export const LlmList: React.FC = ({ userDefault, scrollable, requiresImageGeneration, - includeUserDefault = false, }) => { const llmOptionsByProvider: { [provider: string]: { diff --git a/web/src/lib/hooks.ts b/web/src/lib/hooks.ts index 3645f8a5b56..82a515c08de 100644 --- a/web/src/lib/hooks.ts +++ b/web/src/lib/hooks.ts @@ -16,6 +16,7 @@ import { UsersResponse } from "./users/interfaces"; import { Credential } from "./connectors/credentials"; import { SettingsContext } from "@/components/settings/SettingsProvider"; import { PersonaCategory } from "@/app/admin/assistants/interfaces"; +import { isAnthropic } from "@/app/admin/configuration/llm/interfaces"; const CREDENTIAL_URL = "/api/manage/admin/credential"; @@ -71,7 +72,9 @@ export const useConnectorCredentialIndexingStatus = ( getEditable = false ) => { const { mutate } = useSWRConfig(); - const url = `${INDEXING_STATUS_URL}${getEditable ? "?get_editable=true" : ""}`; + const url = `${INDEXING_STATUS_URL}${ + getEditable ? "?get_editable=true" : "" + }`; const swrResponse = useSWR[]>( url, errorHandlingFetcher, @@ -157,7 +160,7 @@ export interface LlmOverrideManager { globalDefault: LlmOverride; setGlobalDefault: React.Dispatch>; temperature: number | null; - setTemperature: React.Dispatch>; + updateTemperature: (temperature: number | null) => void; updateModelOverrideForChatSession: (chatSession?: ChatSession) => void; } export function useLlmOverride( @@ -212,6 +215,20 @@ export function useLlmOverride( setTemperature(defaultTemperature !== undefined ? defaultTemperature : 0); }, [defaultTemperature]); + useEffect(() => { + if (isAnthropic(llmOverride.provider, llmOverride.modelName)) { + setTemperature((prevTemp) => Math.min(prevTemp ?? 0, 1.0)); + } + }, [llmOverride]); + + const updateTemperature = (temperature: number | null) => { + if (isAnthropic(llmOverride.provider, llmOverride.modelName)) { + setTemperature((prevTemp) => Math.min(temperature ?? 0, 1.0)); + } else { + setTemperature(temperature); + } + }; + return { updateModelOverrideForChatSession, llmOverride, @@ -219,9 +236,10 @@ export function useLlmOverride( globalDefault, setGlobalDefault, temperature, - setTemperature, + updateTemperature, }; } + /* EE Only APIs */