From c6a5790d3381765050843b1d2bfcb715d78ceda0 Mon Sep 17 00:00:00 2001 From: ItzCrazyKns Date: Sat, 20 Apr 2024 09:32:19 +0530 Subject: [PATCH] feat(config): Use toml instead of env --- .env.example | 5 - .github/ISSUE_TEMPLATE/bug_report.md | 2 +- .github/ISSUE_TEMPLATE/custom.md | 3 - .github/ISSUE_TEMPLATE/feature_request.md | 1 - .gitignore | 3 + .prettierignore | 38 ++++ README.md | 8 +- backend.dockerfile | 5 +- package.json | 5 +- sample.config.toml | 9 + src/agents/academicSearchAgent.ts | 243 ++++++++++++---------- src/agents/imageSearchAgent.ts | 79 +++---- src/agents/redditSearchAgent.ts | 232 +++++++++++---------- src/agents/webSearchAgent.ts | 232 +++++++++++---------- src/agents/wolframAlphaSearchAgent.ts | 154 +++++++------- src/agents/writingAssistant.ts | 40 ++-- src/agents/youtubeSearchAgent.ts | 233 +++++++++++---------- src/app.ts | 7 +- src/config.ts | 32 +++ src/core/searxng.ts | 5 +- src/routes/images.ts | 12 +- src/utils/computeSimilarity.ts | 7 +- src/websocket/connectionManager.ts | 15 +- src/websocket/messageHandler.ts | 16 +- src/websocket/websocketServer.ts | 4 +- yarn.lock | 5 + 26 files changed, 799 insertions(+), 596 deletions(-) delete mode 100644 .env.example create mode 100644 .prettierignore create mode 100644 sample.config.toml create mode 100644 src/config.ts diff --git a/.env.example b/.env.example deleted file mode 100644 index bc67919..0000000 --- a/.env.example +++ /dev/null @@ -1,5 +0,0 @@ -PORT=3001 -OPENAI_API_KEY= -SIMILARITY_MEASURE=cosine # cosine or dot -SEARXNG_API_URL= # no need to fill this if using docker -MODEL_NAME=gpt-3.5-turbo \ No newline at end of file diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md index e065bb4..1de1177 100644 --- a/.github/ISSUE_TEMPLATE/bug_report.md +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -4,7 +4,6 @@ about: Create an issue to help us fix bugs title: '' labels: bug assignees: '' - --- **Describe the bug** @@ -12,6 +11,7 @@ A clear and concise description of what the bug is. **To Reproduce** Steps to reproduce the behavior: + 1. Go to '...' 2. Click on '....' 3. Scroll down to '....' diff --git a/.github/ISSUE_TEMPLATE/custom.md b/.github/ISSUE_TEMPLATE/custom.md index 48d5f81..96a4735 100644 --- a/.github/ISSUE_TEMPLATE/custom.md +++ b/.github/ISSUE_TEMPLATE/custom.md @@ -4,7 +4,4 @@ about: Describe this issue template's purpose here. title: '' labels: '' assignees: '' - --- - - diff --git a/.github/ISSUE_TEMPLATE/feature_request.md b/.github/ISSUE_TEMPLATE/feature_request.md index 11fc491..5f0a04c 100644 --- a/.github/ISSUE_TEMPLATE/feature_request.md +++ b/.github/ISSUE_TEMPLATE/feature_request.md @@ -4,7 +4,6 @@ about: Suggest an idea for this project title: '' labels: enhancement assignees: '' - --- **Is your feature request related to a problem? Please describe.** diff --git a/.gitignore b/.gitignore index 0f857e0..d64d5cc 100644 --- a/.gitignore +++ b/.gitignore @@ -19,6 +19,9 @@ yarn-error.log .env.test.local .env.production.local +# Config files +config.toml + # Log files logs/ *.log diff --git a/.prettierignore b/.prettierignore new file mode 100644 index 0000000..c184fdb --- /dev/null +++ b/.prettierignore @@ -0,0 +1,38 @@ +# Ignore all files in the node_modules directory +node_modules + +# Ignore all files in the .next directory (Next.js build output) +.next + +# Ignore all files in the .out directory (TypeScript build output) +.out + +# Ignore all files in the .cache directory (Prettier cache) +.cache + +# Ignore all files in the .vscode directory (Visual Studio Code settings) +.vscode + +# Ignore all files in the .idea directory (IntelliJ IDEA settings) +.idea + +# Ignore all files in the dist directory (build output) +dist + +# Ignore all files in the build directory (build output) +build + +# Ignore all files in the coverage directory (test coverage reports) +coverage + +# Ignore all files with the .log extension +*.log + +# Ignore all files with the .tmp extension +*.tmp + +# Ignore all files with the .swp extension +*.swp + +# Ignore all files with the .DS_Store extension (macOS specific) +.DS_Store \ No newline at end of file diff --git a/README.md b/README.md index 3f2f63d..942ba9e 100644 --- a/README.md +++ b/README.md @@ -56,10 +56,10 @@ There are mainly 2 ways of installing Perplexica - With Docker, Without Docker. 3. After cloning, navigate to the directory containing the project files. -4. Rename the `.env.example` file to `.env`. For Docker setups, you need only fill in the following fields: +4. Rename the `sample.config.toml` file to `config.toml`. For Docker setups, you need only fill in the following fields: - - `OPENAI_API_KEY` - - `SIMILARITY_MEASURE` (This is filled by default; you can leave it as is if you are unsure about it.) + - `OPENAI`: Your OpenAI API key. + - `SIMILARITY_MEASURE`: The similarity measure to use (This is filled by default; you can leave it as is if you are unsure about it.) 5. Ensure you are in the directory containing the `docker-compose.yaml` file and execute: @@ -75,7 +75,7 @@ There are mainly 2 ways of installing Perplexica - With Docker, Without Docker. For setups without Docker: -1. Follow the initial steps to clone the repository and rename the `.env.example` file to `.env` in the root directory. You will need to fill in all the fields in this file. +1. Follow the initial steps to clone the repository and rename the `sample.config.toml` file to `config.toml` in the root directory. You will need to fill in all the fields in this file. 2. Additionally, rename the `.env.example` file to `.env` in the `ui` folder and complete all fields. 3. The non-Docker setup requires manual configuration of both the backend and frontend. diff --git a/backend.dockerfile b/backend.dockerfile index 6cbd192..8bf34f0 100644 --- a/backend.dockerfile +++ b/backend.dockerfile @@ -1,16 +1,17 @@ FROM node:alpine ARG SEARXNG_API_URL -ENV SEARXNG_API_URL=${SEARXNG_API_URL} WORKDIR /home/perplexica COPY src /home/perplexica/src COPY tsconfig.json /home/perplexica/ -COPY .env /home/perplexica/ +COPY config.toml /home/perplexica/ COPY package.json /home/perplexica/ COPY yarn.lock /home/perplexica/ +RUN sed -i "s|SEARXNG = \".*\"|SEARXNG = \"${SEARXNG_API_URL}\"|g" /home/perplexica/config.toml + RUN yarn install RUN yarn build diff --git a/package.json b/package.json index c2f1aba..5006a93 100644 --- a/package.json +++ b/package.json @@ -4,9 +4,9 @@ "license": "MIT", "author": "ItzCrazyKns", "scripts": { - "start": "node --env-file=.env dist/app.js", + "start": "node dist/app.js", "build": "tsc", - "dev": "nodemon -r dotenv/config src/app.ts", + "dev": "nodemon src/app.ts", "format": "prettier . --check", "format:write": "prettier . --write" }, @@ -19,6 +19,7 @@ "typescript": "^5.4.3" }, "dependencies": { + "@iarna/toml": "^2.2.5", "@langchain/openai": "^0.0.25", "axios": "^1.6.8", "compute-cosine-similarity": "^1.1.0", diff --git a/sample.config.toml b/sample.config.toml new file mode 100644 index 0000000..1082184 --- /dev/null +++ b/sample.config.toml @@ -0,0 +1,9 @@ +[GENERAL] +PORT = 3001 # Port to run the server on +SIMILARITY_MEASURE = "cosine" # "cosine" or "dot" + +[API_KEYS] +OPENAI = "sk-1234567890abcdef1234567890abcdef" # OpenAI API key + +[API_ENDPOINTS] +SEARXNG = "http://localhost:32768" # SearxNG API ULR \ No newline at end of file diff --git a/src/agents/academicSearchAgent.ts b/src/agents/academicSearchAgent.ts index edb7a63..466088f 100644 --- a/src/agents/academicSearchAgent.ts +++ b/src/agents/academicSearchAgent.ts @@ -9,24 +9,16 @@ import { RunnableMap, RunnableLambda, } from '@langchain/core/runnables'; -import { ChatOpenAI, OpenAIEmbeddings } from '@langchain/openai'; import { StringOutputParser } from '@langchain/core/output_parsers'; import { Document } from '@langchain/core/documents'; import { searchSearxng } from '../core/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'; -const llm = new ChatOpenAI({ - modelName: process.env.MODEL_NAME, - temperature: 0.7, -}); - -const embeddings = new OpenAIEmbeddings({ - modelName: 'text-embedding-3-large', -}); - 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. @@ -49,7 +41,7 @@ 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 'Acadedemic', this means you will be searching for academic papers and articles on the web. + 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 containg 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. @@ -104,122 +96,140 @@ const handleStream = async ( } }; -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, - ); - - const docEmbeddings = await embeddings.embedDocuments( - docsWithContent.map((doc) => doc.pageContent), - ); - - const queryEmbedding = await 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; -}; - type BasicChainInput = { chat_history: BaseMessage[]; query: string; }; -const basicAcademicSearchRetrieverChain = RunnableSequence.from([ - PromptTemplate.fromTemplate(basicAcademicSearchRetrieverPrompt), - llm, - strParser, - RunnableLambda.from(async (input: string) => { - if (input === 'not_needed') { - return { query: '', docs: [] }; +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', + 'internet_archive_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, +) => { + 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 res = await searchSearxng(input, { - language: 'en', - engines: [ - 'arxiv', - 'google_scholar', - 'internet_archive_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 }), - }, - }), + const docsWithContent = docs.filter( + (doc) => doc.pageContent && doc.pageContent.length > 0, ); - return { query: input, docs: documents }; - }), -]); + const docEmbeddings = await embeddings.embedDocuments( + docsWithContent.map((doc) => doc.pageContent), + ); -const basicAcademicSearchAnsweringChain = 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), + const queryEmbedding = await 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}'], ]), - }), - ChatPromptTemplate.fromMessages([ - ['system', basicAcademicSearchResponsePrompt], - new MessagesPlaceholder('chat_history'), - ['user', '{query}'], - ]), - llm, - strParser, -]).withConfig({ - runName: 'FinalResponseGenerator', -}); + llm, + strParser, + ]).withConfig({ + runName: 'FinalResponseGenerator', + }); +}; -const basicAcademicSearch = (query: string, history: BaseMessage[]) => { +const basicAcademicSearch = ( + query: string, + history: BaseMessage[], + llm: BaseChatModel, + embeddings: Embeddings, +) => { const emitter = new eventEmitter(); try { + const basicAcademicSearchAnsweringChain = + createBasicAcademicSearchAnsweringChain(llm, embeddings); + const stream = basicAcademicSearchAnsweringChain.streamEvents( { chat_history: history, @@ -242,8 +252,13 @@ const basicAcademicSearch = (query: string, history: BaseMessage[]) => { return emitter; }; -const handleAcademicSearch = (message: string, history: BaseMessage[]) => { - const emitter = basicAcademicSearch(message, history); +const handleAcademicSearch = ( + message: string, + history: BaseMessage[], + llm: BaseChatModel, + embeddings: Embeddings, +) => { + const emitter = basicAcademicSearch(message, history, llm, embeddings); return emitter; }; diff --git a/src/agents/imageSearchAgent.ts b/src/agents/imageSearchAgent.ts index bf49de0..3adf631 100644 --- a/src/agents/imageSearchAgent.ts +++ b/src/agents/imageSearchAgent.ts @@ -4,16 +4,11 @@ import { RunnableLambda, } from '@langchain/core/runnables'; import { PromptTemplate } from '@langchain/core/prompts'; -import { ChatOpenAI } from '@langchain/openai'; import formatChatHistoryAsString from '../utils/formatHistory'; import { BaseMessage } from '@langchain/core/messages'; import { StringOutputParser } from '@langchain/core/output_parsers'; import { searchSearxng } from '../core/searxng'; - -const llm = new ChatOpenAI({ - modelName: process.env.MODEL_NAME, - temperature: 0.7, -}); +import type { BaseChatModel } from '@langchain/core/language_models/chat_models'; const imageSearchChainPrompt = ` You will be given a conversation below and a follow up question. You need to rephrase the follow-up question so it is a standalone question that can be used by the LLM to search the web for images. @@ -43,38 +38,48 @@ type ImageSearchChainInput = { const strParser = new StringOutputParser(); -const imageSearchChain = RunnableSequence.from([ - RunnableMap.from({ - chat_history: (input: ImageSearchChainInput) => { - return formatChatHistoryAsString(input.chat_history); - }, - query: (input: ImageSearchChainInput) => { - return input.query; - }, - }), - PromptTemplate.fromTemplate(imageSearchChainPrompt), - llm, - strParser, - RunnableLambda.from(async (input: string) => { - const res = await searchSearxng(input, { - categories: ['images'], - engines: ['bing_images', 'google_images'], - }); +const createImageSearchChain = (llm: BaseChatModel) => { + return RunnableSequence.from([ + RunnableMap.from({ + chat_history: (input: ImageSearchChainInput) => { + return formatChatHistoryAsString(input.chat_history); + }, + query: (input: ImageSearchChainInput) => { + return input.query; + }, + }), + PromptTemplate.fromTemplate(imageSearchChainPrompt), + llm, + strParser, + RunnableLambda.from(async (input: string) => { + const res = await searchSearxng(input, { + categories: ['images'], + engines: ['bing_images', 'google_images'], + }); - const images = []; + const images = []; - res.results.forEach((result) => { - if (result.img_src && result.url && result.title) { - images.push({ - img_src: result.img_src, - url: result.url, - title: result.title, - }); - } - }); + res.results.forEach((result) => { + if (result.img_src && result.url && result.title) { + images.push({ + img_src: result.img_src, + url: result.url, + title: result.title, + }); + } + }); - return images.slice(0, 10); - }), -]); + return images.slice(0, 10); + }), + ]); +}; -export default imageSearchChain; +const handleImageSearch = ( + input: ImageSearchChainInput, + llm: BaseChatModel, +) => { + const imageSearchChain = createImageSearchChain(llm); + return imageSearchChain.invoke(input); +}; + +export default handleImageSearch; diff --git a/src/agents/redditSearchAgent.ts b/src/agents/redditSearchAgent.ts index 3b6a274..dca3332 100644 --- a/src/agents/redditSearchAgent.ts +++ b/src/agents/redditSearchAgent.ts @@ -9,24 +9,16 @@ import { RunnableMap, RunnableLambda, } from '@langchain/core/runnables'; -import { ChatOpenAI, OpenAIEmbeddings } from '@langchain/openai'; import { StringOutputParser } from '@langchain/core/output_parsers'; import { Document } from '@langchain/core/documents'; import { searchSearxng } from '../core/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'; -const llm = new ChatOpenAI({ - modelName: process.env.MODEL_NAME, - temperature: 0.7, -}); - -const embeddings = new OpenAIEmbeddings({ - modelName: 'text-embedding-3-large', -}); - 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. @@ -104,118 +96,135 @@ const handleStream = async ( } }; -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, - ); - - const docEmbeddings = await embeddings.embedDocuments( - docsWithContent.map((doc) => doc.pageContent), - ); - - const queryEmbedding = await 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) - .filter((sim) => sim.similarity > 0.3) - .map((sim) => docsWithContent[sim.index]); - - return sortedDocs; -}; - type BasicChainInput = { chat_history: BaseMessage[]; query: string; }; -const basicRedditSearchRetrieverChain = RunnableSequence.from([ - PromptTemplate.fromTemplate(basicRedditSearchRetrieverPrompt), - llm, - strParser, - RunnableLambda.from(async (input: string) => { - if (input === 'not_needed') { - return { query: '', docs: [] }; +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, +) => { + 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 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 }), - }, - }), + const docsWithContent = docs.filter( + (doc) => doc.pageContent && doc.pageContent.length > 0, ); - return { query: input, docs: documents }; - }), -]); + const docEmbeddings = await embeddings.embedDocuments( + docsWithContent.map((doc) => doc.pageContent), + ); -const basicRedditSearchAnsweringChain = 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), + const queryEmbedding = await 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) + .filter((sim) => sim.similarity > 0.3) + .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}'], ]), - }), - ChatPromptTemplate.fromMessages([ - ['system', basicRedditSearchResponsePrompt], - new MessagesPlaceholder('chat_history'), - ['user', '{query}'], - ]), - llm, - strParser, -]).withConfig({ - runName: 'FinalResponseGenerator', -}); + llm, + strParser, + ]).withConfig({ + runName: 'FinalResponseGenerator', + }); +}; -const basicRedditSearch = (query: string, history: BaseMessage[]) => { +const basicRedditSearch = ( + query: string, + history: BaseMessage[], + llm: BaseChatModel, + embeddings: Embeddings, +) => { const emitter = new eventEmitter(); try { + const basicRedditSearchAnsweringChain = + createBasicRedditSearchAnsweringChain(llm, embeddings); const stream = basicRedditSearchAnsweringChain.streamEvents( { chat_history: history, @@ -238,8 +247,13 @@ const basicRedditSearch = (query: string, history: BaseMessage[]) => { return emitter; }; -const handleRedditSearch = (message: string, history: BaseMessage[]) => { - const emitter = basicRedditSearch(message, history); +const handleRedditSearch = ( + message: string, + history: BaseMessage[], + llm: BaseChatModel, + embeddings: Embeddings, +) => { + const emitter = basicRedditSearch(message, history, llm, embeddings); return emitter; }; diff --git a/src/agents/webSearchAgent.ts b/src/agents/webSearchAgent.ts index 047eb3d..66db2a1 100644 --- a/src/agents/webSearchAgent.ts +++ b/src/agents/webSearchAgent.ts @@ -9,24 +9,16 @@ import { RunnableMap, RunnableLambda, } from '@langchain/core/runnables'; -import { ChatOpenAI, OpenAIEmbeddings } from '@langchain/openai'; import { StringOutputParser } from '@langchain/core/output_parsers'; import { Document } from '@langchain/core/documents'; import { searchSearxng } from '../core/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'; -const llm = new ChatOpenAI({ - modelName: process.env.MODEL_NAME, - temperature: 0.7, -}); - -const embeddings = new OpenAIEmbeddings({ - modelName: 'text-embedding-3-large', -}); - const basicSearchRetrieverPrompt = ` 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. @@ -104,117 +96,136 @@ const handleStream = async ( } }; -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, - ); - - const docEmbeddings = await embeddings.embedDocuments( - docsWithContent.map((doc) => doc.pageContent), - ); - - const queryEmbedding = await 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) - .filter((sim) => sim.similarity > 0.5) - .slice(0, 15) - .map((sim) => docsWithContent[sim.index]); - - return sortedDocs; -}; - type BasicChainInput = { chat_history: BaseMessage[]; query: string; }; -const basicWebSearchRetrieverChain = RunnableSequence.from([ - PromptTemplate.fromTemplate(basicSearchRetrieverPrompt), - llm, - strParser, - RunnableLambda.from(async (input: string) => { - if (input === 'not_needed') { - return { query: '', docs: [] }; +const createBasicWebSearchRetrieverChain = (llm: BaseChatModel) => { + return RunnableSequence.from([ + PromptTemplate.fromTemplate(basicSearchRetrieverPrompt), + llm, + strParser, + RunnableLambda.from(async (input: string) => { + if (input === 'not_needed') { + return { query: '', docs: [] }; + } + + const res = await searchSearxng(input, { + 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: input, docs: documents }; + }), + ]); +}; + +const createBasicWebSearchAnsweringChain = ( + llm: BaseChatModel, + embeddings: Embeddings, +) => { + 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 res = await searchSearxng(input, { - 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 }), - }, - }), + const docsWithContent = docs.filter( + (doc) => doc.pageContent && doc.pageContent.length > 0, ); - return { query: input, docs: documents }; - }), -]); + const docEmbeddings = await embeddings.embedDocuments( + docsWithContent.map((doc) => doc.pageContent), + ); -const basicWebSearchAnsweringChain = 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), + const queryEmbedding = await 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) + .filter((sim) => sim.similarity > 0.5) + .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}'], ]), - }), - ChatPromptTemplate.fromMessages([ - ['system', basicWebSearchResponsePrompt], - new MessagesPlaceholder('chat_history'), - ['user', '{query}'], - ]), - llm, - strParser, -]).withConfig({ - runName: 'FinalResponseGenerator', -}); + llm, + strParser, + ]).withConfig({ + runName: 'FinalResponseGenerator', + }); +}; -const basicWebSearch = (query: string, history: BaseMessage[]) => { +const basicWebSearch = ( + query: string, + history: BaseMessage[], + llm: BaseChatModel, + embeddings: Embeddings, +) => { const emitter = new eventEmitter(); try { + const basicWebSearchAnsweringChain = createBasicWebSearchAnsweringChain( + llm, + embeddings, + ); + const stream = basicWebSearchAnsweringChain.streamEvents( { chat_history: history, @@ -237,8 +248,13 @@ const basicWebSearch = (query: string, history: BaseMessage[]) => { return emitter; }; -const handleWebSearch = (message: string, history: BaseMessage[]) => { - const emitter = basicWebSearch(message, history); +const handleWebSearch = ( + message: string, + history: BaseMessage[], + llm: BaseChatModel, + embeddings: Embeddings, +) => { + const emitter = basicWebSearch(message, history, llm, embeddings); return emitter; }; diff --git a/src/agents/wolframAlphaSearchAgent.ts b/src/agents/wolframAlphaSearchAgent.ts index 4ab1990..a68110d 100644 --- a/src/agents/wolframAlphaSearchAgent.ts +++ b/src/agents/wolframAlphaSearchAgent.ts @@ -9,19 +9,15 @@ import { RunnableMap, RunnableLambda, } from '@langchain/core/runnables'; -import { ChatOpenAI, OpenAI } from '@langchain/openai'; import { StringOutputParser } from '@langchain/core/output_parsers'; import { Document } from '@langchain/core/documents'; import { searchSearxng } from '../core/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'; -const llm = new ChatOpenAI({ - modelName: process.env.MODEL_NAME, - temperature: 0.7, -}); - 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. @@ -99,81 +95,94 @@ const handleStream = async ( } }; -const processDocs = async (docs: Document[]) => { - return docs - .map((_, index) => `${index + 1}. ${docs[index].pageContent}`) - .join('\n'); -}; - type BasicChainInput = { chat_history: BaseMessage[]; query: string; }; -const basicWolframAlphaSearchRetrieverChain = RunnableSequence.from([ - PromptTemplate.fromTemplate(basicWolframAlphaSearchRetrieverPrompt), - llm, - strParser, - RunnableLambda.from(async (input: string) => { - if (input === 'not_needed') { - return { query: '', docs: [] }; - } +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 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 }), - }, + 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), }), - ); - - return { query: input, docs: documents }; - }), -]); - -const basicWolframAlphaSearchAnsweringChain = 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), + basicWolframAlphaSearchRetrieverChain + .pipe(({ query, docs }) => { + return docs; + }) + .withConfig({ + runName: 'FinalSourceRetriever', + }) + .pipe(processDocs), + ]), + }), + ChatPromptTemplate.fromMessages([ + ['system', basicWolframAlphaSearchResponsePrompt], + new MessagesPlaceholder('chat_history'), + ['user', '{query}'], ]), - }), - ChatPromptTemplate.fromMessages([ - ['system', basicWolframAlphaSearchResponsePrompt], - new MessagesPlaceholder('chat_history'), - ['user', '{query}'], - ]), - llm, - strParser, -]).withConfig({ - runName: 'FinalResponseGenerator', -}); + llm, + strParser, + ]).withConfig({ + runName: 'FinalResponseGenerator', + }); +}; -const basicWolframAlphaSearch = (query: string, history: BaseMessage[]) => { +const basicWolframAlphaSearch = ( + query: string, + history: BaseMessage[], + llm: BaseChatModel, +) => { const emitter = new eventEmitter(); try { + const basicWolframAlphaSearchAnsweringChain = + createBasicWolframAlphaSearchAnsweringChain(llm); const stream = basicWolframAlphaSearchAnsweringChain.streamEvents( { chat_history: history, @@ -196,8 +205,13 @@ const basicWolframAlphaSearch = (query: string, history: BaseMessage[]) => { return emitter; }; -const handleWolframAlphaSearch = (message: string, history: BaseMessage[]) => { - const emitter = basicWolframAlphaSearch(message, history); +const handleWolframAlphaSearch = ( + message: string, + history: BaseMessage[], + llm: BaseChatModel, + embeddings: Embeddings, +) => { + const emitter = basicWolframAlphaSearch(message, history, llm); return emitter; }; diff --git a/src/agents/writingAssistant.ts b/src/agents/writingAssistant.ts index 0fc5097..ff5365e 100644 --- a/src/agents/writingAssistant.ts +++ b/src/agents/writingAssistant.ts @@ -4,15 +4,11 @@ import { MessagesPlaceholder, } from '@langchain/core/prompts'; import { RunnableSequence } from '@langchain/core/runnables'; -import { ChatOpenAI } from '@langchain/openai'; import { StringOutputParser } from '@langchain/core/output_parsers'; import type { StreamEvent } from '@langchain/core/tracers/log_stream'; import eventEmitter from 'events'; - -const llm = new ChatOpenAI({ - modelName: process.env.MODEL_NAME, - temperature: 0.7, -}); +import type { BaseChatModel } from '@langchain/core/language_models/chat_models'; +import type { Embeddings } from '@langchain/core/embeddings'; 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. @@ -44,22 +40,30 @@ const handleStream = async ( } }; -const writingAssistantChain = RunnableSequence.from([ - ChatPromptTemplate.fromMessages([ - ['system', writingAssistantPrompt], - new MessagesPlaceholder('chat_history'), - ['user', '{query}'], - ]), - llm, - strParser, -]).withConfig({ - runName: 'FinalResponseGenerator', -}); +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[]) => { +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, diff --git a/src/agents/youtubeSearchAgent.ts b/src/agents/youtubeSearchAgent.ts index 7c1bcf5..4ed8b41 100644 --- a/src/agents/youtubeSearchAgent.ts +++ b/src/agents/youtubeSearchAgent.ts @@ -9,24 +9,16 @@ import { RunnableMap, RunnableLambda, } from '@langchain/core/runnables'; -import { ChatOpenAI, OpenAIEmbeddings } from '@langchain/openai'; import { StringOutputParser } from '@langchain/core/output_parsers'; import { Document } from '@langchain/core/documents'; import { searchSearxng } from '../core/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'; -const llm = new ChatOpenAI({ - modelName: process.env.MODEL_NAME, - temperature: 0.7, -}); - -const embeddings = new OpenAIEmbeddings({ - modelName: 'text-embedding-3-large', -}); - 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. @@ -104,118 +96,136 @@ const handleStream = async ( } }; -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, - ); - - const docEmbeddings = await embeddings.embedDocuments( - docsWithContent.map((doc) => doc.pageContent), - ); - - const queryEmbedding = await 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) - .filter((sim) => sim.similarity > 0.3) - .map((sim) => docsWithContent[sim.index]); - - return sortedDocs; -}; - type BasicChainInput = { chat_history: BaseMessage[]; query: string; }; -const basicYoutubeSearchRetrieverChain = RunnableSequence.from([ - PromptTemplate.fromTemplate(basicYoutubeSearchRetrieverPrompt), - llm, - strParser, - RunnableLambda.from(async (input: string) => { - if (input === 'not_needed') { - return { query: '', docs: [] }; +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, +) => { + 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 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 }), - }, - }), + const docsWithContent = docs.filter( + (doc) => doc.pageContent && doc.pageContent.length > 0, ); - return { query: input, docs: documents }; - }), -]); + const docEmbeddings = await embeddings.embedDocuments( + docsWithContent.map((doc) => doc.pageContent), + ); -const basicYoutubeSearchAnsweringChain = 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), + const queryEmbedding = await 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) + .filter((sim) => sim.similarity > 0.3) + .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}'], ]), - }), - ChatPromptTemplate.fromMessages([ - ['system', basicYoutubeSearchResponsePrompt], - new MessagesPlaceholder('chat_history'), - ['user', '{query}'], - ]), - llm, - strParser, -]).withConfig({ - runName: 'FinalResponseGenerator', -}); + llm, + strParser, + ]).withConfig({ + runName: 'FinalResponseGenerator', + }); +}; -const basicYoutubeSearch = (query: string, history: BaseMessage[]) => { +const basicYoutubeSearch = ( + query: string, + history: BaseMessage[], + llm: BaseChatModel, + embeddings: Embeddings, +) => { const emitter = new eventEmitter(); try { + const basicYoutubeSearchAnsweringChain = + createBasicYoutubeSearchAnsweringChain(llm, embeddings); + const stream = basicYoutubeSearchAnsweringChain.streamEvents( { chat_history: history, @@ -238,8 +248,13 @@ const basicYoutubeSearch = (query: string, history: BaseMessage[]) => { return emitter; }; -const handleYoutubeSearch = (message: string, history: BaseMessage[]) => { - const emitter = basicYoutubeSearch(message, history); +const handleYoutubeSearch = ( + message: string, + history: BaseMessage[], + llm: BaseChatModel, + embeddings: Embeddings, +) => { + const emitter = basicYoutubeSearch(message, history, llm, embeddings); return emitter; }; diff --git a/src/app.ts b/src/app.ts index 993cb23..19f95bc 100644 --- a/src/app.ts +++ b/src/app.ts @@ -3,6 +3,9 @@ import express from 'express'; import cors from 'cors'; import http from 'http'; import routes from './routes'; +import { getPort } from './config'; + +const port = getPort(); const app = express(); const server = http.createServer(app); @@ -19,8 +22,8 @@ app.get('/api', (_, res) => { res.status(200).json({ status: 'ok' }); }); -server.listen(process.env.PORT!, () => { - console.log(`API server started on port ${process.env.PORT}`); +server.listen(port, () => { + console.log(`API server started on port ${port}`); }); startWebSocketServer(server); diff --git a/src/config.ts b/src/config.ts new file mode 100644 index 0000000..c83522f --- /dev/null +++ b/src/config.ts @@ -0,0 +1,32 @@ +import fs from 'fs'; +import path from 'path'; +import toml, { JsonMap } from '@iarna/toml'; + +const configFileName = 'config.toml'; + +interface Config { + GENERAL: { + PORT: number; + SIMILARITY_MEASURE: string; + }; + API_KEYS: { + OPENAI: string; + }; + API_ENDPOINTS: { + SEARXNG: string; + }; +} + +const loadConfig = () => + toml.parse( + fs.readFileSync(path.join(process.cwd(), `${configFileName}`), 'utf-8'), + ) as any as Config; + +export const getPort = () => loadConfig().GENERAL.PORT; + +export const getSimilarityMeasure = () => + loadConfig().GENERAL.SIMILARITY_MEASURE; + +export const getOpenaiApiKey = () => loadConfig().API_KEYS.OPENAI; + +export const getSearxngApiEndpoint = () => loadConfig().API_ENDPOINTS.SEARXNG; diff --git a/src/core/searxng.ts b/src/core/searxng.ts index 3bb4a53..297e50f 100644 --- a/src/core/searxng.ts +++ b/src/core/searxng.ts @@ -1,4 +1,5 @@ import axios from 'axios'; +import { getSearxngApiEndpoint } from '../config'; interface SearxngSearchOptions { categories?: string[]; @@ -20,7 +21,9 @@ export const searchSearxng = async ( query: string, opts?: SearxngSearchOptions, ) => { - const url = new URL(`${process.env.SEARXNG_API_URL}/search?format=json`); + const searxngURL = getSearxngApiEndpoint(); + + const url = new URL(`${searxngURL}/search?format=json`); url.searchParams.append('q', query); if (opts) { diff --git a/src/routes/images.ts b/src/routes/images.ts index 5a33ac6..dd3925f 100644 --- a/src/routes/images.ts +++ b/src/routes/images.ts @@ -1,5 +1,7 @@ import express from 'express'; -import imageSearchChain from '../agents/imageSearchAgent'; +import handleImageSearch from '../agents/imageSearchAgent'; +import { ChatOpenAI } from '@langchain/openai'; +import { getOpenaiApiKey } from '../config'; const router = express.Router(); @@ -7,11 +9,13 @@ router.post('/', async (req, res) => { try { const { query, chat_history } = req.body; - const images = await imageSearchChain.invoke({ - query, - chat_history, + const llm = new ChatOpenAI({ + temperature: 0.7, + openAIApiKey: getOpenaiApiKey(), }); + const images = await handleImageSearch({ query, chat_history }, llm); + res.status(200).json({ images }); } catch (err) { res.status(500).json({ message: 'An error has occurred.' }); diff --git a/src/utils/computeSimilarity.ts b/src/utils/computeSimilarity.ts index 1b07cc7..6e36b75 100644 --- a/src/utils/computeSimilarity.ts +++ b/src/utils/computeSimilarity.ts @@ -1,10 +1,13 @@ import dot from 'compute-dot'; import cosineSimilarity from 'compute-cosine-similarity'; +import { getSimilarityMeasure } from '../config'; const computeSimilarity = (x: number[], y: number[]): number => { - if (process.env.SIMILARITY_MEASURE === 'cosine') { + const similarityMeasure = getSimilarityMeasure(); + + if (similarityMeasure === 'cosine') { return cosineSimilarity(x, y); - } else if (process.env.SIMILARITY_MEASURE === 'dot') { + } else if (similarityMeasure === 'dot') { return dot(x, y); } diff --git a/src/websocket/connectionManager.ts b/src/websocket/connectionManager.ts index a5746e4..2dc8d59 100644 --- a/src/websocket/connectionManager.ts +++ b/src/websocket/connectionManager.ts @@ -1,10 +1,23 @@ import { WebSocket } from 'ws'; import { handleMessage } from './messageHandler'; +import { ChatOpenAI, OpenAIEmbeddings } from '@langchain/openai'; +import { getOpenaiApiKey } from '../config'; export const handleConnection = (ws: WebSocket) => { + const llm = new ChatOpenAI({ + temperature: 0.7, + openAIApiKey: getOpenaiApiKey(), + }); + + const embeddings = new OpenAIEmbeddings({ + openAIApiKey: getOpenaiApiKey(), + modelName: 'text-embedding-3-large', + }); + ws.on( 'message', - async (message) => await handleMessage(message.toString(), ws), + async (message) => + await handleMessage(message.toString(), ws, llm, embeddings), ); ws.on('close', () => console.log('Connection closed')); diff --git a/src/websocket/messageHandler.ts b/src/websocket/messageHandler.ts index 83fa50d..48774bf 100644 --- a/src/websocket/messageHandler.ts +++ b/src/websocket/messageHandler.ts @@ -6,6 +6,8 @@ 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'; type Message = { type: string; @@ -58,7 +60,12 @@ const handleEmitterEvents = ( }); }; -export const handleMessage = async (message: string, ws: WebSocket) => { +export const handleMessage = async ( + message: string, + ws: WebSocket, + llm: BaseChatModel, + embeddings: Embeddings, +) => { try { const parsedMessage = JSON.parse(message) as Message; const id = Math.random().toString(36).substring(7); @@ -83,7 +90,12 @@ export const handleMessage = async (message: string, ws: WebSocket) => { if (parsedMessage.type === 'message') { const handler = searchHandlers[parsedMessage.focusMode]; if (handler) { - const emitter = handler(parsedMessage.content, history); + const emitter = handler( + parsedMessage.content, + history, + llm, + embeddings, + ); handleEmitterEvents(emitter, ws, id); } else { ws.send(JSON.stringify({ type: 'error', data: 'Invalid focus mode' })); diff --git a/src/websocket/websocketServer.ts b/src/websocket/websocketServer.ts index 8aca021..451f9f2 100644 --- a/src/websocket/websocketServer.ts +++ b/src/websocket/websocketServer.ts @@ -1,15 +1,17 @@ import { WebSocketServer } from 'ws'; import { handleConnection } from './connectionManager'; import http from 'http'; +import { getPort } from '../config'; export const initServer = ( server: http.Server, ) => { + const port = getPort(); const wss = new WebSocketServer({ server }); wss.on('connection', (ws) => { handleConnection(ws); }); - console.log(`WebSocket server started on port ${process.env.PORT}`); + console.log(`WebSocket server started on port ${port}`); }; diff --git a/yarn.lock b/yarn.lock index 8518bb2..080d4c0 100644 --- a/yarn.lock +++ b/yarn.lock @@ -24,6 +24,11 @@ dependencies: "@jridgewell/trace-mapping" "0.3.9" +"@iarna/toml@^2.2.5": + version "2.2.5" + resolved "https://registry.yarnpkg.com/@iarna/toml/-/toml-2.2.5.tgz#b32366c89b43c6f8cefbdefac778b9c828e3ba8c" + integrity sha512-trnsAYxU3xnS1gPHPyU961coFyLkh4gAD/0zQ5mymY4yOZ+CYvsPqUbOFSw0aDM4y0tV7tiFxL/1XfXPNC6IPg== + "@jridgewell/resolve-uri@^3.0.3": version "3.1.2" resolved "https://registry.yarnpkg.com/@jridgewell/resolve-uri/-/resolve-uri-3.1.2.tgz#7a0ee601f60f99a20c7c7c5ff0c80388c1189bd6"