├── .env.example.txt ├── .eslintrc.json ├── .gitignore ├── README.md ├── docs ├── doc.html └── doc_files │ ├── image001.png │ ├── image002.jpg │ ├── image003.jpg │ ├── image004.png │ ├── image005.jpg │ ├── image006.png │ └── image007.jpg ├── next.config.js ├── package-lock.json ├── package.json ├── prisma └── schema.prisma ├── public ├── favicon.ico ├── next.svg ├── thirteen.svg └── vercel.svg ├── src ├── crawler.ts └── pages │ ├── _app.tsx │ ├── _document.tsx │ ├── api │ ├── chat.ts │ ├── conversationLog.ts │ ├── crawl.ts │ ├── createTokenRequest.ts │ └── database.js │ └── index.tsx └── tsconfig.json /.env.example.txt: -------------------------------------------------------------------------------- 1 | OPENAI_API_KEY=sk-ltjEScc6ot0DSoCQt2eUT3BlbkFJo111111111111111 2 | PINECONE_API_KEY=051a516d-46b2-4200-81111111111 3 | PINECONE_ENVIRONMENT=northamerica-northeast1-gcp 4 | PINECONE_INDEX=chatbot-index 5 | DATABASE_URL=mongodb+srv://USERNAME:PASSWORD@cluster0.xd1ua.mongodb.net/conversations?retryWrites=true&w=majority 6 | ABLY_API_KEY=QzvNyQ.2RDnbw:0E0Av7lCsCwcos5ZEwA3Rdi11111111111111111 7 | API_ROOT=http://localhost:3000 -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "next/core-web-vitals" 3 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # next.js 12 | /.next/ 13 | /out/ 14 | 15 | # production 16 | /build 17 | 18 | .env 19 | # misc 20 | .DS_Store 21 | *.pem 22 | 23 | # debug 24 | npm-debug.log* 25 | yarn-debug.log* 26 | yarn-error.log* 27 | 28 | # local env files 29 | .env*.local 30 | 31 | # vercel 32 | .vercel 33 | 34 | # typescript 35 | *.tsbuildinfo 36 | next-env.d.ts 37 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Multi-User Chatbot with Langchain and Pinecone in Next.JS 2 | 3 | This is a Multi-User Chatbot with Langchain and pinecone in Next.JS. The concept of this application is very similar to my Node.js [LangChain Document Helper](https://github.com/Ashot72/LangChain-Documentation-Helper) application. You can read about LangChain, Vector database such as Pinecone, embeddings etc., in the Langchain Document Helper app. 4 | 5 | As a cloud-based database, [MongoDB](https://www.mongodb.com/atlas/database) is used with [Prisma ORM](https://www.prisma.io/ ). 6 | 7 | We can crawl a single site or multiple sites. The sites can be of the same domain, and the app will not crawl the pages that have already been crawled. You can also specify different domains with commas. In this application I will crawl a single site which is [Lightning Tools](https://lightningtools.com/) the company I work for. 8 | 9 | ``` 10 | Crawling samples 11 | 12 | http://localhost:3000/api/crawl?urls=https://lightningtools.com/&limit=1000 13 | 14 | http://localhost:3000/api/crawl?urls=https://lightningtools.com/,https://lightningtools.com/about-us&limit=1000 15 | 16 | http://localhost:3000/api/crawl?urls=https://lightningtools.com/,https://www.microsoft.com/&limit=1000 17 | 18 | ``` 19 | LLMs are stateless, which means they have no concept of memory. That means that they do not maintain the chain of conversation on their own. We need to build a mechanism that will maintain conversation history that will be part of the context for each response we get back from the chatbox. For that reason, we use [ably](https://ably.com/) 20 | 21 | When you start the chat, you must specify a username. This name should be unique in general, as with this name you can access the entire conversation log and also display it on a screen. In a real app, a user must be authenticated, and username can be their unique email address, ensuring no two users can have the same usernames. 22 | 23 | To get started. 24 | ``` 25 | Clone the repository 26 | 27 | git clone https://github.com/Ashot72/AI-Chatbot 28 | cd AI-Chatbot 29 | 30 | Add .env file base on env.example.txt file and add respective keys 31 | 32 | # installs dependencies 33 | npm install 34 | 35 | # to run locally 36 | npm run dev 37 | 38 | ``` 39 | 40 | Go to [AI Chatbot Video](https://youtu.be/TkZCDJJrQqw) page 41 | 42 | Go to [AI Chatbot Description](https://ashot72.github.io/AI-Chatbot/doc.html) page 43 | -------------------------------------------------------------------------------- /docs/doc.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 | 5 | 6 | 39 | 40 | 41 | 42 | 43 | 44 |AI Chatbot
48 | 49 |Video Link: https://youtu.be/TkZCDJJrQqw
52 | 53 |GitHub 54 | Repository: https://github.com/Ashot72/AI-Chatbot
56 | 57 |58 | 59 |
This Next.js 60 | application is very similar to my Langchain Document Helper https://github.com/Ashot72/LangChain-Documentation-Helper application. You can
64 | 65 |read about 66 | LangChain, Vector database such as Pinecone, embeddings etc.
67 | 68 |Figure 1
72 | 73 |We should 74 | crawl a single site or multiple sites. The sites can be of the same domain, and 75 | the app will not crawl the pages that have already been crawled. You can also 76 | specify
77 | 78 |different domains 79 | with commas. In this application I will crawl a single site which is https://lightningtools.com/ the company I work for.
83 | 84 |85 | 86 |
Figure 2
90 | 91 |We crawl the 92 | site and put into Pinecone vector database.
93 | 94 |95 | 96 |
Figure 3
100 | 101 |You can look 102 | at crawl.ts and crawler.ts files for crawling.
103 | 104 |LLMs are stateless, 105 | which means they have no concept of memory. That means that they do not 106 | maintain the chain of conversation on their own. We need to build
107 | 108 |a mechanism that 109 | will maintain conversation history that will be part of the context for each 110 | response we get back from the chatbox.
111 | 112 |For that reason, 113 | we use ably https://ably.com/
115 | 116 |Ably is a 117 | real-time delivery platform that provides infrastructure and APIs for 118 | developers to build scalable and reliable real-time applications. It can be 119 | used to handle real-time
120 | 121 |communication, 122 | data synchronization, and messaging across various platforms and devices. As 123 | out chatbot gains more users, the number of messages exchanged between the bot
124 | 125 |and the 126 | users will increase. Ably is built to handle such growth in traffic without any 127 | performance degradation.
128 | 129 |Ably also 130 | ensures message delivery and provides message history, even in cases of 131 | temporary disconnections or network issues. Implementing this level of 132 | reliability using only
133 | 134 |WebSockets 135 | can be challenging and time-consuming.
136 | 137 |Ably also 138 | provides built-in security features like token-based authentication and 139 | fine-grained access control, simplifying the process of securing chatbot's 141 | real-time communication.
142 | 143 |144 | 145 |
Figure 4
149 | 150 |You should 151 | go to Ably site and create a free API KEY.
152 | 153 |154 | 155 |
Figure 5
159 | 160 |When you 161 | start the chat, you must specify a username. This name should be unique in 162 | general, as with this name you can access the entire conversation log
163 | 164 |and also 165 | display it on a screen. In a real app, a user must be authenticated, and 166 | username can be their unique email address, ensuring no two users can have the 167 | same usernames.
168 | 169 |170 | 171 |
Figure 6
175 | 176 |We use https://www.mongodb.com/atlas/database a MongoDB cloud database, to store 180 | and register each user conversation. This allows you to enhance the app and
181 | 182 |display 183 | users' conversation logs on the screen.
185 | 186 |187 | 188 |
Figure 7
192 | 193 |We use 194 | Prisma ORM https://www.prisma.io/ to access MongoDB.
197 | 198 |199 | 200 |
201 | 202 |
203 | 204 |
205 | 206 |
207 | 208 |