diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 0000000..faa9fa9 --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1 @@ +patreon: itzcrazykns diff --git a/README.md b/README.md index 64d2540..d1388b0 100644 --- a/README.md +++ b/README.md @@ -85,11 +85,12 @@ There are mainly 2 ways of installing Perplexica - With Docker, Without Docker. ### Non-Docker Installation -1. Clone the repository and rename the `sample.config.toml` file to `config.toml` in the root directory. Ensure you complete all required fields in this file. -2. Rename the `.env.example` file to `.env` in the `ui` folder and fill in all necessary fields. -3. After populating the configuration and environment files, run `npm i` in both the `ui` folder and the root directory. -4. Install the dependencies and then execute `npm run build` in both the `ui` folder and the root directory. -5. Finally, start both the frontend and the backend by running `npm run start` in both the `ui` folder and the root directory. +1. Install SearXNG and allow `JSON` format in the SearXNG settings. +2. Clone the repository and rename the `sample.config.toml` file to `config.toml` in the root directory. Ensure you complete all required fields in this file. +3. Rename the `.env.example` file to `.env` in the `ui` folder and fill in all necessary fields. +4. After populating the configuration and environment files, run `npm i` in both the `ui` folder and the root directory. +5. Install the dependencies and then execute `npm run build` in both the `ui` folder and the root directory. +6. Finally, start both the frontend and the backend by running `npm run start` in both the `ui` folder and the root directory. **Note**: Using Docker is recommended as it simplifies the setup process, especially for managing environment variables and dependencies. @@ -110,11 +111,7 @@ If you're encountering an Ollama connection error, it is likely due to the backe 3. **Linux Users - Expose Ollama to Network:** - - Serve Ollama over your network with the command: - - ```bash - OLLAMA_HOST=0.0.0.0 ollama serve - ``` + - Inside `/etc/systemd/system/ollama.service`, you need to add `Environment="OLLAMA_HOST=0.0.0.0"`. Then restart Ollama by `systemctl restart ollama`. For more information see [Ollama docs](https://github.com/ollama/ollama/blob/main/docs/faq.md#setting-environment-variables-on-linux) - Ensure that the port (default is 11434) is not blocked by your firewall. @@ -146,11 +143,11 @@ If you find Perplexica useful, consider giving us a star on GitHub. This helps m ### Donations -We also accept donations to help sustain our project. If you would like to contribute, you can use the following button to make a donation in cryptocurrency. Thank you for your support! +We also accept donations to help sustain our project. If you would like to contribute, you can use the following options to donate. Thank you for your support! - - Crypto donation button by NOWPayments - +| Cards | Ethereum | +| ----------------------------------- | ----------------------------------------------------- | +| https://www.patreon.com/itzcrazykns | Address: `0xB025a84b2F269570Eb8D4b05DEdaA41D8525B6DD` | ## Contribution diff --git a/backend.dockerfile b/backend.dockerfile index 5e482cd..4886573 100644 --- a/backend.dockerfile +++ b/backend.dockerfile @@ -1,4 +1,4 @@ -FROM nikolaik/python-nodejs:python3.12-nodejs20-bullseye +FROM node:slim ARG SEARXNG_API_URL @@ -15,7 +15,7 @@ RUN sed -i "s|SEARXNG = \".*\"|SEARXNG = \"${SEARXNG_API_URL}\"|g" /home/perplex RUN mkdir /home/perplexica/data -RUN yarn install +RUN yarn install RUN yarn build CMD ["yarn", "start"] \ No newline at end of file diff --git a/package.json b/package.json index 8f23d8a..bc2f6ff 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "perplexica-backend", - "version": "1.7.0", + "version": "1.7.1", "license": "MIT", "author": "ItzCrazyKns", "scripts": { @@ -24,6 +24,7 @@ }, "dependencies": { "@iarna/toml": "^2.2.5", + "@langchain/community": "^0.2.16", "@langchain/openai": "^0.0.25", "@xenova/transformers": "^2.17.1", "axios": "^1.6.8", diff --git a/src/agents/academicSearchAgent.ts b/src/agents/academicSearchAgent.ts index 5c11307..d797119 100644 --- a/src/agents/academicSearchAgent.ts +++ b/src/agents/academicSearchAgent.ts @@ -44,7 +44,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 '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). + 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. @@ -52,7 +52,7 @@ const basicAcademicSearchResponsePrompt = ` 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. - Aything 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 + 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. diff --git a/src/agents/redditSearchAgent.ts b/src/agents/redditSearchAgent.ts index 34e9ec2..3c60c68 100644 --- a/src/agents/redditSearchAgent.ts +++ b/src/agents/redditSearchAgent.ts @@ -44,7 +44,7 @@ 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 containg a brief description of the content of that page). + 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. @@ -52,7 +52,7 @@ const basicRedditSearchResponsePrompt = ` 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. - Aything 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 + 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. diff --git a/src/agents/suggestionGeneratorAgent.ts b/src/agents/suggestionGeneratorAgent.ts index 0efdfa9..6ba255d 100644 --- a/src/agents/suggestionGeneratorAgent.ts +++ b/src/agents/suggestionGeneratorAgent.ts @@ -47,7 +47,7 @@ const generateSuggestions = ( input: SuggestionGeneratorInput, llm: BaseChatModel, ) => { - (llm as ChatOpenAI).temperature = 0; + (llm as unknown as ChatOpenAI).temperature = 0; const suggestionGeneratorChain = createSuggestionGeneratorChain(llm); return suggestionGeneratorChain.invoke(input); }; diff --git a/src/agents/webSearchAgent.ts b/src/agents/webSearchAgent.ts index 1364742..04de148 100644 --- a/src/agents/webSearchAgent.ts +++ b/src/agents/webSearchAgent.ts @@ -44,7 +44,7 @@ Rephrased question: const basicWebSearchResponsePrompt = ` You are Perplexica, an AI model who is expert at searching the web and answering user's queries. - 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). + 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. @@ -52,7 +52,7 @@ const basicWebSearchResponsePrompt = ` 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. - Aything 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 + 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. diff --git a/src/agents/wolframAlphaSearchAgent.ts b/src/agents/wolframAlphaSearchAgent.ts index f810a1e..b80fcf3 100644 --- a/src/agents/wolframAlphaSearchAgent.ts +++ b/src/agents/wolframAlphaSearchAgent.ts @@ -43,7 +43,7 @@ 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 containg a brief description of the content of that page). + 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. @@ -51,7 +51,7 @@ const basicWolframAlphaSearchResponsePrompt = ` 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. - Aything 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 + 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. diff --git a/src/agents/youtubeSearchAgent.ts b/src/agents/youtubeSearchAgent.ts index 4e82cc7..334f67e 100644 --- a/src/agents/youtubeSearchAgent.ts +++ b/src/agents/youtubeSearchAgent.ts @@ -44,7 +44,7 @@ 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 containg a brief description of the content of that page). + 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. @@ -52,7 +52,7 @@ const basicYoutubeSearchResponsePrompt = ` 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. - Aything 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 + 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. diff --git a/src/lib/providers.ts b/src/lib/providers.ts deleted file mode 100644 index 08c43ce..0000000 --- a/src/lib/providers.ts +++ /dev/null @@ -1,198 +0,0 @@ -import { ChatOpenAI, OpenAIEmbeddings } from '@langchain/openai'; -import { ChatOllama } from '@langchain/community/chat_models/ollama'; -import { OllamaEmbeddings } from '@langchain/community/embeddings/ollama'; -import { HuggingFaceTransformersEmbeddings } from './huggingfaceTransformer'; -import { - getGroqApiKey, - getOllamaApiEndpoint, - getOllamaAuthHeader, - getOpenaiApiKey, -} from '../config'; -import logger from '../utils/logger'; - -function getOllamaHeaders() { - const ollamaAuthHeader = getOllamaAuthHeader(); - let headers; - if (typeof ollamaAuthHeader !== undefined) { - return { - 'Content-Type': 'application/json', - 'Authorization': ollamaAuthHeader - }; - } else { - return { 'Content-Type': 'application/json' }; - } -} - -export const getAvailableChatModelProviders = async () => { - const openAIApiKey = getOpenaiApiKey(); - const groqApiKey = getGroqApiKey(); - const ollamaEndpoint = getOllamaApiEndpoint(); - const ollamaAuthHeader = getOllamaAuthHeader(); - - const models = {}; - - if (openAIApiKey) { - try { - models['openai'] = { - 'GPT-3.5 turbo': new ChatOpenAI({ - openAIApiKey, - modelName: 'gpt-3.5-turbo', - temperature: 0.7, - }), - 'GPT-4': new ChatOpenAI({ - openAIApiKey, - modelName: 'gpt-4', - temperature: 0.7, - }), - 'GPT-4 turbo': new ChatOpenAI({ - openAIApiKey, - modelName: 'gpt-4-turbo', - temperature: 0.7, - }), - 'GPT-4 omni': new ChatOpenAI({ - openAIApiKey, - modelName: 'gpt-4o', - temperature: 0.7, - }), - }; - } catch (err) { - logger.error(`Error loading OpenAI models: ${err}`); - } - } - - if (groqApiKey) { - try { - models['groq'] = { - 'LLaMA3 8b': new ChatOpenAI( - { - openAIApiKey: groqApiKey, - modelName: 'llama3-8b-8192', - temperature: 0.7, - }, - { - baseURL: 'https://api.groq.com/openai/v1', - }, - ), - 'LLaMA3 70b': new ChatOpenAI( - { - openAIApiKey: groqApiKey, - modelName: 'llama3-70b-8192', - temperature: 0.7, - }, - { - baseURL: 'https://api.groq.com/openai/v1', - }, - ), - 'Mixtral 8x7b': new ChatOpenAI( - { - openAIApiKey: groqApiKey, - modelName: 'mixtral-8x7b-32768', - temperature: 0.7, - }, - { - baseURL: 'https://api.groq.com/openai/v1', - }, - ), - 'Gemma 7b': new ChatOpenAI( - { - openAIApiKey: groqApiKey, - modelName: 'gemma-7b-it', - temperature: 0.7, - }, - { - baseURL: 'https://api.groq.com/openai/v1', - }, - ), - }; - } catch (err) { - logger.error(`Error loading Groq models: ${err}`); - } - } - - if (ollamaEndpoint) { - try { - const headers = getOllamaHeaders(); - const response = await fetch(`${ollamaEndpoint}/api/tags`, { headers }); - - const { models: ollamaModels } = (await response.json()) as any; - - models['ollama'] = ollamaModels.reduce((acc, model) => { - acc[model.model] = new ChatOllama({ - baseUrl: ollamaEndpoint, - model: model.model, - headers, - temperature: 0.7, - }); - return acc; - }, {}); - } catch (err) { - logger.error(`Error loading Ollama models: ${err}`); - } - } - - models['custom_openai'] = {}; - - return models; -}; - -export const getAvailableEmbeddingModelProviders = async () => { - const openAIApiKey = getOpenaiApiKey(); - const ollamaEndpoint = getOllamaApiEndpoint(); - const ollamaAuthHeader = getOllamaAuthHeader(); - - const models = {}; - - if (openAIApiKey) { - try { - models['openai'] = { - 'Text embedding 3 small': new OpenAIEmbeddings({ - openAIApiKey, - modelName: 'text-embedding-3-small', - }), - 'Text embedding 3 large': new OpenAIEmbeddings({ - openAIApiKey, - modelName: 'text-embedding-3-large', - }), - }; - } catch (err) { - logger.error(`Error loading OpenAI embeddings: ${err}`); - } - } - - if (ollamaEndpoint) { - const headers = getOllamaHeaders(); - try { - const response = await fetch(`${ollamaEndpoint}/api/tags`, { headers }); - const { models: ollamaModels } = (await response.json()) as any; - - models['ollama'] = ollamaModels.reduce((acc, model) => { - acc[model.model] = new OllamaEmbeddings({ - baseUrl: ollamaEndpoint, - headers, - model: model.model, - }); - return acc; - }, {}); - } catch (err) { - logger.error(`Error loading Ollama embeddings: ${err}`); - } - } - - try { - models['local'] = { - 'BGE Small': new HuggingFaceTransformersEmbeddings({ - modelName: 'Xenova/bge-small-en-v1.5', - }), - 'GTE Small': new HuggingFaceTransformersEmbeddings({ - modelName: 'Xenova/gte-small', - }), - 'Bert Multilingual': new HuggingFaceTransformersEmbeddings({ - modelName: 'Xenova/bert-base-multilingual-uncased', - }), - }; - } catch (err) { - logger.error(`Error loading local embeddings: ${err}`); - } - - return models; -}; diff --git a/src/lib/providers/groq.ts b/src/lib/providers/groq.ts new file mode 100644 index 0000000..35bd125 --- /dev/null +++ b/src/lib/providers/groq.ts @@ -0,0 +1,59 @@ +import { ChatOpenAI } from '@langchain/openai'; +import { getGroqApiKey } from '../../config'; +import logger from '../../utils/logger'; + +export const loadGroqChatModels = async () => { + const groqApiKey = getGroqApiKey(); + + if (!groqApiKey) return {}; + + try { + const chatModels = { + 'LLaMA3 8b': new ChatOpenAI( + { + openAIApiKey: groqApiKey, + modelName: 'llama3-8b-8192', + temperature: 0.7, + }, + { + baseURL: 'https://api.groq.com/openai/v1', + }, + ), + 'LLaMA3 70b': new ChatOpenAI( + { + openAIApiKey: groqApiKey, + modelName: 'llama3-70b-8192', + temperature: 0.7, + }, + { + baseURL: 'https://api.groq.com/openai/v1', + }, + ), + 'Mixtral 8x7b': new ChatOpenAI( + { + openAIApiKey: groqApiKey, + modelName: 'mixtral-8x7b-32768', + temperature: 0.7, + }, + { + baseURL: 'https://api.groq.com/openai/v1', + }, + ), + 'Gemma 7b': new ChatOpenAI( + { + openAIApiKey: groqApiKey, + modelName: 'gemma-7b-it', + temperature: 0.7, + }, + { + baseURL: 'https://api.groq.com/openai/v1', + }, + ), + }; + + return chatModels; + } catch (err) { + logger.error(`Error loading Groq models: ${err}`); + return {}; + } +}; diff --git a/src/lib/providers/index.ts b/src/lib/providers/index.ts new file mode 100644 index 0000000..b1d4502 --- /dev/null +++ b/src/lib/providers/index.ts @@ -0,0 +1,44 @@ +import { loadGroqChatModels } from './groq'; +import { loadOllamaChatModels, loadOllamaEmbeddingsModels } from './ollama'; +import { loadOpenAIChatModels, loadOpenAIEmbeddingsModels } from './openai'; +import { loadTransformersEmbeddingsModels } from './transformers'; + +const chatModelProviders = { + openai: loadOpenAIChatModels, + groq: loadGroqChatModels, + ollama: loadOllamaChatModels, +}; + +const embeddingModelProviders = { + openai: loadOpenAIEmbeddingsModels, + local: loadTransformersEmbeddingsModels, + ollama: loadOllamaEmbeddingsModels, +}; + +export const getAvailableChatModelProviders = async () => { + const models = {}; + + for (const provider in chatModelProviders) { + const providerModels = await chatModelProviders[provider](); + if (Object.keys(providerModels).length > 0) { + models[provider] = providerModels + } + } + + models['custom_openai'] = {} + + return models; +}; + +export const getAvailableEmbeddingModelProviders = async () => { + const models = {}; + + for (const provider in embeddingModelProviders) { + const providerModels = await embeddingModelProviders[provider](); + if (Object.keys(providerModels).length > 0) { + models[provider] = providerModels + } + } + + return models; +}; diff --git a/src/lib/providers/ollama.ts b/src/lib/providers/ollama.ts new file mode 100644 index 0000000..7fc8ec0 --- /dev/null +++ b/src/lib/providers/ollama.ts @@ -0,0 +1,75 @@ +import { OllamaEmbeddings } from '@langchain/community/embeddings/ollama'; +import { getOllamaApiEndpoint, getOllamaAuthHeader } from '../../config'; +import logger from '../../utils/logger'; +import { ChatOllama } from '@langchain/community/chat_models/ollama'; + +function getOllamaHeaders() { + const ollamaAuthHeader = getOllamaAuthHeader(); + if (typeof ollamaAuthHeader !== undefined) { + return { + 'Content-Type': 'application/json', + 'Authorization': ollamaAuthHeader + }; + } else { + return { 'Content-Type': 'application/json' }; + } +} + +export const loadOllamaChatModels = async () => { + const ollamaEndpoint = getOllamaApiEndpoint(); + const ollamaHeaders = getOllamaHeaders(); + + if (!ollamaEndpoint) return {}; + + try { + const response = await fetch(`${ollamaEndpoint}/api/tags`, { + headers: ollamaHeaders + }); + + const { models: ollamaModels } = (await response.json()) as any; + + const chatModels = ollamaModels.reduce((acc, model) => { + acc[model.model] = new ChatOllama({ + baseUrl: ollamaEndpoint, + headers: ollamaHeaders, + model: model.model, + temperature: 0.7, + }); + return acc; + }, {}); + + return chatModels; + } catch (err) { + logger.error(`Error loading Ollama models: ${err}`); + return {}; + } +}; + +export const loadOllamaEmbeddingsModels = async () => { + const ollamaEndpoint = getOllamaApiEndpoint(); + const ollamaHeaders = getOllamaHeaders(); + + if (!ollamaEndpoint) return {}; + + try { + const response = await fetch(`${ollamaEndpoint}/api/tags`, { + headers: ollamaHeaders + }); + + const { models: ollamaModels } = (await response.json()) as any; + + const embeddingsModels = ollamaModels.reduce((acc, model) => { + acc[model.model] = new OllamaEmbeddings({ + baseUrl: ollamaEndpoint, + headers: ollamaHeaders, + model: model.model, + }); + return acc; + }, {}); + + return embeddingsModels; + } catch (err) { + logger.error(`Error loading Ollama embeddings model: ${err}`); + return {}; + } +}; diff --git a/src/lib/providers/openai.ts b/src/lib/providers/openai.ts new file mode 100644 index 0000000..afc7ab8 --- /dev/null +++ b/src/lib/providers/openai.ts @@ -0,0 +1,63 @@ +import { ChatOpenAI, OpenAIEmbeddings } from '@langchain/openai'; +import { getOpenaiApiKey } from '../../config'; +import logger from '../../utils/logger'; + +export const loadOpenAIChatModels = async () => { + const openAIApiKey = getOpenaiApiKey(); + + if (!openAIApiKey) return {}; + + try { + const chatModels = { + 'GPT-3.5 turbo': new ChatOpenAI({ + openAIApiKey, + modelName: 'gpt-3.5-turbo', + temperature: 0.7, + }), + 'GPT-4': new ChatOpenAI({ + openAIApiKey, + modelName: 'gpt-4', + temperature: 0.7, + }), + 'GPT-4 turbo': new ChatOpenAI({ + openAIApiKey, + modelName: 'gpt-4-turbo', + temperature: 0.7, + }), + 'GPT-4 omni': new ChatOpenAI({ + openAIApiKey, + modelName: 'gpt-4o', + temperature: 0.7, + }), + }; + + return chatModels; + } catch (err) { + logger.error(`Error loading OpenAI models: ${err}`); + return {}; + } +}; + +export const loadOpenAIEmbeddingsModels = async () => { + const openAIApiKey = getOpenaiApiKey(); + + if (!openAIApiKey) return {}; + + try { + const embeddingModels = { + 'Text embedding 3 small': new OpenAIEmbeddings({ + openAIApiKey, + modelName: 'text-embedding-3-small', + }), + 'Text embedding 3 large': new OpenAIEmbeddings({ + openAIApiKey, + modelName: 'text-embedding-3-large', + }), + }; + + return embeddingModels; + } catch (err) { + logger.error(`Error loading OpenAI embeddings model: ${err}`); + return {}; + } +}; diff --git a/src/lib/providers/transformers.ts b/src/lib/providers/transformers.ts new file mode 100644 index 0000000..0ec7052 --- /dev/null +++ b/src/lib/providers/transformers.ts @@ -0,0 +1,23 @@ +import logger from '../../utils/logger'; +import { HuggingFaceTransformersEmbeddings } from '../huggingfaceTransformer'; + +export const loadTransformersEmbeddingsModels = async () => { + try { + const embeddingModels = { + 'BGE Small': new HuggingFaceTransformersEmbeddings({ + modelName: 'Xenova/bge-small-en-v1.5', + }), + 'GTE Small': new HuggingFaceTransformersEmbeddings({ + modelName: 'Xenova/gte-small', + }), + 'Bert Multilingual': new HuggingFaceTransformersEmbeddings({ + modelName: 'Xenova/bert-base-multilingual-uncased', + }), + }; + + return embeddingModels; + } catch (err) { + logger.error(`Error loading Transformers embeddings model: ${err}`); + return {}; + } +}; diff --git a/src/routes/chats.ts b/src/routes/chats.ts index aacfb60..afa74f9 100644 --- a/src/routes/chats.ts +++ b/src/routes/chats.ts @@ -40,4 +40,27 @@ router.get('/:id', async (req, res) => { } }); +router.delete(`/:id`, async (req, res) => { + try { + const chatExists = await db.query.chats.findFirst({ + where: eq(chats.id, req.params.id), + }); + + if (!chatExists) { + return res.status(404).json({ message: 'Chat not found' }); + } + + await db.delete(chats).where(eq(chats.id, req.params.id)).execute(); + await db + .delete(messages) + .where(eq(messages.chatId, req.params.id)) + .execute(); + + return res.status(200).json({ message: 'Chat deleted successfully' }); + } catch (err) { + res.status(500).json({ message: 'An error has occurred.' }); + logger.error(`Error in deleting chat: ${err.message}`); + } +}); + export default router; diff --git a/src/websocket/connectionManager.ts b/src/websocket/connectionManager.ts index 5cb075b..70e20d9 100644 --- a/src/websocket/connectionManager.ts +++ b/src/websocket/connectionManager.ts @@ -45,7 +45,7 @@ export const handleConnection = async ( chatModelProviders[chatModelProvider][chatModel] && chatModelProvider != 'custom_openai' ) { - llm = chatModelProviders[chatModelProvider][chatModel] as + llm = chatModelProviders[chatModelProvider][chatModel] as unknown as | BaseChatModel | undefined; } else if (chatModelProvider == 'custom_openai') { @@ -56,7 +56,7 @@ export const handleConnection = async ( configuration: { baseURL: searchParams.get('openAIBaseURL'), }, - }); + }) as unknown as BaseChatModel; } if ( diff --git a/ui/app/library/page.tsx b/ui/app/library/page.tsx index 6ba2fe4..8294fc1 100644 --- a/ui/app/library/page.tsx +++ b/ui/app/library/page.tsx @@ -1,11 +1,12 @@ 'use client'; +import DeleteChat from '@/components/DeleteChat'; import { formatTimeDifference } from '@/lib/utils'; -import { BookOpenText, ClockIcon, ScanEye } from 'lucide-react'; +import { BookOpenText, ClockIcon, Delete, ScanEye } from 'lucide-react'; import Link from 'next/link'; import { useEffect, useState } from 'react'; -interface Chat { +export interface Chat { id: string; title: string; createdAt: string; @@ -92,6 +93,11 @@ const Page = () => { {formatTimeDifference(new Date(), chat.createdAt)} Ago

+ ))} diff --git a/ui/components/ChatWindow.tsx b/ui/components/ChatWindow.tsx index 675df49..b1a87a2 100644 --- a/ui/components/ChatWindow.tsx +++ b/ui/components/ChatWindow.tsx @@ -83,6 +83,55 @@ const useSocket = ( 'embeddingModelProvider', embeddingModelProvider, ); + } else { + const providers = await fetch( + `${process.env.NEXT_PUBLIC_API_URL}/models`, + { + headers: { + 'Content-Type': 'application/json', + }, + }, + ).then(async (res) => await res.json()); + + const chatModelProviders = providers.chatModelProviders; + const embeddingModelProviders = providers.embeddingModelProviders; + + if ( + Object.keys(chatModelProviders).length > 0 && + !chatModelProviders[chatModelProvider] + ) { + chatModelProvider = Object.keys(chatModelProviders)[0]; + localStorage.setItem('chatModelProvider', chatModelProvider); + } + + if ( + chatModelProvider && + !chatModelProviders[chatModelProvider][chatModel] + ) { + chatModel = Object.keys(chatModelProviders[chatModelProvider])[0]; + localStorage.setItem('chatModel', chatModel); + } + + if ( + Object.keys(embeddingModelProviders).length > 0 && + !embeddingModelProviders[embeddingModelProvider] + ) { + embeddingModelProvider = Object.keys(embeddingModelProviders)[0]; + localStorage.setItem( + 'embeddingModelProvider', + embeddingModelProvider, + ); + } + + if ( + embeddingModelProvider && + !embeddingModelProviders[embeddingModelProvider][embeddingModel] + ) { + embeddingModel = Object.keys( + embeddingModelProviders[embeddingModelProvider], + )[0]; + localStorage.setItem('embeddingModel', embeddingModel); + } } const wsURL = new URL(url); diff --git a/ui/components/DeleteChat.tsx b/ui/components/DeleteChat.tsx new file mode 100644 index 0000000..165f86e --- /dev/null +++ b/ui/components/DeleteChat.tsx @@ -0,0 +1,114 @@ +import { Delete, Trash } from 'lucide-react'; +import { Dialog, Transition } from '@headlessui/react'; +import { Fragment, useState } from 'react'; +import { toast } from 'sonner'; +import { Chat } from '@/app/library/page'; + +const DeleteChat = ({ + chatId, + chats, + setChats, +}: { + chatId: string; + chats: Chat[]; + setChats: (chats: Chat[]) => void; +}) => { + const [confirmationDialogOpen, setConfirmationDialogOpen] = useState(false); + const [loading, setLoading] = useState(false); + + const handleDelete = async () => { + setLoading(true); + try { + const res = await fetch( + `${process.env.NEXT_PUBLIC_API_URL}/chats/${chatId}`, + { + method: 'DELETE', + headers: { + 'Content-Type': 'application/json', + }, + }, + ); + + if (res.status != 200) { + throw new Error('Failed to delete chat'); + } + + const newChats = chats.filter((chat) => chat.id !== chatId); + + setChats(newChats); + } catch (err: any) { + toast.error(err.message); + } finally { + setConfirmationDialogOpen(false); + setLoading(false); + } + }; + + return ( + <> + + + { + if (!loading) { + setConfirmationDialogOpen(false); + } + }} + > + +
+
+ + + + Delete Confirmation + + + Are you sure you want to delete this chat? + +
+ + +
+
+
+
+
+
+
+ + ); +}; + +export default DeleteChat; diff --git a/ui/package.json b/ui/package.json index 567ce66..bec350a 100644 --- a/ui/package.json +++ b/ui/package.json @@ -1,6 +1,6 @@ { "name": "perplexica-frontend", - "version": "1.7.0", + "version": "1.7.1", "license": "MIT", "author": "ItzCrazyKns", "scripts": { diff --git a/yarn.lock b/yarn.lock index d418edf..dceddbd 100644 --- a/yarn.lock +++ b/yarn.lock @@ -307,6 +307,23 @@ "@jridgewell/resolve-uri" "^3.0.3" "@jridgewell/sourcemap-codec" "^1.4.10" +"@langchain/community@^0.2.16": + version "0.2.16" + resolved "https://registry.yarnpkg.com/@langchain/community/-/community-0.2.16.tgz#5888baf7fc7ea272c5f91aaa0e71bc444167262d" + integrity sha512-dFDcMabKACvuRd0w6EIRLWf1ubPGZEeEwFt9v1jiEr4HCFxH0OF+iM1QUCcVRbB2fK5lqmKeTD1XAeZV8+AyXA== + dependencies: + "@langchain/core" "~0.2.11" + "@langchain/openai" "~0.1.0" + binary-extensions "^2.2.0" + expr-eval "^2.0.2" + flat "^5.0.2" + js-yaml "^4.1.0" + langchain "0.2.3" + langsmith "~0.1.30" + uuid "^9.0.0" + zod "^3.22.3" + zod-to-json-schema "^3.22.5" + "@langchain/community@~0.0.41": version "0.0.43" resolved "https://registry.yarnpkg.com/@langchain/community/-/community-0.0.43.tgz#017e2f9b3209b3999482f10df5aec2520731a63c" @@ -320,6 +337,24 @@ uuid "^9.0.0" zod "^3.22.3" +"@langchain/core@>0.1.56 <0.3.0", "@langchain/core@>0.2.0 <0.3.0", "@langchain/core@>=0.2.5 <0.3.0", "@langchain/core@~0.2.0", "@langchain/core@~0.2.11": + version "0.2.11" + resolved "https://registry.yarnpkg.com/@langchain/core/-/core-0.2.11.tgz#5f47467e20e56b250831baef20083657c6facb4c" + integrity sha512-d4SNL7WI0c3oHrV4WxCRH1/TNqdePXEzYjYwIb4aEH6lW1aM0utGhLbNthX+aYkOL4Ynx2FoG4h91ECIipiKWQ== + dependencies: + ansi-styles "^5.0.0" + camelcase "6" + decamelize "1.2.0" + js-tiktoken "^1.0.12" + langsmith "~0.1.30" + ml-distance "^4.0.0" + mustache "^4.2.0" + p-queue "^6.6.2" + p-retry "4" + uuid "^9.0.0" + zod "^3.22.4" + zod-to-json-schema "^3.22.3" + "@langchain/core@~0.1.44", "@langchain/core@~0.1.45": version "0.1.52" resolved "https://registry.yarnpkg.com/@langchain/core/-/core-0.1.52.tgz#7619310b83ffa841628efe2e1eda873ca714d068" @@ -348,6 +383,36 @@ zod "^3.22.4" zod-to-json-schema "^3.22.3" +"@langchain/openai@~0.0.28": + version "0.0.34" + resolved "https://registry.yarnpkg.com/@langchain/openai/-/openai-0.0.34.tgz#36c9bca0721ab9f7e5d40927e7c0429cacbd5b56" + integrity sha512-M+CW4oXle5fdoz2T2SwdOef8pl3/1XmUx1vjn2mXUVM/128aO0l23FMF0SNBsAbRV6P+p/TuzjodchJbi0Ht/A== + dependencies: + "@langchain/core" ">0.1.56 <0.3.0" + js-tiktoken "^1.0.12" + openai "^4.41.1" + zod "^3.22.4" + zod-to-json-schema "^3.22.3" + +"@langchain/openai@~0.1.0": + version "0.1.3" + resolved "https://registry.yarnpkg.com/@langchain/openai/-/openai-0.1.3.tgz#6eb0994e970d85ffa9aaeafb94449024ccf6ca63" + integrity sha512-riv/JC9x2A8b7GcHu8sx+mlZJ8KAwSSi231IPTlcciYnKozmrQ5H0vrtiD31fxiDbaRsk7tyCpkSBIOQEo7CyQ== + dependencies: + "@langchain/core" ">=0.2.5 <0.3.0" + js-tiktoken "^1.0.12" + openai "^4.49.1" + zod "^3.22.4" + zod-to-json-schema "^3.22.3" + +"@langchain/textsplitters@~0.0.0": + version "0.0.3" + resolved "https://registry.yarnpkg.com/@langchain/textsplitters/-/textsplitters-0.0.3.tgz#1a3cc93dd2ab330edb225400ded190a22fea14e3" + integrity sha512-cXWgKE3sdWLSqAa8ykbCcUsUF1Kyr5J3HOWYGuobhPEycXW4WI++d5DhzdpL238mzoEXTi90VqfSCra37l5YqA== + dependencies: + "@langchain/core" ">0.2.0 <0.3.0" + js-tiktoken "^1.0.12" + "@protobufjs/aspromise@^1.1.1", "@protobufjs/aspromise@^1.1.2": version "1.1.2" resolved "https://registry.yarnpkg.com/@protobufjs/aspromise/-/aspromise-1.1.2.tgz#9b8b0cc663d669a7d8f6f5d0893a14d348f30fbf" @@ -1508,6 +1573,13 @@ is-stream@^2.0.0: resolved "https://registry.yarnpkg.com/is-stream/-/is-stream-2.0.1.tgz#fac1e3d53b97ad5a9d0ae9cef2389f5810a5c077" integrity sha512-hFoiJiTl63nn+kstHGBtewWSKnQLpyb155KHheA1l39uvtO9nWIop1p3udqPcUd/xbF1VLMO4n7OI6p7RbngDg== +js-tiktoken@^1.0.12: + version "1.0.12" + resolved "https://registry.yarnpkg.com/js-tiktoken/-/js-tiktoken-1.0.12.tgz#af0f5cf58e5e7318240d050c8413234019424211" + integrity sha512-L7wURW1fH9Qaext0VzaUDpFGVQgjkdE3Dgsy9/+yXyGEpBKnylTd0mU0bfbNkKDlXRb6TEsZkwuflu1B8uQbJQ== + dependencies: + base64-js "^1.5.1" + js-tiktoken@^1.0.7, js-tiktoken@^1.0.8: version "1.0.10" resolved "https://registry.yarnpkg.com/js-tiktoken/-/js-tiktoken-1.0.10.tgz#2b343ec169399dcee8f9ef9807dbd4fafd3b30dc" @@ -1532,6 +1604,28 @@ kuler@^2.0.0: resolved "https://registry.yarnpkg.com/kuler/-/kuler-2.0.0.tgz#e2c570a3800388fb44407e851531c1d670b061b3" integrity sha512-Xq9nH7KlWZmXAtodXDDRE7vs6DU1gTU8zYDHDiWLSip45Egwq3plLHzPn27NgvzL2r1LMPC1vdqh98sQxtqj4A== +langchain@0.2.3: + version "0.2.3" + resolved "https://registry.yarnpkg.com/langchain/-/langchain-0.2.3.tgz#c14bb05cf871b21bd63b84b3ab89580b1d62539f" + integrity sha512-T9xR7zd+Nj0oXy6WoYKmZLy0DlQiDLFPGYWdOXDxy+AvqlujoPdVQgDSpdqiOHvAjezrByAoKxoHCz5XMwTP/Q== + dependencies: + "@langchain/core" "~0.2.0" + "@langchain/openai" "~0.0.28" + "@langchain/textsplitters" "~0.0.0" + binary-extensions "^2.2.0" + js-tiktoken "^1.0.12" + js-yaml "^4.1.0" + jsonpointer "^5.0.1" + langchainhub "~0.0.8" + langsmith "~0.1.7" + ml-distance "^4.0.0" + openapi-types "^12.1.3" + p-retry "4" + uuid "^9.0.0" + yaml "^2.2.1" + zod "^3.22.4" + zod-to-json-schema "^3.22.3" + langchain@^0.1.30: version "0.1.30" resolved "https://registry.yarnpkg.com/langchain/-/langchain-0.1.30.tgz#e1adb3f1849fcd5c596c668300afd5dc8cb37a97" @@ -1571,6 +1665,23 @@ langsmith@~0.1.1, langsmith@~0.1.7: p-retry "4" uuid "^9.0.0" +langsmith@~0.1.30: + version "0.1.34" + resolved "https://registry.yarnpkg.com/langsmith/-/langsmith-0.1.34.tgz#801310495fef258ed9c22bb5575120e2c06d51cf" + integrity sha512-aMv2k8kEaovhTuZnK6/6DMCoM7Jurvm1AzdESn+yN+HramRxp3sK32jFRz3ogkXP6GjAjOIofcnNkzhHXSUXGA== + dependencies: + "@types/uuid" "^9.0.1" + commander "^10.0.1" + lodash.set "^4.3.2" + p-queue "^6.6.2" + p-retry "4" + uuid "^9.0.0" + +lodash.set@^4.3.2: + version "4.3.2" + resolved "https://registry.yarnpkg.com/lodash.set/-/lodash.set-4.3.2.tgz#d8757b1da807dde24816b0d6a84bea1a76230b23" + integrity sha512-4hNPN5jlm/N/HLMCO43v8BXKq9Z7QdAGc/VGrRD61w8gN9g/6jF9A4L1pbUgBLCffi0w9VsXfTOij5x8iTyFvg== + logform@^2.3.2, logform@^2.4.0: version "2.6.0" resolved "https://registry.yarnpkg.com/logform/-/logform-2.6.0.tgz#8c82a983f05d6eaeb2d75e3decae7a768b2bf9b5" @@ -1714,6 +1825,11 @@ ms@2.1.3, ms@^2.0.0, ms@^2.1.1: resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.3.tgz#574c8138ce1d2b5861f0b44579dbadd60c6615b2" integrity sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA== +mustache@^4.2.0: + version "4.2.0" + resolved "https://registry.yarnpkg.com/mustache/-/mustache-4.2.0.tgz#e5892324d60a12ec9c2a73359edca52972bf6f64" + integrity sha512-71ippSywq5Yb7/tVYyGbkBggbU8H3u5Rz56fH60jGFgr8uHwxs+aSKeqmluIVzM0m0kB7xQjKS6qPfd0b2ZoqQ== + napi-build-utils@^1.0.1: version "1.0.2" resolved "https://registry.yarnpkg.com/napi-build-utils/-/napi-build-utils-1.0.2.tgz#b1fddc0b2c46e380a0b7a76f984dd47c41a13806" @@ -1858,6 +1974,20 @@ openai@^4.26.0: node-fetch "^2.6.7" web-streams-polyfill "^3.2.1" +openai@^4.41.1, openai@^4.49.1: + version "4.52.2" + resolved "https://registry.yarnpkg.com/openai/-/openai-4.52.2.tgz#5d67271f3df84c0b54676b08990eaa9402151759" + integrity sha512-mMc0XgFuVSkcm0lRIi8zaw++otC82ZlfkCur1qguXYWPETr/+ZwL9A/vvp3YahX+shpaT6j03dwsmUyLAfmEfg== + dependencies: + "@types/node" "^18.11.18" + "@types/node-fetch" "^2.6.4" + abort-controller "^3.0.0" + agentkeepalive "^4.2.1" + form-data-encoder "1.7.2" + formdata-node "^4.3.2" + node-fetch "^2.6.7" + web-streams-polyfill "^3.2.1" + openapi-types@^12.1.3: version "12.1.3" resolved "https://registry.yarnpkg.com/openapi-types/-/openapi-types-12.1.3.tgz#471995eb26c4b97b7bd356aacf7b91b73e777dd3" @@ -2462,6 +2592,11 @@ zod-to-json-schema@^3.22.3: resolved "https://registry.yarnpkg.com/zod-to-json-schema/-/zod-to-json-schema-3.22.5.tgz#3646e81cfc318dbad2a22519e5ce661615418673" integrity sha512-+akaPo6a0zpVCCseDed504KBJUQpEW5QZw7RMneNmKw+fGaML1Z9tUNLnHHAC8x6dzVRO1eB2oEMyZRnuBZg7Q== +zod-to-json-schema@^3.22.5: + version "3.23.1" + resolved "https://registry.yarnpkg.com/zod-to-json-schema/-/zod-to-json-schema-3.23.1.tgz#5225925b8ed5fa20096bd99be076c4b29b53d309" + integrity sha512-oT9INvydob1XV0v1d2IadrR74rLtDInLvDFfAa1CG0Pmg/vxATk7I2gSelfj271mbzeM4Da0uuDQE/Nkj3DWNw== + zod@^3.22.3, zod@^3.22.4: version "3.22.4" resolved "https://registry.yarnpkg.com/zod/-/zod-3.22.4.tgz#f31c3a9386f61b1f228af56faa9255e845cf3fff"