mirror of
				https://github.com/ItzCrazyKns/Perplexica.git
				synced 2025-11-03 20:28:14 +00:00 
			
		
		
		
	User customizable context window for ollama models.
This commit is contained in:
		@@ -23,6 +23,7 @@ interface SettingsType {
 | 
			
		||||
  customOpenaiApiKey: string;
 | 
			
		||||
  customOpenaiApiUrl: string;
 | 
			
		||||
  customOpenaiModelName: string;
 | 
			
		||||
  ollamaContextWindow: number;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
interface InputProps extends React.InputHTMLAttributes<HTMLInputElement> {
 | 
			
		||||
@@ -112,6 +113,11 @@ const Page = () => {
 | 
			
		||||
  const [automaticImageSearch, setAutomaticImageSearch] = useState(false);
 | 
			
		||||
  const [automaticVideoSearch, setAutomaticVideoSearch] = useState(false);
 | 
			
		||||
  const [savingStates, setSavingStates] = useState<Record<string, boolean>>({});
 | 
			
		||||
  const [contextWindowSize, setContextWindowSize] = useState(2048);
 | 
			
		||||
  const [isCustomContextWindow, setIsCustomContextWindow] = useState(false);
 | 
			
		||||
  const predefinedContextSizes = [
 | 
			
		||||
    1024, 2048, 3072, 4096, 8192, 16384, 32768, 65536, 131072
 | 
			
		||||
  ];
 | 
			
		||||
 | 
			
		||||
  useEffect(() => {
 | 
			
		||||
    const fetchConfig = async () => {
 | 
			
		||||
@@ -123,6 +129,7 @@ const Page = () => {
 | 
			
		||||
      });
 | 
			
		||||
 | 
			
		||||
      const data = (await res.json()) as SettingsType;
 | 
			
		||||
 | 
			
		||||
      setConfig(data);
 | 
			
		||||
 | 
			
		||||
      const chatModelProvidersKeys = Object.keys(data.chatModelProviders || {});
 | 
			
		||||
@@ -171,6 +178,9 @@ const Page = () => {
 | 
			
		||||
      setAutomaticVideoSearch(
 | 
			
		||||
        localStorage.getItem('autoVideoSearch') === 'true',
 | 
			
		||||
      );
 | 
			
		||||
      const storedContextWindow = parseInt(localStorage.getItem('ollamaContextWindow') ?? '2048');
 | 
			
		||||
      setContextWindowSize(storedContextWindow);
 | 
			
		||||
      setIsCustomContextWindow(!predefinedContextSizes.includes(storedContextWindow));
 | 
			
		||||
 | 
			
		||||
      setIsLoading(false);
 | 
			
		||||
    };
 | 
			
		||||
@@ -331,6 +341,8 @@ const Page = () => {
 | 
			
		||||
        localStorage.setItem('embeddingModelProvider', value);
 | 
			
		||||
      } else if (key === 'embeddingModel') {
 | 
			
		||||
        localStorage.setItem('embeddingModel', value);
 | 
			
		||||
      } else if (key === 'ollamaContextWindow') {
 | 
			
		||||
        localStorage.setItem('ollamaContextWindow', value.toString());
 | 
			
		||||
      }
 | 
			
		||||
    } catch (err) {
 | 
			
		||||
      console.error('Failed to save:', err);
 | 
			
		||||
@@ -548,6 +560,69 @@ const Page = () => {
 | 
			
		||||
                                ];
 | 
			
		||||
                          })()}
 | 
			
		||||
                        />
 | 
			
		||||
                        {selectedChatModelProvider === 'ollama' && (
 | 
			
		||||
                          <div className="flex flex-col space-y-1">
 | 
			
		||||
                            <p className="text-black/70 dark:text-white/70 text-sm">
 | 
			
		||||
                              Chat Context Window Size
 | 
			
		||||
                            </p>
 | 
			
		||||
                            <Select
 | 
			
		||||
                              value={isCustomContextWindow ? 'custom' : contextWindowSize.toString()}
 | 
			
		||||
                              onChange={(e) => {
 | 
			
		||||
                                const value = e.target.value;
 | 
			
		||||
                                if (value === 'custom') {
 | 
			
		||||
                                  setIsCustomContextWindow(true);
 | 
			
		||||
                                } else {
 | 
			
		||||
                                  setIsCustomContextWindow(false);
 | 
			
		||||
                                  const numValue = parseInt(value);
 | 
			
		||||
                                  setContextWindowSize(numValue);
 | 
			
		||||
                                  setConfig((prev) => ({
 | 
			
		||||
                                    ...prev!,
 | 
			
		||||
                                    ollamaContextWindow: numValue,
 | 
			
		||||
                                  }));
 | 
			
		||||
                                  saveConfig('ollamaContextWindow', numValue);
 | 
			
		||||
                                }
 | 
			
		||||
                              }}
 | 
			
		||||
                              options={[
 | 
			
		||||
                                ...predefinedContextSizes.map(size => ({
 | 
			
		||||
                                  value: size.toString(),
 | 
			
		||||
                                  label: `${size.toLocaleString()} tokens`
 | 
			
		||||
                                })),
 | 
			
		||||
                                { value: 'custom', label: 'Custom...' }
 | 
			
		||||
                              ]}
 | 
			
		||||
                            />
 | 
			
		||||
                            {isCustomContextWindow && (
 | 
			
		||||
                              <div className="mt-2">
 | 
			
		||||
                                <Input
 | 
			
		||||
                                  type="number"
 | 
			
		||||
                                  min={512}
 | 
			
		||||
                                  value={contextWindowSize}
 | 
			
		||||
                                  placeholder="Custom context window size (minimum 512)"
 | 
			
		||||
                                  isSaving={savingStates['ollamaContextWindow']}
 | 
			
		||||
                                  onChange={(e) => {
 | 
			
		||||
                                    // Allow any value to be typed
 | 
			
		||||
                                    const value = parseInt(e.target.value) || contextWindowSize;
 | 
			
		||||
                                    setContextWindowSize(value);
 | 
			
		||||
                                  }}
 | 
			
		||||
                                  onSave={(value) => {
 | 
			
		||||
                                    // Validate only when saving
 | 
			
		||||
                                    const numValue = Math.max(512, parseInt(value) || 2048);
 | 
			
		||||
                                    setContextWindowSize(numValue);
 | 
			
		||||
                                    setConfig((prev) => ({
 | 
			
		||||
                                      ...prev!,
 | 
			
		||||
                                      ollamaContextWindow: numValue,
 | 
			
		||||
                                    }));
 | 
			
		||||
                                    saveConfig('ollamaContextWindow', numValue);
 | 
			
		||||
                                  }}
 | 
			
		||||
                                />
 | 
			
		||||
                              </div>
 | 
			
		||||
                            )}
 | 
			
		||||
                            <p className="text-xs text-black/60 dark:text-white/60 mt-0.5">
 | 
			
		||||
                              {isCustomContextWindow 
 | 
			
		||||
                                ? "Adjust the context window size for Ollama models (minimum 512 tokens)" 
 | 
			
		||||
                                : "Adjust the context window size for Ollama models"}
 | 
			
		||||
                            </p>
 | 
			
		||||
                          </div>
 | 
			
		||||
                        )}
 | 
			
		||||
                      </div>
 | 
			
		||||
                    )}
 | 
			
		||||
                </div>
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user