Compare commits

..

12 Commits

Author SHA1 Message Date
yeet b86cf3c315 Merge remote-tracking branch 'origin/master' into ollama-auth 2024-09-25 13:55:17 +02:00
yeet f16d4f52b1 Reapply "Merge remote-tracking branch 'origin/master' into ollama-auth"
This reverts commit 4876a36369.
2024-09-25 13:52:21 +02:00
ItzCrazyKns a0aad69f62 feat(readme): update readme 2024-09-25 16:56:41 +05:30
ItzCrazyKns 1cfa3398a3 feat(package): bump version 2024-09-25 16:54:44 +05:30
ItzCrazyKns ead2d98a9f feat(search): update types 2024-09-25 16:54:19 +05:30
ItzCrazyKns c52d6ac290 feat(docs): add search API docs 2024-09-25 16:54:07 +05:30
ItzCrazyKns 2785cdd97a feat(routes): add search route 2024-09-25 15:27:48 +05:30
ItzCrazyKns 1589f16d5a feat(providers): add `displayName` property 2024-09-24 22:34:43 +05:30
ItzCrazyKns 40f551c426 feat(search-button): add empty check 2024-09-15 10:16:20 +05:30
ItzCrazyKns 1fcd64ad42 feat(docker-file): use SearXNG URL from env 2024-09-05 18:40:07 +05:30
ItzCrazyKns 07e5615860 feat(docker-compose): link `config.toml` as vol. 2024-09-04 18:54:54 +05:30
ItzCrazyKns c4f52adb45 feat(textarea): handle "/" keys 2024-09-02 11:44:40 +05:30
34 changed files with 746 additions and 2663 deletions

View File

@ -12,6 +12,7 @@
- [Non-Docker Installation](#non-docker-installation) - [Non-Docker Installation](#non-docker-installation)
- [Ollama Connection Errors](#ollama-connection-errors) - [Ollama Connection Errors](#ollama-connection-errors)
- [Using as a Search Engine](#using-as-a-search-engine) - [Using as a Search Engine](#using-as-a-search-engine)
- [Using Perplexica's API](#using-perplexicas-api)
- [One-Click Deployment](#one-click-deployment) - [One-Click Deployment](#one-click-deployment)
- [Upcoming Features](#upcoming-features) - [Upcoming Features](#upcoming-features)
- [Support Us](#support-us) - [Support Us](#support-us)
@ -45,6 +46,7 @@ Want to know more about its architecture and how it works? You can read it [here
- **Wolfram Alpha Search Mode:** Answers queries that need calculations or data analysis using Wolfram Alpha. - **Wolfram Alpha Search Mode:** Answers queries that need calculations or data analysis using Wolfram Alpha.
- **Reddit Search Mode:** Searches Reddit for discussions and opinions related to the query. - **Reddit Search Mode:** Searches Reddit for discussions and opinions related to the query.
- **Current Information:** Some search tools might give you outdated info because they use data from crawling bots and convert them into embeddings and store them in a index. Unlike them, Perplexica uses SearxNG, a metasearch engine to get the results and rerank and get the most relevant source out of it, ensuring you always get the latest information without the overhead of daily data updates. - **Current Information:** Some search tools might give you outdated info because they use data from crawling bots and convert them into embeddings and store them in a index. Unlike them, Perplexica uses SearxNG, a metasearch engine to get the results and rerank and get the most relevant source out of it, ensuring you always get the latest information without the overhead of daily data updates.
- **API**: Integrate Perplexica into your existing applications and make use of its capibilities.
It has many more features like image and video search. Some of the planned features are mentioned in [upcoming features](#upcoming-features). It has many more features like image and video search. Some of the planned features are mentioned in [upcoming features](#upcoming-features).
@ -125,6 +127,12 @@ If you wish to use Perplexica as an alternative to traditional search engines li
3. Add a new site search with the following URL: `http://localhost:3000/?q=%s`. Replace `localhost` with your IP address or domain name, and `3000` with the port number if Perplexica is not hosted locally. 3. Add a new site search with the following URL: `http://localhost:3000/?q=%s`. Replace `localhost` with your IP address or domain name, and `3000` with the port number if Perplexica is not hosted locally.
4. Click the add button. Now, you can use Perplexica directly from your browser's search bar. 4. Click the add button. Now, you can use Perplexica directly from your browser's search bar.
## Using Perplexica's API
Perplexica also provides an API for developers looking to integrate its powerful search engine into their own applications. You can run searches, use multiple models and get answers to your queries.
For more details, check out the full documentation [here](https://github.com/ItzCrazyKns/Perplexica/tree/master/docs/API/SEARCH.md).
## One-Click Deployment ## One-Click Deployment
[![Deploy to RepoCloud](https://d16t0pc4846x52.cloudfront.net/deploylobe.svg)](https://repocloud.io/details/?app_id=267) [![Deploy to RepoCloud](https://d16t0pc4846x52.cloudfront.net/deploylobe.svg)](https://repocloud.io/details/?app_id=267)
@ -135,6 +143,7 @@ If you wish to use Perplexica as an alternative to traditional search engines li
- [x] Adding support for local LLMs - [x] Adding support for local LLMs
- [x] History Saving features - [x] History Saving features
- [x] Introducing various Focus Modes - [x] Introducing various Focus Modes
- [x] Adding API support
- [ ] Finalizing Copilot Mode - [ ] Finalizing Copilot Mode
- [ ] Adding Discover - [ ] Adding Discover

View File

@ -1,6 +1,7 @@
FROM node:slim FROM node:slim
ARG SEARXNG_API_URL ARG SEARXNG_API_URL
ENV SEARXNG_API_URL=${SEARXNG_API_URL}
WORKDIR /home/perplexica WORKDIR /home/perplexica
@ -11,8 +12,6 @@ COPY drizzle.config.ts /home/perplexica/
COPY package.json /home/perplexica/ COPY package.json /home/perplexica/
COPY yarn.lock /home/perplexica/ COPY yarn.lock /home/perplexica/
RUN sed -i "s|SEARXNG = \".*\"|SEARXNG = \"${SEARXNG_API_URL}\"|g" /home/perplexica/config.toml
RUN mkdir /home/perplexica/data RUN mkdir /home/perplexica/data
RUN yarn install RUN yarn install

View File

@ -21,6 +21,7 @@ services:
- 3001:3001 - 3001:3001
volumes: volumes:
- backend-dbstore:/home/perplexica/data - backend-dbstore:/home/perplexica/data
- ./config.toml:/home/perplexica/config.toml
extra_hosts: extra_hosts:
- 'host.docker.internal:host-gateway' - 'host.docker.internal:host-gateway'
networks: networks:

105
docs/API/SEARCH.md Normal file
View File

@ -0,0 +1,105 @@
# Perplexica Search API Documentation
## Overview
Perplexicas Search API makes it easy to use our AI-powered search engine. You can run different types of searches, pick the models you want to use, and get the most recent info. Follow the following headings to learn more about Perplexica's search API.
## Endpoint
### **POST** `/api/search`
### Request
The API accepts a JSON object in the request body, where you define the focus mode, chat models, embedding models, and your query.
#### Request Body Structure
```json
{
"chatModel": {
"provider": "openai",
"model": "gpt-4o-mini"
},
"embeddingModel": {
"provider": "openai",
"model": "text-embedding-3-large"
},
"focusMode": "webSearch",
"query": "What is Perplexica",
"history": []
}
```
### Request Parameters
- **`chatModel`** (object, optional): Defines the chat model to be used for the query.
- `provider`: Specifies the provider for the chat model (e.g., `openai`, `ollama`).
- `model`: The specific model from the chosen provider (e.g., `gpt-4o-mini`).
- Optional fields for custom OpenAI configuration:
- `customOpenAIBaseURL`: If youre using a custom OpenAI instance, provide the base URL.
- `customOpenAIKey`: The API key for a custom OpenAI instance.
- **`embeddingModel`** (object, optional): Defines the embedding model for similarity-based searching.
- `provider`: The provider for the embedding model (e.g., `openai`).
- `model`: The specific embedding model (e.g., `text-embedding-3-large`).
- **`focusMode`** (string, required): Specifies which focus mode to use. Available modes:
- `webSearch`, `academicSearch`, `writingAssistant`, `wolframAlphaSearch`, `youtubeSearch`, `redditSearch`.
- **`query`** (string, required): The search query or question.
- **`history`** (array, optional): An array of message pairs representing the conversation history. Each pair consists of a role (either 'human' or 'assistant') and the message content. This allows the system to use the context of the conversation to refine results. Example:
```json
[
["human", "What is Perplexica?"],
["assistant", "Perplexica is an AI-powered search engine..."]
]
```
### Response
The response from the API includes both the final message and the sources used to generate that message.
#### Example Response
```json
{
"message": "Perplexica is an innovative, open-source AI-powered search engine designed to enhance the way users search for information online. Here are some key features and characteristics of Perplexica:\n\n- **AI-Powered Technology**: It utilizes advanced machine learning algorithms to not only retrieve information but also to understand the context and intent behind user queries, providing more relevant results [1][5].\n\n- **Open-Source**: Being open-source, Perplexica offers flexibility and transparency, allowing users to explore its functionalities without the constraints of proprietary software [3][10].",
"sources": [
{
"pageContent": "Perplexica is an innovative, open-source AI-powered search engine designed to enhance the way users search for information online.",
"metadata": {
"title": "What is Perplexica, and how does it function as an AI-powered search ...",
"url": "https://askai.glarity.app/search/What-is-Perplexica--and-how-does-it-function-as-an-AI-powered-search-engine"
}
},
{
"pageContent": "Perplexica is an open-source AI-powered search tool that dives deep into the internet to find precise answers.",
"metadata": {
"title": "Sahar Mor's Post",
"url": "https://www.linkedin.com/posts/sahar-mor_a-new-open-source-project-called-perplexica-activity-7204489745668694016-ncja"
}
}
....
]
}
```
### Fields in the Response
- **`message`** (string): The search result, generated based on the query and focus mode.
- **`sources`** (array): A list of sources that were used to generate the search result. Each source includes:
- `pageContent`: A snippet of the relevant content from the source.
- `metadata`: Metadata about the source, including:
- `title`: The title of the webpage.
- `url`: The URL of the webpage.
### Error Handling
If an error occurs during the search process, the API will return an appropriate error message with an HTTP status code.
- **400**: If the request is malformed or missing required fields (e.g., no focus mode or query).
- **500**: If an internal server error occurs during the search.

View File

@ -1,6 +1,6 @@
{ {
"name": "perplexica-backend", "name": "perplexica-backend",
"version": "1.9.0-rc1", "version": "1.9.0-rc3",
"license": "MIT", "license": "MIT",
"author": "ItzCrazyKns", "author": "ItzCrazyKns",
"scripts": { "scripts": {

File diff suppressed because it is too large Load Diff

View File

@ -19,6 +19,7 @@ import formatChatHistoryAsString from '../utils/formatHistory';
import eventEmitter from 'events'; import eventEmitter from 'events';
import computeSimilarity from '../utils/computeSimilarity'; import computeSimilarity from '../utils/computeSimilarity';
import logger from '../utils/logger'; import logger from '../utils/logger';
import { IterableReadableStream } from '@langchain/core/utils/stream';
const basicAcademicSearchRetrieverPrompt = ` 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. 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.
@ -66,7 +67,7 @@ const basicAcademicSearchResponsePrompt = `
const strParser = new StringOutputParser(); const strParser = new StringOutputParser();
const handleStream = async ( const handleStream = async (
stream: AsyncGenerator<StreamEvent, any, unknown>, stream: IterableReadableStream<StreamEvent>,
emitter: eventEmitter, emitter: eventEmitter,
) => { ) => {
for await (const event of stream) { for await (const event of stream) {

View File

@ -19,6 +19,7 @@ import formatChatHistoryAsString from '../utils/formatHistory';
import eventEmitter from 'events'; import eventEmitter from 'events';
import computeSimilarity from '../utils/computeSimilarity'; import computeSimilarity from '../utils/computeSimilarity';
import logger from '../utils/logger'; import logger from '../utils/logger';
import { IterableReadableStream } from '@langchain/core/utils/stream';
const basicRedditSearchRetrieverPrompt = ` 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. 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.
@ -66,7 +67,7 @@ const basicRedditSearchResponsePrompt = `
const strParser = new StringOutputParser(); const strParser = new StringOutputParser();
const handleStream = async ( const handleStream = async (
stream: AsyncGenerator<StreamEvent, any, unknown>, stream: IterableReadableStream<StreamEvent>,
emitter: eventEmitter, emitter: eventEmitter,
) => { ) => {
for await (const event of stream) { for await (const event of stream) {

View File

@ -22,22 +22,38 @@ import logger from '../utils/logger';
import LineListOutputParser from '../lib/outputParsers/listLineOutputParser'; import LineListOutputParser from '../lib/outputParsers/listLineOutputParser';
import { getDocumentsFromLinks } from '../lib/linkDocument'; import { getDocumentsFromLinks } from '../lib/linkDocument';
import LineOutputParser from '../lib/outputParsers/lineOutputParser'; import LineOutputParser from '../lib/outputParsers/lineOutputParser';
import { IterableReadableStream } from '@langchain/core/utils/stream';
import { ChatOpenAI } from '@langchain/openai';
const basicSearchRetrieverPrompt = ` 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. You are an AI question rephraser. You will be given a conversation and a follow-up question, you will have to rephrase the follow up question so it is a standalone question and can be used by another LLM to search the web for information to answer it.
If it is a writing task or a simple hi, hello rather than a question, you need to return \`not_needed\` as the response. If it is a smple writing task or a greeting (unless the greeting contains a question after it) like Hi, Hello, How are you, etc. than a question then you need to return \`not_needed\` as the response (This is because the LLM won't need to search the web for finding information on this topic).
If the question contains some links and asks to answer from those links or even if they don't you need to return the links inside 'links' XML block and the question inside 'question' XML block. If there are no links then you need to return the question without any XML block. If the user asks some question from some URL or wants you to summarize a PDF or a webpage (via URL) you need to return the links inside the \`links\` XML block and the question inside the \`question\` XML block. If the user wants to you to summarize the webpage or the PDF you need to return \`summarize\` inside the \`question\` XML block in place of a question and the link to summarize in the \`links\` XML block.
If the user asks to summarrize the content from some links you need to return \`Summarize\` as the question inside the 'question' XML block and the links inside the 'links' XML block. You must always return the rephrased question inside the \`question\` XML block, if there are no links in the follow-up question then don't insert a \`links\` XML block in your response.
Example: There are several examples attached for your reference inside the below \`examples\` XML block
1. Follow up question: What is the capital of France?
Rephrased question: \`Capital of france\`
2. Follow up question: What is the population of New York City? <examples>
Rephrased question: \`Population of New York City\` 1. Follow up question: What is the capital of France
Rephrased question:\`
<question>
Capital of france
</question>
\`
2. Hi, how are you?
Rephrased question\`
<question>
not_needed
</question>
\`
3. Follow up question: What is Docker? 3. Follow up question: What is Docker?
Rephrased question: \`What is Docker\` Rephrased question: \`
<question>
What is Docker
</question>
\`
4. Follow up question: Can you tell me what is X from https://example.com 4. Follow up question: Can you tell me what is X from https://example.com
Rephrased question: \` Rephrased question: \`
@ -53,16 +69,20 @@ https://example.com
5. Follow up question: Summarize the content from https://example.com 5. Follow up question: Summarize the content from https://example.com
Rephrased question: \` Rephrased question: \`
<question> <question>
Summarize summarize
</question> </question>
<links> <links>
https://example.com https://example.com
</links> </links>
\` \`
</examples>
Conversation: Anything below is the part of the actual conversation and you need to use conversation and the follow-up question to rephrase the follow-up question as a standalone question based on the guidelines shared above.
<conversation>
{chat_history} {chat_history}
</conversation>
Follow up question: {query} Follow up question: {query}
Rephrased question: Rephrased question:
@ -95,7 +115,7 @@ const basicWebSearchResponsePrompt = `
const strParser = new StringOutputParser(); const strParser = new StringOutputParser();
const handleStream = async ( const handleStream = async (
stream: AsyncGenerator<StreamEvent, any, unknown>, stream: IterableReadableStream<StreamEvent>,
emitter: eventEmitter, emitter: eventEmitter,
) => { ) => {
for await (const event of stream) { for await (const event of stream) {
@ -132,15 +152,13 @@ type BasicChainInput = {
}; };
const createBasicWebSearchRetrieverChain = (llm: BaseChatModel) => { const createBasicWebSearchRetrieverChain = (llm: BaseChatModel) => {
(llm as unknown as ChatOpenAI).temperature = 0;
return RunnableSequence.from([ return RunnableSequence.from([
PromptTemplate.fromTemplate(basicSearchRetrieverPrompt), PromptTemplate.fromTemplate(basicSearchRetrieverPrompt),
llm, llm,
strParser, strParser,
RunnableLambda.from(async (input: string) => { RunnableLambda.from(async (input: string) => {
if (input === 'not_needed') {
return { query: '', docs: [] };
}
const linksOutputParser = new LineListOutputParser({ const linksOutputParser = new LineListOutputParser({
key: 'links', key: 'links',
}); });
@ -152,9 +170,13 @@ const createBasicWebSearchRetrieverChain = (llm: BaseChatModel) => {
const links = await linksOutputParser.parse(input); const links = await linksOutputParser.parse(input);
let question = await questionOutputParser.parse(input); let question = await questionOutputParser.parse(input);
if (question === 'not_needed') {
return { query: '', docs: [] };
}
if (links.length > 0) { if (links.length > 0) {
if (question.length === 0) { if (question.length === 0) {
question = 'Summarize'; question = 'summarize';
} }
let docs = []; let docs = [];
@ -226,7 +248,7 @@ const createBasicWebSearchRetrieverChain = (llm: BaseChatModel) => {
return { query: question, docs: docs }; return { query: question, docs: docs };
} else { } else {
const res = await searchSearxng(input, { const res = await searchSearxng(question, {
language: 'en', language: 'en',
}); });
@ -242,7 +264,7 @@ const createBasicWebSearchRetrieverChain = (llm: BaseChatModel) => {
}), }),
); );
return { query: input, docs: documents }; return { query: question, docs: documents };
} }
}), }),
]); ]);
@ -271,7 +293,7 @@ const createBasicWebSearchAnsweringChain = (
return docs; return docs;
} }
if (query === 'Summarize') { if (query.toLocaleLowerCase() === 'summarize') {
return docs; return docs;
} }
@ -294,7 +316,7 @@ const createBasicWebSearchAnsweringChain = (
}); });
const sortedDocs = similarity const sortedDocs = similarity
.filter((sim) => sim.similarity > 0.5) .filter((sim) => sim.similarity > 0.3)
.sort((a, b) => b.similarity - a.similarity) .sort((a, b) => b.similarity - a.similarity)
.slice(0, 15) .slice(0, 15)
.map((sim) => docsWithContent[sim.index]); .map((sim) => docsWithContent[sim.index]);

View File

@ -18,6 +18,7 @@ import type { Embeddings } from '@langchain/core/embeddings';
import formatChatHistoryAsString from '../utils/formatHistory'; import formatChatHistoryAsString from '../utils/formatHistory';
import eventEmitter from 'events'; import eventEmitter from 'events';
import logger from '../utils/logger'; import logger from '../utils/logger';
import { IterableReadableStream } from '@langchain/core/utils/stream';
const basicWolframAlphaSearchRetrieverPrompt = ` 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. 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.
@ -65,7 +66,7 @@ const basicWolframAlphaSearchResponsePrompt = `
const strParser = new StringOutputParser(); const strParser = new StringOutputParser();
const handleStream = async ( const handleStream = async (
stream: AsyncGenerator<StreamEvent, any, unknown>, stream: IterableReadableStream<StreamEvent>,
emitter: eventEmitter, emitter: eventEmitter,
) => { ) => {
for await (const event of stream) { for await (const event of stream) {

View File

@ -10,6 +10,7 @@ import eventEmitter from 'events';
import type { BaseChatModel } from '@langchain/core/language_models/chat_models'; import type { BaseChatModel } from '@langchain/core/language_models/chat_models';
import type { Embeddings } from '@langchain/core/embeddings'; import type { Embeddings } from '@langchain/core/embeddings';
import logger from '../utils/logger'; import logger from '../utils/logger';
import { IterableReadableStream } from '@langchain/core/utils/stream';
const writingAssistantPrompt = ` 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. 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.
@ -19,7 +20,7 @@ Since you are a writing assistant, you would not perform web searches. If you th
const strParser = new StringOutputParser(); const strParser = new StringOutputParser();
const handleStream = async ( const handleStream = async (
stream: AsyncGenerator<StreamEvent, any, unknown>, stream: IterableReadableStream<StreamEvent>,
emitter: eventEmitter, emitter: eventEmitter,
) => { ) => {
for await (const event of stream) { for await (const event of stream) {

View File

@ -19,6 +19,7 @@ import formatChatHistoryAsString from '../utils/formatHistory';
import eventEmitter from 'events'; import eventEmitter from 'events';
import computeSimilarity from '../utils/computeSimilarity'; import computeSimilarity from '../utils/computeSimilarity';
import logger from '../utils/logger'; import logger from '../utils/logger';
import { IterableReadableStream } from '@langchain/core/utils/stream';
const basicYoutubeSearchRetrieverPrompt = ` 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. 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.
@ -66,7 +67,7 @@ const basicYoutubeSearchResponsePrompt = `
const strParser = new StringOutputParser(); const strParser = new StringOutputParser();
const handleStream = async ( const handleStream = async (
stream: AsyncGenerator<StreamEvent, any, unknown>, stream: IterableReadableStream<StreamEvent>,
emitter: eventEmitter, emitter: eventEmitter,
) => { ) => {
for await (const event of stream) { for await (const event of stream) {

View File

@ -41,7 +41,8 @@ export const getGroqApiKey = () => loadConfig().API_KEYS.GROQ;
export const getAnthropicApiKey = () => loadConfig().API_KEYS.ANTHROPIC; export const getAnthropicApiKey = () => loadConfig().API_KEYS.ANTHROPIC;
export const getSearxngApiEndpoint = () => loadConfig().API_ENDPOINTS.SEARXNG; export const getSearxngApiEndpoint = () =>
process.env.SEARXNG_API_URL || loadConfig().API_ENDPOINTS.SEARXNG;
export const getOllamaApiEndpoint = () => loadConfig().API_ENDPOINTS.OLLAMA; export const getOllamaApiEndpoint = () => loadConfig().API_ENDPOINTS.OLLAMA;

View File

@ -3,6 +3,7 @@ import { htmlToText } from 'html-to-text';
import { RecursiveCharacterTextSplitter } from 'langchain/text_splitter'; import { RecursiveCharacterTextSplitter } from 'langchain/text_splitter';
import { Document } from '@langchain/core/documents'; import { Document } from '@langchain/core/documents';
import pdfParse from 'pdf-parse'; import pdfParse from 'pdf-parse';
import logger from '../utils/logger';
export const getDocumentsFromLinks = async ({ links }: { links: string[] }) => { export const getDocumentsFromLinks = async ({ links }: { links: string[] }) => {
const splitter = new RecursiveCharacterTextSplitter(); const splitter = new RecursiveCharacterTextSplitter();
@ -16,66 +17,81 @@ export const getDocumentsFromLinks = async ({ links }: { links: string[] }) => {
? link ? link
: `https://${link}`; : `https://${link}`;
const res = await axios.get(link, { try {
responseType: 'arraybuffer', const res = await axios.get(link, {
}); responseType: 'arraybuffer',
});
const isPdf = res.headers['content-type'] === 'application/pdf'; const isPdf = res.headers['content-type'] === 'application/pdf';
if (isPdf) { if (isPdf) {
const pdfText = await pdfParse(res.data); const pdfText = await pdfParse(res.data);
const parsedText = pdfText.text const parsedText = pdfText.text
.replace(/(\r\n|\n|\r)/gm, ' ')
.replace(/\s+/g, ' ')
.trim();
const splittedText = await splitter.splitText(parsedText);
const title = 'PDF Document';
const linkDocs = splittedText.map((text) => {
return new Document({
pageContent: text,
metadata: {
title: title,
url: link,
},
});
});
docs.push(...linkDocs);
return;
}
const parsedText = htmlToText(res.data.toString('utf8'), {
selectors: [
{
selector: 'a',
options: {
ignoreHref: true,
},
},
],
})
.replace(/(\r\n|\n|\r)/gm, ' ') .replace(/(\r\n|\n|\r)/gm, ' ')
.replace(/\s+/g, ' ') .replace(/\s+/g, ' ')
.trim(); .trim();
const splittedText = await splitter.splitText(parsedText); const splittedText = await splitter.splitText(parsedText);
const title = 'PDF Document'; const title = res.data
.toString('utf8')
.match(/<title>(.*?)<\/title>/)?.[1];
const linkDocs = splittedText.map((text) => { const linkDocs = splittedText.map((text) => {
return new Document({ return new Document({
pageContent: text, pageContent: text,
metadata: { metadata: {
title: title, title: title || link,
url: link, url: link,
}, },
}); });
}); });
docs.push(...linkDocs); docs.push(...linkDocs);
return; } catch (err) {
} logger.error(
`Error at generating documents from links: ${err.message}`,
const parsedText = htmlToText(res.data.toString('utf8'), { );
selectors: [ docs.push(
{ new Document({
selector: 'a', pageContent: `Failed to retrieve content from the link: ${err.message}`,
options: { metadata: {
ignoreHref: true, title: 'Failed to retrieve content',
url: link,
}, },
}, }),
], );
}) }
.replace(/(\r\n|\n|\r)/gm, ' ')
.replace(/\s+/g, ' ')
.trim();
const splittedText = await splitter.splitText(parsedText);
const title = res.data
.toString('utf8')
.match(/<title>(.*?)<\/title>/)?.[1];
const linkDocs = splittedText.map((text) => {
return new Document({
pageContent: text,
metadata: {
title: title || link,
url: link,
},
});
});
docs.push(...linkDocs);
}), }),
); );

View File

@ -9,26 +9,38 @@ export const loadAnthropicChatModels = async () => {
try { try {
const chatModels = { const chatModels = {
'Claude 3.5 Sonnet': new ChatAnthropic({ 'claude-3-5-sonnet-20240620': {
temperature: 0.7, displayName: 'Claude 3.5 Sonnet',
anthropicApiKey: anthropicApiKey, model: new ChatAnthropic({
model: 'claude-3-5-sonnet-20240620', temperature: 0.7,
}), anthropicApiKey: anthropicApiKey,
'Claude 3 Opus': new ChatAnthropic({ model: 'claude-3-5-sonnet-20240620',
temperature: 0.7, }),
anthropicApiKey: anthropicApiKey, },
model: 'claude-3-opus-20240229', 'claude-3-opus-20240229': {
}), displayName: 'Claude 3 Opus',
'Claude 3 Sonnet': new ChatAnthropic({ model: new ChatAnthropic({
temperature: 0.7, temperature: 0.7,
anthropicApiKey: anthropicApiKey, anthropicApiKey: anthropicApiKey,
model: 'claude-3-sonnet-20240229', model: 'claude-3-opus-20240229',
}), }),
'Claude 3 Haiku': new ChatAnthropic({ },
temperature: 0.7, 'claude-3-sonnet-20240229': {
anthropicApiKey: anthropicApiKey, displayName: 'Claude 3 Sonnet',
model: 'claude-3-haiku-20240307', model: new ChatAnthropic({
}), temperature: 0.7,
anthropicApiKey: anthropicApiKey,
model: 'claude-3-sonnet-20240229',
}),
},
'claude-3-haiku-20240307': {
displayName: 'Claude 3 Haiku',
model: new ChatAnthropic({
temperature: 0.7,
anthropicApiKey: anthropicApiKey,
model: 'claude-3-haiku-20240307',
}),
},
}; };
return chatModels; return chatModels;

View File

@ -9,76 +9,97 @@ export const loadGroqChatModels = async () => {
try { try {
const chatModels = { const chatModels = {
'Llama 3.1 70B': new ChatOpenAI( 'llama-3.1-70b-versatile': {
{ displayName: 'Llama 3.1 70B',
openAIApiKey: groqApiKey, model: new ChatOpenAI(
modelName: 'llama-3.1-70b-versatile', {
temperature: 0.7, openAIApiKey: groqApiKey,
}, modelName: 'llama-3.1-70b-versatile',
{ temperature: 0.7,
baseURL: 'https://api.groq.com/openai/v1', },
}, {
), baseURL: 'https://api.groq.com/openai/v1',
'Llama 3.1 8B': new ChatOpenAI( },
{ ),
openAIApiKey: groqApiKey, },
modelName: 'llama-3.1-8b-instant', 'llama-3.1-8b-instant': {
temperature: 0.7, displayName: 'Llama 3.1 8B',
}, model: new ChatOpenAI(
{ {
baseURL: 'https://api.groq.com/openai/v1', openAIApiKey: groqApiKey,
}, modelName: 'llama-3.1-8b-instant',
), temperature: 0.7,
'LLaMA3 8b': new ChatOpenAI( },
{ {
openAIApiKey: groqApiKey, baseURL: 'https://api.groq.com/openai/v1',
modelName: 'llama3-8b-8192', },
temperature: 0.7, ),
}, },
{ 'llama3-8b-8192': {
baseURL: 'https://api.groq.com/openai/v1', displayName: 'LLaMA3 8B',
}, model: new ChatOpenAI(
), {
'LLaMA3 70b': new ChatOpenAI( openAIApiKey: groqApiKey,
{ modelName: 'llama3-8b-8192',
openAIApiKey: groqApiKey, temperature: 0.7,
modelName: 'llama3-70b-8192', },
temperature: 0.7, {
}, baseURL: 'https://api.groq.com/openai/v1',
{ },
baseURL: 'https://api.groq.com/openai/v1', ),
}, },
), 'llama3-70b-8192': {
'Mixtral 8x7b': new ChatOpenAI( displayName: 'LLaMA3 70B',
{ model: new ChatOpenAI(
openAIApiKey: groqApiKey, {
modelName: 'mixtral-8x7b-32768', openAIApiKey: groqApiKey,
temperature: 0.7, modelName: 'llama3-70b-8192',
}, temperature: 0.7,
{ },
baseURL: 'https://api.groq.com/openai/v1', {
}, baseURL: 'https://api.groq.com/openai/v1',
), },
'Gemma 7b': new ChatOpenAI( ),
{ },
openAIApiKey: groqApiKey, 'mixtral-8x7b-32768': {
modelName: 'gemma-7b-it', displayName: 'Mixtral 8x7B',
temperature: 0.7, model: new ChatOpenAI(
}, {
{ openAIApiKey: groqApiKey,
baseURL: 'https://api.groq.com/openai/v1', modelName: 'mixtral-8x7b-32768',
}, temperature: 0.7,
), },
'Gemma2 9b': new ChatOpenAI( {
{ baseURL: 'https://api.groq.com/openai/v1',
openAIApiKey: groqApiKey, },
modelName: 'gemma2-9b-it', ),
temperature: 0.7, },
}, 'gemma-7b-it': {
{ displayName: 'Gemma 7B',
baseURL: 'https://api.groq.com/openai/v1', model: new ChatOpenAI(
}, {
), openAIApiKey: groqApiKey,
modelName: 'gemma-7b-it',
temperature: 0.7,
},
{
baseURL: 'https://api.groq.com/openai/v1',
},
),
},
'gemma2-9b-it': {
displayName: 'Gemma2 9B',
model: new ChatOpenAI(
{
openAIApiKey: groqApiKey,
modelName: 'gemma2-9b-it',
temperature: 0.7,
},
{
baseURL: 'https://api.groq.com/openai/v1',
},
),
},
}; };
return chatModels; return chatModels;

View File

@ -4,72 +4,80 @@ import logger from '../../utils/logger';
import { ChatOllama } from '@langchain/community/chat_models/ollama'; import { ChatOllama } from '@langchain/community/chat_models/ollama';
function getOllamaHeaders() { function getOllamaHeaders() {
const ollamaAuthHeader = getOllamaAuthHeader(); const ollamaAuthHeader = getOllamaAuthHeader();
if (typeof ollamaAuthHeader !== undefined) { if (typeof ollamaAuthHeader !== undefined) {
return { return {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
'Authorization': ollamaAuthHeader 'Authorization': ollamaAuthHeader
}; };
} else { } else {
return { 'Content-Type': 'application/json' }; return { 'Content-Type': 'application/json' };
} }
} }
export const loadOllamaChatModels = async () => { export const loadOllamaChatModels = async () => {
const ollamaEndpoint = getOllamaApiEndpoint(); const ollamaEndpoint = getOllamaApiEndpoint();
const ollamaHeaders = getOllamaHeaders(); const ollamaHeaders = getOllamaHeaders();
if (!ollamaEndpoint) return {}; if (!ollamaEndpoint) return {};
try { try {
const response = await fetch(`${ollamaEndpoint}/api/tags`, { const response = await fetch(`${ollamaEndpoint}/api/tags`, {
headers: ollamaHeaders headers: ollamaHeaders
}); });
const { models: ollamaModels } = (await response.json()) as any; const { models: ollamaModels } = (await response.json()) as any;
const chatModels = ollamaModels.reduce((acc, model) => { const chatModels = ollamaModels.reduce((acc, model) => {
acc[model.model] = new ChatOllama({ acc[model.model] = {
baseUrl: ollamaEndpoint, displayName: model.name,
headers: ollamaHeaders, model: new ChatOllama({
model: model.model, baseUrl: ollamaEndpoint,
temperature: 0.7, headers: ollamaHeaders,
}); model: model.model,
return acc; temperature: 0.7,
}, {}); }),
};
return chatModels; return acc;
} catch (err) { }, {});
logger.error(`Error loading Ollama models: ${err}`);
return {}; return chatModels;
} } catch (err) {
logger.error(`Error loading Ollama models: ${err}`);
return {};
}
}; };
export const loadOllamaEmbeddingsModels = async () => { export const loadOllamaEmbeddingsModels = async () => {
const ollamaEndpoint = getOllamaApiEndpoint(); const ollamaEndpoint = getOllamaApiEndpoint();
const ollamaHeaders = getOllamaHeaders(); const ollamaHeaders = getOllamaHeaders();
if (!ollamaEndpoint) return {}; if (!ollamaEndpoint) return {};
try { try {
const response = await fetch(`${ollamaEndpoint}/api/tags`, { const response = await fetch(`${ollamaEndpoint}/api/tags`, {
headers: ollamaHeaders headers: ollamaHeaders
}); });
const { models: ollamaModels } = (await response.json()) as any; const { models: ollamaModels } = (await response.json()) as any;
const embeddingsModels = ollamaModels.reduce((acc, model) => { const embeddingsModels = ollamaModels.reduce((acc, model) => {
acc[model.model] = new OllamaEmbeddings({ acc[model.model] = {
baseUrl: ollamaEndpoint, displayName: model.name,
headers: ollamaHeaders, model: new OllamaEmbeddings({
model: model.model, baseUrl: ollamaEndpoint,
}); headers: ollamaHeaders,
return acc; model: model.model,
}, {}); }),
};
return embeddingsModels; return acc;
} catch (err) { }, {});
logger.error(`Error loading Ollama embeddings model: ${err}`);
return {}; return embeddingsModels;
} } catch (err) {
logger.error(`Error loading Ollama embeddings model: ${err}`);
return {};
}
}; };

View File

@ -9,31 +9,46 @@ export const loadOpenAIChatModels = async () => {
try { try {
const chatModels = { const chatModels = {
'GPT-3.5 turbo': new ChatOpenAI({ 'gpt-3.5-turbo': {
openAIApiKey, displayName: 'GPT-3.5 Turbo',
modelName: 'gpt-3.5-turbo', model: new ChatOpenAI({
temperature: 0.7, openAIApiKey,
}), modelName: 'gpt-3.5-turbo',
'GPT-4': new ChatOpenAI({ temperature: 0.7,
openAIApiKey, }),
modelName: 'gpt-4', },
temperature: 0.7, 'gpt-4': {
}), displayName: 'GPT-4',
'GPT-4 turbo': new ChatOpenAI({ model: new ChatOpenAI({
openAIApiKey, openAIApiKey,
modelName: 'gpt-4-turbo', modelName: 'gpt-4',
temperature: 0.7, temperature: 0.7,
}), }),
'GPT-4 omni': new ChatOpenAI({ },
openAIApiKey, 'gpt-4-turbo': {
modelName: 'gpt-4o', displayName: 'GPT-4 turbo',
temperature: 0.7, model: new ChatOpenAI({
}), openAIApiKey,
'GPT-4 omni mini': new ChatOpenAI({ modelName: 'gpt-4-turbo',
openAIApiKey, temperature: 0.7,
modelName: 'gpt-4o-mini', }),
temperature: 0.7, },
}), 'gpt-4o': {
displayName: 'GPT-4 omni',
model: new ChatOpenAI({
openAIApiKey,
modelName: 'gpt-4o',
temperature: 0.7,
}),
},
'gpt-4o-mini': {
displayName: 'GPT-4 omni mini',
model: new ChatOpenAI({
openAIApiKey,
modelName: 'gpt-4o-mini',
temperature: 0.7,
}),
},
}; };
return chatModels; return chatModels;
@ -50,14 +65,20 @@ export const loadOpenAIEmbeddingsModels = async () => {
try { try {
const embeddingModels = { const embeddingModels = {
'Text embedding 3 small': new OpenAIEmbeddings({ 'text-embedding-3-small': {
openAIApiKey, displayName: 'Text Embedding 3 Small',
modelName: 'text-embedding-3-small', model: new OpenAIEmbeddings({
}), openAIApiKey,
'Text embedding 3 large': new OpenAIEmbeddings({ modelName: 'text-embedding-3-small',
openAIApiKey, }),
modelName: 'text-embedding-3-large', },
}), 'text-embedding-3-large': {
displayName: 'Text Embedding 3 Large',
model: new OpenAIEmbeddings({
openAIApiKey,
modelName: 'text-embedding-3-large',
}),
},
}; };
return embeddingModels; return embeddingModels;

View File

@ -4,15 +4,24 @@ import { HuggingFaceTransformersEmbeddings } from '../huggingfaceTransformer';
export const loadTransformersEmbeddingsModels = async () => { export const loadTransformersEmbeddingsModels = async () => {
try { try {
const embeddingModels = { const embeddingModels = {
'BGE Small': new HuggingFaceTransformersEmbeddings({ 'xenova-bge-small-en-v1.5': {
modelName: 'Xenova/bge-small-en-v1.5', displayName: 'BGE Small',
}), model: new HuggingFaceTransformersEmbeddings({
'GTE Small': new HuggingFaceTransformersEmbeddings({ modelName: 'Xenova/bge-small-en-v1.5',
modelName: 'Xenova/gte-small', }),
}), },
'Bert Multilingual': new HuggingFaceTransformersEmbeddings({ 'xenova-gte-small': {
modelName: 'Xenova/bert-base-multilingual-uncased', displayName: 'GTE Small',
}), model: new HuggingFaceTransformersEmbeddings({
modelName: 'Xenova/gte-small',
}),
},
'xenova-bert-base-multilingual-uncased': {
displayName: 'Bert Multilingual',
model: new HuggingFaceTransformersEmbeddings({
modelName: 'Xenova/bert-base-multilingual-uncased',
}),
},
}; };
return embeddingModels; return embeddingModels;

View File

@ -10,38 +10,54 @@ import {
getOpenaiApiKey, getOpenaiApiKey,
updateConfig, updateConfig,
} from '../config'; } from '../config';
import logger from '../utils/logger';
const router = express.Router(); const router = express.Router();
router.get('/', async (_, res) => { router.get('/', async (_, res) => {
const config = {}; try {
const config = {};
const [chatModelProviders, embeddingModelProviders] = await Promise.all([ const [chatModelProviders, embeddingModelProviders] = await Promise.all([
getAvailableChatModelProviders(), getAvailableChatModelProviders(),
getAvailableEmbeddingModelProviders(), getAvailableEmbeddingModelProviders(),
]); ]);
config['chatModelProviders'] = {}; config['chatModelProviders'] = {};
config['embeddingModelProviders'] = {}; config['embeddingModelProviders'] = {};
for (const provider in chatModelProviders) { for (const provider in chatModelProviders) {
config['chatModelProviders'][provider] = Object.keys( config['chatModelProviders'][provider] = Object.keys(
chatModelProviders[provider], chatModelProviders[provider],
); ).map((model) => {
return {
name: model,
displayName: chatModelProviders[provider][model].displayName,
};
});
}
for (const provider in embeddingModelProviders) {
config['embeddingModelProviders'][provider] = Object.keys(
embeddingModelProviders[provider],
).map((model) => {
return {
name: model,
displayName: embeddingModelProviders[provider][model].displayName,
};
});
}
config['openaiApiKey'] = getOpenaiApiKey();
config['ollamaApiUrl'] = getOllamaApiEndpoint();
config['anthropicApiKey'] = getAnthropicApiKey();
config['groqApiKey'] = getGroqApiKey();
res.status(200).json(config);
} catch (err: any) {
res.status(500).json({ message: 'An error has occurred.' });
logger.error(`Error getting config: ${err.message}`);
} }
for (const provider in embeddingModelProviders) {
config['embeddingModelProviders'][provider] = Object.keys(
embeddingModelProviders[provider],
);
}
config['openaiApiKey'] = getOpenaiApiKey();
config['ollamaApiUrl'] = getOllamaApiEndpoint();
config['anthropicApiKey'] = getAnthropicApiKey();
config['groqApiKey'] = getGroqApiKey();
res.status(200).json(config);
}); });
router.post('/', async (req, res) => { router.post('/', async (req, res) => {

View File

@ -26,7 +26,7 @@ router.post('/', async (req, res) => {
let llm: BaseChatModel | undefined; let llm: BaseChatModel | undefined;
if (chatModels[provider] && chatModels[provider][chatModel]) { if (chatModels[provider] && chatModels[provider][chatModel]) {
llm = chatModels[provider][chatModel] as BaseChatModel | undefined; llm = chatModels[provider][chatModel].model as BaseChatModel | undefined;
} }
if (!llm) { if (!llm) {

View File

@ -6,6 +6,7 @@ import modelsRouter from './models';
import suggestionsRouter from './suggestions'; import suggestionsRouter from './suggestions';
import chatsRouter from './chats'; import chatsRouter from './chats';
import settingsRouter from './settings'; import settingsRouter from './settings';
import searchRouter from './search';
const router = express.Router(); const router = express.Router();
@ -16,5 +17,6 @@ router.use('/models', modelsRouter);
router.use('/suggestions', suggestionsRouter); router.use('/suggestions', suggestionsRouter);
router.use('/chats', chatsRouter); router.use('/chats', chatsRouter);
router.use('/settings', settingsRouter); router.use('/settings', settingsRouter);
router.use('/search', searchRouter);
export default router; export default router;

150
src/routes/search.ts Normal file
View File

@ -0,0 +1,150 @@
import express from 'express';
import logger from '../utils/logger';
import { BaseChatModel } from 'langchain/chat_models/base';
import { Embeddings } from 'langchain/embeddings/base';
import { ChatOpenAI } from '@langchain/openai';
import {
getAvailableChatModelProviders,
getAvailableEmbeddingModelProviders,
} from '../lib/providers';
import { searchHandlers } from '../websocket/messageHandler';
import { AIMessage, BaseMessage, HumanMessage } from '@langchain/core/messages';
const router = express.Router();
interface chatModel {
provider: string;
model: string;
customOpenAIBaseURL?: string;
customOpenAIKey?: string;
}
interface embeddingModel {
provider: string;
model: string;
}
interface ChatRequestBody {
focusMode: string;
chatModel?: chatModel;
embeddingModel?: embeddingModel;
query: string;
history: Array<[string, string]>;
}
router.post('/', async (req, res) => {
try {
const body: ChatRequestBody = req.body;
if (!body.focusMode || !body.query) {
return res.status(400).json({ message: 'Missing focus mode or query' });
}
body.history = body.history || [];
const history: BaseMessage[] = body.history.map((msg) => {
if (msg[0] === 'human') {
return new HumanMessage({
content: msg[1],
});
} else {
return new AIMessage({
content: msg[1],
});
}
});
const [chatModelProviders, embeddingModelProviders] = await Promise.all([
getAvailableChatModelProviders(),
getAvailableEmbeddingModelProviders(),
]);
const chatModelProvider =
body.chatModel?.provider || Object.keys(chatModelProviders)[0];
const chatModel =
body.chatModel?.model ||
Object.keys(chatModelProviders[chatModelProvider])[0];
const embeddingModelProvider =
body.embeddingModel?.provider || Object.keys(embeddingModelProviders)[0];
const embeddingModel =
body.embeddingModel?.model ||
Object.keys(embeddingModelProviders[embeddingModelProvider])[0];
let llm: BaseChatModel | undefined;
let embeddings: Embeddings | undefined;
if (body.chatModel?.provider === 'custom_openai') {
if (
!body.chatModel?.customOpenAIBaseURL ||
!body.chatModel?.customOpenAIKey
) {
return res
.status(400)
.json({ message: 'Missing custom OpenAI base URL or key' });
}
llm = new ChatOpenAI({
modelName: body.chatModel.model,
openAIApiKey: body.chatModel.customOpenAIKey,
temperature: 0.7,
configuration: {
baseURL: body.chatModel.customOpenAIBaseURL,
},
}) as unknown as BaseChatModel;
} else if (
chatModelProviders[chatModelProvider] &&
chatModelProviders[chatModelProvider][chatModel]
) {
llm = chatModelProviders[chatModelProvider][chatModel]
.model as unknown as BaseChatModel | undefined;
}
if (
embeddingModelProviders[embeddingModelProvider] &&
embeddingModelProviders[embeddingModelProvider][embeddingModel]
) {
embeddings = embeddingModelProviders[embeddingModelProvider][
embeddingModel
].model as Embeddings | undefined;
}
if (!llm || !embeddings) {
return res.status(400).json({ message: 'Invalid model selected' });
}
const searchHandler = searchHandlers[body.focusMode];
if (!searchHandler) {
return res.status(400).json({ message: 'Invalid focus mode' });
}
const emitter = searchHandler(body.query, history, llm, embeddings);
let message = '';
let sources = [];
emitter.on('data', (data) => {
const parsedData = JSON.parse(data);
if (parsedData.type === 'response') {
message += parsedData.data;
} else if (parsedData.type === 'sources') {
sources = parsedData.data;
}
});
emitter.on('end', () => {
res.status(200).json({ message, sources });
});
emitter.on('error', (data) => {
const parsedData = JSON.parse(data);
res.status(500).json({ message: parsedData.data });
});
} catch (err: any) {
logger.error(`Error in getting search results: ${err.message}`);
res.status(500).json({ message: 'An error has occurred.' });
}
});
export default router;

View File

@ -26,7 +26,7 @@ router.post('/', async (req, res) => {
let llm: BaseChatModel | undefined; let llm: BaseChatModel | undefined;
if (chatModels[provider] && chatModels[provider][chatModel]) { if (chatModels[provider] && chatModels[provider][chatModel]) {
llm = chatModels[provider][chatModel] as BaseChatModel | undefined; llm = chatModels[provider][chatModel].model as BaseChatModel | undefined;
} }
if (!llm) { if (!llm) {

View File

@ -26,7 +26,7 @@ router.post('/', async (req, res) => {
let llm: BaseChatModel | undefined; let llm: BaseChatModel | undefined;
if (chatModels[provider] && chatModels[provider][chatModel]) { if (chatModels[provider] && chatModels[provider][chatModel]) {
llm = chatModels[provider][chatModel] as BaseChatModel | undefined; llm = chatModels[provider][chatModel].model as BaseChatModel | undefined;
} }
if (!llm) { if (!llm) {

View File

@ -45,9 +45,8 @@ export const handleConnection = async (
chatModelProviders[chatModelProvider][chatModel] && chatModelProviders[chatModelProvider][chatModel] &&
chatModelProvider != 'custom_openai' chatModelProvider != 'custom_openai'
) { ) {
llm = chatModelProviders[chatModelProvider][chatModel] as unknown as llm = chatModelProviders[chatModelProvider][chatModel]
| BaseChatModel .model as unknown as BaseChatModel | undefined;
| undefined;
} else if (chatModelProvider == 'custom_openai') { } else if (chatModelProvider == 'custom_openai') {
llm = new ChatOpenAI({ llm = new ChatOpenAI({
modelName: chatModel, modelName: chatModel,
@ -65,7 +64,7 @@ export const handleConnection = async (
) { ) {
embeddings = embeddingModelProviders[embeddingModelProvider][ embeddings = embeddingModelProviders[embeddingModelProvider][
embeddingModel embeddingModel
] as Embeddings | undefined; ].model as Embeddings | undefined;
} }
if (!llm || !embeddings) { if (!llm || !embeddings) {

View File

@ -28,7 +28,7 @@ type WSMessage = {
history: Array<[string, string]>; history: Array<[string, string]>;
}; };
const searchHandlers = { export const searchHandlers = {
webSearch: handleWebSearch, webSearch: handleWebSearch,
academicSearch: handleAcademicSearch, academicSearch: handleAcademicSearch,
writingAssistant: handleWritingAssistant, writingAssistant: handleWritingAssistant,

View File

@ -83,7 +83,9 @@ const useSocket = (
chatModelProvider = Object.keys(chatModelProviders)[0]; chatModelProvider = Object.keys(chatModelProviders)[0];
if (chatModelProvider === 'custom_openai') { if (chatModelProvider === 'custom_openai') {
toast.error('Seems like you are using the custom OpenAI provider, please open the settings and configure the API key and base URL'); toast.error(
'Seems like you are using the custom OpenAI provider, please open the settings and configure the API key and base URL',
);
setError(true); setError(true);
return; return;
} else { } else {
@ -220,7 +222,7 @@ const useSocket = (
if (data.type === 'error') { if (data.type === 'error') {
toast.error(data.data); toast.error(data.data);
} }
}) });
setWs(ws); setWs(ws);
}; };
@ -235,13 +237,6 @@ const useSocket = (
setError(true); setError(true);
} }
} }
return () => {
if (ws?.readyState === 1) {
ws?.close();
console.log('[DEBUG] closed');
}
};
}, [ws, url, setIsWSReady, setError]); }, [ws, url, setIsWSReady, setError]);
return ws; return ws;
@ -348,6 +343,15 @@ const ChatWindow = ({ id }: { id?: string }) => {
// eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps
}, []); }, []);
useEffect(() => {
return () => {
if (ws?.readyState === 1) {
ws.close();
console.log('[DEBUG] closed');
}
};
}, []);
const messagesRef = useRef<Message[]>([]); const messagesRef = useRef<Message[]>([]);
useEffect(() => { useEffect(() => {

View File

@ -18,14 +18,21 @@ const EmptyChatMessageInput = ({
const inputRef = useRef<HTMLTextAreaElement | null>(null); const inputRef = useRef<HTMLTextAreaElement | null>(null);
const handleKeyDown = (e: KeyboardEvent) => {
if (e.key === '/') {
e.preventDefault();
inputRef.current?.focus();
}
};
useEffect(() => { useEffect(() => {
const handleKeyDown = (e: KeyboardEvent) => {
const activeElement = document.activeElement;
const isInputFocused =
activeElement?.tagName === 'INPUT' ||
activeElement?.tagName === 'TEXTAREA' ||
activeElement?.hasAttribute('contenteditable');
if (e.key === '/' && !isInputFocused) {
e.preventDefault();
inputRef.current?.focus();
}
};
document.addEventListener('keydown', handleKeyDown); document.addEventListener('keydown', handleKeyDown);
return () => { return () => {

View File

@ -27,14 +27,21 @@ const MessageInput = ({
const inputRef = useRef<HTMLTextAreaElement | null>(null); const inputRef = useRef<HTMLTextAreaElement | null>(null);
const handleKeyDown = (e: KeyboardEvent) => {
if (e.key === '/') {
e.preventDefault();
inputRef.current?.focus();
}
};
useEffect(() => { useEffect(() => {
const handleKeyDown = (e: KeyboardEvent) => {
const activeElement = document.activeElement;
const isInputFocused =
activeElement?.tagName === 'INPUT' ||
activeElement?.tagName === 'TEXTAREA' ||
activeElement?.hasAttribute('contenteditable');
if (e.key === '/' && !isInputFocused) {
e.preventDefault();
inputRef.current?.focus();
}
};
document.addEventListener('keydown', handleKeyDown); document.addEventListener('keydown', handleKeyDown);
return () => { return () => {

View File

@ -51,7 +51,7 @@ const SearchImages = ({
const data = await res.json(); const data = await res.json();
const images = data.images; const images = data.images ?? [];
setImages(images); setImages(images);
setSlides( setSlides(
images.map((image: Image) => { images.map((image: Image) => {

View File

@ -64,7 +64,7 @@ const Searchvideos = ({
const data = await res.json(); const data = await res.json();
const videos = data.videos; const videos = data.videos ?? [];
setVideos(videos); setVideos(videos);
setSlides( setSlides(
videos.map((video: Video) => { videos.map((video: Video) => {

View File

@ -49,10 +49,10 @@ export const Select = ({ className, options, ...restProps }: SelectProps) => {
interface SettingsType { interface SettingsType {
chatModelProviders: { chatModelProviders: {
[key: string]: string[]; [key: string]: [Record<string, any>];
}; };
embeddingModelProviders: { embeddingModelProviders: {
[key: string]: string[]; [key: string]: [Record<string, any>];
}; };
openaiApiKey: string; openaiApiKey: string;
groqApiKey: string; groqApiKey: string;
@ -68,6 +68,10 @@ const SettingsDialog = ({
setIsOpen: (isOpen: boolean) => void; setIsOpen: (isOpen: boolean) => void;
}) => { }) => {
const [config, setConfig] = useState<SettingsType | null>(null); const [config, setConfig] = useState<SettingsType | null>(null);
const [chatModels, setChatModels] = useState<Record<string, any>>({});
const [embeddingModels, setEmbeddingModels] = useState<Record<string, any>>(
{},
);
const [selectedChatModelProvider, setSelectedChatModelProvider] = useState< const [selectedChatModelProvider, setSelectedChatModelProvider] = useState<
string | null string | null
>(null); >(null);
@ -118,7 +122,7 @@ const SettingsDialog = ({
const chatModel = const chatModel =
localStorage.getItem('chatModel') || localStorage.getItem('chatModel') ||
(data.chatModelProviders && (data.chatModelProviders &&
data.chatModelProviders[chatModelProvider]?.[0]) || data.chatModelProviders[chatModelProvider]?.[0].name) ||
''; '';
const embeddingModelProvider = const embeddingModelProvider =
localStorage.getItem('embeddingModelProvider') || localStorage.getItem('embeddingModelProvider') ||
@ -127,7 +131,7 @@ const SettingsDialog = ({
const embeddingModel = const embeddingModel =
localStorage.getItem('embeddingModel') || localStorage.getItem('embeddingModel') ||
(data.embeddingModelProviders && (data.embeddingModelProviders &&
data.embeddingModelProviders[embeddingModelProvider]?.[0]) || data.embeddingModelProviders[embeddingModelProvider]?.[0].name) ||
''; '';
setSelectedChatModelProvider(chatModelProvider); setSelectedChatModelProvider(chatModelProvider);
@ -136,6 +140,8 @@ const SettingsDialog = ({
setSelectedEmbeddingModel(embeddingModel); setSelectedEmbeddingModel(embeddingModel);
setCustomOpenAIApiKey(localStorage.getItem('openAIApiKey') || ''); setCustomOpenAIApiKey(localStorage.getItem('openAIApiKey') || '');
setCustomOpenAIBaseURL(localStorage.getItem('openAIBaseURL') || ''); setCustomOpenAIBaseURL(localStorage.getItem('openAIBaseURL') || '');
setChatModels(data.chatModelProviders || {});
setEmbeddingModels(data.embeddingModelProviders || {});
setIsLoading(false); setIsLoading(false);
}; };
@ -233,7 +239,8 @@ const SettingsDialog = ({
setSelectedChatModel(''); setSelectedChatModel('');
} else { } else {
setSelectedChatModel( setSelectedChatModel(
config.chatModelProviders[e.target.value][0], config.chatModelProviders[e.target.value][0]
.name,
); );
} }
}} }}
@ -268,8 +275,8 @@ const SettingsDialog = ({
return chatModelProvider return chatModelProvider
? chatModelProvider.length > 0 ? chatModelProvider.length > 0
? chatModelProvider.map((model) => ({ ? chatModelProvider.map((model) => ({
value: model, value: model.name,
label: model, label: model.displayName,
})) }))
: [ : [
{ {
@ -345,7 +352,8 @@ const SettingsDialog = ({
onChange={(e) => { onChange={(e) => {
setSelectedEmbeddingModelProvider(e.target.value); setSelectedEmbeddingModelProvider(e.target.value);
setSelectedEmbeddingModel( setSelectedEmbeddingModel(
config.embeddingModelProviders[e.target.value][0], config.embeddingModelProviders[e.target.value][0]
.name,
); );
}} }}
options={Object.keys( options={Object.keys(
@ -378,8 +386,8 @@ const SettingsDialog = ({
return embeddingModelProvider return embeddingModelProvider
? embeddingModelProvider.length > 0 ? embeddingModelProvider.length > 0
? embeddingModelProvider.map((model) => ({ ? embeddingModelProvider.map((model) => ({
label: model, label: model.displayName,
value: model, value: model.name,
})) }))
: [ : [
{ {

View File

@ -1,6 +1,6 @@
{ {
"name": "perplexica-frontend", "name": "perplexica-frontend",
"version": "1.9.0-rc1", "version": "1.9.0-rc3",
"license": "MIT", "license": "MIT",
"author": "ItzCrazyKns", "author": "ItzCrazyKns",
"scripts": { "scripts": {