├── .gitignore
├── LICENSE
├── README.md
├── package-lock.json
├── package.json
├── public
├── favicon.ico
├── index.html
├── logo192.png
├── logo512.png
├── manifest.json
└── robots.txt
├── src
├── App.css
├── App.js
├── App.test.js
├── components
│ ├── ActionProvider.js
│ ├── Config.js
│ ├── LangchainProcessor.js
│ ├── MessageParser.js
│ └── langchain_options
│ │ ├── AWSRoute.js
│ │ ├── CloudflareWorkersRoute.js
│ │ ├── LLMChat.js
│ │ └── SimpleChain.js
├── index.css
├── index.js
├── logo.svg
├── reportWebVitals.js
└── setupTests.js
└── tailwind.config.js
/.gitignore:
--------------------------------------------------------------------------------
1 | # dependencies
2 | /node_modules
3 | /.pnp
4 | .pnp.js
5 |
6 | # testing
7 | /coverage
8 |
9 | # production
10 | /build
11 |
12 | # misc
13 | .DS_Store
14 | .env
15 | .env.local
16 | .env.development.local
17 | .env.test.local
18 | .env.production.local
19 |
20 | # logs
21 | npm-debug.log*
22 | yarn-debug.log*
23 | yarn-error.log*
24 |
25 | # IDEs and editors
26 | .vscode/
27 | .idea/
28 | *.swp
29 | *.swo
30 | *.swn
31 |
32 | # OS-specific
33 | *.DS_Store
34 | *.log
35 | *.tmp
36 | Thumbs.db
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2023 ida silfverskiold
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # React-Langchain-Chatbot
2 |
3 | Basic ChatGPT clone using OpenAI GPT-3.5-Turbo or GPT-4 via Langchain. Good playground for Langchain AI chatbots.
4 |
5 | Boostrapping with [React-Chatbot-Kit](https://fredrikoseberg.github.io/react-chatbot-kit-docs/docs/getting-started) and [LangchainJS](https://js.langchain.com/docs/get_started/introduction).
6 |
7 | ## Setup and Run the Project
8 |
9 | ### Prerequisites
10 |
11 | - Node.js v18x and npm (If not installed, download from [Node.js official website](https://nodejs.org/))
12 |
13 | ### Steps
14 |
15 | 1. **Clone the Repository**:
16 |
17 | ```bash
18 | git clone https://github.com/ilsilfverskiold/react-langchain-chatbot.git
19 | cd react-langchain-chatbot
20 |
21 | 3. **Install Dependencies**:
22 |
23 | ```bash
24 | npm install
25 |
26 | 4. **Set Up OpenAI API Key**:
27 |
28 | - Obtain your OpenAI API key.
29 | - Create a .env file in the root directory and add your OpenAI key.
30 |
31 | ```bash
32 | REACT_APP_OPEN_AI_API_KEY=your_openai_api_key
33 |
34 | 4. **Start the Server**:
35 |
36 | ```bash
37 | npm start
38 |
39 | ## Options
40 |
41 | 1. **ChatGPT Clone**: The script has already been set in `src/components/LangchainProcessor.js` so you can run it as is. If you want to change the prompt template you can do so directly in this file.
42 |
43 | ```javascript
44 | // The default prompt template is
45 | const promptTemplate = `
46 | You are an ironic and nihilistic chatbot so always answer like so. Don't answer in a "response: answer" format.
47 | Question: {question}
48 | `;
49 | ```
50 |
51 | 2. **A Simple Chain**: Go to `/langchain_options` and find `SimpleChain.js` rename it `LangchainProcessor.js` and replace it with the `LangchainProcessor.js` in the components folder. You can build on top of this yourself but at the moment it is only using the question and not allowing for past answers. Make sure you tweak the prompt template in there.
52 |
53 | 3. **Q/A with URL**: Here you will first need to set up your Workers route correctly. See [this repository](https://github.com/ilsilfverskiold/cloudflare-workers-langchain) that will go through it step by step. When you have a working endpoint, go to `/langchain_options` in this directory and find `CloudflateWorkersRoute.js` rename it `LangchainProcessor.js` and replace it with the `LangchainProcessor.js` in the components folder.
54 | - Make sure you set REACT_APP_CLOUDFLARE_WORKERS_AUTH in your .env file that you have set up with your Worker
55 | - Make sure you set the URL for the POST request to the endpoint that you'll receive with the deployment of your worker
56 | - If you are experiencing CORS errors Make sure you allow your IP to access the endpoint (this you set up via your worker)
57 |
58 | 4. **Q/A with Text file**: Here you will first need to set up your AWS application correctly. See [this repository](https://github.com/ilsilfverskiold/langchainjs-aws-service) or [this tutorial](https://medium.com/gitconnected/deploying-an-ai-powered-q-a-bot-on-aws-with-langchainjs-and-serverless-9361d0778fbd) that will go through it step by step. When you have a working endpoint, go to `/langchain_options` in this directory and find `AWSRoute.js` rename it `LangchainProcessor.js` and replace it with the `LangchainProcessor.js` in the components folder. Or just replace the code directly in the `LangchainProcessor.js` component.
59 |
60 | - Make sure you set all your process.env keys in a .env file
61 |
62 | ```bash
63 | REACT_APP_OPEN_AI_API_KEY=
64 | REACT_APP_AWS_POST_URL=https://xxxx.execute-api.eu-central-1.amazonaws.com/dev/question
65 | REACT_APP_AWS_API_KEY=
66 | REACT_APP_AWS_BUCKET_NAME=my-langchain-bucket
67 |
68 | - To set your system and prompt template look into the code in `AWSRoute.js`
69 |
70 | ```javascript
71 | // Define the request body
72 | const requestBody = {
73 | ...
74 | promptTemplate: "Use the following pieces of context to answer the question at the end. \n {context}\n Question: {question}\nHelpful Answer:",
75 | systemTemplate: "I want you to act as a customer service bot called Socky the Happy bot that I am having a conversation with.\nYou are a bot that will provide funny answers to the customer. \n If you can't answer the question say I don't know."
76 | };
77 | ```
78 |
79 | - If you are experiencing CORS errors Make sure you allow your IP to access the endpoint (look at your AWS lambda scripts)
80 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "sandbox",
3 | "version": "0.1.0",
4 | "private": true,
5 | "dependencies": {
6 | "@testing-library/jest-dom": "^5.17.0",
7 | "@testing-library/react": "^13.4.0",
8 | "@testing-library/user-event": "^13.5.0",
9 | "langchain": "^0.0.131",
10 | "react": "^18.2.0",
11 | "react-chatbot-kit": "^2.1.2",
12 | "react-dom": "^18.2.0",
13 | "react-scripts": "5.0.1",
14 | "web-vitals": "^2.1.4"
15 | },
16 | "scripts": {
17 | "start": "react-scripts start",
18 | "build": "react-scripts build",
19 | "test": "react-scripts test",
20 | "eject": "react-scripts eject"
21 | },
22 | "eslintConfig": {
23 | "extends": [
24 | "react-app",
25 | "react-app/jest"
26 | ]
27 | },
28 | "browserslist": {
29 | "production": [
30 | ">0.2%",
31 | "not dead",
32 | "not op_mini all"
33 | ],
34 | "development": [
35 | "last 1 chrome version",
36 | "last 1 firefox version",
37 | "last 1 safari version"
38 | ]
39 | },
40 | "devDependencies": {
41 | "tailwindcss": "^3.3.3"
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/public/favicon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ilsilfverskiold/react-langchain-chatbot/0f8b290e5c182698863d06faf6eefa3cea622a10/public/favicon.ico
--------------------------------------------------------------------------------
/public/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |