diff --git a/README.md b/README.md
index df257a8..92d6308 100644
--- a/README.md
+++ b/README.md
@@ -11,6 +11,7 @@
- [Getting Started with Docker (Recommended)](#getting-started-with-docker-recommended)
- [Non-Docker Installation](#non-docker-installation)
- [Ollama connection errors](#ollama-connection-errors)
+- [Using as a Search Engine](#using-as-a-search-engine)
- [One-Click Deployment](#one-click-deployment)
- [Upcoming Features](#upcoming-features)
- [Support Us](#support-us)
@@ -92,6 +93,8 @@ There are mainly 2 ways of installing Perplexica - With Docker, Without Docker.
**Note**: Using Docker is recommended as it simplifies the setup process, especially for managing environment variables and dependencies.
+See the [installation documentation](https://github.com/ItzCrazyKns/Perplexica/tree/master/docs/installation) for more information like exposing it your network, etc.
+
### Ollama connection errors
If you're facing an Ollama connection error, it is often related to the backend not being able to connect to Ollama's API. How can you fix it? You can fix it by updating your Ollama API URL in the settings menu to the following:
@@ -102,6 +105,15 @@ On Linux: `http://private_ip_of_computer_hosting_ollama:11434`
You need to edit the ports accordingly.
+## Using as a Search Engine
+
+If you wish to use Perplexica as an alternative to traditional search engines like Google or Bing, or if you want to add a shortcut for quick access from your browser's search bar, follow these steps:
+
+1. Open your browser's settings.
+2. Navigate to the 'Search Engines' section.
+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.
+
## One-Click Deployment
[](https://repocloud.io/details/?app_id=267)
diff --git a/backend.dockerfile b/backend.dockerfile
index 8bf34f0..47c5d81 100644
--- a/backend.dockerfile
+++ b/backend.dockerfile
@@ -1,4 +1,4 @@
-FROM node:alpine
+FROM node:buster-slim
ARG SEARXNG_API_URL
diff --git a/docker-compose.yaml b/docker-compose.yaml
index 0ed889f..f3b09a0 100644
--- a/docker-compose.yaml
+++ b/docker-compose.yaml
@@ -1,11 +1,9 @@
services:
searxng:
- build:
- context: .
- dockerfile: searxng.dockerfile
+ image: docker.io/searxng/searxng:latest
+ volumes:
+ - ./searxng:/etc/searxng:rw
restart: always
- expose:
- - 4000
ports:
- 4000:8080
networks:
@@ -30,8 +28,6 @@ services:
GOOGLE_APPLICATION_CREDENTIALS: /var/keys/gcp_service_account.json
depends_on:
- searxng
- expose:
- - 3001
ports:
- 3001:3001
networks:
@@ -47,8 +43,6 @@ services:
- NEXT_PUBLIC_WS_URL=ws://127.0.0.1:3001
depends_on:
- perplexica-backend
- expose:
- - 3000
ports:
- 3000:3000
networks:
diff --git a/docs/architecture/WORKING.md b/docs/architecture/WORKING.md
index 8718b22..e39de7a 100644
--- a/docs/architecture/WORKING.md
+++ b/docs/architecture/WORKING.md
@@ -5,7 +5,7 @@ Curious about how Perplexica works? Don't worry, we'll cover it here. Before we
We'll understand how Perplexica works by taking an example of a scenario where a user asks: "How does an A.C. work?". We'll break down the process into steps to make it easier to understand. The steps are as follows:
1. The message is sent via WS to the backend server where it invokes the chain. The chain will depend on your focus mode. For this example, let's assume we use the "webSearch" focus mode.
-2. The chain is now invoked; first, the message is passed to another chain where it first predicts (using the chat history and the question) whether there is a need for sources or searching the web. If there is, it will generate a query (in accordance with the chat history) for searching the web that we'll take up later. If not, the chain will end there, and then the answer generator chain, also known as the response generator, will be started.
+2. The chain is now invoked; first, the message is passed to another chain where it first predicts (using the chat history and the question) whether there is a need for sources and searching the web. If there is, it will generate a query (in accordance with the chat history) for searching the web that we'll take up later. If not, the chain will end there, and then the answer generator chain, also known as the response generator, will be started.
3. The query returned by the first chain is passed to SearXNG to search the web for information.
4. After the information is retrieved, it is based on keyword-based search. We then convert the information into embeddings and the query as well, then we perform a similarity search to find the most relevant sources to answer the query.
5. After all this is done, the sources are passed to the response generator. This chain takes all the chat history, the query, and the sources. It generates a response that is streamed to the UI.
diff --git a/docs/installation/NETWORKING.md b/docs/installation/NETWORKING.md
new file mode 100644
index 0000000..baad296
--- /dev/null
+++ b/docs/installation/NETWORKING.md
@@ -0,0 +1,109 @@
+# Expose Perplexica to a network
+
+This guide will show you how to make Perplexica available over a network. Follow these steps to allow computers on the same network to interact with Perplexica. Choose the instructions that match the operating system you are using.
+
+## Windows
+
+1. Open PowerShell as Administrator
+
+2. Navigate to the directory containing the `docker-compose.yaml` file
+
+3. Stop and remove the existing Perplexica containers and images:
+
+```
+docker compose down --rmi all
+```
+
+4. Open the `docker-compose.yaml` file in a text editor like Notepad++
+
+5. Replace `127.0.0.1` with the IP address of the server Perplexica is running on in these two lines:
+
+```
+args:
+ - NEXT_PUBLIC_API_URL=http://127.0.0.1:3001/api
+ - NEXT_PUBLIC_WS_URL=ws://127.0.0.1:3001
+```
+
+6. Save and close the `docker-compose.yaml` file
+
+7. Rebuild and restart the Perplexica container:
+
+```
+docker compose up -d --build
+```
+
+## macOS
+
+1. Open the Terminal application
+
+2. Navigate to the directory with the `docker-compose.yaml` file:
+
+```
+cd /path/to/docker-compose.yaml
+```
+
+3. Stop and remove existing containers and images:
+
+```
+docker compose down --rmi all
+```
+
+4. Open `docker-compose.yaml` in a text editor like Sublime Text:
+
+```
+nano docker-compose.yaml
+```
+
+5. Replace `127.0.0.1` with the server IP in these lines:
+
+```
+args:
+ - NEXT_PUBLIC_API_URL=http://127.0.0.1:3001/api
+ - NEXT_PUBLIC_WS_URL=ws://127.0.0.1:3001
+```
+
+6. Save and exit the editor
+
+7. Rebuild and restart Perplexica:
+
+```
+docker compose up -d --build
+```
+
+## Linux
+
+1. Open the terminal
+
+2. Navigate to the `docker-compose.yaml` directory:
+
+```
+cd /path/to/docker-compose.yaml
+```
+
+3. Stop and remove containers and images:
+
+```
+docker compose down --rmi all
+```
+
+4. Edit `docker-compose.yaml`:
+
+```
+nano docker-compose.yaml
+```
+
+5. Replace `127.0.0.1` with the server IP:
+
+```
+args:
+ - NEXT_PUBLIC_API_URL=http://127.0.0.1:3001/api
+ - NEXT_PUBLIC_WS_URL=ws://127.0.0.1:3001
+```
+
+6. Save and exit the editor
+
+7. Rebuild and restart Perplexica:
+
+```
+docker compose up -d --build
+```
diff --git a/package.json b/package.json
index 0f43b87..8a07a7a 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "perplexica-backend",
- "version": "1.3.4",
+ "version": "1.5.0",
"license": "MIT",
"author": "ItzCrazyKns",
"scripts": {
@@ -23,6 +23,7 @@
"@iarna/toml": "^2.2.5",
"@langchain/google-vertexai": "^0.0.16",
"@langchain/openai": "^0.0.25",
+ "@xenova/transformers": "^2.17.1",
"axios": "^1.6.8",
"compute-cosine-similarity": "^1.1.0",
"compute-dot": "^1.1.0",
diff --git a/sample.config.toml b/sample.config.toml
index 7bc8880..8d35666 100644
--- a/sample.config.toml
+++ b/sample.config.toml
@@ -8,4 +8,4 @@ GROQ = "" # Groq API key - gsk_1234567890abcdef1234567890abcdef
[API_ENDPOINTS]
SEARXNG = "http://localhost:32768" # SearxNG API URL
-OLLAMA = "" # Ollama API URL - http://host.docker.internal:11434
+OLLAMA = "" # Ollama API URL - http://host.docker.internal:11434
\ No newline at end of file
diff --git a/searxng.dockerfile b/searxng.dockerfile
deleted file mode 100644
index 8bcd2b2..0000000
--- a/searxng.dockerfile
+++ /dev/null
@@ -1,3 +0,0 @@
-FROM searxng/searxng
-
-COPY searxng-settings.yml /etc/searxng/settings.yml
\ No newline at end of file
diff --git a/searxng/limiter.toml b/searxng/limiter.toml
new file mode 100644
index 0000000..ae69bd3
--- /dev/null
+++ b/searxng/limiter.toml
@@ -0,0 +1,3 @@
+[botdetection.ip_limit]
+# activate link_token method in the ip_limit method
+link_token = true
\ No newline at end of file
diff --git a/searxng-settings.yml b/searxng/settings.yml
similarity index 100%
rename from searxng-settings.yml
rename to searxng/settings.yml
diff --git a/searxng/uwsgi.ini b/searxng/uwsgi.ini
new file mode 100644
index 0000000..dd1247a
--- /dev/null
+++ b/searxng/uwsgi.ini
@@ -0,0 +1,50 @@
+[uwsgi]
+# Who will run the code
+uid = searxng
+gid = searxng
+
+# Number of workers (usually CPU count)
+# default value: %k (= number of CPU core, see Dockerfile)
+workers = %k
+
+# Number of threads per worker
+# default value: 4 (see Dockerfile)
+threads = 4
+
+# The right granted on the created socket
+chmod-socket = 666
+
+# Plugin to use and interpreter config
+single-interpreter = true
+master = true
+plugin = python3
+lazy-apps = true
+enable-threads = 4
+
+# Module to import
+module = searx.webapp
+
+# Virtualenv and python path
+pythonpath = /usr/local/searxng/
+chdir = /usr/local/searxng/searx/
+
+# automatically set processes name to something meaningful
+auto-procname = true
+
+# Disable request logging for privacy
+disable-logging = true
+log-5xx = true
+
+# Set the max size of a request (request-body excluded)
+buffer-size = 8192
+
+# No keep alive
+# See https://github.com/searx/searx-docker/issues/24
+add-header = Connection: close
+
+# uwsgi serves the static files
+static-map = /static=/usr/local/searxng/searx/static
+# expires set to one day
+static-expires = /* 86400
+static-gzip-all = True
+offload-threads = 4
diff --git a/src/agents/suggestionGeneratorAgent.ts b/src/agents/suggestionGeneratorAgent.ts
new file mode 100644
index 0000000..0efdfa9
--- /dev/null
+++ b/src/agents/suggestionGeneratorAgent.ts
@@ -0,0 +1,55 @@
+import { RunnableSequence, RunnableMap } from '@langchain/core/runnables';
+import ListLineOutputParser from '../lib/outputParsers/listLineOutputParser';
+import { PromptTemplate } from '@langchain/core/prompts';
+import formatChatHistoryAsString from '../utils/formatHistory';
+import { BaseMessage } from '@langchain/core/messages';
+import { BaseChatModel } from '@langchain/core/language_models/chat_models';
+import { ChatOpenAI } from '@langchain/openai';
+
+const suggestionGeneratorPrompt = `
+You are an AI suggestion generator for an AI powered search engine. You will be given a conversation below. You need to generate 4-5 suggestions based on the conversation. The suggestion should be relevant to the conversation that can be used by the user to ask the chat model for more information.
+You need to make sure the suggestions are relevant to the conversation and are helpful to the user. Keep a note that the user might use these suggestions to ask a chat model for more information.
+Make sure the suggestions are medium in length and are informative and relevant to the conversation.
+
+Provide these suggestions separated by newlines between the XML tags and . For example:
+
+
+Tell me more about SpaceX and their recent projects
+What is the latest news on SpaceX?
+Who is the CEO of SpaceX?
+
+
+Conversation:
+{chat_history}
+`;
+
+type SuggestionGeneratorInput = {
+ chat_history: BaseMessage[];
+};
+
+const outputParser = new ListLineOutputParser({
+ key: 'suggestions',
+});
+
+const createSuggestionGeneratorChain = (llm: BaseChatModel) => {
+ return RunnableSequence.from([
+ RunnableMap.from({
+ chat_history: (input: SuggestionGeneratorInput) =>
+ formatChatHistoryAsString(input.chat_history),
+ }),
+ PromptTemplate.fromTemplate(suggestionGeneratorPrompt),
+ llm,
+ outputParser,
+ ]);
+};
+
+const generateSuggestions = (
+ input: SuggestionGeneratorInput,
+ llm: BaseChatModel,
+) => {
+ (llm as ChatOpenAI).temperature = 0;
+ const suggestionGeneratorChain = createSuggestionGeneratorChain(llm);
+ return suggestionGeneratorChain.invoke(input);
+};
+
+export default generateSuggestions;
diff --git a/src/lib/huggingfaceTransformer.ts b/src/lib/huggingfaceTransformer.ts
new file mode 100644
index 0000000..7a959ca
--- /dev/null
+++ b/src/lib/huggingfaceTransformer.ts
@@ -0,0 +1,82 @@
+import { Embeddings, type EmbeddingsParams } from '@langchain/core/embeddings';
+import { chunkArray } from '@langchain/core/utils/chunk_array';
+
+export interface HuggingFaceTransformersEmbeddingsParams
+ extends EmbeddingsParams {
+ modelName: string;
+
+ model: string;
+
+ timeout?: number;
+
+ batchSize?: number;
+
+ stripNewLines?: boolean;
+}
+
+export class HuggingFaceTransformersEmbeddings
+ extends Embeddings
+ implements HuggingFaceTransformersEmbeddingsParams
+{
+ modelName = 'Xenova/all-MiniLM-L6-v2';
+
+ model = 'Xenova/all-MiniLM-L6-v2';
+
+ batchSize = 512;
+
+ stripNewLines = true;
+
+ timeout?: number;
+
+ private pipelinePromise: Promise;
+
+ constructor(fields?: Partial) {
+ super(fields ?? {});
+
+ this.modelName = fields?.model ?? fields?.modelName ?? this.model;
+ this.model = this.modelName;
+ this.stripNewLines = fields?.stripNewLines ?? this.stripNewLines;
+ this.timeout = fields?.timeout;
+ }
+
+ async embedDocuments(texts: string[]): Promise {
+ const batches = chunkArray(
+ this.stripNewLines ? texts.map((t) => t.replace(/\n/g, ' ')) : texts,
+ this.batchSize,
+ );
+
+ const batchRequests = batches.map((batch) => this.runEmbedding(batch));
+ const batchResponses = await Promise.all(batchRequests);
+ const embeddings: number[][] = [];
+
+ for (let i = 0; i < batchResponses.length; i += 1) {
+ const batchResponse = batchResponses[i];
+ for (let j = 0; j < batchResponse.length; j += 1) {
+ embeddings.push(batchResponse[j]);
+ }
+ }
+
+ return embeddings;
+ }
+
+ async embedQuery(text: string): Promise {
+ const data = await this.runEmbedding([
+ this.stripNewLines ? text.replace(/\n/g, ' ') : text,
+ ]);
+ return data[0];
+ }
+
+ private async runEmbedding(texts: string[]) {
+ const { pipeline } = await import('@xenova/transformers');
+
+ const pipe = await (this.pipelinePromise ??= pipeline(
+ 'feature-extraction',
+ this.model,
+ ));
+
+ return this.caller.call(async () => {
+ const output = await pipe(texts, { pooling: 'mean', normalize: true });
+ return output.tolist();
+ });
+ }
+}
diff --git a/src/lib/outputParsers/listLineOutputParser.ts b/src/lib/outputParsers/listLineOutputParser.ts
new file mode 100644
index 0000000..57a9bbc
--- /dev/null
+++ b/src/lib/outputParsers/listLineOutputParser.ts
@@ -0,0 +1,43 @@
+import { BaseOutputParser } from '@langchain/core/output_parsers';
+
+interface LineListOutputParserArgs {
+ key?: string;
+}
+
+class LineListOutputParser extends BaseOutputParser {
+ private key = 'questions';
+
+ constructor(args?: LineListOutputParserArgs) {
+ super();
+ this.key = args.key ?? this.key;
+ }
+
+ static lc_name() {
+ return 'LineListOutputParser';
+ }
+
+ lc_namespace = ['langchain', 'output_parsers', 'line_list_output_parser'];
+
+ async parse(text: string): Promise {
+ const regex = /^(\s*(-|\*|\d+\.\s|\d+\)\s|\u2022)\s*)+/;
+ const startKeyIndex = text.indexOf(`<${this.key}>`);
+ const endKeyIndex = text.indexOf(`${this.key}>`);
+ const questionsStartIndex =
+ startKeyIndex === -1 ? 0 : startKeyIndex + `<${this.key}>`.length;
+ const questionsEndIndex = endKeyIndex === -1 ? text.length : endKeyIndex;
+ const lines = text
+ .slice(questionsStartIndex, questionsEndIndex)
+ .trim()
+ .split('\n')
+ .filter((line) => line.trim() !== '')
+ .map((line) => line.replace(regex, ''));
+
+ return lines;
+ }
+
+ getFormatInstructions(): string {
+ throw new Error('Not implemented.');
+ }
+}
+
+export default LineListOutputParser;
diff --git a/src/lib/providers.ts b/src/lib/providers.ts
index 77c1e83..74aa198 100644
--- a/src/lib/providers.ts
+++ b/src/lib/providers.ts
@@ -2,6 +2,7 @@ import { ChatOpenAI, OpenAIEmbeddings } from '@langchain/openai';
import { ChatOllama } from '@langchain/community/chat_models/ollama';
import { VertexAI } from "@langchain/google-vertexai";
import { OllamaEmbeddings } from '@langchain/community/embeddings/ollama';
+import { HuggingFaceTransformersEmbeddings } from './huggingfaceTransformer';
import { hasGCPCredentials } from '../auth';
import {
getGroqApiKey,
@@ -35,6 +36,11 @@ export const getAvailableChatModelProviders = async () => {
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}`);
@@ -180,5 +186,21 @@ export const getAvailableEmbeddingModelProviders = async () => {
}
}
+ 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/routes/index.ts b/src/routes/index.ts
index 04390cd..257e677 100644
--- a/src/routes/index.ts
+++ b/src/routes/index.ts
@@ -3,6 +3,7 @@ import imagesRouter from './images';
import videosRouter from './videos';
import configRouter from './config';
import modelsRouter from './models';
+import suggestionsRouter from './suggestions';
const router = express.Router();
@@ -10,5 +11,6 @@ router.use('/images', imagesRouter);
router.use('/videos', videosRouter);
router.use('/config', configRouter);
router.use('/models', modelsRouter);
+router.use('/suggestions', suggestionsRouter);
export default router;
diff --git a/src/routes/suggestions.ts b/src/routes/suggestions.ts
new file mode 100644
index 0000000..10e5715
--- /dev/null
+++ b/src/routes/suggestions.ts
@@ -0,0 +1,46 @@
+import express from 'express';
+import generateSuggestions from '../agents/suggestionGeneratorAgent';
+import { BaseChatModel } from '@langchain/core/language_models/chat_models';
+import { getAvailableChatModelProviders } from '../lib/providers';
+import { HumanMessage, AIMessage } from '@langchain/core/messages';
+import logger from '../utils/logger';
+
+const router = express.Router();
+
+router.post('/', async (req, res) => {
+ try {
+ let { chat_history, chat_model, chat_model_provider } = req.body;
+
+ chat_history = chat_history.map((msg: any) => {
+ if (msg.role === 'user') {
+ return new HumanMessage(msg.content);
+ } else if (msg.role === 'assistant') {
+ return new AIMessage(msg.content);
+ }
+ });
+
+ const chatModels = await getAvailableChatModelProviders();
+ const provider = chat_model_provider || Object.keys(chatModels)[0];
+ const chatModel = chat_model || Object.keys(chatModels[provider])[0];
+
+ let llm: BaseChatModel | undefined;
+
+ if (chatModels[provider] && chatModels[provider][chatModel]) {
+ llm = chatModels[provider][chatModel] as BaseChatModel | undefined;
+ }
+
+ if (!llm) {
+ res.status(500).json({ message: 'Invalid LLM model selected' });
+ return;
+ }
+
+ const suggestions = await generateSuggestions({ chat_history }, llm);
+
+ res.status(200).json({ suggestions: suggestions });
+ } catch (err) {
+ res.status(500).json({ message: 'An error has occurred.' });
+ logger.error(`Error in generating suggestions: ${err.message}`);
+ }
+});
+
+export default router;
diff --git a/tsconfig.json b/tsconfig.json
index 5bdba67..48e6042 100644
--- a/tsconfig.json
+++ b/tsconfig.json
@@ -1,7 +1,8 @@
{
"compilerOptions": {
"lib": ["ESNext"],
- "module": "commonjs",
+ "module": "Node16",
+ "moduleResolution": "Node16",
"target": "ESNext",
"outDir": "dist",
"sourceMap": false,
diff --git a/ui/app/page.tsx b/ui/app/page.tsx
index 982763a..e18aca9 100644
--- a/ui/app/page.tsx
+++ b/ui/app/page.tsx
@@ -1,5 +1,6 @@
import ChatWindow from '@/components/ChatWindow';
import { Metadata } from 'next';
+import { Suspense } from 'react';
export const metadata: Metadata = {
title: 'Chat - Perplexica',
@@ -9,7 +10,9 @@ export const metadata: Metadata = {
const Home = () => {
return (