From b29126594485e905f54a6fb40ac5ea3b39a62473 Mon Sep 17 00:00:00 2001 From: ItzCrazyKns Date: Sun, 30 Jun 2024 10:42:01 +0530 Subject: [PATCH 01/15] feat(package): add `@langchain/community` --- package.json | 1 + yarn.lock | 135 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 136 insertions(+) diff --git a/package.json b/package.json index 1f6a722..f141b84 100644 --- a/package.json +++ b/package.json @@ -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/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" From 6ae825999a953bf1dc66c6b5dd8c81b4b7741362 Mon Sep 17 00:00:00 2001 From: ItzCrazyKns Date: Sun, 30 Jun 2024 10:45:35 +0530 Subject: [PATCH 02/15] feat(readme): update manual install --- README.md | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 64d2540..da14052 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. From fcf9b644af65218c68a74cd55df1834f5d3b6973 Mon Sep 17 00:00:00 2001 From: ItzCrazyKns <95534749+ItzCrazyKns@users.noreply.github.com> Date: Sun, 30 Jun 2024 12:34:32 +0530 Subject: [PATCH 03/15] Create FUNDING.yml --- .github/FUNDING.yml | 1 + 1 file changed, 1 insertion(+) create mode 100644 .github/FUNDING.yml 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 From ce593daab95c2213ecd4e63047cee9f7ecf769d5 Mon Sep 17 00:00:00 2001 From: ItzCrazyKns <95534749+ItzCrazyKns@users.noreply.github.com> Date: Sun, 30 Jun 2024 12:39:37 +0530 Subject: [PATCH 04/15] Update README.md --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index da14052..acd11f5 100644 --- a/README.md +++ b/README.md @@ -147,11 +147,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 From c74e16e01cbc820f275e59094eea4dd4df90a026 Mon Sep 17 00:00:00 2001 From: ItzCrazyKns Date: Tue, 2 Jul 2024 10:51:47 +0530 Subject: [PATCH 05/15] feat(chats): add delete functionality --- src/routes/chats.ts | 23 +++++++ ui/app/library/page.tsx | 10 ++- ui/components/DeleteChat.tsx | 114 +++++++++++++++++++++++++++++++++++ 3 files changed, 145 insertions(+), 2 deletions(-) create mode 100644 ui/components/DeleteChat.tsx 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/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/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; From 5217d21366ca41f5a4693e43f36b7bb79083359e Mon Sep 17 00:00:00 2001 From: ItzCrazyKns Date: Wed, 3 Jul 2024 20:47:31 +0530 Subject: [PATCH 06/15] feat(dockerfile): revert to `node:slim` --- backend.dockerfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 From 80818983d8af50bfb9c7c3e43c8c073297e54439 Mon Sep 17 00:00:00 2001 From: ItzCrazyKns Date: Wed, 3 Jul 2024 20:49:13 +0530 Subject: [PATCH 07/15] feat(package): bump version --- package.json | 2 +- ui/package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index f141b84..4f2bb32 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": { 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": { From c63c9b5c8ad9f587e6d7fcbaf025d2a1bd69e8a2 Mon Sep 17 00:00:00 2001 From: ItzCrazyKns Date: Wed, 3 Jul 2024 21:02:21 +0530 Subject: [PATCH 08/15] feat(readme): update ollama guide --- README.md | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/README.md b/README.md index acd11f5..9e7f7d8 100644 --- a/README.md +++ b/README.md @@ -111,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. From 25b5dbd63e9733766c2e0a90e3ac13522b43d4a7 Mon Sep 17 00:00:00 2001 From: ItzCrazyKns Date: Sat, 6 Jul 2024 14:19:33 +0530 Subject: [PATCH 09/15] feat(providers): separate each provider --- README.md | 6 +- backend.dockerfile | 2 +- src/lib/providers.ts | 187 ------------------------------ src/lib/providers/groq.ts | 57 +++++++++ src/lib/providers/index.ts | 36 ++++++ src/lib/providers/ollama.ts | 59 ++++++++++ src/lib/providers/openai.ts | 59 ++++++++++ src/lib/providers/transformers.ts | 23 ++++ 8 files changed, 238 insertions(+), 191 deletions(-) delete mode 100644 src/lib/providers.ts create mode 100644 src/lib/providers/groq.ts create mode 100644 src/lib/providers/index.ts create mode 100644 src/lib/providers/ollama.ts create mode 100644 src/lib/providers/openai.ts create mode 100644 src/lib/providers/transformers.ts diff --git a/README.md b/README.md index 9e7f7d8..d1388b0 100644 --- a/README.md +++ b/README.md @@ -145,9 +145,9 @@ If you find Perplexica useful, consider giving us a star on GitHub. This helps m 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! -| Cards | Ethereum | -|---|---| -| https://www.patreon.com/itzcrazykns | Address: `0xB025a84b2F269570Eb8D4b05DEdaA41D8525B6DD` | +| Cards | Ethereum | +| ----------------------------------- | ----------------------------------------------------- | +| https://www.patreon.com/itzcrazykns | Address: `0xB025a84b2F269570Eb8D4b05DEdaA41D8525B6DD` | ## Contribution diff --git a/backend.dockerfile b/backend.dockerfile index 4886573..910aae7 100644 --- a/backend.dockerfile +++ b/backend.dockerfile @@ -1,4 +1,4 @@ -FROM node:slim +FROM node:buster-slim ARG SEARXNG_API_URL diff --git a/src/lib/providers.ts b/src/lib/providers.ts deleted file mode 100644 index 3223193..0000000 --- a/src/lib/providers.ts +++ /dev/null @@ -1,187 +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, - getOpenaiApiKey, -} from '../config'; -import logger from '../utils/logger'; - -export const getAvailableChatModelProviders = async () => { - const openAIApiKey = getOpenaiApiKey(); - const groqApiKey = getGroqApiKey(); - const ollamaEndpoint = getOllamaApiEndpoint(); - - 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 response = await fetch(`${ollamaEndpoint}/api/tags`, { - headers: { - 'Content-Type': 'application/json', - }, - }); - - const { models: ollamaModels } = (await response.json()) as any; - - models['ollama'] = ollamaModels.reduce((acc, model) => { - acc[model.model] = new ChatOllama({ - baseUrl: ollamaEndpoint, - model: model.model, - 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 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) { - try { - const response = await fetch(`${ollamaEndpoint}/api/tags`, { - headers: { - 'Content-Type': 'application/json', - }, - }); - - const { models: ollamaModels } = (await response.json()) as any; - - models['ollama'] = ollamaModels.reduce((acc, model) => { - acc[model.model] = new OllamaEmbeddings({ - baseUrl: ollamaEndpoint, - 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..ecdce4d --- /dev/null +++ b/src/lib/providers/groq.ts @@ -0,0 +1,57 @@ +import { ChatOpenAI } from '@langchain/openai'; +import { getGroqApiKey } from '../../config'; +import logger from '../../utils/logger'; + +export const loadGroqChatModels = async () => { + const groqApiKey = getGroqApiKey(); + + 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..5807f94 --- /dev/null +++ b/src/lib/providers/index.ts @@ -0,0 +1,36 @@ +import { loadGroqChatModels } from './groq'; +import { loadOllamaChatModels } from './ollama'; +import { loadOpenAIChatModels, loadOpenAIEmbeddingsModel } from './openai'; +import { loadTransformersEmbeddingsModel } from './transformers'; + +const chatModelProviders = { + openai: loadOpenAIChatModels, + groq: loadGroqChatModels, + ollama: loadOllamaChatModels, +}; + +const embeddingModelProviders = { + openai: loadOpenAIEmbeddingsModel, + local: loadTransformersEmbeddingsModel, + ollama: loadOllamaChatModels, +}; + +export const getAvailableChatModelProviders = async () => { + const models = {}; + + for (const provider in chatModelProviders) { + models[provider] = await chatModelProviders[provider](); + } + + return models; +}; + +export const getAvailableEmbeddingModelProviders = async () => { + const models = {}; + + for (const provider in embeddingModelProviders) { + models[provider] = await embeddingModelProviders[provider](); + } + + return models; +}; diff --git a/src/lib/providers/ollama.ts b/src/lib/providers/ollama.ts new file mode 100644 index 0000000..febe5e8 --- /dev/null +++ b/src/lib/providers/ollama.ts @@ -0,0 +1,59 @@ +import { OllamaEmbeddings } from '@langchain/community/embeddings/ollama'; +import { getOllamaApiEndpoint } from '../../config'; +import logger from '../../utils/logger'; +import { ChatOllama } from '@langchain/community/chat_models/ollama'; + +export const loadOllamaChatModels = async () => { + const ollamaEndpoint = getOllamaApiEndpoint(); + + try { + const response = await fetch(`${ollamaEndpoint}/api/tags`, { + headers: { + 'Content-Type': 'application/json', + }, + }); + + const { models: ollamaModels } = (await response.json()) as any; + + const chatModels = ollamaModels.reduce((acc, model) => { + acc[model.model] = new ChatOllama({ + baseUrl: ollamaEndpoint, + model: model.model, + temperature: 0.7, + }); + return acc; + }, {}); + + return chatModels; + } catch (err) { + logger.error(`Error loading Ollama models: ${err}`); + return {}; + } +}; + +export const loadOpenAIEmbeddingsModel = async () => { + const ollamaEndpoint = getOllamaApiEndpoint(); + + try { + const response = await fetch(`${ollamaEndpoint}/api/tags`, { + headers: { + 'Content-Type': 'application/json', + }, + }); + + const { models: ollamaModels } = (await response.json()) as any; + + const embeddingsModels = ollamaModels.reduce((acc, model) => { + acc[model.model] = new OllamaEmbeddings({ + baseUrl: ollamaEndpoint, + 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..705f1a4 --- /dev/null +++ b/src/lib/providers/openai.ts @@ -0,0 +1,59 @@ +import { ChatOpenAI, OpenAIEmbeddings } from '@langchain/openai'; +import { getOpenaiApiKey } from '../../config'; +import logger from '../../utils/logger'; + +export const loadOpenAIChatModels = async () => { + const openAIApiKey = getOpenaiApiKey(); + + 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 loadOpenAIEmbeddingsModel = async () => { + const openAIApiKey = getOpenaiApiKey(); + + 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..7ef8596 --- /dev/null +++ b/src/lib/providers/transformers.ts @@ -0,0 +1,23 @@ +import logger from '../../utils/logger'; +import { HuggingFaceTransformersEmbeddings } from '../huggingfaceTransformer'; + +export const loadTransformersEmbeddingsModel = 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 {}; + } +}; From 2678c36e448b966e0e18d89e16ddfaa06caf0239 Mon Sep 17 00:00:00 2001 From: ItzCrazyKns Date: Sat, 6 Jul 2024 15:12:51 +0530 Subject: [PATCH 10/15] feat(agents): fix grammar in prompt, closes 239 & 203 --- src/agents/academicSearchAgent.ts | 4 ++-- src/agents/redditSearchAgent.ts | 4 ++-- src/agents/webSearchAgent.ts | 4 ++-- src/agents/wolframAlphaSearchAgent.ts | 4 ++-- src/agents/youtubeSearchAgent.ts | 4 ++-- 5 files changed, 10 insertions(+), 10 deletions(-) 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/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. From f4b58c71575923919936eb441fff581ae2d5eebe Mon Sep 17 00:00:00 2001 From: ItzCrazyKns Date: Sat, 6 Jul 2024 15:13:05 +0530 Subject: [PATCH 11/15] feat(dockerfile): revert base image back to slim --- backend.dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend.dockerfile b/backend.dockerfile index 910aae7..4886573 100644 --- a/backend.dockerfile +++ b/backend.dockerfile @@ -1,4 +1,4 @@ -FROM node:buster-slim +FROM node:slim ARG SEARXNG_API_URL From 3ffb20b777dd037106bdafab043cc582d2a20480 Mon Sep 17 00:00:00 2001 From: ItzCrazyKns Date: Mon, 8 Jul 2024 01:31:11 +0530 Subject: [PATCH 12/15] feat(backend): fix type errors --- src/agents/suggestionGeneratorAgent.ts | 2 +- src/websocket/connectionManager.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) 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/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 ( From 3b4b8a8b0227cee590014f99e10aefd9e1176791 Mon Sep 17 00:00:00 2001 From: ItzCrazyKns Date: Mon, 8 Jul 2024 15:24:45 +0530 Subject: [PATCH 13/15] feat(providers): add `custom_openai` --- src/lib/providers/index.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/lib/providers/index.ts b/src/lib/providers/index.ts index 5807f94..41342b5 100644 --- a/src/lib/providers/index.ts +++ b/src/lib/providers/index.ts @@ -22,6 +22,8 @@ export const getAvailableChatModelProviders = async () => { models[provider] = await chatModelProviders[provider](); } + models['custom_openai'] = {} + return models; }; From 8539ce82ad4b289956fc9c57bd1fb505aa26451d Mon Sep 17 00:00:00 2001 From: ItzCrazyKns Date: Mon, 8 Jul 2024 15:39:27 +0530 Subject: [PATCH 14/15] feat(providers): fix loading issues --- src/lib/providers/groq.ts | 2 ++ src/lib/providers/index.ts | 22 ++++++++++++++-------- src/lib/providers/ollama.ts | 6 +++++- src/lib/providers/openai.ts | 6 +++++- src/lib/providers/transformers.ts | 2 +- 5 files changed, 27 insertions(+), 11 deletions(-) diff --git a/src/lib/providers/groq.ts b/src/lib/providers/groq.ts index ecdce4d..35bd125 100644 --- a/src/lib/providers/groq.ts +++ b/src/lib/providers/groq.ts @@ -5,6 +5,8 @@ import logger from '../../utils/logger'; export const loadGroqChatModels = async () => { const groqApiKey = getGroqApiKey(); + if (!groqApiKey) return {}; + try { const chatModels = { 'LLaMA3 8b': new ChatOpenAI( diff --git a/src/lib/providers/index.ts b/src/lib/providers/index.ts index 41342b5..b1d4502 100644 --- a/src/lib/providers/index.ts +++ b/src/lib/providers/index.ts @@ -1,7 +1,7 @@ import { loadGroqChatModels } from './groq'; -import { loadOllamaChatModels } from './ollama'; -import { loadOpenAIChatModels, loadOpenAIEmbeddingsModel } from './openai'; -import { loadTransformersEmbeddingsModel } from './transformers'; +import { loadOllamaChatModels, loadOllamaEmbeddingsModels } from './ollama'; +import { loadOpenAIChatModels, loadOpenAIEmbeddingsModels } from './openai'; +import { loadTransformersEmbeddingsModels } from './transformers'; const chatModelProviders = { openai: loadOpenAIChatModels, @@ -10,16 +10,19 @@ const chatModelProviders = { }; const embeddingModelProviders = { - openai: loadOpenAIEmbeddingsModel, - local: loadTransformersEmbeddingsModel, - ollama: loadOllamaChatModels, + openai: loadOpenAIEmbeddingsModels, + local: loadTransformersEmbeddingsModels, + ollama: loadOllamaEmbeddingsModels, }; export const getAvailableChatModelProviders = async () => { const models = {}; for (const provider in chatModelProviders) { - models[provider] = await chatModelProviders[provider](); + const providerModels = await chatModelProviders[provider](); + if (Object.keys(providerModels).length > 0) { + models[provider] = providerModels + } } models['custom_openai'] = {} @@ -31,7 +34,10 @@ export const getAvailableEmbeddingModelProviders = async () => { const models = {}; for (const provider in embeddingModelProviders) { - models[provider] = await embeddingModelProviders[provider](); + 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 index febe5e8..b2901ff 100644 --- a/src/lib/providers/ollama.ts +++ b/src/lib/providers/ollama.ts @@ -6,6 +6,8 @@ import { ChatOllama } from '@langchain/community/chat_models/ollama'; export const loadOllamaChatModels = async () => { const ollamaEndpoint = getOllamaApiEndpoint(); + if (!ollamaEndpoint) return {}; + try { const response = await fetch(`${ollamaEndpoint}/api/tags`, { headers: { @@ -31,9 +33,11 @@ export const loadOllamaChatModels = async () => { } }; -export const loadOpenAIEmbeddingsModel = async () => { +export const loadOllamaEmbeddingsModels = async () => { const ollamaEndpoint = getOllamaApiEndpoint(); + if (!ollamaEndpoint) return {}; + try { const response = await fetch(`${ollamaEndpoint}/api/tags`, { headers: { diff --git a/src/lib/providers/openai.ts b/src/lib/providers/openai.ts index 705f1a4..afc7ab8 100644 --- a/src/lib/providers/openai.ts +++ b/src/lib/providers/openai.ts @@ -5,6 +5,8 @@ import logger from '../../utils/logger'; export const loadOpenAIChatModels = async () => { const openAIApiKey = getOpenaiApiKey(); + if (!openAIApiKey) return {}; + try { const chatModels = { 'GPT-3.5 turbo': new ChatOpenAI({ @@ -36,9 +38,11 @@ export const loadOpenAIChatModels = async () => { } }; -export const loadOpenAIEmbeddingsModel = async () => { +export const loadOpenAIEmbeddingsModels = async () => { const openAIApiKey = getOpenaiApiKey(); + if (!openAIApiKey) return {}; + try { const embeddingModels = { 'Text embedding 3 small': new OpenAIEmbeddings({ diff --git a/src/lib/providers/transformers.ts b/src/lib/providers/transformers.ts index 7ef8596..0ec7052 100644 --- a/src/lib/providers/transformers.ts +++ b/src/lib/providers/transformers.ts @@ -1,7 +1,7 @@ import logger from '../../utils/logger'; import { HuggingFaceTransformersEmbeddings } from '../huggingfaceTransformer'; -export const loadTransformersEmbeddingsModel = async () => { +export const loadTransformersEmbeddingsModels = async () => { try { const embeddingModels = { 'BGE Small': new HuggingFaceTransformersEmbeddings({ From 27e6f5b9e1adeda5aeb5665a366d21d03cb07f30 Mon Sep 17 00:00:00 2001 From: ItzCrazyKns Date: Tue, 9 Jul 2024 16:21:45 +0530 Subject: [PATCH 15/15] feat(chat-window): unselect unavailable model --- ui/components/ChatWindow.tsx | 49 ++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) 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);