From f3e918c3e39debf7310a052799d6f1ae41f7b5f0 Mon Sep 17 00:00:00 2001 From: Damien Laureaux Date: Fri, 15 Nov 2024 07:04:45 +0100 Subject: [PATCH 01/31] chore(docs): fix Markdown lint issues in the docs --- docs/API/SEARCH.md | 34 ++++++------- docs/architecture/README.md | 2 +- docs/architecture/WORKING.md | 6 +-- docs/installation/NETWORKING.md | 90 ++++++++++++++++----------------- docs/installation/UPDATING.md | 24 ++++----- 5 files changed, 78 insertions(+), 78 deletions(-) diff --git a/docs/API/SEARCH.md b/docs/API/SEARCH.md index f87e788..9405bc5 100644 --- a/docs/API/SEARCH.md +++ b/docs/API/SEARCH.md @@ -79,24 +79,24 @@ The response from the API includes both the final message and the sources used t ```json { - "message": "Perplexica is an innovative, open-source AI-powered search engine designed to enhance the way users search for information online. Here are some key features and characteristics of Perplexica:\n\n- **AI-Powered Technology**: It utilizes advanced machine learning algorithms to not only retrieve information but also to understand the context and intent behind user queries, providing more relevant results [1][5].\n\n- **Open-Source**: Being open-source, Perplexica offers flexibility and transparency, allowing users to explore its functionalities without the constraints of proprietary software [3][10].", - "sources": [ - { - "pageContent": "Perplexica is an innovative, open-source AI-powered search engine designed to enhance the way users search for information online.", - "metadata": { - "title": "What is Perplexica, and how does it function as an AI-powered search ...", - "url": "https://askai.glarity.app/search/What-is-Perplexica--and-how-does-it-function-as-an-AI-powered-search-engine" - } - }, - { - "pageContent": "Perplexica is an open-source AI-powered search tool that dives deep into the internet to find precise answers.", - "metadata": { - "title": "Sahar Mor's Post", - "url": "https://www.linkedin.com/posts/sahar-mor_a-new-open-source-project-called-perplexica-activity-7204489745668694016-ncja" - } - } + "message": "Perplexica is an innovative, open-source AI-powered search engine designed to enhance the way users search for information online. Here are some key features and characteristics of Perplexica:\n\n- **AI-Powered Technology**: It utilizes advanced machine learning algorithms to not only retrieve information but also to understand the context and intent behind user queries, providing more relevant results [1][5].\n\n- **Open-Source**: Being open-source, Perplexica offers flexibility and transparency, allowing users to explore its functionalities without the constraints of proprietary software [3][10].", + "sources": [ + { + "pageContent": "Perplexica is an innovative, open-source AI-powered search engine designed to enhance the way users search for information online.", + "metadata": { + "title": "What is Perplexica, and how does it function as an AI-powered search ...", + "url": "https://askai.glarity.app/search/What-is-Perplexica--and-how-does-it-function-as-an-AI-powered-search-engine" + } + }, + { + "pageContent": "Perplexica is an open-source AI-powered search tool that dives deep into the internet to find precise answers.", + "metadata": { + "title": "Sahar Mor's Post", + "url": "https://www.linkedin.com/posts/sahar-mor_a-new-open-source-project-called-perplexica-activity-7204489745668694016-ncja" + } + } .... - ] + ] } ``` diff --git a/docs/architecture/README.md b/docs/architecture/README.md index b1fcfcb..5732471 100644 --- a/docs/architecture/README.md +++ b/docs/architecture/README.md @@ -1,4 +1,4 @@ -## Perplexica's Architecture +# Perplexica's Architecture Perplexica's architecture consists of the following key components: diff --git a/docs/architecture/WORKING.md b/docs/architecture/WORKING.md index e39de7a..75b20fd 100644 --- a/docs/architecture/WORKING.md +++ b/docs/architecture/WORKING.md @@ -1,4 +1,4 @@ -## How does Perplexica work? +# How does Perplexica work? Curious about how Perplexica works? Don't worry, we'll cover it here. Before we begin, make sure you've read about the architecture of Perplexica to ensure you understand what it's made up of. Haven't read it? You can read it [here](https://github.com/ItzCrazyKns/Perplexica/tree/master/docs/architecture/README.md). @@ -10,10 +10,10 @@ We'll understand how Perplexica works by taking an example of a scenario where a 4. After the information is retrieved, it is based on keyword-based search. We then convert the information into embeddings and the query as well, then we perform a similarity search to find the most relevant sources to answer the query. 5. After all this is done, the sources are passed to the response generator. This chain takes all the chat history, the query, and the sources. It generates a response that is streamed to the UI. -### How are the answers cited? +## How are the answers cited? The LLMs are prompted to do so. We've prompted them so well that they cite the answers themselves, and using some UI magic, we display it to the user. -### Image and Video Search +## Image and Video Search Image and video searches are conducted in a similar manner. A query is always generated first, then we search the web for images and videos that match the query. These results are then returned to the user. diff --git a/docs/installation/NETWORKING.md b/docs/installation/NETWORKING.md index baad296..6cb9bfe 100644 --- a/docs/installation/NETWORKING.md +++ b/docs/installation/NETWORKING.md @@ -10,27 +10,27 @@ This guide will show you how to make Perplexica available over a network. Follow 3. Stop and remove the existing Perplexica containers and images: -``` -docker compose down --rmi all -``` + ```bash + docker compose down --rmi all + ``` 4. Open the `docker-compose.yaml` file in a text editor like Notepad++ 5. Replace `127.0.0.1` with the IP address of the server Perplexica is running on in these two lines: -``` -args: - - NEXT_PUBLIC_API_URL=http://127.0.0.1:3001/api - - NEXT_PUBLIC_WS_URL=ws://127.0.0.1:3001 -``` + ```bash + args: + - NEXT_PUBLIC_API_URL=http://127.0.0.1:3001/api + - NEXT_PUBLIC_WS_URL=ws://127.0.0.1:3001 + ``` 6. Save and close the `docker-compose.yaml` file 7. Rebuild and restart the Perplexica container: -``` -docker compose up -d --build -``` + ```bash + docker compose up -d --build + ``` ## macOS @@ -38,37 +38,37 @@ docker compose up -d --build 2. Navigate to the directory with the `docker-compose.yaml` file: -``` -cd /path/to/docker-compose.yaml -``` + ```bash + cd /path/to/docker-compose.yaml + ``` 3. Stop and remove existing containers and images: -``` -docker compose down --rmi all -``` + ```bash + docker compose down --rmi all + ``` 4. Open `docker-compose.yaml` in a text editor like Sublime Text: -``` -nano docker-compose.yaml -``` + ```bash + nano docker-compose.yaml + ``` 5. Replace `127.0.0.1` with the server IP in these lines: -``` -args: - - NEXT_PUBLIC_API_URL=http://127.0.0.1:3001/api - - NEXT_PUBLIC_WS_URL=ws://127.0.0.1:3001 -``` + ```bash + args: + - NEXT_PUBLIC_API_URL=http://127.0.0.1:3001/api + - NEXT_PUBLIC_WS_URL=ws://127.0.0.1:3001 + ``` 6. Save and exit the editor 7. Rebuild and restart Perplexica: -``` -docker compose up -d --build -``` + ```bash + docker compose up -d --build + ``` ## Linux @@ -76,34 +76,34 @@ docker compose up -d --build 2. Navigate to the `docker-compose.yaml` directory: -``` -cd /path/to/docker-compose.yaml -``` + ```bash + cd /path/to/docker-compose.yaml + ``` 3. Stop and remove containers and images: -``` -docker compose down --rmi all -``` + ```bash + docker compose down --rmi all + ``` 4. Edit `docker-compose.yaml`: -``` -nano docker-compose.yaml -``` + ```bash + nano docker-compose.yaml + ``` 5. Replace `127.0.0.1` with the server IP: -``` -args: - - NEXT_PUBLIC_API_URL=http://127.0.0.1:3001/api - - NEXT_PUBLIC_WS_URL=ws://127.0.0.1:3001 -``` + ```bash + args: + - NEXT_PUBLIC_API_URL=http://127.0.0.1:3001/api + - NEXT_PUBLIC_WS_URL=ws://127.0.0.1:3001 + ``` 6. Save and exit the editor 7. Rebuild and restart Perplexica: -``` -docker compose up -d --build -``` + ```bash + docker compose up -d --build + ``` diff --git a/docs/installation/UPDATING.md b/docs/installation/UPDATING.md index 031a3e8..b41b05a 100644 --- a/docs/installation/UPDATING.md +++ b/docs/installation/UPDATING.md @@ -6,23 +6,23 @@ To update Perplexica to the latest version, follow these steps: 1. Clone the latest version of Perplexica from GitHub: -```bash - git clone https://github.com/ItzCrazyKns/Perplexica.git -``` + ```bash + git clone https://github.com/ItzCrazyKns/Perplexica.git + ``` 2. Navigate to the Project Directory. 3. Pull latest images from registry. -```bash -docker compose pull -``` + ```bash + docker compose pull + ``` 4. Update and Recreate containers. -```bash -docker compose up -d -``` + ```bash + docker compose up -d + ``` 5. Once the command completes running go to http://localhost:3000 and verify the latest changes. @@ -30,9 +30,9 @@ docker compose up -d 1. Clone the latest version of Perplexica from GitHub: -```bash - git clone https://github.com/ItzCrazyKns/Perplexica.git -``` + ```bash + git clone https://github.com/ItzCrazyKns/Perplexica.git + ``` 2. Navigate to the Project Directory 3. Execute `npm i` in both the `ui` folder and the root directory. From ecad0655779e75699223d0a7ff7e0775491e6345 Mon Sep 17 00:00:00 2001 From: ItzCrazyKns <95534749+ItzCrazyKns@users.noreply.github.com> Date: Wed, 27 Nov 2024 15:13:46 +0530 Subject: [PATCH 02/31] feat(searchAgent): handle empty fileIds --- src/agents/webSearchAgent.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/agents/webSearchAgent.ts b/src/agents/webSearchAgent.ts index 4303762..709d6aa 100644 --- a/src/agents/webSearchAgent.ts +++ b/src/agents/webSearchAgent.ts @@ -485,7 +485,7 @@ const basicWebSearch = ( llm: BaseChatModel, embeddings: Embeddings, optimizationMode: 'speed' | 'balanced' | 'quality', - fileIds: string[], + fileIds: string[] = [], ) => { const emitter = new eventEmitter(); From 177746235a347e1468ade07a7ef425d4011a0bc2 Mon Sep 17 00:00:00 2001 From: ItzCrazyKns <95534749+ItzCrazyKns@users.noreply.github.com> Date: Thu, 28 Nov 2024 20:47:18 +0530 Subject: [PATCH 03/31] feat(providers): add gemini --- package.json | 1 + sample.config.toml | 1 + src/config.ts | 3 ++ src/lib/providers/gemini.ts | 69 ++++++++++++++++++++++++++++++++ src/lib/providers/index.ts | 3 ++ src/routes/config.ts | 5 ++- ui/components/SettingsDialog.tsx | 17 ++++++++ yarn.lock | 53 ++++++++++++++++++++++++ 8 files changed, 151 insertions(+), 1 deletion(-) create mode 100644 src/lib/providers/gemini.ts diff --git a/package.json b/package.json index 6a67746..0368b21 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,7 @@ "@langchain/anthropic": "^0.2.3", "@langchain/community": "^0.2.16", "@langchain/openai": "^0.0.25", + "@langchain/google-genai": "^0.0.23", "@xenova/transformers": "^2.17.1", "axios": "^1.6.8", "better-sqlite3": "^11.0.0", diff --git a/sample.config.toml b/sample.config.toml index dddcc03..50ba95d 100644 --- a/sample.config.toml +++ b/sample.config.toml @@ -7,6 +7,7 @@ KEEP_ALIVE = "5m" # How long to keep Ollama models loaded into memory. (Instead OPENAI = "" # OpenAI API key - sk-1234567890abcdef1234567890abcdef GROQ = "" # Groq API key - gsk_1234567890abcdef1234567890abcdef ANTHROPIC = "" # Anthropic API key - sk-ant-1234567890abcdef1234567890abcdef +GEMINI = "" # Gemini API key - sk-1234567890abcdef1234567890abcdef [API_ENDPOINTS] SEARXNG = "http://localhost:32768" # SearxNG API URL diff --git a/src/config.ts b/src/config.ts index 8624e7f..001c259 100644 --- a/src/config.ts +++ b/src/config.ts @@ -14,6 +14,7 @@ interface Config { OPENAI: string; GROQ: string; ANTHROPIC: string; + GEMINI: string; }; API_ENDPOINTS: { SEARXNG: string; @@ -43,6 +44,8 @@ export const getGroqApiKey = () => loadConfig().API_KEYS.GROQ; export const getAnthropicApiKey = () => loadConfig().API_KEYS.ANTHROPIC; +export const getGeminiApiKey = () => loadConfig().API_KEYS.GEMINI; + export const getSearxngApiEndpoint = () => process.env.SEARXNG_API_URL || loadConfig().API_ENDPOINTS.SEARXNG; diff --git a/src/lib/providers/gemini.ts b/src/lib/providers/gemini.ts new file mode 100644 index 0000000..95764cf --- /dev/null +++ b/src/lib/providers/gemini.ts @@ -0,0 +1,69 @@ +import { + ChatGoogleGenerativeAI, + GoogleGenerativeAIEmbeddings, +} from '@langchain/google-genai'; +import { getGeminiApiKey } from '../../config'; +import logger from '../../utils/logger'; + +export const loadGeminiChatModels = async () => { + const geminiApiKey = getGeminiApiKey(); + + if (!geminiApiKey) return {}; + + try { + const chatModels = { + 'gemini-1.5-flash': { + displayName: 'Gemini 1.5 Flash', + model: new ChatGoogleGenerativeAI({ + modelName: 'gemini-1.5-flash', + temperature: 0.7, + apiKey: geminiApiKey, + }), + }, + 'gemini-1.5-flash-8b': { + displayName: 'Gemini 1.5 Flash 8B', + model: new ChatGoogleGenerativeAI({ + modelName: 'gemini-1.5-flash-8b', + temperature: 0.7, + apiKey: geminiApiKey, + }), + }, + 'gemini-1.5-pro': { + displayName: 'Gemini 1.5 Pro', + model: new ChatGoogleGenerativeAI({ + modelName: 'gemini-1.5-pro', + temperature: 0.7, + apiKey: geminiApiKey, + }), + }, + }; + + return chatModels; + } catch (err) { + logger.error(`Error loading Gemini models: ${err}`); + return {}; + } +}; + +export const loadGeminiEmbeddingsModels = async () => { + const geminiApiKey = getGeminiApiKey(); + + if (!geminiApiKey) return {}; + + try { + const embeddingModels = { + 'text-embedding-004': { + displayName: 'Text Embedding', + model: new GoogleGenerativeAIEmbeddings({ + apiKey: geminiApiKey, + modelName: 'text-embedding-004', + }), + }, + }; + + return embeddingModels; + } catch (err) { + logger.error(`Error loading Gemini embeddings model: ${err}`); + return {}; + } +}; diff --git a/src/lib/providers/index.ts b/src/lib/providers/index.ts index d919fd4..98846e7 100644 --- a/src/lib/providers/index.ts +++ b/src/lib/providers/index.ts @@ -3,18 +3,21 @@ import { loadOllamaChatModels, loadOllamaEmbeddingsModels } from './ollama'; import { loadOpenAIChatModels, loadOpenAIEmbeddingsModels } from './openai'; import { loadAnthropicChatModels } from './anthropic'; import { loadTransformersEmbeddingsModels } from './transformers'; +import { loadGeminiChatModels, loadGeminiEmbeddingsModels } from './gemini'; const chatModelProviders = { openai: loadOpenAIChatModels, groq: loadGroqChatModels, ollama: loadOllamaChatModels, anthropic: loadAnthropicChatModels, + gemini: loadGeminiChatModels, }; const embeddingModelProviders = { openai: loadOpenAIEmbeddingsModels, local: loadTransformersEmbeddingsModels, ollama: loadOllamaEmbeddingsModels, + gemini: loadGeminiEmbeddingsModels, }; export const getAvailableChatModelProviders = async () => { diff --git a/src/routes/config.ts b/src/routes/config.ts index f635e4b..38192b7 100644 --- a/src/routes/config.ts +++ b/src/routes/config.ts @@ -7,6 +7,7 @@ import { getGroqApiKey, getOllamaApiEndpoint, getAnthropicApiKey, + getGeminiApiKey, getOpenaiApiKey, updateConfig, } from '../config'; @@ -52,7 +53,8 @@ router.get('/', async (_, res) => { config['ollamaApiUrl'] = getOllamaApiEndpoint(); config['anthropicApiKey'] = getAnthropicApiKey(); config['groqApiKey'] = getGroqApiKey(); - + config['geminiApiKey'] = getGeminiApiKey(); + res.status(200).json(config); } catch (err: any) { res.status(500).json({ message: 'An error has occurred.' }); @@ -68,6 +70,7 @@ router.post('/', async (req, res) => { OPENAI: config.openaiApiKey, GROQ: config.groqApiKey, ANTHROPIC: config.anthropicApiKey, + GEMINI: config.geminiApiKey, }, API_ENDPOINTS: { OLLAMA: config.ollamaApiUrl, diff --git a/ui/components/SettingsDialog.tsx b/ui/components/SettingsDialog.tsx index 716dd7d..163857b 100644 --- a/ui/components/SettingsDialog.tsx +++ b/ui/components/SettingsDialog.tsx @@ -63,6 +63,7 @@ interface SettingsType { openaiApiKey: string; groqApiKey: string; anthropicApiKey: string; + geminiApiKey: string; ollamaApiUrl: string; } @@ -476,6 +477,22 @@ const SettingsDialog = ({ } /> +
+

+ Gemini API Key +

+ + setConfig({ + ...config, + geminiApiKey: e.target.value, + }) + } + /> +
)} {isLoading && ( diff --git a/yarn.lock b/yarn.lock index 0ea5916..5764b3c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -293,6 +293,11 @@ resolved "https://registry.yarnpkg.com/@esbuild/win32-x64/-/win32-x64-0.19.12.tgz#c57c8afbb4054a3ab8317591a0b7320360b444ae" integrity sha512-T1QyPSDCyMXaO3pzBkF96E8xMkiRYbUEZADd29SyPGabqxMViNoii+NcK7eWJAEoU6RZyEm5lVSIjTmcdoB9HA== +"@google/generative-ai@^0.7.0": + version "0.7.1" + resolved "https://registry.yarnpkg.com/@google/generative-ai/-/generative-ai-0.7.1.tgz#eb187c75080c0706245699dbc06816c830d8c6a7" + integrity sha512-WTjMLLYL/xfA5BW6xAycRPiAX7FNHKAxrid/ayqC1QMam0KAK0NbMeS9Lubw80gVg5xFMLE+H7pw4wdNzTOlxw== + "@huggingface/jinja@^0.2.2": version "0.2.2" resolved "https://registry.yarnpkg.com/@huggingface/jinja/-/jinja-0.2.2.tgz#faeb205a9d6995089bef52655ddd8245d3190627" @@ -380,6 +385,23 @@ zod "^3.22.4" zod-to-json-schema "^3.22.3" +"@langchain/core@>=0.2.16 <0.3.0": + version "0.2.36" + resolved "https://registry.yarnpkg.com/@langchain/core/-/core-0.2.36.tgz#75754c33aa5b9310dcf117047374a1ae011005a4" + integrity sha512-qHLvScqERDeH7y2cLuJaSAlMwg3f/3Oc9nayRSXRU2UuaK/SOhI42cxiPLj1FnuHJSmN0rBQFkrLx02gI4mcVg== + dependencies: + ansi-styles "^5.0.0" + camelcase "6" + decamelize "1.2.0" + js-tiktoken "^1.0.12" + langsmith "^0.1.56-rc.1" + mustache "^4.2.0" + p-queue "^6.6.2" + p-retry "4" + uuid "^10.0.0" + zod "^3.22.4" + zod-to-json-schema "^3.22.3" + "@langchain/core@>=0.2.9 <0.3.0": version "0.2.15" resolved "https://registry.yarnpkg.com/@langchain/core/-/core-0.2.15.tgz#1bb99ac4fffe935c7ba37edcaa91abfba3c82219" @@ -415,6 +437,15 @@ zod "^3.22.4" zod-to-json-schema "^3.22.3" +"@langchain/google-genai@^0.0.23": + version "0.0.23" + resolved "https://registry.yarnpkg.com/@langchain/google-genai/-/google-genai-0.0.23.tgz#e73af501bc1df4c7642b531759b82dc3eb7ae459" + integrity sha512-MTSCJEoKsfU1inz0PWvAjITdNFM4s41uvBCwLpcgx3jWJIEisczFD82x86ahYqJlb2fD6tohYSaCH/4tKAdkXA== + dependencies: + "@google/generative-ai" "^0.7.0" + "@langchain/core" ">=0.2.16 <0.3.0" + zod-to-json-schema "^3.22.4" + "@langchain/openai@^0.0.25", "@langchain/openai@~0.0.19": version "0.0.25" resolved "https://registry.yarnpkg.com/@langchain/openai/-/openai-0.0.25.tgz#8332abea1e3acb9b1169f90636e518c0ee90622e" @@ -712,6 +743,11 @@ resolved "https://registry.yarnpkg.com/@types/triple-beam/-/triple-beam-1.3.5.tgz#74fef9ffbaa198eb8b588be029f38b00299caa2c" integrity sha512-6WaYesThRMCl19iryMYP7/x2OVgCtbIVflDGFpWnb9irXI3UjYE4AzmYuiUKY1AJstGijoY+MgUszMgRxIYTYw== +"@types/uuid@^10.0.0": + version "10.0.0" + resolved "https://registry.yarnpkg.com/@types/uuid/-/uuid-10.0.0.tgz#e9c07fe50da0f53dc24970cca94d619ff03f6f6d" + integrity sha512-7gqG38EyHgyP1S+7+xomFtL+ZNHcKv6DwNaCZmJmo1vgMugyF3TCnXVg4t1uk89mLNwnLtnY3TpOpCOyp1/xHQ== + "@types/uuid@^9.0.1": version "9.0.8" resolved "https://registry.yarnpkg.com/@types/uuid/-/uuid-9.0.8.tgz#7545ba4fc3c003d6c756f651f3bf163d8f0f29ba" @@ -1900,6 +1936,18 @@ langchainhub@~0.0.8: resolved "https://registry.yarnpkg.com/langchainhub/-/langchainhub-0.0.8.tgz#fd4b96dc795e22e36c1a20bad31b61b0c33d3110" integrity sha512-Woyb8YDHgqqTOZvWIbm2CaFDGfZ4NTSyXV687AG4vXEfoNo7cGQp7nhl7wL3ehenKWmNEmcxCLgOZzW8jE6lOQ== +langsmith@^0.1.56-rc.1: + version "0.1.68" + resolved "https://registry.yarnpkg.com/langsmith/-/langsmith-0.1.68.tgz#848332e822fe5e6734a07f1c36b6530cc1798afb" + integrity sha512-otmiysWtVAqzMx3CJ4PrtUBhWRG5Co8Z4o7hSZENPjlit9/j3/vm3TSvbaxpDYakZxtMjhkcJTqrdYFipISEiQ== + dependencies: + "@types/uuid" "^10.0.0" + commander "^10.0.1" + p-queue "^6.6.2" + p-retry "4" + semver "^7.6.3" + uuid "^10.0.0" + langsmith@~0.1.1, langsmith@~0.1.7: version "0.1.14" resolved "https://registry.yarnpkg.com/langsmith/-/langsmith-0.1.14.tgz#2b889dbcfb49547614df276a4a5a063092a1585d" @@ -2568,6 +2616,11 @@ semver@^7.3.5, semver@^7.5.3, semver@^7.5.4: dependencies: lru-cache "^6.0.0" +semver@^7.6.3: + version "7.6.3" + resolved "https://registry.yarnpkg.com/semver/-/semver-7.6.3.tgz#980f7b5550bc175fb4dc09403085627f9eb33143" + integrity sha512-oVekP1cKtI+CTDvHWYFUcMtsK/00wmAEfyqKfNdARm8u1wNVhSgaX7A8d4UuIlUI5e84iEwOhs7ZPYRmzU9U6A== + send@0.18.0: version "0.18.0" resolved "https://registry.yarnpkg.com/send/-/send-0.18.0.tgz#670167cc654b05f5aa4a767f9113bb371bc706be" From 92f66266b0a0fafd153ad653708105511c10e88d Mon Sep 17 00:00:00 2001 From: ItzCrazyKns <95534749+ItzCrazyKns@users.noreply.github.com> Date: Fri, 29 Nov 2024 18:05:28 +0530 Subject: [PATCH 04/31] feat(agents): add a unified agent --- src/search/metaSearchAgent.ts | 486 ++++++++++++++++++++++++++++++++++ 1 file changed, 486 insertions(+) create mode 100644 src/search/metaSearchAgent.ts diff --git a/src/search/metaSearchAgent.ts b/src/search/metaSearchAgent.ts new file mode 100644 index 0000000..0dde085 --- /dev/null +++ b/src/search/metaSearchAgent.ts @@ -0,0 +1,486 @@ +import { ChatOpenAI } from '@langchain/openai'; +import type { BaseChatModel } from '@langchain/core/language_models/chat_models'; +import type { Embeddings } from '@langchain/core/embeddings'; +import { + ChatPromptTemplate, + MessagesPlaceholder, + PromptTemplate, +} from '@langchain/core/prompts'; +import { + RunnableLambda, + RunnableMap, + RunnableSequence, +} from '@langchain/core/runnables'; +import { BaseMessage } from '@langchain/core/messages'; +import { StringOutputParser } from '@langchain/core/output_parsers'; +import LineListOutputParser from '../lib/outputParsers/listLineOutputParser'; +import LineOutputParser from '../lib/outputParsers/lineOutputParser'; +import { getDocumentsFromLinks } from '../utils/documents'; +import { Document } from 'langchain/document'; +import { searchSearxng } from '../lib/searxng'; +import path from 'path'; +import fs from 'fs'; +import computeSimilarity from '../utils/computeSimilarity'; +import formatChatHistoryAsString from '../utils/formatHistory'; +import eventEmitter from 'events'; +import { StreamEvent } from '@langchain/core/tracers/log_stream'; +import { IterableReadableStream } from '@langchain/core/utils/stream'; + +export interface MetaSearchAgentType { + searchAndAnswer: ( + message: string, + history: BaseMessage[], + llm: BaseChatModel, + embeddings: Embeddings, + optimizationMode: 'speed' | 'balanced' | 'quality', + fileIds: string[], + ) => Promise; +} + +interface Config { + searchWeb: boolean; + rerank: boolean; + summarizer: boolean; + rerankThreshold: number; + queryGeneratorPrompt: string; + responsePrompt: string; + activeEngines: string[]; +} + +type BasicChainInput = { + chat_history: BaseMessage[]; + query: string; +}; + +class MetaSearchAgent implements MetaSearchAgentType { + private config: Config; + private strParser = new StringOutputParser(); + + constructor(config: Config) { + this.config = config; + } + + private async createSearchRetrieverChain(llm: BaseChatModel) { + (llm as unknown as ChatOpenAI).temperature = 0; + + return RunnableSequence.from([ + PromptTemplate.fromTemplate(this.config.queryGeneratorPrompt), + llm, + this.strParser, + RunnableLambda.from(async (input: string) => { + const linksOutputParser = new LineListOutputParser({ + key: 'links', + }); + + const questionOutputParser = new LineOutputParser({ + key: 'question', + }); + + const links = await linksOutputParser.parse(input); + let question = this.config.summarizer + ? await questionOutputParser.parse(input) + : input; + + if (question === 'not_needed') { + return { query: '', docs: [] }; + } + + if (links.length > 0) { + if (question.length === 0) { + question = 'summarize'; + } + + let docs = []; + + const linkDocs = await getDocumentsFromLinks({ links }); + + const docGroups: Document[] = []; + + linkDocs.map((doc) => { + const URLDocExists = docGroups.find( + (d) => + d.metadata.url === doc.metadata.url && + d.metadata.totalDocs < 10, + ); + + if (!URLDocExists) { + docGroups.push({ + ...doc, + metadata: { + ...doc.metadata, + totalDocs: 1, + }, + }); + } + + const docIndex = docGroups.findIndex( + (d) => + d.metadata.url === doc.metadata.url && + d.metadata.totalDocs < 10, + ); + + if (docIndex !== -1) { + docGroups[docIndex].pageContent = + docGroups[docIndex].pageContent + `\n\n` + doc.pageContent; + docGroups[docIndex].metadata.totalDocs += 1; + } + }); + + await Promise.all( + docGroups.map(async (doc) => { + const res = await llm.invoke(` + You are a web search summarizer, tasked with summarizing a piece of text retrieved from a web search. Your job is to summarize the + text into a detailed, 2-4 paragraph explanation that captures the main ideas and provides a comprehensive answer to the query. + If the query is \"summarize\", you should provide a detailed summary of the text. If the query is a specific question, you should answer it in the summary. + + - **Journalistic tone**: The summary should sound professional and journalistic, not too casual or vague. + - **Thorough and detailed**: Ensure that every key point from the text is captured and that the summary directly answers the query. + - **Not too lengthy, but detailed**: The summary should be informative but not excessively long. Focus on providing detailed information in a concise format. + + The text will be shared inside the \`text\` XML tag, and the query inside the \`query\` XML tag. + + + 1. \` + Docker is a set of platform-as-a-service products that use OS-level virtualization to deliver software in packages called containers. + It was first released in 2013 and is developed by Docker, Inc. Docker is designed to make it easier to create, deploy, and run applications + by using containers. + + + + What is Docker and how does it work? + + + Response: + Docker is a revolutionary platform-as-a-service product developed by Docker, Inc., that uses container technology to make application + deployment more efficient. It allows developers to package their software with all necessary dependencies, making it easier to run in + any environment. Released in 2013, Docker has transformed the way applications are built, deployed, and managed. + \` + 2. \` + The theory of relativity, or simply relativity, encompasses two interrelated theories of Albert Einstein: special relativity and general + relativity. However, the word "relativity" is sometimes used in reference to Galilean invariance. The term "theory of relativity" was based + on the expression "relative theory" used by Max Planck in 1906. The theory of relativity usually encompasses two interrelated theories by + Albert Einstein: special relativity and general relativity. Special relativity applies to all physical phenomena in the absence of gravity. + General relativity explains the law of gravitation and its relation to other forces of nature. It applies to the cosmological and astrophysical + realm, including astronomy. + + + + summarize + + + Response: + The theory of relativity, developed by Albert Einstein, encompasses two main theories: special relativity and general relativity. Special + relativity applies to all physical phenomena in the absence of gravity, while general relativity explains the law of gravitation and its + relation to other forces of nature. The theory of relativity is based on the concept of "relative theory," as introduced by Max Planck in + 1906. It is a fundamental theory in physics that has revolutionized our understanding of the universe. + \` + + + Everything below is the actual data you will be working with. Good luck! + + + ${question} + + + + ${doc.pageContent} + + + Make sure to answer the query in the summary. + `); + + const document = new Document({ + pageContent: res.content as string, + metadata: { + title: doc.metadata.title, + url: doc.metadata.url, + }, + }); + + docs.push(document); + }), + ); + + return { query: question, docs: docs }; + } else { + const res = await searchSearxng(question, { + language: 'en', + engines: this.config.activeEngines, + }); + + const documents = res.results.map( + (result) => + new Document({ + pageContent: result.content, + metadata: { + title: result.title, + url: result.url, + ...(result.img_src && { img_src: result.img_src }), + }, + }), + ); + + return { query: question, docs: documents }; + } + }), + ]); + } + + private async createAnsweringChain( + llm: BaseChatModel, + fileIds: string[], + embeddings: Embeddings, + optimizationMode: 'speed' | 'balanced' | 'quality', + ) { + return RunnableSequence.from([ + RunnableMap.from({ + query: (input: BasicChainInput) => input.query, + chat_history: (input: BasicChainInput) => input.chat_history, + context: RunnableLambda.from(async (input: BasicChainInput) => { + const processedHistory = formatChatHistoryAsString( + input.chat_history, + ); + + let docs: Document[] | null = null; + let query = input.query; + + if (this.config.searchWeb) { + const searchRetrieverChain = + await this.createSearchRetrieverChain(llm); + + const searchRetrieverResult = await searchRetrieverChain.invoke({ + chat_history: processedHistory, + query, + }); + + query = searchRetrieverResult.query; + docs = searchRetrieverResult.docs; + } + + const sortedDocs = await this.rerankDocs( + query, + docs ?? [], + fileIds, + embeddings, + optimizationMode, + ); + + return sortedDocs; + }) + .withConfig({ + runName: 'FinalSourceRetriever', + }) + .pipe(this.processDocs), + }), + ChatPromptTemplate.fromMessages([ + ['system', this.config.responsePrompt], + new MessagesPlaceholder('chat_history'), + ['user', '{query}'], + ]), + llm, + this.strParser, + ]).withConfig({ + runName: 'FinalResponseGenerator', + }); + } + + private async rerankDocs( + query: string, + docs: Document[], + fileIds: string[], + embeddings: Embeddings, + optimizationMode: 'speed' | 'balanced' | 'quality', + ) { + if (docs.length === 0 && fileIds.length === 0) { + return docs; + } + + const filesData = fileIds + .map((file) => { + const filePath = path.join(process.cwd(), 'uploads', file); + + const contentPath = filePath + '-extracted.json'; + const embeddingsPath = filePath + '-embeddings.json'; + + const content = JSON.parse(fs.readFileSync(contentPath, 'utf8')); + const embeddings = JSON.parse(fs.readFileSync(embeddingsPath, 'utf8')); + + const fileSimilaritySearchObject = content.contents.map( + (c: string, i) => { + return { + fileName: content.title, + content: c, + embeddings: embeddings.embeddings[i], + }; + }, + ); + + return fileSimilaritySearchObject; + }) + .flat(); + + if (query.toLocaleLowerCase() === 'summarize') { + return docs.slice(0, 15); + } + + const docsWithContent = docs.filter( + (doc) => doc.pageContent && doc.pageContent.length > 0, + ); + + if (optimizationMode === 'speed' || this.config.rerank === false) { + if (filesData.length > 0) { + const [queryEmbedding] = await Promise.all([ + embeddings.embedQuery(query), + ]); + + const fileDocs = filesData.map((fileData) => { + return new Document({ + pageContent: fileData.content, + metadata: { + title: fileData.fileName, + url: `File`, + }, + }); + }); + + const similarity = filesData.map((fileData, i) => { + const sim = computeSimilarity(queryEmbedding, fileData.embeddings); + + return { + index: i, + similarity: sim, + }; + }); + + let sortedDocs = similarity + .filter( + (sim) => sim.similarity > (this.config.rerankThreshold ?? 0.3), + ) + .sort((a, b) => b.similarity - a.similarity) + .slice(0, 15) + .map((sim) => fileDocs[sim.index]); + + sortedDocs = + docsWithContent.length > 0 ? sortedDocs.slice(0, 8) : sortedDocs; + + return [ + ...sortedDocs, + ...docsWithContent.slice(0, 15 - sortedDocs.length), + ]; + } else { + return docsWithContent.slice(0, 15); + } + } else if (optimizationMode === 'balanced') { + const [docEmbeddings, queryEmbedding] = await Promise.all([ + embeddings.embedDocuments( + docsWithContent.map((doc) => doc.pageContent), + ), + embeddings.embedQuery(query), + ]); + + docsWithContent.push( + ...filesData.map((fileData) => { + return new Document({ + pageContent: fileData.content, + metadata: { + title: fileData.fileName, + url: `File`, + }, + }); + }), + ); + + docEmbeddings.push(...filesData.map((fileData) => fileData.embeddings)); + + const similarity = docEmbeddings.map((docEmbedding, i) => { + const sim = computeSimilarity(queryEmbedding, docEmbedding); + + return { + index: i, + similarity: sim, + }; + }); + + const sortedDocs = similarity + .filter((sim) => sim.similarity > (this.config.rerankThreshold ?? 0.3)) + .sort((a, b) => b.similarity - a.similarity) + .slice(0, 15) + .map((sim) => docsWithContent[sim.index]); + + return sortedDocs; + } + } + + private processDocs(docs: Document[]) { + return docs + .map((_, index) => `${index + 1}. ${docs[index].pageContent}`) + .join('\n'); + } + + private async handleStream( + stream: IterableReadableStream, + emitter: eventEmitter, + ) { + for await (const event of stream) { + if ( + event.event === 'on_chain_end' && + event.name === 'FinalSourceRetriever' + ) { + ``; + emitter.emit( + 'data', + JSON.stringify({ type: 'sources', data: event.data.output }), + ); + } + if ( + event.event === 'on_chain_stream' && + event.name === 'FinalResponseGenerator' + ) { + emitter.emit( + 'data', + JSON.stringify({ type: 'response', data: event.data.chunk }), + ); + } + if ( + event.event === 'on_chain_end' && + event.name === 'FinalResponseGenerator' + ) { + emitter.emit('end'); + } + } + } + + async searchAndAnswer( + message: string, + history: BaseMessage[], + llm: BaseChatModel, + embeddings: Embeddings, + optimizationMode: 'speed' | 'balanced' | 'quality', + fileIds: string[], + ) { + const emitter = new eventEmitter(); + + const answeringChain = await this.createAnsweringChain( + llm, + fileIds, + embeddings, + optimizationMode, + ); + + const stream = answeringChain.streamEvents( + { + chat_history: history, + query: message, + }, + { + version: 'v1', + }, + ); + + this.handleStream(stream, emitter); + + return emitter; + } +} + +export default MetaSearchAgent; From dc4a843d8a3aaa76d5675ac34be9df2ab3247b44 Mon Sep 17 00:00:00 2001 From: ItzCrazyKns <95534749+ItzCrazyKns@users.noreply.github.com> Date: Fri, 29 Nov 2024 18:06:00 +0530 Subject: [PATCH 05/31] feat(agents): switch to `MetaSearchAgent` --- src/agents/academicSearchAgent.ts | 280 --------- src/agents/redditSearchAgent.ts | 276 --------- src/agents/webSearchAgent.ts | 541 ------------------ src/agents/wolframAlphaSearchAgent.ts | 220 ------- src/agents/writingAssistant.ts | 91 --- src/agents/youtubeSearchAgent.ts | 277 --------- src/{agents => chains}/imageSearchAgent.ts | 0 .../suggestionGeneratorAgent.ts | 0 src/{agents => chains}/videoSearchAgent.ts | 0 src/prompts/academicSearch.ts | 42 ++ src/prompts/index.ts | 32 ++ src/prompts/redditSearch.ts | 42 ++ src/prompts/webSearch.ts | 86 +++ src/prompts/wolframAlpha.ts | 42 ++ src/prompts/writingAssistant.ts | 13 + src/prompts/youtubeSearch.ts | 42 ++ src/routes/images.ts | 2 +- src/routes/search.ts | 10 +- src/routes/suggestions.ts | 2 +- src/routes/videos.ts | 2 +- src/websocket/messageHandler.ts | 169 ++++-- 21 files changed, 418 insertions(+), 1751 deletions(-) delete mode 100644 src/agents/academicSearchAgent.ts delete mode 100644 src/agents/redditSearchAgent.ts delete mode 100644 src/agents/webSearchAgent.ts delete mode 100644 src/agents/wolframAlphaSearchAgent.ts delete mode 100644 src/agents/writingAssistant.ts delete mode 100644 src/agents/youtubeSearchAgent.ts rename src/{agents => chains}/imageSearchAgent.ts (100%) rename src/{agents => chains}/suggestionGeneratorAgent.ts (100%) rename src/{agents => chains}/videoSearchAgent.ts (100%) create mode 100644 src/prompts/academicSearch.ts create mode 100644 src/prompts/index.ts create mode 100644 src/prompts/redditSearch.ts create mode 100644 src/prompts/webSearch.ts create mode 100644 src/prompts/wolframAlpha.ts create mode 100644 src/prompts/writingAssistant.ts create mode 100644 src/prompts/youtubeSearch.ts diff --git a/src/agents/academicSearchAgent.ts b/src/agents/academicSearchAgent.ts deleted file mode 100644 index bad4065..0000000 --- a/src/agents/academicSearchAgent.ts +++ /dev/null @@ -1,280 +0,0 @@ -import { BaseMessage } from '@langchain/core/messages'; -import { - PromptTemplate, - ChatPromptTemplate, - MessagesPlaceholder, -} from '@langchain/core/prompts'; -import { - RunnableSequence, - RunnableMap, - RunnableLambda, -} from '@langchain/core/runnables'; -import { StringOutputParser } from '@langchain/core/output_parsers'; -import { Document } from '@langchain/core/documents'; -import { searchSearxng } from '../lib/searxng'; -import type { StreamEvent } from '@langchain/core/tracers/log_stream'; -import type { BaseChatModel } from '@langchain/core/language_models/chat_models'; -import type { Embeddings } from '@langchain/core/embeddings'; -import formatChatHistoryAsString from '../utils/formatHistory'; -import eventEmitter from 'events'; -import computeSimilarity from '../utils/computeSimilarity'; -import logger from '../utils/logger'; -import { IterableReadableStream } from '@langchain/core/utils/stream'; - -const basicAcademicSearchRetrieverPrompt = ` -You will be given a conversation below and a follow up question. You need to rephrase the follow-up question if needed so it is a standalone question that can be used by the LLM to search the web for information. -If it is a writing task or a simple hi, hello rather than a question, you need to return \`not_needed\` as the response. - -Example: -1. Follow up question: How does stable diffusion work? -Rephrased: Stable diffusion working - -2. Follow up question: What is linear algebra? -Rephrased: Linear algebra - -3. Follow up question: What is the third law of thermodynamics? -Rephrased: Third law of thermodynamics - -Conversation: -{chat_history} - -Follow up question: {query} -Rephrased question: -`; - -const basicAcademicSearchResponsePrompt = ` - You are Perplexica, an AI model who is expert at searching the web and answering user's queries. You are set on focus mode 'Academic', this means you will be searching for academic papers and articles on the web. - - Generate a response that is informative and relevant to the user's query based on provided context (the context consits of search results containing a brief description of the content of that page). - You must use this context to answer the user's query in the best way possible. Use an unbaised and journalistic tone in your response. Do not repeat the text. - You must not tell the user to open any link or visit any website to get the answer. You must provide the answer in the response itself. If the user asks for links you can provide them. - Your responses should be medium to long in length be informative and relevant to the user's query. You can use markdowns to format your response. You should use bullet points to list the information. Make sure the answer is not short and is informative. - You have to cite the answer using [number] notation. You must cite the sentences with their relevent context number. You must cite each and every part of the answer so the user can know where the information is coming from. - Place these citations at the end of that particular sentence. You can cite the same sentence multiple times if it is relevant to the user's query like [number1][number2]. - However you do not need to cite it using the same number. You can use different numbers to cite the same sentence multiple times. The number refers to the number of the search result (passed in the context) used to generate that part of the answer. - - Anything inside the following \`context\` HTML block provided below is for your knowledge returned by the search engine and is not shared by the user. You have to answer question on the basis of it and cite the relevant information from it but you do not have to - talk about the context in your response. - - - {context} - - - If you think there's nothing relevant in the search results, you can say that 'Hmm, sorry I could not find any relevant information on this topic. Would you like me to search again or ask something else?'. - Anything between the \`context\` is retrieved from a search engine and is not a part of the conversation with the user. Today's date is ${new Date().toISOString()} -`; - -const strParser = new StringOutputParser(); - -const handleStream = async ( - stream: IterableReadableStream, - emitter: eventEmitter, -) => { - for await (const event of stream) { - if ( - event.event === 'on_chain_end' && - event.name === 'FinalSourceRetriever' - ) { - emitter.emit( - 'data', - JSON.stringify({ type: 'sources', data: event.data.output }), - ); - } - if ( - event.event === 'on_chain_stream' && - event.name === 'FinalResponseGenerator' - ) { - emitter.emit( - 'data', - JSON.stringify({ type: 'response', data: event.data.chunk }), - ); - } - if ( - event.event === 'on_chain_end' && - event.name === 'FinalResponseGenerator' - ) { - emitter.emit('end'); - } - } -}; - -type BasicChainInput = { - chat_history: BaseMessage[]; - query: string; -}; - -const createBasicAcademicSearchRetrieverChain = (llm: BaseChatModel) => { - return RunnableSequence.from([ - PromptTemplate.fromTemplate(basicAcademicSearchRetrieverPrompt), - llm, - strParser, - RunnableLambda.from(async (input: string) => { - if (input === 'not_needed') { - return { query: '', docs: [] }; - } - - const res = await searchSearxng(input, { - language: 'en', - engines: ['arxiv', 'google scholar', 'pubmed'], - }); - - const documents = res.results.map( - (result) => - new Document({ - pageContent: result.content, - metadata: { - title: result.title, - url: result.url, - ...(result.img_src && { img_src: result.img_src }), - }, - }), - ); - - return { query: input, docs: documents }; - }), - ]); -}; - -const createBasicAcademicSearchAnsweringChain = ( - llm: BaseChatModel, - embeddings: Embeddings, - optimizationMode: 'speed' | 'balanced' | 'quality', -) => { - const basicAcademicSearchRetrieverChain = - createBasicAcademicSearchRetrieverChain(llm); - - const processDocs = async (docs: Document[]) => { - return docs - .map((_, index) => `${index + 1}. ${docs[index].pageContent}`) - .join('\n'); - }; - - const rerankDocs = async ({ - query, - docs, - }: { - query: string; - docs: Document[]; - }) => { - if (docs.length === 0) { - return docs; - } - - const docsWithContent = docs.filter( - (doc) => doc.pageContent && doc.pageContent.length > 0, - ); - - if (optimizationMode === 'speed') { - return docsWithContent.slice(0, 15); - } else if (optimizationMode === 'balanced') { - const [docEmbeddings, queryEmbedding] = await Promise.all([ - embeddings.embedDocuments( - docsWithContent.map((doc) => doc.pageContent), - ), - embeddings.embedQuery(query), - ]); - - const similarity = docEmbeddings.map((docEmbedding, i) => { - const sim = computeSimilarity(queryEmbedding, docEmbedding); - - return { - index: i, - similarity: sim, - }; - }); - - const sortedDocs = similarity - .sort((a, b) => b.similarity - a.similarity) - .slice(0, 15) - .map((sim) => docsWithContent[sim.index]); - - return sortedDocs; - } - }; - - return RunnableSequence.from([ - RunnableMap.from({ - query: (input: BasicChainInput) => input.query, - chat_history: (input: BasicChainInput) => input.chat_history, - context: RunnableSequence.from([ - (input) => ({ - query: input.query, - chat_history: formatChatHistoryAsString(input.chat_history), - }), - basicAcademicSearchRetrieverChain - .pipe(rerankDocs) - .withConfig({ - runName: 'FinalSourceRetriever', - }) - .pipe(processDocs), - ]), - }), - ChatPromptTemplate.fromMessages([ - ['system', basicAcademicSearchResponsePrompt], - new MessagesPlaceholder('chat_history'), - ['user', '{query}'], - ]), - llm, - strParser, - ]).withConfig({ - runName: 'FinalResponseGenerator', - }); -}; - -const basicAcademicSearch = ( - query: string, - history: BaseMessage[], - llm: BaseChatModel, - embeddings: Embeddings, - optimizationMode: 'speed' | 'balanced' | 'quality', -) => { - const emitter = new eventEmitter(); - - try { - const basicAcademicSearchAnsweringChain = - createBasicAcademicSearchAnsweringChain( - llm, - embeddings, - optimizationMode, - ); - - const stream = basicAcademicSearchAnsweringChain.streamEvents( - { - chat_history: history, - query: query, - }, - { - version: 'v1', - }, - ); - - handleStream(stream, emitter); - } catch (err) { - emitter.emit( - 'error', - JSON.stringify({ data: 'An error has occurred please try again later' }), - ); - logger.error(`Error in academic search: ${err}`); - } - - return emitter; -}; - -const handleAcademicSearch = ( - message: string, - history: BaseMessage[], - llm: BaseChatModel, - embeddings: Embeddings, - optimizationMode: 'speed' | 'balanced' | 'quality', -) => { - const emitter = basicAcademicSearch( - message, - history, - llm, - embeddings, - optimizationMode, - ); - return emitter; -}; - -export default handleAcademicSearch; diff --git a/src/agents/redditSearchAgent.ts b/src/agents/redditSearchAgent.ts deleted file mode 100644 index 2c44c13..0000000 --- a/src/agents/redditSearchAgent.ts +++ /dev/null @@ -1,276 +0,0 @@ -import { BaseMessage } from '@langchain/core/messages'; -import { - PromptTemplate, - ChatPromptTemplate, - MessagesPlaceholder, -} from '@langchain/core/prompts'; -import { - RunnableSequence, - RunnableMap, - RunnableLambda, -} from '@langchain/core/runnables'; -import { StringOutputParser } from '@langchain/core/output_parsers'; -import { Document } from '@langchain/core/documents'; -import { searchSearxng } from '../lib/searxng'; -import type { StreamEvent } from '@langchain/core/tracers/log_stream'; -import type { BaseChatModel } from '@langchain/core/language_models/chat_models'; -import type { Embeddings } from '@langchain/core/embeddings'; -import formatChatHistoryAsString from '../utils/formatHistory'; -import eventEmitter from 'events'; -import computeSimilarity from '../utils/computeSimilarity'; -import logger from '../utils/logger'; -import { IterableReadableStream } from '@langchain/core/utils/stream'; - -const basicRedditSearchRetrieverPrompt = ` -You will be given a conversation below and a follow up question. You need to rephrase the follow-up question if needed so it is a standalone question that can be used by the LLM to search the web for information. -If it is a writing task or a simple hi, hello rather than a question, you need to return \`not_needed\` as the response. - -Example: -1. Follow up question: Which company is most likely to create an AGI -Rephrased: Which company is most likely to create an AGI - -2. Follow up question: Is Earth flat? -Rephrased: Is Earth flat? - -3. Follow up question: Is there life on Mars? -Rephrased: Is there life on Mars? - -Conversation: -{chat_history} - -Follow up question: {query} -Rephrased question: -`; - -const basicRedditSearchResponsePrompt = ` - You are Perplexica, an AI model who is expert at searching the web and answering user's queries. You are set on focus mode 'Reddit', this means you will be searching for information, opinions and discussions on the web using Reddit. - - Generate a response that is informative and relevant to the user's query based on provided context (the context consits of search results containing a brief description of the content of that page). - You must use this context to answer the user's query in the best way possible. Use an unbaised and journalistic tone in your response. Do not repeat the text. - You must not tell the user to open any link or visit any website to get the answer. You must provide the answer in the response itself. If the user asks for links you can provide them. - Your responses should be medium to long in length be informative and relevant to the user's query. You can use markdowns to format your response. You should use bullet points to list the information. Make sure the answer is not short and is informative. - You have to cite the answer using [number] notation. You must cite the sentences with their relevent context number. You must cite each and every part of the answer so the user can know where the information is coming from. - Place these citations at the end of that particular sentence. You can cite the same sentence multiple times if it is relevant to the user's query like [number1][number2]. - However you do not need to cite it using the same number. You can use different numbers to cite the same sentence multiple times. The number refers to the number of the search result (passed in the context) used to generate that part of the answer. - - Anything inside the following \`context\` HTML block provided below is for your knowledge returned by Reddit and is not shared by the user. You have to answer question on the basis of it and cite the relevant information from it but you do not have to - talk about the context in your response. - - - {context} - - - If you think there's nothing relevant in the search results, you can say that 'Hmm, sorry I could not find any relevant information on this topic. Would you like me to search again or ask something else?'. - Anything between the \`context\` is retrieved from Reddit and is not a part of the conversation with the user. Today's date is ${new Date().toISOString()} -`; - -const strParser = new StringOutputParser(); - -const handleStream = async ( - stream: IterableReadableStream, - emitter: eventEmitter, -) => { - for await (const event of stream) { - if ( - event.event === 'on_chain_end' && - event.name === 'FinalSourceRetriever' - ) { - emitter.emit( - 'data', - JSON.stringify({ type: 'sources', data: event.data.output }), - ); - } - if ( - event.event === 'on_chain_stream' && - event.name === 'FinalResponseGenerator' - ) { - emitter.emit( - 'data', - JSON.stringify({ type: 'response', data: event.data.chunk }), - ); - } - if ( - event.event === 'on_chain_end' && - event.name === 'FinalResponseGenerator' - ) { - emitter.emit('end'); - } - } -}; - -type BasicChainInput = { - chat_history: BaseMessage[]; - query: string; -}; - -const createBasicRedditSearchRetrieverChain = (llm: BaseChatModel) => { - return RunnableSequence.from([ - PromptTemplate.fromTemplate(basicRedditSearchRetrieverPrompt), - llm, - strParser, - RunnableLambda.from(async (input: string) => { - if (input === 'not_needed') { - return { query: '', docs: [] }; - } - - const res = await searchSearxng(input, { - language: 'en', - engines: ['reddit'], - }); - - const documents = res.results.map( - (result) => - new Document({ - pageContent: result.content ? result.content : result.title, - metadata: { - title: result.title, - url: result.url, - ...(result.img_src && { img_src: result.img_src }), - }, - }), - ); - - return { query: input, docs: documents }; - }), - ]); -}; - -const createBasicRedditSearchAnsweringChain = ( - llm: BaseChatModel, - embeddings: Embeddings, - optimizationMode: 'speed' | 'balanced' | 'quality', -) => { - const basicRedditSearchRetrieverChain = - createBasicRedditSearchRetrieverChain(llm); - - const processDocs = async (docs: Document[]) => { - return docs - .map((_, index) => `${index + 1}. ${docs[index].pageContent}`) - .join('\n'); - }; - - const rerankDocs = async ({ - query, - docs, - }: { - query: string; - docs: Document[]; - }) => { - if (docs.length === 0) { - return docs; - } - - const docsWithContent = docs.filter( - (doc) => doc.pageContent && doc.pageContent.length > 0, - ); - - if (optimizationMode === 'speed') { - return docsWithContent.slice(0, 15); - } else if (optimizationMode === 'balanced') { - const [docEmbeddings, queryEmbedding] = await Promise.all([ - embeddings.embedDocuments( - docsWithContent.map((doc) => doc.pageContent), - ), - embeddings.embedQuery(query), - ]); - - const similarity = docEmbeddings.map((docEmbedding, i) => { - const sim = computeSimilarity(queryEmbedding, docEmbedding); - - return { - index: i, - similarity: sim, - }; - }); - - const sortedDocs = similarity - .filter((sim) => sim.similarity > 0.3) - .sort((a, b) => b.similarity - a.similarity) - .slice(0, 15) - .map((sim) => docsWithContent[sim.index]); - - return sortedDocs; - } - }; - - return RunnableSequence.from([ - RunnableMap.from({ - query: (input: BasicChainInput) => input.query, - chat_history: (input: BasicChainInput) => input.chat_history, - context: RunnableSequence.from([ - (input) => ({ - query: input.query, - chat_history: formatChatHistoryAsString(input.chat_history), - }), - basicRedditSearchRetrieverChain - .pipe(rerankDocs) - .withConfig({ - runName: 'FinalSourceRetriever', - }) - .pipe(processDocs), - ]), - }), - ChatPromptTemplate.fromMessages([ - ['system', basicRedditSearchResponsePrompt], - new MessagesPlaceholder('chat_history'), - ['user', '{query}'], - ]), - llm, - strParser, - ]).withConfig({ - runName: 'FinalResponseGenerator', - }); -}; - -const basicRedditSearch = ( - query: string, - history: BaseMessage[], - llm: BaseChatModel, - embeddings: Embeddings, - optimizationMode: 'speed' | 'balanced' | 'quality', -) => { - const emitter = new eventEmitter(); - - try { - const basicRedditSearchAnsweringChain = - createBasicRedditSearchAnsweringChain(llm, embeddings, optimizationMode); - const stream = basicRedditSearchAnsweringChain.streamEvents( - { - chat_history: history, - query: query, - }, - { - version: 'v1', - }, - ); - - handleStream(stream, emitter); - } catch (err) { - emitter.emit( - 'error', - JSON.stringify({ data: 'An error has occurred please try again later' }), - ); - logger.error(`Error in RedditSearch: ${err}`); - } - - return emitter; -}; - -const handleRedditSearch = ( - message: string, - history: BaseMessage[], - llm: BaseChatModel, - embeddings: Embeddings, - optimizationMode: 'speed' | 'balanced' | 'quality', -) => { - const emitter = basicRedditSearch( - message, - history, - llm, - embeddings, - optimizationMode, - ); - return emitter; -}; - -export default handleRedditSearch; diff --git a/src/agents/webSearchAgent.ts b/src/agents/webSearchAgent.ts deleted file mode 100644 index 4303762..0000000 --- a/src/agents/webSearchAgent.ts +++ /dev/null @@ -1,541 +0,0 @@ -import { BaseMessage } from '@langchain/core/messages'; -import { - PromptTemplate, - ChatPromptTemplate, - MessagesPlaceholder, -} from '@langchain/core/prompts'; -import { - RunnableSequence, - RunnableMap, - RunnableLambda, -} from '@langchain/core/runnables'; -import { StringOutputParser } from '@langchain/core/output_parsers'; -import { Document } from '@langchain/core/documents'; -import { searchSearxng } from '../lib/searxng'; -import type { StreamEvent } from '@langchain/core/tracers/log_stream'; -import type { BaseChatModel } from '@langchain/core/language_models/chat_models'; -import type { Embeddings } from '@langchain/core/embeddings'; -import formatChatHistoryAsString from '../utils/formatHistory'; -import eventEmitter from 'events'; -import computeSimilarity from '../utils/computeSimilarity'; -import logger from '../utils/logger'; -import LineListOutputParser from '../lib/outputParsers/listLineOutputParser'; -import LineOutputParser from '../lib/outputParsers/lineOutputParser'; -import { IterableReadableStream } from '@langchain/core/utils/stream'; -import { ChatOpenAI } from '@langchain/openai'; -import path from 'path'; -import fs from 'fs'; -import { getDocumentsFromLinks } from '../utils/documents'; - -const basicSearchRetrieverPrompt = ` -You are an AI question rephraser. You will be given a conversation and a follow-up question, you will have to rephrase the follow up question so it is a standalone question and can be used by another LLM to search the web for information to answer it. -If it is a smple writing task or a greeting (unless the greeting contains a question after it) like Hi, Hello, How are you, etc. than a question then you need to return \`not_needed\` as the response (This is because the LLM won't need to search the web for finding information on this topic). -If the user asks some question from some URL or wants you to summarize a PDF or a webpage (via URL) you need to return the links inside the \`links\` XML block and the question inside the \`question\` XML block. If the user wants to you to summarize the webpage or the PDF you need to return \`summarize\` inside the \`question\` XML block in place of a question and the link to summarize in the \`links\` XML block. -You must always return the rephrased question inside the \`question\` XML block, if there are no links in the follow-up question then don't insert a \`links\` XML block in your response. - -There are several examples attached for your reference inside the below \`examples\` XML block - - -1. Follow up question: What is the capital of France -Rephrased question:\` - -Capital of france - -\` - -2. Hi, how are you? -Rephrased question\` - -not_needed - -\` - -3. Follow up question: What is Docker? -Rephrased question: \` - -What is Docker - -\` - -4. Follow up question: Can you tell me what is X from https://example.com -Rephrased question: \` - -Can you tell me what is X? - - - -https://example.com - -\` - -5. Follow up question: Summarize the content from https://example.com -Rephrased question: \` - -summarize - - - -https://example.com - -\` - - -Anything below is the part of the actual conversation and you need to use conversation and the follow-up question to rephrase the follow-up question as a standalone question based on the guidelines shared above. - - -{chat_history} - - -Follow up question: {query} -Rephrased question: -`; - -const basicWebSearchResponsePrompt = ` - You are Perplexica, an AI model who is expert at searching the web and answering user's queries. You are also an expert at summarizing web pages or documents and searching for content in them. - - Generate a response that is informative and relevant to the user's query based on provided context (the context consits of search results containing a brief description of the content of that page). - You must use this context to answer the user's query in the best way possible. Use an unbaised and journalistic tone in your response. Do not repeat the text. - You must not tell the user to open any link or visit any website to get the answer. You must provide the answer in the response itself. If the user asks for links you can provide them. - If the query contains some links and the user asks to answer from those links you will be provided the entire content of the page inside the \`context\` XML block. You can then use this content to answer the user's query. - If the user asks to summarize content from some links, you will be provided the entire content of the page inside the \`context\` XML block. You can then use this content to summarize the text. The content provided inside the \`context\` block will be already summarized by another model so you just need to use that content to answer the user's query. - Your responses should be medium to long in length be informative and relevant to the user's query. You can use markdowns to format your response. You should use bullet points to list the information. Make sure the answer is not short and is informative. - You have to cite the answer using [number] notation. You must cite the sentences with their relevent context number. You must cite each and every part of the answer so the user can know where the information is coming from. - Place these citations at the end of that particular sentence. You can cite the same sentence multiple times if it is relevant to the user's query like [number1][number2]. - However you do not need to cite it using the same number. You can use different numbers to cite the same sentence multiple times. The number refers to the number of the search result (passed in the context) used to generate that part of the answer. - - Anything inside the following \`context\` HTML block provided below is for your knowledge returned by the search engine and is not shared by the user. You have to answer question on the basis of it and cite the relevant information from it but you do not have to - talk about the context in your response. - - - {context} - - - If you think there's nothing relevant in the search results, you can say that 'Hmm, sorry I could not find any relevant information on this topic. Would you like me to search again or ask something else?'. You do not need to do this for summarization tasks. - Anything between the \`context\` is retrieved from a search engine and is not a part of the conversation with the user. Today's date is ${new Date().toISOString()} -`; - -const strParser = new StringOutputParser(); - -const handleStream = async ( - stream: IterableReadableStream, - emitter: eventEmitter, -) => { - for await (const event of stream) { - if ( - event.event === 'on_chain_end' && - event.name === 'FinalSourceRetriever' - ) { - emitter.emit( - 'data', - JSON.stringify({ type: 'sources', data: event.data.output }), - ); - } - if ( - event.event === 'on_chain_stream' && - event.name === 'FinalResponseGenerator' - ) { - emitter.emit( - 'data', - JSON.stringify({ type: 'response', data: event.data.chunk }), - ); - } - if ( - event.event === 'on_chain_end' && - event.name === 'FinalResponseGenerator' - ) { - emitter.emit('end'); - } - } -}; - -type BasicChainInput = { - chat_history: BaseMessage[]; - query: string; -}; - -const createBasicWebSearchRetrieverChain = (llm: BaseChatModel) => { - (llm as unknown as ChatOpenAI).temperature = 0; - - return RunnableSequence.from([ - PromptTemplate.fromTemplate(basicSearchRetrieverPrompt), - llm, - strParser, - RunnableLambda.from(async (input: string) => { - const linksOutputParser = new LineListOutputParser({ - key: 'links', - }); - - const questionOutputParser = new LineOutputParser({ - key: 'question', - }); - - const links = await linksOutputParser.parse(input); - let question = await questionOutputParser.parse(input); - - if (question === 'not_needed') { - return { query: '', docs: [] }; - } - - if (links.length > 0) { - if (question.length === 0) { - question = 'summarize'; - } - - let docs = []; - - const linkDocs = await getDocumentsFromLinks({ links }); - - const docGroups: Document[] = []; - - linkDocs.map((doc) => { - const URLDocExists = docGroups.find( - (d) => - d.metadata.url === doc.metadata.url && d.metadata.totalDocs < 10, - ); - - if (!URLDocExists) { - docGroups.push({ - ...doc, - metadata: { - ...doc.metadata, - totalDocs: 1, - }, - }); - } - - const docIndex = docGroups.findIndex( - (d) => - d.metadata.url === doc.metadata.url && d.metadata.totalDocs < 10, - ); - - if (docIndex !== -1) { - docGroups[docIndex].pageContent = - docGroups[docIndex].pageContent + `\n\n` + doc.pageContent; - docGroups[docIndex].metadata.totalDocs += 1; - } - }); - - await Promise.all( - docGroups.map(async (doc) => { - const res = await llm.invoke(` - You are a web search summarizer, tasked with summarizing a piece of text retrieved from a web search. Your job is to summarize the - text into a detailed, 2-4 paragraph explanation that captures the main ideas and provides a comprehensive answer to the query. - If the query is \"summarize\", you should provide a detailed summary of the text. If the query is a specific question, you should answer it in the summary. - - - **Journalistic tone**: The summary should sound professional and journalistic, not too casual or vague. - - **Thorough and detailed**: Ensure that every key point from the text is captured and that the summary directly answers the query. - - **Not too lengthy, but detailed**: The summary should be informative but not excessively long. Focus on providing detailed information in a concise format. - - The text will be shared inside the \`text\` XML tag, and the query inside the \`query\` XML tag. - - - 1. \` - Docker is a set of platform-as-a-service products that use OS-level virtualization to deliver software in packages called containers. - It was first released in 2013 and is developed by Docker, Inc. Docker is designed to make it easier to create, deploy, and run applications - by using containers. - - - - What is Docker and how does it work? - - - Response: - Docker is a revolutionary platform-as-a-service product developed by Docker, Inc., that uses container technology to make application - deployment more efficient. It allows developers to package their software with all necessary dependencies, making it easier to run in - any environment. Released in 2013, Docker has transformed the way applications are built, deployed, and managed. - \` - 2. \` - The theory of relativity, or simply relativity, encompasses two interrelated theories of Albert Einstein: special relativity and general - relativity. However, the word "relativity" is sometimes used in reference to Galilean invariance. The term "theory of relativity" was based - on the expression "relative theory" used by Max Planck in 1906. The theory of relativity usually encompasses two interrelated theories by - Albert Einstein: special relativity and general relativity. Special relativity applies to all physical phenomena in the absence of gravity. - General relativity explains the law of gravitation and its relation to other forces of nature. It applies to the cosmological and astrophysical - realm, including astronomy. - - - - summarize - - - Response: - The theory of relativity, developed by Albert Einstein, encompasses two main theories: special relativity and general relativity. Special - relativity applies to all physical phenomena in the absence of gravity, while general relativity explains the law of gravitation and its - relation to other forces of nature. The theory of relativity is based on the concept of "relative theory," as introduced by Max Planck in - 1906. It is a fundamental theory in physics that has revolutionized our understanding of the universe. - \` - - - Everything below is the actual data you will be working with. Good luck! - - - ${question} - - - - ${doc.pageContent} - - - Make sure to answer the query in the summary. - `); - - const document = new Document({ - pageContent: res.content as string, - metadata: { - title: doc.metadata.title, - url: doc.metadata.url, - }, - }); - - docs.push(document); - }), - ); - - return { query: question, docs: docs }; - } else { - const res = await searchSearxng(question, { - language: 'en', - }); - - const documents = res.results.map( - (result) => - new Document({ - pageContent: result.content, - metadata: { - title: result.title, - url: result.url, - ...(result.img_src && { img_src: result.img_src }), - }, - }), - ); - - return { query: question, docs: documents }; - } - }), - ]); -}; - -const createBasicWebSearchAnsweringChain = ( - llm: BaseChatModel, - embeddings: Embeddings, - optimizationMode: 'speed' | 'balanced' | 'quality', - fileIds: string[], -) => { - const basicWebSearchRetrieverChain = createBasicWebSearchRetrieverChain(llm); - - const processDocs = async (docs: Document[]) => { - return docs - .map((_, index) => `${index + 1}. ${docs[index].pageContent}`) - .join('\n'); - }; - - const rerankDocs = async ({ - query, - docs, - }: { - query: string; - docs: Document[]; - }) => { - if (docs.length === 0) { - return docs; - } - - const filesData = fileIds - .map((file) => { - const filePath = path.join(process.cwd(), 'uploads', file); - - const contentPath = filePath + '-extracted.json'; - const embeddingsPath = filePath + '-embeddings.json'; - - const content = JSON.parse(fs.readFileSync(contentPath, 'utf8')); - const embeddings = JSON.parse(fs.readFileSync(embeddingsPath, 'utf8')); - - const fileSimilaritySearchObject = content.contents.map( - (c: string, i) => { - return { - fileName: content.title, - content: c, - embeddings: embeddings.embeddings[i], - }; - }, - ); - - return fileSimilaritySearchObject; - }) - .flat(); - - if (query.toLocaleLowerCase() === 'summarize') { - return docs.slice(0, 15); - } - - const docsWithContent = docs.filter( - (doc) => doc.pageContent && doc.pageContent.length > 0, - ); - - if (optimizationMode === 'speed') { - if (filesData.length > 0) { - const [queryEmbedding] = await Promise.all([ - embeddings.embedQuery(query), - ]); - - const fileDocs = filesData.map((fileData) => { - return new Document({ - pageContent: fileData.content, - metadata: { - title: fileData.fileName, - url: `File`, - }, - }); - }); - - const similarity = filesData.map((fileData, i) => { - const sim = computeSimilarity(queryEmbedding, fileData.embeddings); - - return { - index: i, - similarity: sim, - }; - }); - - const sortedDocs = similarity - .filter((sim) => sim.similarity > 0.3) - .sort((a, b) => b.similarity - a.similarity) - .slice(0, 8) - .map((sim) => fileDocs[sim.index]); - - return [ - ...sortedDocs, - ...docsWithContent.slice(0, 15 - sortedDocs.length), - ]; - } else { - return docsWithContent.slice(0, 15); - } - } else if (optimizationMode === 'balanced') { - const [docEmbeddings, queryEmbedding] = await Promise.all([ - embeddings.embedDocuments( - docsWithContent.map((doc) => doc.pageContent), - ), - embeddings.embedQuery(query), - ]); - - docsWithContent.push( - ...filesData.map((fileData) => { - return new Document({ - pageContent: fileData.content, - metadata: { - title: fileData.fileName, - url: `File`, - }, - }); - }), - ); - - docEmbeddings.push(...filesData.map((fileData) => fileData.embeddings)); - - const similarity = docEmbeddings.map((docEmbedding, i) => { - const sim = computeSimilarity(queryEmbedding, docEmbedding); - - return { - index: i, - similarity: sim, - }; - }); - - const sortedDocs = similarity - .filter((sim) => sim.similarity > 0.3) - .sort((a, b) => b.similarity - a.similarity) - .slice(0, 15) - .map((sim) => docsWithContent[sim.index]); - - return sortedDocs; - } - }; - - return RunnableSequence.from([ - RunnableMap.from({ - query: (input: BasicChainInput) => input.query, - chat_history: (input: BasicChainInput) => input.chat_history, - context: RunnableSequence.from([ - (input) => ({ - query: input.query, - chat_history: formatChatHistoryAsString(input.chat_history), - }), - basicWebSearchRetrieverChain - .pipe(rerankDocs) - .withConfig({ - runName: 'FinalSourceRetriever', - }) - .pipe(processDocs), - ]), - }), - ChatPromptTemplate.fromMessages([ - ['system', basicWebSearchResponsePrompt], - new MessagesPlaceholder('chat_history'), - ['user', '{query}'], - ]), - llm, - strParser, - ]).withConfig({ - runName: 'FinalResponseGenerator', - }); -}; - -const basicWebSearch = ( - query: string, - history: BaseMessage[], - llm: BaseChatModel, - embeddings: Embeddings, - optimizationMode: 'speed' | 'balanced' | 'quality', - fileIds: string[], -) => { - const emitter = new eventEmitter(); - - try { - const basicWebSearchAnsweringChain = createBasicWebSearchAnsweringChain( - llm, - embeddings, - optimizationMode, - fileIds, - ); - - const stream = basicWebSearchAnsweringChain.streamEvents( - { - chat_history: history, - query: query, - }, - { - version: 'v1', - }, - ); - - handleStream(stream, emitter); - } catch (err) { - emitter.emit( - 'error', - JSON.stringify({ data: 'An error has occurred please try again later' }), - ); - logger.error(`Error in websearch: ${err}`); - } - - return emitter; -}; - -const handleWebSearch = ( - message: string, - history: BaseMessage[], - llm: BaseChatModel, - embeddings: Embeddings, - optimizationMode: 'speed' | 'balanced' | 'quality', - fileIds: string[], -) => { - const emitter = basicWebSearch( - message, - history, - llm, - embeddings, - optimizationMode, - fileIds, - ); - return emitter; -}; - -export default handleWebSearch; diff --git a/src/agents/wolframAlphaSearchAgent.ts b/src/agents/wolframAlphaSearchAgent.ts deleted file mode 100644 index 1de8d0f..0000000 --- a/src/agents/wolframAlphaSearchAgent.ts +++ /dev/null @@ -1,220 +0,0 @@ -import { BaseMessage } from '@langchain/core/messages'; -import { - PromptTemplate, - ChatPromptTemplate, - MessagesPlaceholder, -} from '@langchain/core/prompts'; -import { - RunnableSequence, - RunnableMap, - RunnableLambda, -} from '@langchain/core/runnables'; -import { StringOutputParser } from '@langchain/core/output_parsers'; -import { Document } from '@langchain/core/documents'; -import { searchSearxng } from '../lib/searxng'; -import type { StreamEvent } from '@langchain/core/tracers/log_stream'; -import type { BaseChatModel } from '@langchain/core/language_models/chat_models'; -import type { Embeddings } from '@langchain/core/embeddings'; -import formatChatHistoryAsString from '../utils/formatHistory'; -import eventEmitter from 'events'; -import logger from '../utils/logger'; -import { IterableReadableStream } from '@langchain/core/utils/stream'; - -const basicWolframAlphaSearchRetrieverPrompt = ` -You will be given a conversation below and a follow up question. You need to rephrase the follow-up question if needed so it is a standalone question that can be used by the LLM to search the web for information. -If it is a writing task or a simple hi, hello rather than a question, you need to return \`not_needed\` as the response. - -Example: -1. Follow up question: What is the atomic radius of S? -Rephrased: Atomic radius of S - -2. Follow up question: What is linear algebra? -Rephrased: Linear algebra - -3. Follow up question: What is the third law of thermodynamics? -Rephrased: Third law of thermodynamics - -Conversation: -{chat_history} - -Follow up question: {query} -Rephrased question: -`; - -const basicWolframAlphaSearchResponsePrompt = ` - You are Perplexica, an AI model who is expert at searching the web and answering user's queries. You are set on focus mode 'Wolfram Alpha', this means you will be searching for information on the web using Wolfram Alpha. It is a computational knowledge engine that can answer factual queries and perform computations. - - Generate a response that is informative and relevant to the user's query based on provided context (the context consits of search results containing a brief description of the content of that page). - You must use this context to answer the user's query in the best way possible. Use an unbaised and journalistic tone in your response. Do not repeat the text. - You must not tell the user to open any link or visit any website to get the answer. You must provide the answer in the response itself. If the user asks for links you can provide them. - Your responses should be medium to long in length be informative and relevant to the user's query. You can use markdowns to format your response. You should use bullet points to list the information. Make sure the answer is not short and is informative. - You have to cite the answer using [number] notation. You must cite the sentences with their relevent context number. You must cite each and every part of the answer so the user can know where the information is coming from. - Place these citations at the end of that particular sentence. You can cite the same sentence multiple times if it is relevant to the user's query like [number1][number2]. - However you do not need to cite it using the same number. You can use different numbers to cite the same sentence multiple times. The number refers to the number of the search result (passed in the context) used to generate that part of the answer. - - Anything inside the following \`context\` HTML block provided below is for your knowledge returned by Wolfram Alpha and is not shared by the user. You have to answer question on the basis of it and cite the relevant information from it but you do not have to - talk about the context in your response. - - - {context} - - - If you think there's nothing relevant in the search results, you can say that 'Hmm, sorry I could not find any relevant information on this topic. Would you like me to search again or ask something else?'. - Anything between the \`context\` is retrieved from Wolfram Alpha and is not a part of the conversation with the user. Today's date is ${new Date().toISOString()} -`; - -const strParser = new StringOutputParser(); - -const handleStream = async ( - stream: IterableReadableStream, - emitter: eventEmitter, -) => { - for await (const event of stream) { - if ( - event.event === 'on_chain_end' && - event.name === 'FinalSourceRetriever' - ) { - emitter.emit( - 'data', - JSON.stringify({ type: 'sources', data: event.data.output }), - ); - } - if ( - event.event === 'on_chain_stream' && - event.name === 'FinalResponseGenerator' - ) { - emitter.emit( - 'data', - JSON.stringify({ type: 'response', data: event.data.chunk }), - ); - } - if ( - event.event === 'on_chain_end' && - event.name === 'FinalResponseGenerator' - ) { - emitter.emit('end'); - } - } -}; - -type BasicChainInput = { - chat_history: BaseMessage[]; - query: string; -}; - -const createBasicWolframAlphaSearchRetrieverChain = (llm: BaseChatModel) => { - return RunnableSequence.from([ - PromptTemplate.fromTemplate(basicWolframAlphaSearchRetrieverPrompt), - llm, - strParser, - RunnableLambda.from(async (input: string) => { - if (input === 'not_needed') { - return { query: '', docs: [] }; - } - - const res = await searchSearxng(input, { - language: 'en', - engines: ['wolframalpha'], - }); - - const documents = res.results.map( - (result) => - new Document({ - pageContent: result.content, - metadata: { - title: result.title, - url: result.url, - ...(result.img_src && { img_src: result.img_src }), - }, - }), - ); - - return { query: input, docs: documents }; - }), - ]); -}; - -const createBasicWolframAlphaSearchAnsweringChain = (llm: BaseChatModel) => { - const basicWolframAlphaSearchRetrieverChain = - createBasicWolframAlphaSearchRetrieverChain(llm); - - const processDocs = (docs: Document[]) => { - return docs - .map((_, index) => `${index + 1}. ${docs[index].pageContent}`) - .join('\n'); - }; - - return RunnableSequence.from([ - RunnableMap.from({ - query: (input: BasicChainInput) => input.query, - chat_history: (input: BasicChainInput) => input.chat_history, - context: RunnableSequence.from([ - (input) => ({ - query: input.query, - chat_history: formatChatHistoryAsString(input.chat_history), - }), - basicWolframAlphaSearchRetrieverChain - .pipe(({ query, docs }) => { - return docs; - }) - .withConfig({ - runName: 'FinalSourceRetriever', - }) - .pipe(processDocs), - ]), - }), - ChatPromptTemplate.fromMessages([ - ['system', basicWolframAlphaSearchResponsePrompt], - new MessagesPlaceholder('chat_history'), - ['user', '{query}'], - ]), - llm, - strParser, - ]).withConfig({ - runName: 'FinalResponseGenerator', - }); -}; - -const basicWolframAlphaSearch = ( - query: string, - history: BaseMessage[], - llm: BaseChatModel, -) => { - const emitter = new eventEmitter(); - - try { - const basicWolframAlphaSearchAnsweringChain = - createBasicWolframAlphaSearchAnsweringChain(llm); - const stream = basicWolframAlphaSearchAnsweringChain.streamEvents( - { - chat_history: history, - query: query, - }, - { - version: 'v1', - }, - ); - - handleStream(stream, emitter); - } catch (err) { - emitter.emit( - 'error', - JSON.stringify({ data: 'An error has occurred please try again later' }), - ); - logger.error(`Error in WolframAlphaSearch: ${err}`); - } - - return emitter; -}; - -const handleWolframAlphaSearch = ( - message: string, - history: BaseMessage[], - llm: BaseChatModel, - embeddings: Embeddings, -) => { - const emitter = basicWolframAlphaSearch(message, history, llm); - return emitter; -}; - -export default handleWolframAlphaSearch; diff --git a/src/agents/writingAssistant.ts b/src/agents/writingAssistant.ts deleted file mode 100644 index 7e275df..0000000 --- a/src/agents/writingAssistant.ts +++ /dev/null @@ -1,91 +0,0 @@ -import { BaseMessage } from '@langchain/core/messages'; -import { - ChatPromptTemplate, - MessagesPlaceholder, -} from '@langchain/core/prompts'; -import { RunnableSequence } from '@langchain/core/runnables'; -import { StringOutputParser } from '@langchain/core/output_parsers'; -import type { StreamEvent } from '@langchain/core/tracers/log_stream'; -import eventEmitter from 'events'; -import type { BaseChatModel } from '@langchain/core/language_models/chat_models'; -import type { Embeddings } from '@langchain/core/embeddings'; -import logger from '../utils/logger'; -import { IterableReadableStream } from '@langchain/core/utils/stream'; - -const writingAssistantPrompt = ` -You are Perplexica, an AI model who is expert at searching the web and answering user's queries. You are currently set on focus mode 'Writing Assistant', this means you will be helping the user write a response to a given query. -Since you are a writing assistant, you would not perform web searches. If you think you lack information to answer the query, you can ask the user for more information or suggest them to switch to a different focus mode. -`; - -const strParser = new StringOutputParser(); - -const handleStream = async ( - stream: IterableReadableStream, - emitter: eventEmitter, -) => { - for await (const event of stream) { - if ( - event.event === 'on_chain_stream' && - event.name === 'FinalResponseGenerator' - ) { - emitter.emit( - 'data', - JSON.stringify({ type: 'response', data: event.data.chunk }), - ); - } - if ( - event.event === 'on_chain_end' && - event.name === 'FinalResponseGenerator' - ) { - emitter.emit('end'); - } - } -}; - -const createWritingAssistantChain = (llm: BaseChatModel) => { - return RunnableSequence.from([ - ChatPromptTemplate.fromMessages([ - ['system', writingAssistantPrompt], - new MessagesPlaceholder('chat_history'), - ['user', '{query}'], - ]), - llm, - strParser, - ]).withConfig({ - runName: 'FinalResponseGenerator', - }); -}; - -const handleWritingAssistant = ( - query: string, - history: BaseMessage[], - llm: BaseChatModel, - embeddings: Embeddings, -) => { - const emitter = new eventEmitter(); - - try { - const writingAssistantChain = createWritingAssistantChain(llm); - const stream = writingAssistantChain.streamEvents( - { - chat_history: history, - query: query, - }, - { - version: 'v1', - }, - ); - - handleStream(stream, emitter); - } catch (err) { - emitter.emit( - 'error', - JSON.stringify({ data: 'An error has occurred please try again later' }), - ); - logger.error(`Error in writing assistant: ${err}`); - } - - return emitter; -}; - -export default handleWritingAssistant; diff --git a/src/agents/youtubeSearchAgent.ts b/src/agents/youtubeSearchAgent.ts deleted file mode 100644 index 2f53bc9..0000000 --- a/src/agents/youtubeSearchAgent.ts +++ /dev/null @@ -1,277 +0,0 @@ -import { BaseMessage } from '@langchain/core/messages'; -import { - PromptTemplate, - ChatPromptTemplate, - MessagesPlaceholder, -} from '@langchain/core/prompts'; -import { - RunnableSequence, - RunnableMap, - RunnableLambda, -} from '@langchain/core/runnables'; -import { StringOutputParser } from '@langchain/core/output_parsers'; -import { Document } from '@langchain/core/documents'; -import { searchSearxng } from '../lib/searxng'; -import type { StreamEvent } from '@langchain/core/tracers/log_stream'; -import type { BaseChatModel } from '@langchain/core/language_models/chat_models'; -import type { Embeddings } from '@langchain/core/embeddings'; -import formatChatHistoryAsString from '../utils/formatHistory'; -import eventEmitter from 'events'; -import computeSimilarity from '../utils/computeSimilarity'; -import logger from '../utils/logger'; -import { IterableReadableStream } from '@langchain/core/utils/stream'; - -const basicYoutubeSearchRetrieverPrompt = ` -You will be given a conversation below and a follow up question. You need to rephrase the follow-up question if needed so it is a standalone question that can be used by the LLM to search the web for information. -If it is a writing task or a simple hi, hello rather than a question, you need to return \`not_needed\` as the response. - -Example: -1. Follow up question: How does an A.C work? -Rephrased: A.C working - -2. Follow up question: Linear algebra explanation video -Rephrased: What is linear algebra? - -3. Follow up question: What is theory of relativity? -Rephrased: What is theory of relativity? - -Conversation: -{chat_history} - -Follow up question: {query} -Rephrased question: -`; - -const basicYoutubeSearchResponsePrompt = ` - You are Perplexica, an AI model who is expert at searching the web and answering user's queries. You are set on focus mode 'Youtube', this means you will be searching for videos on the web using Youtube and providing information based on the video's transcript. - - Generate a response that is informative and relevant to the user's query based on provided context (the context consits of search results containing a brief description of the content of that page). - You must use this context to answer the user's query in the best way possible. Use an unbaised and journalistic tone in your response. Do not repeat the text. - You must not tell the user to open any link or visit any website to get the answer. You must provide the answer in the response itself. If the user asks for links you can provide them. - Your responses should be medium to long in length be informative and relevant to the user's query. You can use markdowns to format your response. You should use bullet points to list the information. Make sure the answer is not short and is informative. - You have to cite the answer using [number] notation. You must cite the sentences with their relevent context number. You must cite each and every part of the answer so the user can know where the information is coming from. - Place these citations at the end of that particular sentence. You can cite the same sentence multiple times if it is relevant to the user's query like [number1][number2]. - However you do not need to cite it using the same number. You can use different numbers to cite the same sentence multiple times. The number refers to the number of the search result (passed in the context) used to generate that part of the answer. - - Anything inside the following \`context\` HTML block provided below is for your knowledge returned by Youtube and is not shared by the user. You have to answer question on the basis of it and cite the relevant information from it but you do not have to - talk about the context in your response. - - - {context} - - - If you think there's nothing relevant in the search results, you can say that 'Hmm, sorry I could not find any relevant information on this topic. Would you like me to search again or ask something else?'. - Anything between the \`context\` is retrieved from Youtube and is not a part of the conversation with the user. Today's date is ${new Date().toISOString()} -`; - -const strParser = new StringOutputParser(); - -const handleStream = async ( - stream: IterableReadableStream, - emitter: eventEmitter, -) => { - for await (const event of stream) { - if ( - event.event === 'on_chain_end' && - event.name === 'FinalSourceRetriever' - ) { - emitter.emit( - 'data', - JSON.stringify({ type: 'sources', data: event.data.output }), - ); - } - if ( - event.event === 'on_chain_stream' && - event.name === 'FinalResponseGenerator' - ) { - emitter.emit( - 'data', - JSON.stringify({ type: 'response', data: event.data.chunk }), - ); - } - if ( - event.event === 'on_chain_end' && - event.name === 'FinalResponseGenerator' - ) { - emitter.emit('end'); - } - } -}; - -type BasicChainInput = { - chat_history: BaseMessage[]; - query: string; -}; - -const createBasicYoutubeSearchRetrieverChain = (llm: BaseChatModel) => { - return RunnableSequence.from([ - PromptTemplate.fromTemplate(basicYoutubeSearchRetrieverPrompt), - llm, - strParser, - RunnableLambda.from(async (input: string) => { - if (input === 'not_needed') { - return { query: '', docs: [] }; - } - - const res = await searchSearxng(input, { - language: 'en', - engines: ['youtube'], - }); - - const documents = res.results.map( - (result) => - new Document({ - pageContent: result.content ? result.content : result.title, - metadata: { - title: result.title, - url: result.url, - ...(result.img_src && { img_src: result.img_src }), - }, - }), - ); - - return { query: input, docs: documents }; - }), - ]); -}; - -const createBasicYoutubeSearchAnsweringChain = ( - llm: BaseChatModel, - embeddings: Embeddings, - optimizationMode: 'speed' | 'balanced' | 'quality', -) => { - const basicYoutubeSearchRetrieverChain = - createBasicYoutubeSearchRetrieverChain(llm); - - const processDocs = async (docs: Document[]) => { - return docs - .map((_, index) => `${index + 1}. ${docs[index].pageContent}`) - .join('\n'); - }; - - const rerankDocs = async ({ - query, - docs, - }: { - query: string; - docs: Document[]; - }) => { - if (docs.length === 0) { - return docs; - } - - const docsWithContent = docs.filter( - (doc) => doc.pageContent && doc.pageContent.length > 0, - ); - - if (optimizationMode === 'speed') { - return docsWithContent.slice(0, 15); - } else { - const [docEmbeddings, queryEmbedding] = await Promise.all([ - embeddings.embedDocuments( - docsWithContent.map((doc) => doc.pageContent), - ), - embeddings.embedQuery(query), - ]); - - const similarity = docEmbeddings.map((docEmbedding, i) => { - const sim = computeSimilarity(queryEmbedding, docEmbedding); - - return { - index: i, - similarity: sim, - }; - }); - - const sortedDocs = similarity - .filter((sim) => sim.similarity > 0.3) - .sort((a, b) => b.similarity - a.similarity) - .slice(0, 15) - .map((sim) => docsWithContent[sim.index]); - - return sortedDocs; - } - }; - - return RunnableSequence.from([ - RunnableMap.from({ - query: (input: BasicChainInput) => input.query, - chat_history: (input: BasicChainInput) => input.chat_history, - context: RunnableSequence.from([ - (input) => ({ - query: input.query, - chat_history: formatChatHistoryAsString(input.chat_history), - }), - basicYoutubeSearchRetrieverChain - .pipe(rerankDocs) - .withConfig({ - runName: 'FinalSourceRetriever', - }) - .pipe(processDocs), - ]), - }), - ChatPromptTemplate.fromMessages([ - ['system', basicYoutubeSearchResponsePrompt], - new MessagesPlaceholder('chat_history'), - ['user', '{query}'], - ]), - llm, - strParser, - ]).withConfig({ - runName: 'FinalResponseGenerator', - }); -}; - -const basicYoutubeSearch = ( - query: string, - history: BaseMessage[], - llm: BaseChatModel, - embeddings: Embeddings, - optimizationMode: 'speed' | 'balanced' | 'quality', -) => { - const emitter = new eventEmitter(); - - try { - const basicYoutubeSearchAnsweringChain = - createBasicYoutubeSearchAnsweringChain(llm, embeddings, optimizationMode); - - const stream = basicYoutubeSearchAnsweringChain.streamEvents( - { - chat_history: history, - query: query, - }, - { - version: 'v1', - }, - ); - - handleStream(stream, emitter); - } catch (err) { - emitter.emit( - 'error', - JSON.stringify({ data: 'An error has occurred please try again later' }), - ); - logger.error(`Error in youtube search: ${err}`); - } - - return emitter; -}; - -const handleYoutubeSearch = ( - message: string, - history: BaseMessage[], - llm: BaseChatModel, - embeddings: Embeddings, - optimizationMode: 'speed' | 'balanced' | 'quality', -) => { - const emitter = basicYoutubeSearch( - message, - history, - llm, - embeddings, - optimizationMode, - ); - return emitter; -}; - -export default handleYoutubeSearch; diff --git a/src/agents/imageSearchAgent.ts b/src/chains/imageSearchAgent.ts similarity index 100% rename from src/agents/imageSearchAgent.ts rename to src/chains/imageSearchAgent.ts diff --git a/src/agents/suggestionGeneratorAgent.ts b/src/chains/suggestionGeneratorAgent.ts similarity index 100% rename from src/agents/suggestionGeneratorAgent.ts rename to src/chains/suggestionGeneratorAgent.ts diff --git a/src/agents/videoSearchAgent.ts b/src/chains/videoSearchAgent.ts similarity index 100% rename from src/agents/videoSearchAgent.ts rename to src/chains/videoSearchAgent.ts diff --git a/src/prompts/academicSearch.ts b/src/prompts/academicSearch.ts new file mode 100644 index 0000000..30495ee --- /dev/null +++ b/src/prompts/academicSearch.ts @@ -0,0 +1,42 @@ +export const academicSearchRetrieverPrompt = ` +You will be given a conversation below and a follow up question. You need to rephrase the follow-up question if needed so it is a standalone question that can be used by the LLM to search the web for information. +If it is a writing task or a simple hi, hello rather than a question, you need to return \`not_needed\` as the response. + +Example: +1. Follow up question: How does stable diffusion work? +Rephrased: Stable diffusion working + +2. Follow up question: What is linear algebra? +Rephrased: Linear algebra + +3. Follow up question: What is the third law of thermodynamics? +Rephrased: Third law of thermodynamics + +Conversation: +{chat_history} + +Follow up question: {query} +Rephrased question: +`; + +export const academicSearchResponsePrompt = ` + You are Perplexica, an AI model who is expert at searching the web and answering user's queries. You are set on focus mode 'Academic', this means you will be searching for academic papers and articles on the web. + + Generate a response that is informative and relevant to the user's query based on provided context (the context consits of search results containing a brief description of the content of that page). + You must use this context to answer the user's query in the best way possible. Use an unbaised and journalistic tone in your response. Do not repeat the text. + You must not tell the user to open any link or visit any website to get the answer. You must provide the answer in the response itself. If the user asks for links you can provide them. + Your responses should be medium to long in length be informative and relevant to the user's query. You can use markdowns to format your response. You should use bullet points to list the information. Make sure the answer is not short and is informative. + You have to cite the answer using [number] notation. You must cite the sentences with their relevent context number. You must cite each and every part of the answer so the user can know where the information is coming from. + Place these citations at the end of that particular sentence. You can cite the same sentence multiple times if it is relevant to the user's query like [number1][number2]. + However you do not need to cite it using the same number. You can use different numbers to cite the same sentence multiple times. The number refers to the number of the search result (passed in the context) used to generate that part of the answer. + + Anything inside the following \`context\` HTML block provided below is for your knowledge returned by the search engine and is not shared by the user. You have to answer question on the basis of it and cite the relevant information from it but you do not have to + talk about the context in your response. + + + {context} + + + If you think there's nothing relevant in the search results, you can say that 'Hmm, sorry I could not find any relevant information on this topic. Would you like me to search again or ask something else?'. + Anything between the \`context\` is retrieved from a search engine and is not a part of the conversation with the user. Today's date is ${new Date().toISOString()} +`; diff --git a/src/prompts/index.ts b/src/prompts/index.ts new file mode 100644 index 0000000..f479185 --- /dev/null +++ b/src/prompts/index.ts @@ -0,0 +1,32 @@ +import { + academicSearchResponsePrompt, + academicSearchRetrieverPrompt, +} from './academicSearch'; +import { + redditSearchResponsePrompt, + redditSearchRetrieverPrompt, +} from './redditSearch'; +import { webSearchResponsePrompt, webSearchRetrieverPrompt } from './webSearch'; +import { + wolframAlphaSearchResponsePrompt, + wolframAlphaSearchRetrieverPrompt, +} from './wolframAlpha'; +import { writingAssistantPrompt } from './writingAssistant'; +import { + youtubeSearchResponsePrompt, + youtubeSearchRetrieverPrompt, +} from './youtubeSearch'; + +export default { + webSearchResponsePrompt, + webSearchRetrieverPrompt, + academicSearchResponsePrompt, + academicSearchRetrieverPrompt, + redditSearchResponsePrompt, + redditSearchRetrieverPrompt, + wolframAlphaSearchResponsePrompt, + wolframAlphaSearchRetrieverPrompt, + writingAssistantPrompt, + youtubeSearchResponsePrompt, + youtubeSearchRetrieverPrompt, +}; diff --git a/src/prompts/redditSearch.ts b/src/prompts/redditSearch.ts new file mode 100644 index 0000000..1418eac --- /dev/null +++ b/src/prompts/redditSearch.ts @@ -0,0 +1,42 @@ +export const redditSearchRetrieverPrompt = ` +You will be given a conversation below and a follow up question. You need to rephrase the follow-up question if needed so it is a standalone question that can be used by the LLM to search the web for information. +If it is a writing task or a simple hi, hello rather than a question, you need to return \`not_needed\` as the response. + +Example: +1. Follow up question: Which company is most likely to create an AGI +Rephrased: Which company is most likely to create an AGI + +2. Follow up question: Is Earth flat? +Rephrased: Is Earth flat? + +3. Follow up question: Is there life on Mars? +Rephrased: Is there life on Mars? + +Conversation: +{chat_history} + +Follow up question: {query} +Rephrased question: +`; + +export const redditSearchResponsePrompt = ` + You are Perplexica, an AI model who is expert at searching the web and answering user's queries. You are set on focus mode 'Reddit', this means you will be searching for information, opinions and discussions on the web using Reddit. + + Generate a response that is informative and relevant to the user's query based on provided context (the context consits of search results containing a brief description of the content of that page). + You must use this context to answer the user's query in the best way possible. Use an unbaised and journalistic tone in your response. Do not repeat the text. + You must not tell the user to open any link or visit any website to get the answer. You must provide the answer in the response itself. If the user asks for links you can provide them. + Your responses should be medium to long in length be informative and relevant to the user's query. You can use markdowns to format your response. You should use bullet points to list the information. Make sure the answer is not short and is informative. + You have to cite the answer using [number] notation. You must cite the sentences with their relevent context number. You must cite each and every part of the answer so the user can know where the information is coming from. + Place these citations at the end of that particular sentence. You can cite the same sentence multiple times if it is relevant to the user's query like [number1][number2]. + However you do not need to cite it using the same number. You can use different numbers to cite the same sentence multiple times. The number refers to the number of the search result (passed in the context) used to generate that part of the answer. + + Anything inside the following \`context\` HTML block provided below is for your knowledge returned by Reddit and is not shared by the user. You have to answer question on the basis of it and cite the relevant information from it but you do not have to + talk about the context in your response. + + + {context} + + + If you think there's nothing relevant in the search results, you can say that 'Hmm, sorry I could not find any relevant information on this topic. Would you like me to search again or ask something else?'. + Anything between the \`context\` is retrieved from Reddit and is not a part of the conversation with the user. Today's date is ${new Date().toISOString()} +`; diff --git a/src/prompts/webSearch.ts b/src/prompts/webSearch.ts new file mode 100644 index 0000000..834bbfe --- /dev/null +++ b/src/prompts/webSearch.ts @@ -0,0 +1,86 @@ +export const webSearchRetrieverPrompt = ` +You are an AI question rephraser. You will be given a conversation and a follow-up question, you will have to rephrase the follow up question so it is a standalone question and can be used by another LLM to search the web for information to answer it. +If it is a smple writing task or a greeting (unless the greeting contains a question after it) like Hi, Hello, How are you, etc. than a question then you need to return \`not_needed\` as the response (This is because the LLM won't need to search the web for finding information on this topic). +If the user asks some question from some URL or wants you to summarize a PDF or a webpage (via URL) you need to return the links inside the \`links\` XML block and the question inside the \`question\` XML block. If the user wants to you to summarize the webpage or the PDF you need to return \`summarize\` inside the \`question\` XML block in place of a question and the link to summarize in the \`links\` XML block. +You must always return the rephrased question inside the \`question\` XML block, if there are no links in the follow-up question then don't insert a \`links\` XML block in your response. + +There are several examples attached for your reference inside the below \`examples\` XML block + + +1. Follow up question: What is the capital of France +Rephrased question:\` + +Capital of france + +\` + +2. Hi, how are you? +Rephrased question\` + +not_needed + +\` + +3. Follow up question: What is Docker? +Rephrased question: \` + +What is Docker + +\` + +4. Follow up question: Can you tell me what is X from https://example.com +Rephrased question: \` + +Can you tell me what is X? + + + +https://example.com + +\` + +5. Follow up question: Summarize the content from https://example.com +Rephrased question: \` + +summarize + + + +https://example.com + +\` + + +Anything below is the part of the actual conversation and you need to use conversation and the follow-up question to rephrase the follow-up question as a standalone question based on the guidelines shared above. + + +{chat_history} + + +Follow up question: {query} +Rephrased question: +`; + +export const webSearchResponsePrompt = ` + You are Perplexica, an AI model who is expert at searching the web and answering user's queries. You are also an expert at summarizing web pages or documents and searching for content in them. + + Generate a response that is informative and relevant to the user's query based on provided context (the context consits of search results containing a brief description of the content of that page). + You must use this context to answer the user's query in the best way possible. Use an unbaised and journalistic tone in your response. Do not repeat the text. + You must not tell the user to open any link or visit any website to get the answer. You must provide the answer in the response itself. If the user asks for links you can provide them. + If the query contains some links and the user asks to answer from those links you will be provided the entire content of the page inside the \`context\` XML block. You can then use this content to answer the user's query. + If the user asks to summarize content from some links, you will be provided the entire content of the page inside the \`context\` XML block. You can then use this content to summarize the text. The content provided inside the \`context\` block will be already summarized by another model so you just need to use that content to answer the user's query. + Your responses should be medium to long in length be informative and relevant to the user's query. You can use markdowns to format your response. You should use bullet points to list the information. Make sure the answer is not short and is informative. + You have to cite the answer using [number] notation. You must cite the sentences with their relevent context number. You must cite each and every part of the answer so the user can know where the information is coming from. + Place these citations at the end of that particular sentence. You can cite the same sentence multiple times if it is relevant to the user's query like [number1][number2]. + However you do not need to cite it using the same number. You can use different numbers to cite the same sentence multiple times. The number refers to the number of the search result (passed in the context) used to generate that part of the answer. + + Anything inside the following \`context\` HTML block provided below is for your knowledge returned by the search engine and is not shared by the user. You have to answer question on the basis of it and cite the relevant information from it but you do not have to + talk about the context in your response. + + + {context} + + + If you think there's nothing relevant in the search results, you can say that 'Hmm, sorry I could not find any relevant information on this topic. Would you like me to search again or ask something else?'. You do not need to do this for summarization tasks. + Anything between the \`context\` is retrieved from a search engine and is not a part of the conversation with the user. Today's date is ${new Date().toISOString()} +`; diff --git a/src/prompts/wolframAlpha.ts b/src/prompts/wolframAlpha.ts new file mode 100644 index 0000000..2e9c022 --- /dev/null +++ b/src/prompts/wolframAlpha.ts @@ -0,0 +1,42 @@ +export const wolframAlphaSearchRetrieverPrompt = ` +You will be given a conversation below and a follow up question. You need to rephrase the follow-up question if needed so it is a standalone question that can be used by the LLM to search the web for information. +If it is a writing task or a simple hi, hello rather than a question, you need to return \`not_needed\` as the response. + +Example: +1. Follow up question: What is the atomic radius of S? +Rephrased: Atomic radius of S + +2. Follow up question: What is linear algebra? +Rephrased: Linear algebra + +3. Follow up question: What is the third law of thermodynamics? +Rephrased: Third law of thermodynamics + +Conversation: +{chat_history} + +Follow up question: {query} +Rephrased question: +`; + +export const wolframAlphaSearchResponsePrompt = ` + You are Perplexica, an AI model who is expert at searching the web and answering user's queries. You are set on focus mode 'Wolfram Alpha', this means you will be searching for information on the web using Wolfram Alpha. It is a computational knowledge engine that can answer factual queries and perform computations. + + Generate a response that is informative and relevant to the user's query based on provided context (the context consits of search results containing a brief description of the content of that page). + You must use this context to answer the user's query in the best way possible. Use an unbaised and journalistic tone in your response. Do not repeat the text. + You must not tell the user to open any link or visit any website to get the answer. You must provide the answer in the response itself. If the user asks for links you can provide them. + Your responses should be medium to long in length be informative and relevant to the user's query. You can use markdowns to format your response. You should use bullet points to list the information. Make sure the answer is not short and is informative. + You have to cite the answer using [number] notation. You must cite the sentences with their relevent context number. You must cite each and every part of the answer so the user can know where the information is coming from. + Place these citations at the end of that particular sentence. You can cite the same sentence multiple times if it is relevant to the user's query like [number1][number2]. + However you do not need to cite it using the same number. You can use different numbers to cite the same sentence multiple times. The number refers to the number of the search result (passed in the context) used to generate that part of the answer. + + Anything inside the following \`context\` HTML block provided below is for your knowledge returned by Wolfram Alpha and is not shared by the user. You have to answer question on the basis of it and cite the relevant information from it but you do not have to + talk about the context in your response. + + + {context} + + + If you think there's nothing relevant in the search results, you can say that 'Hmm, sorry I could not find any relevant information on this topic. Would you like me to search again or ask something else?'. + Anything between the \`context\` is retrieved from Wolfram Alpha and is not a part of the conversation with the user. Today's date is ${new Date().toISOString()} +`; diff --git a/src/prompts/writingAssistant.ts b/src/prompts/writingAssistant.ts new file mode 100644 index 0000000..f56bf47 --- /dev/null +++ b/src/prompts/writingAssistant.ts @@ -0,0 +1,13 @@ +export const writingAssistantPrompt = ` +You are Perplexica, an AI model who is expert at searching the web and answering user's queries. You are currently set on focus mode 'Writing Assistant', this means you will be helping the user write a response to a given query. +Since you are a writing assistant, you would not perform web searches. If you think you lack information to answer the query, you can ask the user for more information or suggest them to switch to a different focus mode. +You will be shared a context that can contain information from files user has uploaded to get answers from. You will have to generate answers upon that. + +You have to cite the answer using [number] notation. You must cite the sentences with their relevent context number. You must cite each and every part of the answer so the user can know where the information is coming from. +Place these citations at the end of that particular sentence. You can cite the same sentence multiple times if it is relevant to the user's query like [number1][number2]. +However you do not need to cite it using the same number. You can use different numbers to cite the same sentence multiple times. The number refers to the number of the search result (passed in the context) used to generate that part of the answer. + + +{context} + +`; diff --git a/src/prompts/youtubeSearch.ts b/src/prompts/youtubeSearch.ts new file mode 100644 index 0000000..5c729cb --- /dev/null +++ b/src/prompts/youtubeSearch.ts @@ -0,0 +1,42 @@ +export const youtubeSearchRetrieverPrompt = ` +You will be given a conversation below and a follow up question. You need to rephrase the follow-up question if needed so it is a standalone question that can be used by the LLM to search the web for information. +If it is a writing task or a simple hi, hello rather than a question, you need to return \`not_needed\` as the response. + +Example: +1. Follow up question: How does an A.C work? +Rephrased: A.C working + +2. Follow up question: Linear algebra explanation video +Rephrased: What is linear algebra? + +3. Follow up question: What is theory of relativity? +Rephrased: What is theory of relativity? + +Conversation: +{chat_history} + +Follow up question: {query} +Rephrased question: +`; + +export const youtubeSearchResponsePrompt = ` + You are Perplexica, an AI model who is expert at searching the web and answering user's queries. You are set on focus mode 'Youtube', this means you will be searching for videos on the web using Youtube and providing information based on the video's transcript. + + Generate a response that is informative and relevant to the user's query based on provided context (the context consits of search results containing a brief description of the content of that page). + You must use this context to answer the user's query in the best way possible. Use an unbaised and journalistic tone in your response. Do not repeat the text. + You must not tell the user to open any link or visit any website to get the answer. You must provide the answer in the response itself. If the user asks for links you can provide them. + Your responses should be medium to long in length be informative and relevant to the user's query. You can use markdowns to format your response. You should use bullet points to list the information. Make sure the answer is not short and is informative. + You have to cite the answer using [number] notation. You must cite the sentences with their relevent context number. You must cite each and every part of the answer so the user can know where the information is coming from. + Place these citations at the end of that particular sentence. You can cite the same sentence multiple times if it is relevant to the user's query like [number1][number2]. + However you do not need to cite it using the same number. You can use different numbers to cite the same sentence multiple times. The number refers to the number of the search result (passed in the context) used to generate that part of the answer. + + Anything inside the following \`context\` HTML block provided below is for your knowledge returned by Youtube and is not shared by the user. You have to answer question on the basis of it and cite the relevant information from it but you do not have to + talk about the context in your response. + + + {context} + + + If you think there's nothing relevant in the search results, you can say that 'Hmm, sorry I could not find any relevant information on this topic. Would you like me to search again or ask something else?'. + Anything between the \`context\` is retrieved from Youtube and is not a part of the conversation with the user. Today's date is ${new Date().toISOString()} +`; diff --git a/src/routes/images.ts b/src/routes/images.ts index c54dc40..efa095a 100644 --- a/src/routes/images.ts +++ b/src/routes/images.ts @@ -1,5 +1,5 @@ import express from 'express'; -import handleImageSearch from '../agents/imageSearchAgent'; +import handleImageSearch from '../chains/imageSearchAgent'; import { BaseChatModel } from '@langchain/core/language_models/chat_models'; import { getAvailableChatModelProviders } from '../lib/providers'; import { HumanMessage, AIMessage } from '@langchain/core/messages'; diff --git a/src/routes/search.ts b/src/routes/search.ts index 70fe228..e24b3f9 100644 --- a/src/routes/search.ts +++ b/src/routes/search.ts @@ -1,7 +1,7 @@ import express from 'express'; import logger from '../utils/logger'; -import { BaseChatModel } from 'langchain/chat_models/base'; -import { Embeddings } from 'langchain/embeddings/base'; +import type { BaseChatModel } from '@langchain/core/language_models/chat_models'; +import type { Embeddings } from '@langchain/core/embeddings'; import { ChatOpenAI } from '@langchain/openai'; import { getAvailableChatModelProviders, @@ -9,6 +9,7 @@ import { } from '../lib/providers'; import { searchHandlers } from '../websocket/messageHandler'; import { AIMessage, BaseMessage, HumanMessage } from '@langchain/core/messages'; +import { MetaSearchAgentType } from '../search/metaSearchAgent'; const router = express.Router(); @@ -115,18 +116,19 @@ router.post('/', async (req, res) => { return res.status(400).json({ message: 'Invalid model selected' }); } - const searchHandler = searchHandlers[body.focusMode]; + const searchHandler: MetaSearchAgentType = searchHandlers[body.focusMode]; if (!searchHandler) { return res.status(400).json({ message: 'Invalid focus mode' }); } - const emitter = searchHandler( + const emitter = await searchHandler.searchAndAnswer( body.query, history, llm, embeddings, body.optimizationMode, + [], ); let message = ''; diff --git a/src/routes/suggestions.ts b/src/routes/suggestions.ts index e997b1e..1d46e5b 100644 --- a/src/routes/suggestions.ts +++ b/src/routes/suggestions.ts @@ -1,5 +1,5 @@ import express from 'express'; -import generateSuggestions from '../agents/suggestionGeneratorAgent'; +import generateSuggestions from '../chains/suggestionGeneratorAgent'; import { BaseChatModel } from '@langchain/core/language_models/chat_models'; import { getAvailableChatModelProviders } from '../lib/providers'; import { HumanMessage, AIMessage } from '@langchain/core/messages'; diff --git a/src/routes/videos.ts b/src/routes/videos.ts index a2555f5..ad87460 100644 --- a/src/routes/videos.ts +++ b/src/routes/videos.ts @@ -3,7 +3,7 @@ import { BaseChatModel } from '@langchain/core/language_models/chat_models'; import { getAvailableChatModelProviders } from '../lib/providers'; import { HumanMessage, AIMessage } from '@langchain/core/messages'; import logger from '../utils/logger'; -import handleVideoSearch from '../agents/videoSearchAgent'; +import handleVideoSearch from '../chains/videoSearchAgent'; import { ChatOpenAI } from '@langchain/openai'; const router = express.Router(); diff --git a/src/websocket/messageHandler.ts b/src/websocket/messageHandler.ts index d2a2518..598d98a 100644 --- a/src/websocket/messageHandler.ts +++ b/src/websocket/messageHandler.ts @@ -1,11 +1,5 @@ import { EventEmitter, WebSocket } from 'ws'; import { BaseMessage, AIMessage, HumanMessage } from '@langchain/core/messages'; -import handleWebSearch from '../agents/webSearchAgent'; -import handleAcademicSearch from '../agents/academicSearchAgent'; -import handleWritingAssistant from '../agents/writingAssistant'; -import handleWolframAlphaSearch from '../agents/wolframAlphaSearchAgent'; -import handleYoutubeSearch from '../agents/youtubeSearchAgent'; -import handleRedditSearch from '../agents/redditSearchAgent'; import type { BaseChatModel } from '@langchain/core/language_models/chat_models'; import type { Embeddings } from '@langchain/core/embeddings'; import logger from '../utils/logger'; @@ -14,6 +8,10 @@ import { chats, messages as messagesSchema } from '../db/schema'; import { eq, asc, gt } from 'drizzle-orm'; import crypto from 'crypto'; import { getFileDetails } from '../utils/files'; +import MetaSearchAgent, { + MetaSearchAgentType, +} from '../search/metaSearchAgent'; +import prompts from '../prompts'; type Message = { messageId: string; @@ -23,7 +21,7 @@ type Message = { type WSMessage = { message: Message; - optimizationMode: string; + optimizationMode: 'speed' | 'balanced' | 'quality'; type: string; focusMode: string; history: Array<[string, string]>; @@ -31,12 +29,60 @@ type WSMessage = { }; export const searchHandlers = { - webSearch: handleWebSearch, - academicSearch: handleAcademicSearch, - writingAssistant: handleWritingAssistant, - wolframAlphaSearch: handleWolframAlphaSearch, - youtubeSearch: handleYoutubeSearch, - redditSearch: handleRedditSearch, + webSearch: new MetaSearchAgent({ + activeEngines: [], + queryGeneratorPrompt: prompts.webSearchRetrieverPrompt, + responsePrompt: prompts.webSearchResponsePrompt, + rerank: true, + rerankThreshold: 0.3, + searchWeb: true, + summarizer: true, + }), + academicSearch: new MetaSearchAgent({ + activeEngines: ['arxiv', 'google scholar', 'pubmed'], + queryGeneratorPrompt: prompts.academicSearchRetrieverPrompt, + responsePrompt: prompts.academicSearchResponsePrompt, + rerank: true, + rerankThreshold: 0, + searchWeb: true, + summarizer: false, + }), + writingAssistant: new MetaSearchAgent({ + activeEngines: [], + queryGeneratorPrompt: '', + responsePrompt: prompts.writingAssistantPrompt, + rerank: true, + rerankThreshold: 0, + searchWeb: false, + summarizer: false, + }), + wolframAlphaSearch: new MetaSearchAgent({ + activeEngines: ['wolframalpha'], + queryGeneratorPrompt: prompts.wolframAlphaSearchRetrieverPrompt, + responsePrompt: prompts.wolframAlphaSearchResponsePrompt, + rerank: false, + rerankThreshold: 0, + searchWeb: true, + summarizer: false, + }), + youtubeSearch: new MetaSearchAgent({ + activeEngines: ['youtube'], + queryGeneratorPrompt: prompts.youtubeSearchRetrieverPrompt, + responsePrompt: prompts.youtubeSearchResponsePrompt, + rerank: true, + rerankThreshold: 0.3, + searchWeb: true, + summarizer: false, + }), + redditSearch: new MetaSearchAgent({ + activeEngines: ['reddit'], + queryGeneratorPrompt: prompts.redditSearchRetrieverPrompt, + responsePrompt: prompts.redditSearchResponsePrompt, + rerank: true, + rerankThreshold: 0.3, + searchWeb: true, + summarizer: false, + }), }; const handleEmitterEvents = ( @@ -134,59 +180,64 @@ export const handleMessage = async ( }); if (parsedWSMessage.type === 'message') { - const handler = searchHandlers[parsedWSMessage.focusMode]; + const handler: MetaSearchAgentType = + searchHandlers[parsedWSMessage.focusMode]; if (handler) { - const emitter = handler( - parsedMessage.content, - history, - llm, - embeddings, - parsedWSMessage.optimizationMode, - parsedWSMessage.files, - ); + try { + const emitter = await handler.searchAndAnswer( + parsedMessage.content, + history, + llm, + embeddings, + parsedWSMessage.optimizationMode, + parsedWSMessage.files, + ); - handleEmitterEvents(emitter, ws, aiMessageId, parsedMessage.chatId); + handleEmitterEvents(emitter, ws, aiMessageId, parsedMessage.chatId); - const chat = await db.query.chats.findFirst({ - where: eq(chats.id, parsedMessage.chatId), - }); + const chat = await db.query.chats.findFirst({ + where: eq(chats.id, parsedMessage.chatId), + }); - if (!chat) { - await db - .insert(chats) - .values({ - id: parsedMessage.chatId, - title: parsedMessage.content, - createdAt: new Date().toString(), - focusMode: parsedWSMessage.focusMode, - files: parsedWSMessage.files.map(getFileDetails), - }) - .execute(); - } + if (!chat) { + await db + .insert(chats) + .values({ + id: parsedMessage.chatId, + title: parsedMessage.content, + createdAt: new Date().toString(), + focusMode: parsedWSMessage.focusMode, + files: parsedWSMessage.files.map(getFileDetails), + }) + .execute(); + } - const messageExists = await db.query.messages.findFirst({ - where: eq(messagesSchema.messageId, humanMessageId), - }); + const messageExists = await db.query.messages.findFirst({ + where: eq(messagesSchema.messageId, humanMessageId), + }); - if (!messageExists) { - await db - .insert(messagesSchema) - .values({ - content: parsedMessage.content, - chatId: parsedMessage.chatId, - messageId: humanMessageId, - role: 'user', - metadata: JSON.stringify({ - createdAt: new Date(), - }), - }) - .execute(); - } else { - await db - .delete(messagesSchema) - .where(gt(messagesSchema.id, messageExists.id)) - .execute(); + if (!messageExists) { + await db + .insert(messagesSchema) + .values({ + content: parsedMessage.content, + chatId: parsedMessage.chatId, + messageId: humanMessageId, + role: 'user', + metadata: JSON.stringify({ + createdAt: new Date(), + }), + }) + .execute(); + } else { + await db + .delete(messagesSchema) + .where(gt(messagesSchema.id, messageExists.id)) + .execute(); + } + } catch (err) { + console.log(err); } } else { ws.send( From e4a07995034b226946ff94df87b4069c7e35cd0a Mon Sep 17 00:00:00 2001 From: ItzCrazyKns <95534749+ItzCrazyKns@users.noreply.github.com> Date: Fri, 29 Nov 2024 18:37:02 +0530 Subject: [PATCH 06/31] feat(package): bump version --- package.json | 2 +- src/utils/files.ts | 1 + ui/package.json | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 0368b21..3fce442 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "perplexica-backend", - "version": "1.10.0-rc1", + "version": "1.10.0-rc2", "license": "MIT", "author": "ItzCrazyKns", "scripts": { diff --git a/src/utils/files.ts b/src/utils/files.ts index 3e7a134..e6e91df 100644 --- a/src/utils/files.ts +++ b/src/utils/files.ts @@ -1,5 +1,6 @@ import path from 'path'; import fs from 'fs'; + export const getFileDetails = (fileId: string) => { const fileLoc = path.join( process.cwd(), diff --git a/ui/package.json b/ui/package.json index 6e4fc42..bdda684 100644 --- a/ui/package.json +++ b/ui/package.json @@ -1,6 +1,6 @@ { "name": "perplexica-frontend", - "version": "1.10.0-rc1", + "version": "1.10.0-rc2", "license": "MIT", "author": "ItzCrazyKns", "scripts": { From e08d86444565a1976c4888037a615070360487eb Mon Sep 17 00:00:00 2001 From: ItzCrazyKns <95534749+ItzCrazyKns@users.noreply.github.com> Date: Sat, 30 Nov 2024 20:58:11 +0530 Subject: [PATCH 07/31] feat(focus): only icon on small devices --- ui/components/MessageInputActions/Focus.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ui/components/MessageInputActions/Focus.tsx b/ui/components/MessageInputActions/Focus.tsx index c95be5a..613078b 100644 --- a/ui/components/MessageInputActions/Focus.tsx +++ b/ui/components/MessageInputActions/Focus.tsx @@ -83,7 +83,7 @@ const Focus = ({ {focusMode !== 'webSearch' ? (
{focusModes.find((mode) => mode.key === focusMode)?.icon} -

+

{focusModes.find((mode) => mode.key === focusMode)?.title}

@@ -91,7 +91,7 @@ const Focus = ({ ) : (
-

Focus

+

Focus

)} From 8cb74f1964f3867e46fb431b1585f408b1f44dec Mon Sep 17 00:00:00 2001 From: ItzCrazyKns <95534749+ItzCrazyKns@users.noreply.github.com> Date: Mon, 2 Dec 2024 21:07:59 +0530 Subject: [PATCH 08/31] feat(contribution): update guidelines --- CONTRIBUTING.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 73256bd..b16eccf 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -8,6 +8,7 @@ Perplexica's design consists of two main domains: - **Frontend (`ui` directory)**: This is a Next.js application holding all user interface components. It's a self-contained environment that manages everything the user interacts with. - **Backend (root and `src` directory)**: The backend logic is situated in the `src` folder, but the root directory holds the main `package.json` for backend dependency management. + - All of the focus modes are created using the Meta Search Agent class present in `src/search/metaSearchAgent.ts`. The main logic behind Perplexica lies there. ## Setting Up Your Environment From 5b3e8a321472fb712d66ea38caa839a650ed8a2e Mon Sep 17 00:00:00 2001 From: ItzCrazyKns <95534749+ItzCrazyKns@users.noreply.github.com> Date: Thu, 5 Dec 2024 20:19:22 +0530 Subject: [PATCH 09/31] feat(prompts): implement new prompt --- src/prompts/academicSearch.ts | 47 +++++++++++++++++++++++++--------- src/prompts/redditSearch.ts | 47 +++++++++++++++++++++++++--------- src/prompts/webSearch.ts | 48 +++++++++++++++++++++++++---------- src/prompts/wolframAlpha.ts | 47 +++++++++++++++++++++++++--------- src/prompts/youtubeSearch.ts | 47 +++++++++++++++++++++++++--------- src/search/metaSearchAgent.ts | 1 + 6 files changed, 175 insertions(+), 62 deletions(-) diff --git a/src/prompts/academicSearch.ts b/src/prompts/academicSearch.ts index 30495ee..c2946ff 100644 --- a/src/prompts/academicSearch.ts +++ b/src/prompts/academicSearch.ts @@ -20,23 +20,46 @@ Rephrased question: `; export const academicSearchResponsePrompt = ` - You are Perplexica, an AI model who is expert at searching the web and answering user's queries. You are set on focus mode 'Academic', this means you will be searching for academic papers and articles on the web. + You are Perplexica, an AI model skilled in web search and crafting detailed, engaging, and well-structured answers. You excel at summarizing web pages and extracting relevant information to create professional, blog-style responses. - Generate a response that is informative and relevant to the user's query based on provided context (the context consits of search results containing a brief description of the content of that page). - You must use this context to answer the user's query in the best way possible. Use an unbaised and journalistic tone in your response. Do not repeat the text. - You must not tell the user to open any link or visit any website to get the answer. You must provide the answer in the response itself. If the user asks for links you can provide them. - Your responses should be medium to long in length be informative and relevant to the user's query. You can use markdowns to format your response. You should use bullet points to list the information. Make sure the answer is not short and is informative. - You have to cite the answer using [number] notation. You must cite the sentences with their relevent context number. You must cite each and every part of the answer so the user can know where the information is coming from. - Place these citations at the end of that particular sentence. You can cite the same sentence multiple times if it is relevant to the user's query like [number1][number2]. - However you do not need to cite it using the same number. You can use different numbers to cite the same sentence multiple times. The number refers to the number of the search result (passed in the context) used to generate that part of the answer. + Your task is to provide answers that are: + - **Informative and relevant**: Thoroughly address the user's query using the given context. + - **Well-structured**: Include clear headings and subheadings, and use a professional tone to present information concisely and logically. + - **Engaging and detailed**: Write responses that read like a high-quality blog post, including extra details and relevant insights. + - **Cited and credible**: Use inline citations with [number] notation to refer to the context source(s) for each fact or detail included. + - **Explanatory and Comprehensive**: Strive to explain the topic in depth, offering detailed analysis, insights, and clarifications wherever applicable. - Anything inside the following \`context\` HTML block provided below is for your knowledge returned by the search engine and is not shared by the user. You have to answer question on the basis of it and cite the relevant information from it but you do not have to - talk about the context in your response. + ### Formatting Instructions + - **Structure**: Use a well-organized format with proper headings (e.g., "## Example heading 1" or "## Example heading 2"). Present information in paragraphs or concise bullet points where appropriate. + - **Tone and Style**: Maintain a neutral, journalistic tone with engaging narrative flow. Write as though you're crafting an in-depth article for a professional audience. + - **Markdown Usage**: Format your response with Markdown for clarity. Use headings, subheadings, bold text, and italicized words as needed to enhance readability. + - **Length and Depth**: Provide comprehensive coverage of the topic. Avoid superficial responses and strive for depth without unnecessary repetition. Expand on technical or complex topics to make them easier to understand for a general audience. + - **No main heading/title**: Start your response directly with the introduction unless asked to provide a specific title. + - **Conclusion or Summary**: Include a concluding paragraph that synthesizes the provided information or suggests potential next steps, where appropriate. + + ### Citation Requirements + - Cite every single fact, statement, or sentence using [number] notation corresponding to the source from the provided \`context\`. + - Integrate citations naturally at the end of sentences or clauses as appropriate. For example, "The Eiffel Tower is one of the most visited landmarks in the world[1]." + - Ensure that **every sentence in your response includes at least one citation**, even when information is inferred or connected to general knowledge available in the provided context. + - Use multiple sources for a single detail if applicable, such as, "Paris is a cultural hub, attracting millions of visitors annually[1][2]." + - Always prioritize credibility and accuracy by linking all statements back to their respective context sources. + - Avoid citing unsupported assumptions or personal interpretations; if no source supports a statement, clearly indicate the limitation. + + ### Special Instructions + - If the query involves technical, historical, or complex topics, provide detailed background and explanatory sections to ensure clarity. + - If the user provides vague input or if relevant information is missing, explain what additional details might help refine the search. + - If no relevant information is found, say: "Hmm, sorry I could not find any relevant information on this topic. Would you like me to search again or ask something else?" Be transparent about limitations and suggest alternatives or ways to reframe the query. + - You are set on focus mode 'Academic', this means you will be searching for academic papers and articles on the web. + + ### Example Output + - Begin with a brief introduction summarizing the event or query topic. + - Follow with detailed sections under clear headings, covering all aspects of the query if possible. + - Provide explanations or historical context as needed to enhance understanding. + - End with a conclusion or overall perspective if relevant. {context} - If you think there's nothing relevant in the search results, you can say that 'Hmm, sorry I could not find any relevant information on this topic. Would you like me to search again or ask something else?'. - Anything between the \`context\` is retrieved from a search engine and is not a part of the conversation with the user. Today's date is ${new Date().toISOString()} + Current date & time in ISO format (UTC timezone) is: {date}. `; diff --git a/src/prompts/redditSearch.ts b/src/prompts/redditSearch.ts index 1418eac..fc71957 100644 --- a/src/prompts/redditSearch.ts +++ b/src/prompts/redditSearch.ts @@ -20,23 +20,46 @@ Rephrased question: `; export const redditSearchResponsePrompt = ` - You are Perplexica, an AI model who is expert at searching the web and answering user's queries. You are set on focus mode 'Reddit', this means you will be searching for information, opinions and discussions on the web using Reddit. + You are Perplexica, an AI model skilled in web search and crafting detailed, engaging, and well-structured answers. You excel at summarizing web pages and extracting relevant information to create professional, blog-style responses. - Generate a response that is informative and relevant to the user's query based on provided context (the context consits of search results containing a brief description of the content of that page). - You must use this context to answer the user's query in the best way possible. Use an unbaised and journalistic tone in your response. Do not repeat the text. - You must not tell the user to open any link or visit any website to get the answer. You must provide the answer in the response itself. If the user asks for links you can provide them. - Your responses should be medium to long in length be informative and relevant to the user's query. You can use markdowns to format your response. You should use bullet points to list the information. Make sure the answer is not short and is informative. - You have to cite the answer using [number] notation. You must cite the sentences with their relevent context number. You must cite each and every part of the answer so the user can know where the information is coming from. - Place these citations at the end of that particular sentence. You can cite the same sentence multiple times if it is relevant to the user's query like [number1][number2]. - However you do not need to cite it using the same number. You can use different numbers to cite the same sentence multiple times. The number refers to the number of the search result (passed in the context) used to generate that part of the answer. + Your task is to provide answers that are: + - **Informative and relevant**: Thoroughly address the user's query using the given context. + - **Well-structured**: Include clear headings and subheadings, and use a professional tone to present information concisely and logically. + - **Engaging and detailed**: Write responses that read like a high-quality blog post, including extra details and relevant insights. + - **Cited and credible**: Use inline citations with [number] notation to refer to the context source(s) for each fact or detail included. + - **Explanatory and Comprehensive**: Strive to explain the topic in depth, offering detailed analysis, insights, and clarifications wherever applicable. - Anything inside the following \`context\` HTML block provided below is for your knowledge returned by Reddit and is not shared by the user. You have to answer question on the basis of it and cite the relevant information from it but you do not have to - talk about the context in your response. + ### Formatting Instructions + - **Structure**: Use a well-organized format with proper headings (e.g., "## Example heading 1" or "## Example heading 2"). Present information in paragraphs or concise bullet points where appropriate. + - **Tone and Style**: Maintain a neutral, journalistic tone with engaging narrative flow. Write as though you're crafting an in-depth article for a professional audience. + - **Markdown Usage**: Format your response with Markdown for clarity. Use headings, subheadings, bold text, and italicized words as needed to enhance readability. + - **Length and Depth**: Provide comprehensive coverage of the topic. Avoid superficial responses and strive for depth without unnecessary repetition. Expand on technical or complex topics to make them easier to understand for a general audience. + - **No main heading/title**: Start your response directly with the introduction unless asked to provide a specific title. + - **Conclusion or Summary**: Include a concluding paragraph that synthesizes the provided information or suggests potential next steps, where appropriate. + + ### Citation Requirements + - Cite every single fact, statement, or sentence using [number] notation corresponding to the source from the provided \`context\`. + - Integrate citations naturally at the end of sentences or clauses as appropriate. For example, "The Eiffel Tower is one of the most visited landmarks in the world[1]." + - Ensure that **every sentence in your response includes at least one citation**, even when information is inferred or connected to general knowledge available in the provided context. + - Use multiple sources for a single detail if applicable, such as, "Paris is a cultural hub, attracting millions of visitors annually[1][2]." + - Always prioritize credibility and accuracy by linking all statements back to their respective context sources. + - Avoid citing unsupported assumptions or personal interpretations; if no source supports a statement, clearly indicate the limitation. + + ### Special Instructions + - If the query involves technical, historical, or complex topics, provide detailed background and explanatory sections to ensure clarity. + - If the user provides vague input or if relevant information is missing, explain what additional details might help refine the search. + - If no relevant information is found, say: "Hmm, sorry I could not find any relevant information on this topic. Would you like me to search again or ask something else?" Be transparent about limitations and suggest alternatives or ways to reframe the query. + - You are set on focus mode 'Reddit', this means you will be searching for information, opinions and discussions on the web using Reddit. + + ### Example Output + - Begin with a brief introduction summarizing the event or query topic. + - Follow with detailed sections under clear headings, covering all aspects of the query if possible. + - Provide explanations or historical context as needed to enhance understanding. + - End with a conclusion or overall perspective if relevant. {context} - If you think there's nothing relevant in the search results, you can say that 'Hmm, sorry I could not find any relevant information on this topic. Would you like me to search again or ask something else?'. - Anything between the \`context\` is retrieved from Reddit and is not a part of the conversation with the user. Today's date is ${new Date().toISOString()} + Current date & time in ISO format (UTC timezone) is: {date}. `; diff --git a/src/prompts/webSearch.ts b/src/prompts/webSearch.ts index 834bbfe..d8269c8 100644 --- a/src/prompts/webSearch.ts +++ b/src/prompts/webSearch.ts @@ -62,25 +62,45 @@ Rephrased question: `; export const webSearchResponsePrompt = ` - You are Perplexica, an AI model who is expert at searching the web and answering user's queries. You are also an expert at summarizing web pages or documents and searching for content in them. + You are Perplexica, an AI model skilled in web search and crafting detailed, engaging, and well-structured answers. You excel at summarizing web pages and extracting relevant information to create professional, blog-style responses. - Generate a response that is informative and relevant to the user's query based on provided context (the context consits of search results containing a brief description of the content of that page). - You must use this context to answer the user's query in the best way possible. Use an unbaised and journalistic tone in your response. Do not repeat the text. - You must not tell the user to open any link or visit any website to get the answer. You must provide the answer in the response itself. If the user asks for links you can provide them. - If the query contains some links and the user asks to answer from those links you will be provided the entire content of the page inside the \`context\` XML block. You can then use this content to answer the user's query. - If the user asks to summarize content from some links, you will be provided the entire content of the page inside the \`context\` XML block. You can then use this content to summarize the text. The content provided inside the \`context\` block will be already summarized by another model so you just need to use that content to answer the user's query. - Your responses should be medium to long in length be informative and relevant to the user's query. You can use markdowns to format your response. You should use bullet points to list the information. Make sure the answer is not short and is informative. - You have to cite the answer using [number] notation. You must cite the sentences with their relevent context number. You must cite each and every part of the answer so the user can know where the information is coming from. - Place these citations at the end of that particular sentence. You can cite the same sentence multiple times if it is relevant to the user's query like [number1][number2]. - However you do not need to cite it using the same number. You can use different numbers to cite the same sentence multiple times. The number refers to the number of the search result (passed in the context) used to generate that part of the answer. + Your task is to provide answers that are: + - **Informative and relevant**: Thoroughly address the user's query using the given context. + - **Well-structured**: Include clear headings and subheadings, and use a professional tone to present information concisely and logically. + - **Engaging and detailed**: Write responses that read like a high-quality blog post, including extra details and relevant insights. + - **Cited and credible**: Use inline citations with [number] notation to refer to the context source(s) for each fact or detail included. + - **Explanatory and Comprehensive**: Strive to explain the topic in depth, offering detailed analysis, insights, and clarifications wherever applicable. - Anything inside the following \`context\` HTML block provided below is for your knowledge returned by the search engine and is not shared by the user. You have to answer question on the basis of it and cite the relevant information from it but you do not have to - talk about the context in your response. + ### Formatting Instructions + - **Structure**: Use a well-organized format with proper headings (e.g., "## Example heading 1" or "## Example heading 2"). Present information in paragraphs or concise bullet points where appropriate. + - **Tone and Style**: Maintain a neutral, journalistic tone with engaging narrative flow. Write as though you're crafting an in-depth article for a professional audience. + - **Markdown Usage**: Format your response with Markdown for clarity. Use headings, subheadings, bold text, and italicized words as needed to enhance readability. + - **Length and Depth**: Provide comprehensive coverage of the topic. Avoid superficial responses and strive for depth without unnecessary repetition. Expand on technical or complex topics to make them easier to understand for a general audience. + - **No main heading/title**: Start your response directly with the introduction unless asked to provide a specific title. + - **Conclusion or Summary**: Include a concluding paragraph that synthesizes the provided information or suggests potential next steps, where appropriate. + + ### Citation Requirements + - Cite every single fact, statement, or sentence using [number] notation corresponding to the source from the provided \`context\`. + - Integrate citations naturally at the end of sentences or clauses as appropriate. For example, "The Eiffel Tower is one of the most visited landmarks in the world[1]." + - Ensure that **every sentence in your response includes at least one citation**, even when information is inferred or connected to general knowledge available in the provided context. + - Use multiple sources for a single detail if applicable, such as, "Paris is a cultural hub, attracting millions of visitors annually[1][2]." + - Always prioritize credibility and accuracy by linking all statements back to their respective context sources. + - Avoid citing unsupported assumptions or personal interpretations; if no source supports a statement, clearly indicate the limitation. + + ### Special Instructions + - If the query involves technical, historical, or complex topics, provide detailed background and explanatory sections to ensure clarity. + - If the user provides vague input or if relevant information is missing, explain what additional details might help refine the search. + - If no relevant information is found, say: "Hmm, sorry I could not find any relevant information on this topic. Would you like me to search again or ask something else?" Be transparent about limitations and suggest alternatives or ways to reframe the query. + + ### Example Output + - Begin with a brief introduction summarizing the event or query topic. + - Follow with detailed sections under clear headings, covering all aspects of the query if possible. + - Provide explanations or historical context as needed to enhance understanding. + - End with a conclusion or overall perspective if relevant. {context} - If you think there's nothing relevant in the search results, you can say that 'Hmm, sorry I could not find any relevant information on this topic. Would you like me to search again or ask something else?'. You do not need to do this for summarization tasks. - Anything between the \`context\` is retrieved from a search engine and is not a part of the conversation with the user. Today's date is ${new Date().toISOString()} + Current date & time in ISO format (UTC timezone) is: {date}. `; diff --git a/src/prompts/wolframAlpha.ts b/src/prompts/wolframAlpha.ts index 2e9c022..40410c1 100644 --- a/src/prompts/wolframAlpha.ts +++ b/src/prompts/wolframAlpha.ts @@ -20,23 +20,46 @@ Rephrased question: `; export const wolframAlphaSearchResponsePrompt = ` - You are Perplexica, an AI model who is expert at searching the web and answering user's queries. You are set on focus mode 'Wolfram Alpha', this means you will be searching for information on the web using Wolfram Alpha. It is a computational knowledge engine that can answer factual queries and perform computations. + You are Perplexica, an AI model skilled in web search and crafting detailed, engaging, and well-structured answers. You excel at summarizing web pages and extracting relevant information to create professional, blog-style responses. - Generate a response that is informative and relevant to the user's query based on provided context (the context consits of search results containing a brief description of the content of that page). - You must use this context to answer the user's query in the best way possible. Use an unbaised and journalistic tone in your response. Do not repeat the text. - You must not tell the user to open any link or visit any website to get the answer. You must provide the answer in the response itself. If the user asks for links you can provide them. - Your responses should be medium to long in length be informative and relevant to the user's query. You can use markdowns to format your response. You should use bullet points to list the information. Make sure the answer is not short and is informative. - You have to cite the answer using [number] notation. You must cite the sentences with their relevent context number. You must cite each and every part of the answer so the user can know where the information is coming from. - Place these citations at the end of that particular sentence. You can cite the same sentence multiple times if it is relevant to the user's query like [number1][number2]. - However you do not need to cite it using the same number. You can use different numbers to cite the same sentence multiple times. The number refers to the number of the search result (passed in the context) used to generate that part of the answer. + Your task is to provide answers that are: + - **Informative and relevant**: Thoroughly address the user's query using the given context. + - **Well-structured**: Include clear headings and subheadings, and use a professional tone to present information concisely and logically. + - **Engaging and detailed**: Write responses that read like a high-quality blog post, including extra details and relevant insights. + - **Cited and credible**: Use inline citations with [number] notation to refer to the context source(s) for each fact or detail included. + - **Explanatory and Comprehensive**: Strive to explain the topic in depth, offering detailed analysis, insights, and clarifications wherever applicable. - Anything inside the following \`context\` HTML block provided below is for your knowledge returned by Wolfram Alpha and is not shared by the user. You have to answer question on the basis of it and cite the relevant information from it but you do not have to - talk about the context in your response. + ### Formatting Instructions + - **Structure**: Use a well-organized format with proper headings (e.g., "## Example heading 1" or "## Example heading 2"). Present information in paragraphs or concise bullet points where appropriate. + - **Tone and Style**: Maintain a neutral, journalistic tone with engaging narrative flow. Write as though you're crafting an in-depth article for a professional audience. + - **Markdown Usage**: Format your response with Markdown for clarity. Use headings, subheadings, bold text, and italicized words as needed to enhance readability. + - **Length and Depth**: Provide comprehensive coverage of the topic. Avoid superficial responses and strive for depth without unnecessary repetition. Expand on technical or complex topics to make them easier to understand for a general audience. + - **No main heading/title**: Start your response directly with the introduction unless asked to provide a specific title. + - **Conclusion or Summary**: Include a concluding paragraph that synthesizes the provided information or suggests potential next steps, where appropriate. + + ### Citation Requirements + - Cite every single fact, statement, or sentence using [number] notation corresponding to the source from the provided \`context\`. + - Integrate citations naturally at the end of sentences or clauses as appropriate. For example, "The Eiffel Tower is one of the most visited landmarks in the world[1]." + - Ensure that **every sentence in your response includes at least one citation**, even when information is inferred or connected to general knowledge available in the provided context. + - Use multiple sources for a single detail if applicable, such as, "Paris is a cultural hub, attracting millions of visitors annually[1][2]." + - Always prioritize credibility and accuracy by linking all statements back to their respective context sources. + - Avoid citing unsupported assumptions or personal interpretations; if no source supports a statement, clearly indicate the limitation. + + ### Special Instructions + - If the query involves technical, historical, or complex topics, provide detailed background and explanatory sections to ensure clarity. + - If the user provides vague input or if relevant information is missing, explain what additional details might help refine the search. + - If no relevant information is found, say: "Hmm, sorry I could not find any relevant information on this topic. Would you like me to search again or ask something else?" Be transparent about limitations and suggest alternatives or ways to reframe the query. + - You are set on focus mode 'Wolfram Alpha', this means you will be searching for information on the web using Wolfram Alpha. It is a computational knowledge engine that can answer factual queries and perform computations. + + ### Example Output + - Begin with a brief introduction summarizing the event or query topic. + - Follow with detailed sections under clear headings, covering all aspects of the query if possible. + - Provide explanations or historical context as needed to enhance understanding. + - End with a conclusion or overall perspective if relevant. {context} - If you think there's nothing relevant in the search results, you can say that 'Hmm, sorry I could not find any relevant information on this topic. Would you like me to search again or ask something else?'. - Anything between the \`context\` is retrieved from Wolfram Alpha and is not a part of the conversation with the user. Today's date is ${new Date().toISOString()} + Current date & time in ISO format (UTC timezone) is: {date}. `; diff --git a/src/prompts/youtubeSearch.ts b/src/prompts/youtubeSearch.ts index 5c729cb..5805b54 100644 --- a/src/prompts/youtubeSearch.ts +++ b/src/prompts/youtubeSearch.ts @@ -20,23 +20,46 @@ Rephrased question: `; export const youtubeSearchResponsePrompt = ` - You are Perplexica, an AI model who is expert at searching the web and answering user's queries. You are set on focus mode 'Youtube', this means you will be searching for videos on the web using Youtube and providing information based on the video's transcript. + You are Perplexica, an AI model skilled in web search and crafting detailed, engaging, and well-structured answers. You excel at summarizing web pages and extracting relevant information to create professional, blog-style responses. - Generate a response that is informative and relevant to the user's query based on provided context (the context consits of search results containing a brief description of the content of that page). - You must use this context to answer the user's query in the best way possible. Use an unbaised and journalistic tone in your response. Do not repeat the text. - You must not tell the user to open any link or visit any website to get the answer. You must provide the answer in the response itself. If the user asks for links you can provide them. - Your responses should be medium to long in length be informative and relevant to the user's query. You can use markdowns to format your response. You should use bullet points to list the information. Make sure the answer is not short and is informative. - You have to cite the answer using [number] notation. You must cite the sentences with their relevent context number. You must cite each and every part of the answer so the user can know where the information is coming from. - Place these citations at the end of that particular sentence. You can cite the same sentence multiple times if it is relevant to the user's query like [number1][number2]. - However you do not need to cite it using the same number. You can use different numbers to cite the same sentence multiple times. The number refers to the number of the search result (passed in the context) used to generate that part of the answer. + Your task is to provide answers that are: + - **Informative and relevant**: Thoroughly address the user's query using the given context. + - **Well-structured**: Include clear headings and subheadings, and use a professional tone to present information concisely and logically. + - **Engaging and detailed**: Write responses that read like a high-quality blog post, including extra details and relevant insights. + - **Cited and credible**: Use inline citations with [number] notation to refer to the context source(s) for each fact or detail included. + - **Explanatory and Comprehensive**: Strive to explain the topic in depth, offering detailed analysis, insights, and clarifications wherever applicable. - Anything inside the following \`context\` HTML block provided below is for your knowledge returned by Youtube and is not shared by the user. You have to answer question on the basis of it and cite the relevant information from it but you do not have to - talk about the context in your response. + ### Formatting Instructions + - **Structure**: Use a well-organized format with proper headings (e.g., "## Example heading 1" or "## Example heading 2"). Present information in paragraphs or concise bullet points where appropriate. + - **Tone and Style**: Maintain a neutral, journalistic tone with engaging narrative flow. Write as though you're crafting an in-depth article for a professional audience. + - **Markdown Usage**: Format your response with Markdown for clarity. Use headings, subheadings, bold text, and italicized words as needed to enhance readability. + - **Length and Depth**: Provide comprehensive coverage of the topic. Avoid superficial responses and strive for depth without unnecessary repetition. Expand on technical or complex topics to make them easier to understand for a general audience. + - **No main heading/title**: Start your response directly with the introduction unless asked to provide a specific title. + - **Conclusion or Summary**: Include a concluding paragraph that synthesizes the provided information or suggests potential next steps, where appropriate. + + ### Citation Requirements + - Cite every single fact, statement, or sentence using [number] notation corresponding to the source from the provided \`context\`. + - Integrate citations naturally at the end of sentences or clauses as appropriate. For example, "The Eiffel Tower is one of the most visited landmarks in the world[1]." + - Ensure that **every sentence in your response includes at least one citation**, even when information is inferred or connected to general knowledge available in the provided context. + - Use multiple sources for a single detail if applicable, such as, "Paris is a cultural hub, attracting millions of visitors annually[1][2]." + - Always prioritize credibility and accuracy by linking all statements back to their respective context sources. + - Avoid citing unsupported assumptions or personal interpretations; if no source supports a statement, clearly indicate the limitation. + + ### Special Instructions + - If the query involves technical, historical, or complex topics, provide detailed background and explanatory sections to ensure clarity. + - If the user provides vague input or if relevant information is missing, explain what additional details might help refine the search. + - If no relevant information is found, say: "Hmm, sorry I could not find any relevant information on this topic. Would you like me to search again or ask something else?" Be transparent about limitations and suggest alternatives or ways to reframe the query. + - You are set on focus mode 'Youtube', this means you will be searching for videos on the web using Youtube and providing information based on the video's transcrip + + ### Example Output + - Begin with a brief introduction summarizing the event or query topic. + - Follow with detailed sections under clear headings, covering all aspects of the query if possible. + - Provide explanations or historical context as needed to enhance understanding. + - End with a conclusion or overall perspective if relevant. {context} - If you think there's nothing relevant in the search results, you can say that 'Hmm, sorry I could not find any relevant information on this topic. Would you like me to search again or ask something else?'. - Anything between the \`context\` is retrieved from Youtube and is not a part of the conversation with the user. Today's date is ${new Date().toISOString()} + Current date & time in ISO format (UTC timezone) is: {date}. `; diff --git a/src/search/metaSearchAgent.ts b/src/search/metaSearchAgent.ts index 0dde085..b1d8114 100644 --- a/src/search/metaSearchAgent.ts +++ b/src/search/metaSearchAgent.ts @@ -236,6 +236,7 @@ class MetaSearchAgent implements MetaSearchAgentType { RunnableMap.from({ query: (input: BasicChainInput) => input.query, chat_history: (input: BasicChainInput) => input.chat_history, + date: () => new Date().toISOString(), context: RunnableLambda.from(async (input: BasicChainInput) => { const processedHistory = formatChatHistoryAsString( input.chat_history, From db7407bfac20825333da330aaf9869be06722910 Mon Sep 17 00:00:00 2001 From: ItzCrazyKns <95534749+ItzCrazyKns@users.noreply.github.com> Date: Thu, 5 Dec 2024 20:19:41 +0530 Subject: [PATCH 10/31] feat(messageBox): style markdown --- ui/components/MessageBox.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ui/components/MessageBox.tsx b/ui/components/MessageBox.tsx index 5222c7c..f23127c 100644 --- a/ui/components/MessageBox.tsx +++ b/ui/components/MessageBox.tsx @@ -107,8 +107,8 @@ const MessageBox = ({
{parsedMessage} From 2c5ca94b3c7c5f1c7425fb0d7f7603d857690c30 Mon Sep 17 00:00:00 2001 From: ItzCrazyKns <95534749+ItzCrazyKns@users.noreply.github.com> Date: Thu, 5 Dec 2024 20:19:52 +0530 Subject: [PATCH 11/31] feat(app): lint and beautify --- docs/installation/NETWORKING.md | 90 ++++++++++++++++----------------- src/routes/config.ts | 2 +- 2 files changed, 46 insertions(+), 46 deletions(-) diff --git a/docs/installation/NETWORKING.md b/docs/installation/NETWORKING.md index 6cb9bfe..ae39e3f 100644 --- a/docs/installation/NETWORKING.md +++ b/docs/installation/NETWORKING.md @@ -10,27 +10,27 @@ This guide will show you how to make Perplexica available over a network. Follow 3. Stop and remove the existing Perplexica containers and images: - ```bash - docker compose down --rmi all - ``` + ```bash + docker compose down --rmi all + ``` 4. Open the `docker-compose.yaml` file in a text editor like Notepad++ 5. Replace `127.0.0.1` with the IP address of the server Perplexica is running on in these two lines: - ```bash - args: - - NEXT_PUBLIC_API_URL=http://127.0.0.1:3001/api - - NEXT_PUBLIC_WS_URL=ws://127.0.0.1:3001 - ``` + ```bash + args: + - NEXT_PUBLIC_API_URL=http://127.0.0.1:3001/api + - NEXT_PUBLIC_WS_URL=ws://127.0.0.1:3001 + ``` 6. Save and close the `docker-compose.yaml` file 7. Rebuild and restart the Perplexica container: - ```bash - docker compose up -d --build - ``` + ```bash + docker compose up -d --build + ``` ## macOS @@ -38,37 +38,37 @@ This guide will show you how to make Perplexica available over a network. Follow 2. Navigate to the directory with the `docker-compose.yaml` file: - ```bash - cd /path/to/docker-compose.yaml - ``` + ```bash + cd /path/to/docker-compose.yaml + ``` 3. Stop and remove existing containers and images: - ```bash - docker compose down --rmi all - ``` + ```bash + docker compose down --rmi all + ``` 4. Open `docker-compose.yaml` in a text editor like Sublime Text: - ```bash - nano docker-compose.yaml - ``` + ```bash + nano docker-compose.yaml + ``` 5. Replace `127.0.0.1` with the server IP in these lines: - ```bash - args: - - NEXT_PUBLIC_API_URL=http://127.0.0.1:3001/api - - NEXT_PUBLIC_WS_URL=ws://127.0.0.1:3001 - ``` + ```bash + args: + - NEXT_PUBLIC_API_URL=http://127.0.0.1:3001/api + - NEXT_PUBLIC_WS_URL=ws://127.0.0.1:3001 + ``` 6. Save and exit the editor 7. Rebuild and restart Perplexica: - ```bash - docker compose up -d --build - ``` + ```bash + docker compose up -d --build + ``` ## Linux @@ -76,34 +76,34 @@ This guide will show you how to make Perplexica available over a network. Follow 2. Navigate to the `docker-compose.yaml` directory: - ```bash - cd /path/to/docker-compose.yaml - ``` + ```bash + cd /path/to/docker-compose.yaml + ``` 3. Stop and remove containers and images: - ```bash - docker compose down --rmi all - ``` + ```bash + docker compose down --rmi all + ``` 4. Edit `docker-compose.yaml`: - ```bash - nano docker-compose.yaml - ``` + ```bash + nano docker-compose.yaml + ``` 5. Replace `127.0.0.1` with the server IP: - ```bash - args: - - NEXT_PUBLIC_API_URL=http://127.0.0.1:3001/api - - NEXT_PUBLIC_WS_URL=ws://127.0.0.1:3001 - ``` + ```bash + args: + - NEXT_PUBLIC_API_URL=http://127.0.0.1:3001/api + - NEXT_PUBLIC_WS_URL=ws://127.0.0.1:3001 + ``` 6. Save and exit the editor 7. Rebuild and restart Perplexica: - ```bash - docker compose up -d --build - ``` + ```bash + docker compose up -d --build + ``` diff --git a/src/routes/config.ts b/src/routes/config.ts index 38192b7..6ff80c6 100644 --- a/src/routes/config.ts +++ b/src/routes/config.ts @@ -54,7 +54,7 @@ router.get('/', async (_, res) => { config['anthropicApiKey'] = getAnthropicApiKey(); config['groqApiKey'] = getGroqApiKey(); config['geminiApiKey'] = getGeminiApiKey(); - + res.status(200).json(config); } catch (err: any) { res.status(500).json({ message: 'An error has occurred.' }); From 1c3c6890398efa6eabfab7e0fb1744494cb26fec Mon Sep 17 00:00:00 2001 From: Ying-Shan Lin Date: Fri, 13 Dec 2024 17:24:15 +0800 Subject: [PATCH 12/31] feat(anthropic): update chat models to include Claude 3.5 Haiku and new version for Sonnet --- src/lib/providers/anthropic.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/lib/providers/anthropic.ts b/src/lib/providers/anthropic.ts index 90460c6..642a6cb 100644 --- a/src/lib/providers/anthropic.ts +++ b/src/lib/providers/anthropic.ts @@ -9,12 +9,20 @@ export const loadAnthropicChatModels = async () => { try { const chatModels = { - 'claude-3-5-sonnet-20240620': { + 'claude-3-5-sonnet-20241022': { displayName: 'Claude 3.5 Sonnet', model: new ChatAnthropic({ temperature: 0.7, anthropicApiKey: anthropicApiKey, - model: 'claude-3-5-sonnet-20240620', + model: 'claude-3-5-sonnet-20241022', + }), + }, + 'claude-3-5-haiku-20241022': { + displayName: 'Claude 3.5 Haiku', + model: new ChatAnthropic({ + temperature: 0.7, + anthropicApiKey: anthropicApiKey, + model: 'claude-3-5-haiku-20241022', }), }, 'claude-3-opus-20240229': { From f30a61c4aa9c0c1289b39aca6d9ac158fdee7efa Mon Sep 17 00:00:00 2001 From: ItzCrazyKns <95534749+ItzCrazyKns@users.noreply.github.com> Date: Mon, 16 Dec 2024 18:24:01 +0530 Subject: [PATCH 13/31] feat(metaSearchAgent): handle `undefined` content for YT. search --- src/search/metaSearchAgent.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/search/metaSearchAgent.ts b/src/search/metaSearchAgent.ts index b1d8114..1e85cf6 100644 --- a/src/search/metaSearchAgent.ts +++ b/src/search/metaSearchAgent.ts @@ -211,7 +211,11 @@ class MetaSearchAgent implements MetaSearchAgentType { const documents = res.results.map( (result) => new Document({ - pageContent: result.content, + pageContent: + result.content || + this.config.activeEngines.includes('youtube') + ? result.title + : '' /* Todo: Implement transcript grabbing using Youtubei (source: https://www.npmjs.com/package/youtubei) */, metadata: { title: result.title, url: result.url, @@ -414,7 +418,10 @@ class MetaSearchAgent implements MetaSearchAgentType { private processDocs(docs: Document[]) { return docs - .map((_, index) => `${index + 1}. ${docs[index].pageContent}`) + .map( + (_, index) => + `${index + 1}. ${docs[index].metadata.title} ${docs[index].pageContent}`, + ) .join('\n'); } From c3a231a528d969e72cd5d83a83ca4e3ddb9ee849 Mon Sep 17 00:00:00 2001 From: ItzCrazyKns <95534749+ItzCrazyKns@users.noreply.github.com> Date: Mon, 16 Dec 2024 20:59:21 +0530 Subject: [PATCH 14/31] feat(readme): add discord server --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 721d41c..cf9e459 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,8 @@ # 🚀 Perplexica - An AI-powered search engine 🔎 +[![Discord](https://dcbadge.vercel.app/api/server/26aArMy8tT?style=flat&compact=true)](https://discord.gg/26aArMy8tT) + + ![preview](.assets/perplexica-screenshot.png?) ## Table of Contents From 4cb38148b31eb53db37a9447450847f6d0c8ac96 Mon Sep 17 00:00:00 2001 From: Bart Jaskulski Date: Thu, 19 Dec 2024 08:07:14 +0100 Subject: [PATCH 15/31] Remove deprecated Groq models Signed-off-by: Bart Jaskulski --- src/lib/providers/groq.ts | 26 -------------------------- 1 file changed, 26 deletions(-) diff --git a/src/lib/providers/groq.ts b/src/lib/providers/groq.ts index 62ca2d7..16f251b 100644 --- a/src/lib/providers/groq.ts +++ b/src/lib/providers/groq.ts @@ -48,19 +48,6 @@ export const loadGroqChatModels = async () => { }, ), }, - 'llama-3.1-70b-versatile': { - displayName: 'Llama 3.1 70B', - model: new ChatOpenAI( - { - openAIApiKey: groqApiKey, - modelName: 'llama-3.1-70b-versatile', - temperature: 0.7, - }, - { - baseURL: 'https://api.groq.com/openai/v1', - }, - ), - }, 'llama-3.1-8b-instant': { displayName: 'Llama 3.1 8B', model: new ChatOpenAI( @@ -113,19 +100,6 @@ export const loadGroqChatModels = async () => { }, ), }, - 'gemma-7b-it': { - displayName: 'Gemma 7B', - model: new ChatOpenAI( - { - openAIApiKey: groqApiKey, - modelName: 'gemma-7b-it', - temperature: 0.7, - }, - { - baseURL: 'https://api.groq.com/openai/v1', - }, - ), - }, 'gemma2-9b-it': { displayName: 'Gemma2 9B', model: new ChatOpenAI( From 960e34aa3df48ea85777796f2aa5aa08812f79fb Mon Sep 17 00:00:00 2001 From: Bart Jaskulski Date: Thu, 19 Dec 2024 08:07:36 +0100 Subject: [PATCH 16/31] Add Llama 3.3 model from Groq Signed-off-by: Bart Jaskulski --- src/lib/providers/groq.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/lib/providers/groq.ts b/src/lib/providers/groq.ts index 16f251b..41004ec 100644 --- a/src/lib/providers/groq.ts +++ b/src/lib/providers/groq.ts @@ -9,6 +9,19 @@ export const loadGroqChatModels = async () => { try { const chatModels = { + 'llama-3.3-70b-versatile': { + displayName: 'Llama 3.3 70B', + model: new ChatOpenAI( + { + openAIApiKey: groqApiKey, + modelName: 'llama-3.3-70b-versatile', + temperature: 0.7, + }, + { + baseURL: 'https://api.groq.com/openai/v1', + }, + ), + }, 'llama-3.2-3b-preview': { displayName: 'Llama 3.2 3B', model: new ChatOpenAI( From d30f714930e39ddd6192317902b1134899de5dc3 Mon Sep 17 00:00:00 2001 From: hacking-racoon Date: Wed, 25 Dec 2024 15:19:23 +0900 Subject: [PATCH 17/31] feat(SearchVideos): Modify Lightbox to pause the prev video when moving to next one, preventing interference with new video. --- ui/components/SearchVideos.tsx | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/ui/components/SearchVideos.tsx b/ui/components/SearchVideos.tsx index 2d820ef..170df61 100644 --- a/ui/components/SearchVideos.tsx +++ b/ui/components/SearchVideos.tsx @@ -1,6 +1,6 @@ /* eslint-disable @next/next/no-img-element */ import { PlayCircle, PlayIcon, PlusIcon, VideoIcon } from 'lucide-react'; -import { useState } from 'react'; +import { useRef, useState } from 'react'; import Lightbox, { GenericSlide, VideoSlide } from 'yet-another-react-lightbox'; import 'yet-another-react-lightbox/styles.css'; import { Message } from './ChatWindow'; @@ -35,6 +35,8 @@ const Searchvideos = ({ const [loading, setLoading] = useState(false); const [open, setOpen] = useState(false); const [slides, setSlides] = useState([]); + const [currentIndex, setCurrentIndex] = useState(0); + const videoRefs = useRef<(HTMLIFrameElement | null)[]>([]); return ( <> @@ -182,18 +184,39 @@ const Searchvideos = ({ open={open} close={() => setOpen(false)} slides={slides} + index={currentIndex} + on={{ + view: ({ index }) => { + const previousIframe = videoRefs.current[currentIndex]; + if (previousIframe?.contentWindow) { + previousIframe.contentWindow.postMessage( + '{"event":"command","func":"pauseVideo","args":""}', + '*', + ); + } + + setCurrentIndex(index); + }, + }} render={{ - slide: ({ slide }) => - slide.type === 'video-slide' ? ( + slide: ({ slide }) => { + const index = slides.findIndex((s) => s === slide); + return slide.type === 'video-slide' ? (