Skip to content

Commit 028bce8

Browse files
SebastianJames55sebastian-james
authored andcommitted
- detailed README.md
- to the point queries.sql
1 parent 3fe0395 commit 028bce8

5 files changed

Lines changed: 159 additions & 1 deletion

File tree

README.md

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,39 @@
11
# mind-reader-chatbot
2-
A chatbot aimed to mentally support people who are going through tough times. Powered by MindsDB.
2+
A chatbot aimed to mentally support people who are going through tough times. Powered by MindsDB.
3+
4+
Here's how you can set up an automated AI chatbot for yourself.
5+
6+
## Prerequisites
7+
8+
- [Get an OpenAI API key](https://www.howtogeek.com/885918/how-to-get-an-openai-api-key/)
9+
- [Set up mindsdb locally](https://docs.mindsdb.com/setup/self-hosted/pip/source)
10+
- Configure YugabyteDB
11+
- [Create an account](https://cloud.yugabyte.com/signup?utm_medium=direct&utm_source=docs&utm_campaign=YBM_signup) & a free cluster. Take a note of your DB credentials.
12+
Configure IP allow list
13+
![Configure IP allow list](assets/public_access.png)
14+
Launch cloud shell and login using DB credentials
15+
![Launch cloud shell](assets/cloud%20shell.png)
16+
- Run the following commands:
17+
`create database demo;`
18+
Connect to database:
19+
`\c demo`
20+
```
21+
CREATE TABLE chatbot_input (
22+
id SERIAL PRIMARY KEY,
23+
created_at timestamptz DEFAULT CURRENT_TIMESTAMP,
24+
text varchar(255)
25+
);
26+
```
27+
```
28+
CREATE TABLE chatbot_output (
29+
id SERIAL PRIMARY KEY,
30+
created_at timestamptz DEFAULT CURRENT_TIMESTAMP,
31+
text varchar(1000)
32+
);
33+
```
34+
- Take a note of the host under connection parameters:
35+
![Connection parameters](assets/connection_param.png)
36+
37+
## Usage
38+
39+
Head over to [queries.sql](queries.sql) file. You can run the commands line by line to create your own mind-reader chatbot.

assets/cloud shell.png

78.3 KB
Loading

assets/connection_param.png

51.2 KB
Loading

assets/public_access.png

27.6 KB
Loading

queries.sql

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
-- You can run the following commands in your local mindsdb setup
2+
3+
-- Creating a project. This will contain the model, views and job you are about to create
4+
CREATE PROJECT mind_reader_project;
5+
6+
-- Creating a model which will predict the response
7+
CREATE MODEL mind_reader_project.gpt_model
8+
PREDICT response
9+
USING
10+
engine = 'openai',
11+
max_tokens = 300,
12+
api_key = '<your-openai-api-key>', -- In MindsDB cloud accounts we provide a default key
13+
model_name = 'gpt-3.5-turbo', -- You can also use 'text-davinci-003'
14+
prompt_template = 'Reply like a friend who cares and wants to help.\
15+
Input message: {{text}}\
16+
In less than 550 characters, when there is some sign of distress in the input share healthy habits, \
17+
motivational quotes, inspirational real-life stories.
18+
Provide options to seek out in-person help if you are not able to satisfy.'; -- Prompt which will decide the style of output
19+
20+
-- Confirm if the model status is complete before proceeding
21+
SELECT *
22+
FROM mind_reader_project.models
23+
WHERE name='gpt_model'; -- Name of the model within our project
24+
25+
-- Query the model to get a response
26+
SELECT response
27+
FROM mind_reader_project.gpt_model
28+
WHERE text = "Not doing so great"; -- Input to the model
29+
30+
-- Creating yugabyteDB instance
31+
CREATE DATABASE yugabyte_demo
32+
WITH
33+
engine = 'yugabyte',
34+
parameters = {
35+
"user": "admin", -- User name
36+
"password": "password", -- Password associated with the user
37+
"host": "link", -- Link to the instance which hosts the DB
38+
"port": 5433,
39+
"database": "demo", -- Database name in instance
40+
"schema": "public" -- Schema name in instance
41+
};
42+
43+
-- Viewing the stored inputs
44+
SELECT * from yugabyte_demo.chatbot_input;
45+
46+
-- Storing input into the table
47+
INSERT INTO yugabyte_demo.chatbot_input (text)
48+
VALUES ('feeling lonely');
49+
50+
-- Storing output generated from the model based on the input data
51+
INSERT INTO yugabyte_demo.chatbot_output (
52+
SELECT r.response AS text -- Response from model
53+
FROM yugabyte_demo.chatbot_input t
54+
JOIN mind_reader_project.gpt_model r
55+
);
56+
57+
-- Viewing the responses stored in table
58+
SELECT *
59+
FROM yugabyte_demo.chatbot_output;
60+
61+
-- View that stores the input messages which haven't been replied to yet
62+
CREATE VIEW mind_reader_project.to_reply_to (
63+
SELECT id, text FROM yugabyte_demo.chatbot_input
64+
WHERE id NOT IN (SELECT r.id FROM yugabyte_demo.chatbot_output AS r) -- Fetching input which hasn't been replied to
65+
);
66+
67+
-- View the messages which haven't been replied to
68+
SELECT *
69+
FROM mind_reader_project.to_reply_to;
70+
71+
-- View that stores the responses (against the messages which weren't replied to) from model
72+
CREATE VIEW mind_reader_project.to_chatbot_output (
73+
SELECT * FROM mind_reader_project.to_reply_to
74+
JOIN mind_reader_project.gpt_model
75+
);
76+
77+
-- Job to automate the flow, runs every minute
78+
CREATE JOB mind_reader_project.chatbot_job (
79+
-- Storing the responses from model in output table
80+
INSERT INTO yugabyte_demo.chatbot_output (
81+
SELECT
82+
id,
83+
response AS text
84+
FROM mind_reader_project.to_chatbot_output
85+
)
86+
87+
) EVERY MINUTE;
88+
89+
-- See all the jobs within the project
90+
SELECT *
91+
FROM mind_reader_project.jobs where name = "chatbot_job";
92+
93+
-- Once job is created, input message
94+
INSERT INTO yugabyte_demo.chatbot_input (text)
95+
VALUES ('feeling lonely');
96+
97+
-- Output table will be updated by the job every minute in case of new input messages
98+
SELECT *
99+
FROM yugabyte_demo.chatbot_output;
100+
101+
-- See the whole history of the jobs within the project
102+
SELECT *
103+
FROM mind_reader_project.jobs_history;
104+
105+
-- Drop the database instance from mindsdb
106+
DROP DATABASE yugabyte_demo;
107+
108+
-- Drop the running job
109+
DROP JOB mind_reader_project.chatbot_job;
110+
111+
-- Drop the gpt_model
112+
DROP MODEL mind_reader_project.gpt_model;
113+
114+
-- Drop the to_reply_to view
115+
DROP VIEW mind_reader_project.to_reply_to;
116+
117+
-- Drop the to_chatbot_output view
118+
DROP VIEW mind_reader_project.to_chatbot_output;
119+
120+
-- Drop the mind_reader_project
121+
DROP PROJECT mind_reader_project;

0 commit comments

Comments
 (0)