From 1b689c91c068b8477242bd787fb53021a36aa170 Mon Sep 17 00:00:00 2001 From: n4ze3m Date: Mon, 4 Mar 2024 00:32:01 +0530 Subject: [PATCH] Add search mode settings and textarea focus on form submission --- src/components/Option/Models/index.tsx | 2 +- .../Option/Playground/PlaygroundForm.tsx | 28 +++++++------- src/components/Option/Prompt/index.tsx | 4 +- src/components/Option/Settings/other.tsx | 2 + .../Option/Settings/search-mode.tsx | 37 +++++++++++++++++++ src/hooks/useMessageOption.tsx | 5 ++- src/services/ollama.ts | 14 +++++++ src/web/local-google.ts | 22 +++++++---- 8 files changed, 88 insertions(+), 26 deletions(-) create mode 100644 src/components/Option/Settings/search-mode.tsx diff --git a/src/components/Option/Models/index.tsx b/src/components/Option/Models/index.tsx index d898e66..e1d16e8 100644 --- a/src/components/Option/Models/index.tsx +++ b/src/components/Option/Models/index.tsx @@ -212,7 +212,7 @@ export const ModelsBody = () => { diff --git a/src/components/Option/Playground/PlaygroundForm.tsx b/src/components/Option/Playground/PlaygroundForm.tsx index 1be9399..786f7f7 100644 --- a/src/components/Option/Playground/PlaygroundForm.tsx +++ b/src/components/Option/Playground/PlaygroundForm.tsx @@ -17,8 +17,20 @@ type Props = { } export const PlaygroundForm = ({ dropedFile }: Props) => { - const textareaRef = React.useRef(null) const inputRef = React.useRef(null) + const { + onSubmit, + selectedModel, + chatMode, + speechToTextLanguage, + stopStreamingRequest, + streaming: isSending, + webSearch, + setWebSearch, + selectedQuickPrompt, + textareaRef, + setSelectedQuickPrompt + } = useMessageOption() const textAreaFocus = () => { if (textareaRef.current) { @@ -62,19 +74,6 @@ export const PlaygroundForm = ({ dropedFile }: Props) => { useDynamicTextareaSize(textareaRef, form.values.message, 300) - const { - onSubmit, - selectedModel, - chatMode, - speechToTextLanguage, - stopStreamingRequest, - streaming: isSending, - webSearch, - setWebSearch, - selectedQuickPrompt, - setSelectedQuickPrompt - } = useMessageOption() - const { isListening, start, stop, transcript } = useSpeechRecognition() const { sendWhenEnter, setSendWhenEnter } = useWebUI() @@ -92,6 +91,7 @@ export const PlaygroundForm = ({ dropedFile }: Props) => { textareaRef.current?.focus() const interval = setTimeout(() => { textareaRef.current?.setSelectionRange(word.start, word.end) + setSelectedQuickPrompt(null) }, 100) return () => { clearInterval(interval) diff --git a/src/components/Option/Prompt/index.tsx b/src/components/Option/Prompt/index.tsx index 14aaf77..2d18bbe 100644 --- a/src/components/Option/Prompt/index.tsx +++ b/src/components/Option/Prompt/index.tsx @@ -224,7 +224,7 @@ export const PromptBody = () => { @@ -268,7 +268,7 @@ export const PromptBody = () => { diff --git a/src/components/Option/Settings/other.tsx b/src/components/Option/Settings/other.tsx index e1acd26..6a2e717 100644 --- a/src/components/Option/Settings/other.tsx +++ b/src/components/Option/Settings/other.tsx @@ -5,6 +5,7 @@ import { PageAssitDatabase } from "~libs/db" import { Select } from "antd" import { SUPPORTED_LANGUAGES } from "~utils/supporetd-languages" import { MoonIcon, SunIcon } from "lucide-react" +import { SearchModeSettings } from "./search-mode" export const SettingOther = () => { const { clearChat, speechToTextLanguage, setSpeechToTextLanguage } = @@ -82,6 +83,7 @@ export const SettingOther = () => { Delete + ) } diff --git a/src/components/Option/Settings/search-mode.tsx b/src/components/Option/Settings/search-mode.tsx new file mode 100644 index 0000000..5a1ab85 --- /dev/null +++ b/src/components/Option/Settings/search-mode.tsx @@ -0,0 +1,37 @@ +import { useQuery, useQueryClient } from "@tanstack/react-query" +import { Skeleton, Switch } from "antd" +import { + getIsSimpleInternetSearch, + setIsSimpleInternetSearch +} from "~services/ollama" + +export const SearchModeSettings = () => { + const { data, status } = useQuery({ + queryKey: ["fetchIsSimpleInternetSearch"], + queryFn: () => getIsSimpleInternetSearch() + }) + + const queryClient = useQueryClient() + + if (status === "pending" || status === "error") { + return + } + + return ( +
+ + Perform Simple Internet Search + + + { + setIsSimpleInternetSearch(checked) + queryClient.invalidateQueries({ + queryKey: ["fetchIsSimpleInternetSearch"] + }) + }} + /> +
+ ) +} diff --git a/src/hooks/useMessageOption.tsx b/src/hooks/useMessageOption.tsx index 07534d2..ca2446a 100644 --- a/src/hooks/useMessageOption.tsx +++ b/src/hooks/useMessageOption.tsx @@ -115,6 +115,7 @@ export const useMessageOption = () => { } = useStoreMessageOption() const navigate = useNavigate() + const textareaRef = React.useRef(null) const abortControllerRef = React.useRef(null) @@ -126,6 +127,7 @@ export const useMessageOption = () => { setIsLoading(false) setIsProcessing(false) setStreaming(false) + textareaRef?.current?.focus() navigate("/") } @@ -671,6 +673,7 @@ export const useMessageOption = () => { selectedQuickPrompt, setSelectedQuickPrompt, selectedSystemPrompt, - setSelectedSystemPrompt + setSelectedSystemPrompt, + textareaRef } } diff --git a/src/services/ollama.ts b/src/services/ollama.ts index 976a992..6c0ec14 100644 --- a/src/services/ollama.ts +++ b/src/services/ollama.ts @@ -288,4 +288,18 @@ export const setWebSearchFollowUpPrompt = async (prompt: string) => { export const setWebPrompts = async (prompt: string, followUpPrompt: string) => { await setWebSearchPrompt(prompt) await setWebSearchFollowUpPrompt(followUpPrompt) +} + +export const getIsSimpleInternetSearch = async () => { + const isSimpleInternetSearch = await storage.get("isSimpleInternetSearch") + if (!isSimpleInternetSearch || isSimpleInternetSearch.length === 0) { + return true + } + return isSimpleInternetSearch === "true" +} + + + +export const setIsSimpleInternetSearch = async (isSimpleInternetSearch: boolean) => { + await storage.set("isSimpleInternetSearch", isSimpleInternetSearch.toString()) } \ No newline at end of file diff --git a/src/web/local-google.ts b/src/web/local-google.ts index e76475a..f07cb82 100644 --- a/src/web/local-google.ts +++ b/src/web/local-google.ts @@ -5,7 +5,7 @@ import { MemoryVectorStore } from "langchain/vectorstores/memory" import { cleanUrl } from "~libs/clean-url" import { chromeRunTime } from "~libs/runtime" import { PageAssistHtmlLoader } from "~loader/html" -import { defaultEmbeddingChunkOverlap, defaultEmbeddingChunkSize, defaultEmbeddingModelForRag, getOllamaURL } from "~services/ollama" +import { defaultEmbeddingChunkOverlap, defaultEmbeddingChunkSize, defaultEmbeddingModelForRag, getIsSimpleInternetSearch, getOllamaURL } from "~services/ollama" const BLOCKED_HOSTS = [ "google.com", @@ -40,13 +40,7 @@ export const localGoogleSearch = async (query: string) => { (result) => { const title = result.querySelector("h3")?.textContent const link = result.querySelector("a")?.getAttribute("href") - let content = result.querySelector("div[data-sncf='2']")?.textContent - if(content === "") { - content = result.querySelector("div[data-sncf='1']")?.textContent - if(content === "") { - content = result.querySelector("div[data-sncf='3']")?.textContent - } - } + const content = Array.from(result.querySelectorAll("span")).map((span) => span.textContent).join(" ") return { title, link, content } } ) @@ -65,6 +59,18 @@ export const webSearch = async (query: string) => { const results = await localGoogleSearch(query) const searchResults = results.slice(0, TOTAL_SEARCH_RESULTS) + const isSimpleMode = await getIsSimpleInternetSearch() + + if (isSimpleMode) { + await getOllamaURL() + return searchResults.map((result) => { + return { + url: result.link, + content: result.content + } + }) + } + const docs: Document>[] = []; for (const result of searchResults) { const loader = new PageAssistHtmlLoader({