diff --git a/app/api/chat/route.js b/app/api/chat/route.js index 61b9d21b..40262e74 100644 --- a/app/api/chat/route.js +++ b/app/api/chat/route.js @@ -4,11 +4,21 @@ import { RunnableBranch, RunnableSequence } from "@langchain/core/runnables"; import { PromptTemplate } from "@langchain/core/prompts"; import { StringOutputParser } from "@langchain/core/output_parsers"; import { formatDocumentsAsString } from "langchain/util/document"; -import { RedisVectorStore } from "@langchain/community/vectorstores/redis"; +import { RedisVectorStore } from "@langchain/redis"; import { createClient } from "redis"; import { ContextualCompressionRetriever } from "langchain/retrievers/contextual_compression"; import { EmbeddingsFilter } from "langchain/retrievers/document_compressors/embeddings_filter"; +// import { Schema } from "@langchain/core"; +import { RedisByteStore } from "@langchain/community/storage/ioredis"; +const customSchema = { + id: "CustomSchema", + properties: { + title: { type: "string" }, + url: { type: "string" }, + file: { type: "string" }, + }, +}; // WARNING: PLEASE DO NOT USE Langsmith when using production data // as this has not been checked and approved. // Code for using Langsmith @@ -60,13 +70,18 @@ export async function POST(req) { url: process.env.REDIS_URL ?? `redis://${REDIS_HOST}:6379`, }); await redisClient.connect(); - console.log("Successfully connect to Redis"); + console.log("Successfully connected to Redis"); const baseCompressor = new EmbeddingsFilter({ embeddings: new OpenAIEmbeddings(), similarityThreshold: SIMILARITY_THRESHOLD, }); + const redisByteStore = new RedisByteStore({ + client: redisClient, + schema: customSchema, + }); + const vectorStore = new RedisVectorStore(new OpenAIEmbeddings(), { redisClient: redisClient, indexName: process.env.INDEX_NAME, @@ -111,6 +126,7 @@ export async function POST(req) { const extractDocSource = async (currentQuestion) => { // code to extract sources from retrieverResult relevantDocs = await retriever.getRelevantDocuments(currentQuestion); + console.log("Relevant Docs: ", relevantDocs); return relevantDocs; }; diff --git a/package-lock.json b/package-lock.json index d7e9fb14..cfe2efef 100644 --- a/package-lock.json +++ b/package-lock.json @@ -16,7 +16,9 @@ "@fortawesome/free-brands-svg-icons": "^6.3.0", "@fortawesome/free-solid-svg-icons": "^6.3.0", "@fortawesome/react-fontawesome": "^0.2.0", - "@langchain/core": "^0.1.18", + "@langchain/community": "^0.0.40", + "@langchain/core": "^0.1.48", + "@langchain/redis": "^0.0.2", "@mdx-js/loader": "^2.3.0", "@mdx-js/mdx": "^2.3.0", "@mdx-js/react": "^2.3.0", @@ -3851,14 +3853,14 @@ "dev": true }, "node_modules/@langchain/community": { - "version": "0.0.20", - "resolved": "https://registry.npmjs.org/@langchain/community/-/community-0.0.20.tgz", - "integrity": "sha512-maPMjvF50Z+4eMs7HKmY3wfT+k6IjULqLUVPtVdN1zSGobRvnUIbQMKUY2IXVTZmaMXKBAIob+49X8vjO2snDQ==", + "version": "0.0.40", + "resolved": "https://registry.npmjs.org/@langchain/community/-/community-0.0.40.tgz", + "integrity": "sha512-a+uZ1+FyZ2tLBybAf1bPc+gm/8OgYLU+Z7h/r41ThRTHbMlnT7hiC8LFHX/zsm9H7mZ/dbpRSBuz/MFxS/TlBA==", "dependencies": { - "@langchain/core": "~0.1.16", - "@langchain/openai": "~0.0.10", + "@langchain/core": "~0.1.44", + "@langchain/openai": "~0.0.19", "flat": "^5.0.2", - "langsmith": "~0.0.48", + "langsmith": "~0.1.1", "uuid": "^9.0.0", "zod": "^3.22.3" }, @@ -3875,9 +3877,10 @@ "@aws-sdk/client-sagemaker-runtime": "^3.310.0", "@aws-sdk/client-sfn": "^3.310.0", "@aws-sdk/credential-provider-node": "^3.388.0", + "@azure/search-documents": "^12.0.0", "@clickhouse/client": "^0.2.5", - "@cloudflare/ai": "^1.0.12", - "@datastax/astra-db-ts": "0.1.2", + "@cloudflare/ai": "*", + "@datastax/astra-db-ts": "^0.1.4", "@elastic/elasticsearch": "^8.4.0", "@getmetal/metal-sdk": "*", "@getzep/zep-js": "^0.9.0", @@ -3888,7 +3891,7 @@ "@huggingface/inference": "^2.6.4", "@mozilla/readability": "*", "@opensearch-project/opensearch": "*", - "@pinecone-database/pinecone": "^1.1.0", + "@pinecone-database/pinecone": "*", "@planetscale/database": "^1.8.0", "@qdrant/js-client-rest": "^1.2.0", "@raycast/api": "^1.55.2", @@ -3903,32 +3906,37 @@ "@tensorflow/tfjs-converter": "*", "@tensorflow/tfjs-core": "*", "@upstash/redis": "^1.20.6", + "@upstash/vector": "^1.0.2", "@vercel/kv": "^0.2.3", "@vercel/postgres": "^0.5.0", "@writerai/writer-sdk": "^0.40.2", "@xata.io/client": "^0.28.0", "@xenova/transformers": "^2.5.4", "@zilliz/milvus2-sdk-node": ">=2.2.7", + "better-sqlite3": "^9.4.0", "cassandra-driver": "^4.7.2", "chromadb": "*", - "closevector-common": "0.1.0-alpha.1", - "closevector-node": "0.1.0-alpha.10", - "closevector-web": "0.1.0-alpha.16", - "cohere-ai": ">=6.0.0", + "closevector-common": "0.1.3", + "closevector-node": "0.1.6", + "closevector-web": "0.1.6", + "cohere-ai": "*", "convex": "^1.3.1", + "couchbase": "^4.3.0", "discord.js": "^14.14.1", + "dria": "^0.0.3", "faiss-node": "^0.5.1", - "firebase-admin": "^11.9.0", + "firebase-admin": "^11.9.0 || ^12.0.0", "google-auth-library": "^8.9.0", "googleapis": "^126.0.1", "hnswlib-node": "^1.4.2", "html-to-text": "^9.0.5", "ioredis": "^5.3.2", "jsdom": "*", + "jsonwebtoken": "^9.0.2", "llmonitor": "^0.5.9", "lodash": "^4.17.21", "lunary": "^0.6.11", - "mongodb": "^5.2.0", + "mongodb": ">=5.2.0", "mysql2": "^3.3.3", "neo4j-driver": "*", "node-llama-cpp": "*", @@ -3936,14 +3944,14 @@ "pg-copy-streams": "^6.0.5", "pickleparser": "^0.2.1", "portkey-ai": "^0.1.11", - "redis": "^4.6.4", + "redis": "*", "replicate": "^0.18.0", "typeorm": "^0.3.12", "typesense": "^1.5.3", "usearch": "^1.1.1", "vectordb": "^0.1.4", "voy-search": "0.6.2", - "weaviate-ts-client": "^1.4.0", + "weaviate-ts-client": "*", "web-auth-library": "^1.0.3", "ws": "^8.14.2" }, @@ -3975,6 +3983,9 @@ "@aws-sdk/credential-provider-node": { "optional": true }, + "@azure/search-documents": { + "optional": true + }, "@clickhouse/client": { "optional": true }, @@ -4059,6 +4070,9 @@ "@upstash/redis": { "optional": true }, + "@upstash/vector": { + "optional": true + }, "@vercel/kv": { "optional": true }, @@ -4077,6 +4091,9 @@ "@zilliz/milvus2-sdk-node": { "optional": true }, + "better-sqlite3": { + "optional": true + }, "cassandra-driver": { "optional": true }, @@ -4098,9 +4115,15 @@ "convex": { "optional": true }, + "couchbase": { + "optional": true + }, "discord.js": { "optional": true }, + "dria": { + "optional": true + }, "faiss-node": { "optional": true }, @@ -4125,6 +4148,9 @@ "jsdom": { "optional": true }, + "jsonwebtoken": { + "optional": true + }, "llmonitor": { "optional": true }, @@ -4190,16 +4216,36 @@ } } }, + "node_modules/@langchain/community/node_modules/commander": { + "version": "10.0.1", + "resolved": "https://registry.npmjs.org/commander/-/commander-10.0.1.tgz", + "integrity": "sha512-y4Mg2tXshplEbSGzx7amzPwKKOCGuoSRP/CjEdwwk0FOGlUbq6lKuoyDZTNZkmxHdJtp54hdfY/JUrdL7Xfdug==", + "engines": { + "node": ">=14" + } + }, + "node_modules/@langchain/community/node_modules/langsmith": { + "version": "0.1.13", + "resolved": "https://registry.npmjs.org/langsmith/-/langsmith-0.1.13.tgz", + "integrity": "sha512-iyGrsaWhZ70F1aG8T8Nd4iH33Z0JFMdxbfBbaRV/+LkJDH4PByZHNJbApT6G2pQmmYD0cei9oW7kXp89N5SXXQ==", + "dependencies": { + "@types/uuid": "^9.0.1", + "commander": "^10.0.1", + "p-queue": "^6.6.2", + "p-retry": "4", + "uuid": "^9.0.0" + } + }, "node_modules/@langchain/core": { - "version": "0.1.18", - "resolved": "https://registry.npmjs.org/@langchain/core/-/core-0.1.18.tgz", - "integrity": "sha512-fDjocs899HDq3nf3PE0TveQoVabO0vyedt9i74Yef4Ds2o22TEDSh3MfoMxM8stcHiV3/9i+0wH4Xdm0E2YLdg==", + "version": "0.1.48", + "resolved": "https://registry.npmjs.org/@langchain/core/-/core-0.1.48.tgz", + "integrity": "sha512-kGggyDbaYzCIPGkzvMvm/v0+lcTy1jlX6QZ7PSzUQFYJg5JK399x3AOYIDkbbUVxBAyHRgrWlxVQXH0FW3N6Bg==", "dependencies": { "ansi-styles": "^5.0.0", "camelcase": "6", "decamelize": "1.2.0", "js-tiktoken": "^1.0.8", - "langsmith": "~0.0.48", + "langsmith": "~0.1.7", "ml-distance": "^4.0.0", "p-queue": "^6.6.2", "p-retry": "4", @@ -4233,27 +4279,51 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/@langchain/core/node_modules/commander": { + "version": "10.0.1", + "resolved": "https://registry.npmjs.org/commander/-/commander-10.0.1.tgz", + "integrity": "sha512-y4Mg2tXshplEbSGzx7amzPwKKOCGuoSRP/CjEdwwk0FOGlUbq6lKuoyDZTNZkmxHdJtp54hdfY/JUrdL7Xfdug==", + "engines": { + "node": ">=14" + } + }, + "node_modules/@langchain/core/node_modules/langsmith": { + "version": "0.1.13", + "resolved": "https://registry.npmjs.org/langsmith/-/langsmith-0.1.13.tgz", + "integrity": "sha512-iyGrsaWhZ70F1aG8T8Nd4iH33Z0JFMdxbfBbaRV/+LkJDH4PByZHNJbApT6G2pQmmYD0cei9oW7kXp89N5SXXQ==", + "dependencies": { + "@types/uuid": "^9.0.1", + "commander": "^10.0.1", + "p-queue": "^6.6.2", + "p-retry": "4", + "uuid": "^9.0.0" + } + }, "node_modules/@langchain/openai": { - "version": "0.0.12", - "resolved": "https://registry.npmjs.org/@langchain/openai/-/openai-0.0.12.tgz", - "integrity": "sha512-MR9x1xRXwJpdYlVx9Tga89q/MvxPrSTYyA5vy9tQ8dfQHNWnlgmI4gB/hDIsWUu1ooScagD4wW+aTnohTX+g+g==", + "version": "0.0.21", + "resolved": "https://registry.npmjs.org/@langchain/openai/-/openai-0.0.21.tgz", + "integrity": "sha512-p3BmYmPhoOxSWxuuvjw0vtzJMCfnGnp+cBK9huIcu2ds7DX8OsVNgPhFf7xPodl5fzz142pfjOHQkemqFBo6MA==", "dependencies": { - "@langchain/core": "~0.1.13", + "@langchain/core": "~0.1.45", "js-tiktoken": "^1.0.7", - "openai": "^4.24.2", - "zod": "^3.22.3", - "zod-to-json-schema": "3.20.3" + "openai": "^4.26.0", + "zod": "^3.22.4", + "zod-to-json-schema": "^3.22.3" }, "engines": { "node": ">=18" } }, - "node_modules/@langchain/openai/node_modules/zod-to-json-schema": { - "version": "3.20.3", - "resolved": "https://registry.npmjs.org/zod-to-json-schema/-/zod-to-json-schema-3.20.3.tgz", - "integrity": "sha512-/Q3wnyxAfCt94ZcrGiXXoiAfRqasxl9CX64LZ9fj+4dKH68zulUtU0uk1WMxQPfAxQ0ZI70dKzcoW7hHj+DwSQ==", - "peerDependencies": { - "zod": "^3.20.0" + "node_modules/@langchain/redis": { + "version": "0.0.2", + "resolved": "https://registry.npmjs.org/@langchain/redis/-/redis-0.0.2.tgz", + "integrity": "sha512-kDaW4JotKH4J9QxcsRP0onydxz8QgGD1bYosdpzuZ2a7kYcDeJWMqapR0eFFjOznemgyn27jxUFGG0GhvI3pYA==", + "dependencies": { + "@langchain/core": "~0.1", + "redis": "^4.6.13" + }, + "engines": { + "node": ">=18" } }, "node_modules/@lexical/clipboard": { @@ -6969,9 +7039,9 @@ } }, "node_modules/@redis/client": { - "version": "1.5.12", - "resolved": "https://registry.npmjs.org/@redis/client/-/client-1.5.12.tgz", - "integrity": "sha512-/ZjE18HRzMd80eXIIUIPcH81UoZpwulbo8FmbElrjPqH0QC0SeIKu1BOU49bO5trM5g895kAjhvalt5h77q+4A==", + "version": "1.5.14", + "resolved": "https://registry.npmjs.org/@redis/client/-/client-1.5.14.tgz", + "integrity": "sha512-YGn0GqsRBFUQxklhY7v562VMOP0DcmlrHHs3IV1mFE3cbxe31IITUkqhBcIhVSI/2JqtWAJXg5mjV4aU+zD0HA==", "dependencies": { "cluster-key-slot": "1.1.2", "generic-pool": "3.9.0", @@ -19873,14 +19943,20 @@ } }, "node_modules/jsonwebtoken": { - "version": "9.0.0", - "resolved": "https://registry.npmjs.org/jsonwebtoken/-/jsonwebtoken-9.0.0.tgz", - "integrity": "sha512-tuGfYXxkQGDPnLJ7SibiQgVgeDgfbPq2k2ICcbgqW8WxWLBAxKQM/ZCu/IT8SOSwmaYl4dpTFCW5xZv7YbbWUw==", + "version": "9.0.2", + "resolved": "https://registry.npmjs.org/jsonwebtoken/-/jsonwebtoken-9.0.2.tgz", + "integrity": "sha512-PRp66vJ865SSqOlgqS8hujT5U4AOgMfhrwYIuIhfKaoSCZcirrmASQr8CX7cUg+RMih+hgznrjp99o+W4pJLHQ==", "dependencies": { "jws": "^3.2.2", - "lodash": "^4.17.21", + "lodash.includes": "^4.3.0", + "lodash.isboolean": "^3.0.3", + "lodash.isinteger": "^4.0.4", + "lodash.isnumber": "^3.0.3", + "lodash.isplainobject": "^4.0.6", + "lodash.isstring": "^4.0.1", + "lodash.once": "^4.0.0", "ms": "^2.1.1", - "semver": "^7.3.8" + "semver": "^7.5.4" }, "engines": { "node": ">=12", @@ -20405,34 +20481,57 @@ "integrity": "sha512-TM9YBvyC84ZxE3rgfefxUWiQKLilstD6k7PTGt6wfbtXF8ixIJLOL3VYyV/z+ZiPLsVxAsKAFVwWlWeb2Y8Yyw==", "dev": true }, + "node_modules/lodash.includes": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/lodash.includes/-/lodash.includes-4.3.0.tgz", + "integrity": "sha512-W3Bx6mdkRTGtlJISOvVD/lbqjTlPPUDTMnlXZFnVwi9NKJ6tiAk6LVdlhZMm17VZisqhKcgzpO5Wz91PCt5b0w==" + }, "node_modules/lodash.isarguments": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/lodash.isarguments/-/lodash.isarguments-3.1.0.tgz", "integrity": "sha512-chi4NHZlZqZD18a0imDHnZPrDeBbTtVN7GXMwuGdRH9qotxAjYs3aVLKc7zNOG9eddR5Ksd8rvFEBc9SsggPpg==" }, + "node_modules/lodash.isboolean": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/lodash.isboolean/-/lodash.isboolean-3.0.3.tgz", + "integrity": "sha512-Bz5mupy2SVbPHURB98VAcw+aHh4vRV5IPNhILUCsOzRmsTmSQ17jIuqopAentWoehktxGd9e/hbIXq980/1QJg==" + }, + "node_modules/lodash.isinteger": { + "version": "4.0.4", + "resolved": "https://registry.npmjs.org/lodash.isinteger/-/lodash.isinteger-4.0.4.tgz", + "integrity": "sha512-DBwtEWN2caHQ9/imiNeEA5ys1JoRtRfY3d7V9wkqtbycnAmTvRRmbHKDV4a0EYc678/dia0jrte4tjYwVBaZUA==" + }, "node_modules/lodash.ismatch": { "version": "4.4.0", "resolved": "https://registry.npmjs.org/lodash.ismatch/-/lodash.ismatch-4.4.0.tgz", "integrity": "sha512-fPMfXjGQEV9Xsq/8MTSgUf255gawYRbjwMyDbcvDhXgV7enSZA0hynz6vMPnpAb5iONEzBHBPsT+0zes5Z301g==", "dev": true }, + "node_modules/lodash.isnumber": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/lodash.isnumber/-/lodash.isnumber-3.0.3.tgz", + "integrity": "sha512-QYqzpfwO3/CWf3XP+Z+tkQsfaLL/EnUlXWVkIk5FUPc4sBdTehEqZONuyRt2P67PXAk+NXmTBcc97zw9t1FQrw==" + }, "node_modules/lodash.isplainobject": { "version": "4.0.6", "resolved": "https://registry.npmjs.org/lodash.isplainobject/-/lodash.isplainobject-4.0.6.tgz", - "integrity": "sha512-oSXzaWypCMHkPC3NvBEaPHf0KsA5mvPrOPgQWDsbg8n7orZ290M0BmC/jgRZ4vcJ6DTAhjrsSYgdsW/F+MFOBA==", - "dev": true + "integrity": "sha512-oSXzaWypCMHkPC3NvBEaPHf0KsA5mvPrOPgQWDsbg8n7orZ290M0BmC/jgRZ4vcJ6DTAhjrsSYgdsW/F+MFOBA==" }, "node_modules/lodash.isstring": { "version": "4.0.1", "resolved": "https://registry.npmjs.org/lodash.isstring/-/lodash.isstring-4.0.1.tgz", - "integrity": "sha512-0wJxfxH1wgO3GrbuP+dTTk7op+6L41QCXbGINEmD+ny/G/eCqGzxyCsh7159S+mgDDcoarnBw6PC1PS5+wUGgw==", - "dev": true + "integrity": "sha512-0wJxfxH1wgO3GrbuP+dTTk7op+6L41QCXbGINEmD+ny/G/eCqGzxyCsh7159S+mgDDcoarnBw6PC1PS5+wUGgw==" }, "node_modules/lodash.merge": { "version": "4.6.2", "resolved": "https://registry.npmjs.org/lodash.merge/-/lodash.merge-4.6.2.tgz", "integrity": "sha512-0KpjqXRVvrYyCsX1swR/XTK0va6VQkQM6MNo7PqW77ByjAhoARA8EfrP1N4+KlKj8YS0ZUCtRT/YUuhyYDujIQ==" }, + "node_modules/lodash.once": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/lodash.once/-/lodash.once-4.1.1.tgz", + "integrity": "sha512-Sb487aTOCr9drQVL8pIxOzVhafOjZN9UU54hiN8PU3uAiSV7lx1yYNpbNmex2PK6dSJoNTSJUUswT651yww3Mg==" + }, "node_modules/lodash.uniqby": { "version": "4.7.0", "resolved": "https://registry.npmjs.org/lodash.uniqby/-/lodash.uniqby-4.7.0.tgz", @@ -28594,12 +28693,12 @@ } }, "node_modules/redis": { - "version": "4.6.11", - "resolved": "https://registry.npmjs.org/redis/-/redis-4.6.11.tgz", - "integrity": "sha512-kg1Lt4NZLYkAjPOj/WcyIGWfZfnyfKo1Wg9YKVSlzhFwxpFIl3LYI8BWy1Ab963LLDsTz2+OwdsesHKljB3WMQ==", + "version": "4.6.13", + "resolved": "https://registry.npmjs.org/redis/-/redis-4.6.13.tgz", + "integrity": "sha512-MHgkS4B+sPjCXpf+HfdetBwbRz6vCtsceTmw1pHNYJAsYxrfpOP6dz+piJWGos8wqG7qb3vj/Rrc5qOlmInUuA==", "dependencies": { "@redis/bloom": "1.2.0", - "@redis/client": "1.5.12", + "@redis/client": "1.5.14", "@redis/graph": "1.1.1", "@redis/json": "1.0.6", "@redis/search": "1.1.6", diff --git a/package.json b/package.json index ebb2a213..4d526e95 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,9 @@ "@fortawesome/free-brands-svg-icons": "^6.3.0", "@fortawesome/free-solid-svg-icons": "^6.3.0", "@fortawesome/react-fontawesome": "^0.2.0", - "@langchain/core": "^0.1.18", + "@langchain/community": "^0.0.40", + "@langchain/core": "^0.1.48", + "@langchain/redis": "^0.0.2", "@mdx-js/loader": "^2.3.0", "@mdx-js/mdx": "^2.3.0", "@mdx-js/react": "^2.3.0",