ai_chatbot 0.1.6.5 → 0.1.6.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/ai_chatbot/version.rb +1 -1
- data/lib/ml_model.py +40 -17
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fee6347bdac6070db1bfbc1711f039103c1237c149f42a7f0b848b85eba98314
|
4
|
+
data.tar.gz: c54c29b235f159921d4b33ac36bc56d505983eb03a25500829d72e9741628983
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f80cec458575b77b6be9879007e3f60830886b88bcfbde28974e9fe1e68b59d61424956a0aea0db62e69e5705e2c8d2b2b97d3e6700ee3b9cdd141530816325f
|
7
|
+
data.tar.gz: d2ac4f47ef9ce02255a60e84a6bff019c9036be5368f79e43748ca945a459c4459c5b53ee7a57a8345114018e03d0f5a1eeab70895ac78bafefed6980223dc59
|
data/lib/ai_chatbot/version.rb
CHANGED
data/lib/ml_model.py
CHANGED
@@ -7,9 +7,12 @@ from sklearn.naive_bayes import MultinomialNB
|
|
7
7
|
from sklearn.pipeline import make_pipeline
|
8
8
|
from sklearn.metrics.pairwise import cosine_similarity
|
9
9
|
|
10
|
+
# Load environment variables
|
11
|
+
load_dotenv()
|
12
|
+
|
10
13
|
# Connect to PostgreSQL
|
11
14
|
conn = psycopg2.connect(
|
12
|
-
dbname=
|
15
|
+
dbname=os.getenv("DB_NAME"),
|
13
16
|
user=os.getenv("DB_USERNAME"),
|
14
17
|
password=os.getenv("DB_PASSWORD"),
|
15
18
|
host=os.getenv("DB_HOST"),
|
@@ -17,15 +20,20 @@ conn = psycopg2.connect(
|
|
17
20
|
)
|
18
21
|
cursor = conn.cursor()
|
19
22
|
|
20
|
-
|
21
|
-
|
23
|
+
# Fetch data from DB
|
22
24
|
cursor.execute("SELECT question, answer FROM qa_data")
|
23
25
|
rows = cursor.fetchall()
|
24
26
|
questions = [row[0] for row in rows]
|
25
27
|
answers = [row[1] for row in rows]
|
26
28
|
|
29
|
+
# Define the vectorizer and Naive Bayes model
|
27
30
|
vectorizer = TfidfVectorizer()
|
28
|
-
|
31
|
+
model = MultinomialNB()
|
32
|
+
pipeline = make_pipeline(vectorizer, model)
|
33
|
+
|
34
|
+
# Train model if there is data
|
35
|
+
if questions:
|
36
|
+
pipeline.fit(questions, answers)
|
29
37
|
|
30
38
|
|
31
39
|
def get_prediction(query):
|
@@ -33,7 +41,7 @@ def get_prediction(query):
|
|
33
41
|
return "No questions available in the database."
|
34
42
|
|
35
43
|
query_vec = vectorizer.transform([query])
|
36
|
-
similarities = cosine_similarity(query_vec,
|
44
|
+
similarities = cosine_similarity(query_vec, vectorizer.transform(questions)).flatten()
|
37
45
|
|
38
46
|
max_sim_index = similarities.argmax()
|
39
47
|
max_similarity = similarities[max_sim_index]
|
@@ -44,58 +52,73 @@ def get_prediction(query):
|
|
44
52
|
else:
|
45
53
|
return answers[max_sim_index]
|
46
54
|
|
55
|
+
|
47
56
|
# Function to train the model with new data
|
48
57
|
def train_model(new_question, new_answer):
|
49
58
|
global questions, answers
|
50
59
|
|
51
60
|
# Store in database
|
52
|
-
|
53
|
-
|
54
|
-
|
61
|
+
cursor.execute(
|
62
|
+
"INSERT INTO qa_data (question, answer, created_at, updated_at) VALUES (%s, %s, NOW(), NOW()) ON CONFLICT (question) DO NOTHING",
|
63
|
+
(new_question, new_answer),
|
64
|
+
)
|
55
65
|
conn.commit()
|
56
66
|
|
57
67
|
# Update lists and retrain model
|
58
68
|
questions.append(new_question)
|
59
69
|
answers.append(new_answer)
|
60
|
-
|
70
|
+
pipeline.fit(questions, answers) # Retrain model
|
61
71
|
|
62
72
|
return f"Added: '{new_question}' -> '{new_answer}'"
|
63
73
|
|
74
|
+
|
64
75
|
# Function to update an answer
|
65
76
|
def update_answer(existing_question, new_answer):
|
66
|
-
|
77
|
+
if existing_question not in questions:
|
78
|
+
return f"Question '{existing_question}' not found."
|
79
|
+
|
80
|
+
cursor.execute(
|
81
|
+
"UPDATE qa_data SET answer = %s WHERE question = %s", (new_answer, existing_question)
|
82
|
+
)
|
67
83
|
conn.commit()
|
68
84
|
|
69
85
|
# Update lists and retrain model
|
70
86
|
index = questions.index(existing_question)
|
71
87
|
answers[index] = new_answer
|
72
|
-
|
88
|
+
pipeline.fit(questions, answers)
|
73
89
|
|
74
90
|
return f"Updated: '{existing_question}' -> '{new_answer}'"
|
75
91
|
|
92
|
+
|
76
93
|
# Function to delete a question
|
77
94
|
def delete_question(existing_question):
|
95
|
+
if existing_question not in questions:
|
96
|
+
return f"Question '{existing_question}' not found."
|
97
|
+
|
78
98
|
cursor.execute("DELETE FROM qa_data WHERE question = %s", (existing_question,))
|
79
99
|
conn.commit()
|
80
100
|
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
model.fit(questions, answers)
|
101
|
+
index = questions.index(existing_question)
|
102
|
+
del questions[index]
|
103
|
+
del answers[index]
|
104
|
+
pipeline.fit(questions, answers)
|
86
105
|
|
87
106
|
return f"Deleted: '{existing_question}'"
|
88
107
|
|
108
|
+
|
89
109
|
# Function to list questions
|
90
110
|
def list_questions():
|
91
111
|
cursor.execute("SELECT question FROM qa_data")
|
92
112
|
return [row[0] for row in cursor.fetchall()]
|
93
113
|
|
114
|
+
|
94
115
|
# Function to list answers
|
95
116
|
def list_answers():
|
96
117
|
cursor.execute("SELECT answer FROM qa_data")
|
97
118
|
return [row[0] for row in cursor.fetchall()]
|
98
119
|
|
120
|
+
|
121
|
+
# Command-line execution
|
99
122
|
if __name__ == "__main__":
|
100
123
|
action = sys.argv[1]
|
101
124
|
question = sys.argv[2] if len(sys.argv) > 2 else None
|
@@ -116,4 +139,4 @@ if __name__ == "__main__":
|
|
116
139
|
|
117
140
|
# Close DB connection
|
118
141
|
cursor.close()
|
119
|
-
conn.close()
|
142
|
+
conn.close()
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ai_chatbot
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.6.5
|
4
|
+
version: 0.1.6.5.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sanket
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2025-03-
|
11
|
+
date: 2025-03-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: open3
|
@@ -24,8 +24,8 @@ dependencies:
|
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
|
-
description: Added
|
28
|
-
|
27
|
+
description: Added PostgreSQL support, fixed model error. Version 0.1.6.5.1 Details
|
28
|
+
on Git.
|
29
29
|
email:
|
30
30
|
- sanket.tikhande@gmail.com
|
31
31
|
executables: []
|