ai_chatbot 0.1.6 → 0.1.6.2
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/ai_chatbot.rb +15 -5
- data/lib/ml_model.py +37 -15
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 54c4224d297c82a20b3864dec73a0f1b5e654dfe5e08fbdfad12e09202588bde
|
4
|
+
data.tar.gz: 5ffb9610a7c5c13dbb2d597055c9269ca1578ceb5d16ecb940323d0d6909ed3a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cc4dedd9eed6dd5c5e3543969fab14e29014e9363369e33d5c932e2faf445b916343c87ad1412bd3da338895b739f13e2a2344f6ca9e8f6d2a0cffa548dbc04f
|
7
|
+
data.tar.gz: '093e12aa5db131a81ea46bc1d8eafee4794a99d13775da6d8cb84fefa2238bbc2b5edc1885e647c10b0f2c9d95cd9d9745723af7efd5468331cf5a028d0b2b90'
|
data/lib/ai_chatbot/version.rb
CHANGED
data/lib/ai_chatbot.rb
CHANGED
@@ -15,7 +15,7 @@ module AiChatbot
|
|
15
15
|
|
16
16
|
# Method to train the model with a new question-answer pair
|
17
17
|
def self.train_model(new_question, new_answer)
|
18
|
-
stdout, stderr, status = Open3.capture3("python3", "#{__dir__}/ml_model.py", "
|
18
|
+
stdout, stderr, status = Open3.capture3("python3", "#{__dir__}/ml_model.py", "train_model", new_question, new_answer)
|
19
19
|
|
20
20
|
if status.success?
|
21
21
|
return stdout.strip
|
@@ -27,7 +27,7 @@ module AiChatbot
|
|
27
27
|
|
28
28
|
# Method to train the model with a new question-answer pair
|
29
29
|
def self.update_answer(existing_question, new_answer)
|
30
|
-
stdout, stderr, status = Open3.capture3("python3", "#{__dir__}/ml_model.py", "update_answer",
|
30
|
+
stdout, stderr, status = Open3.capture3("python3", "#{__dir__}/ml_model.py", "update_answer", existing_question, new_answer)
|
31
31
|
|
32
32
|
if status.success?
|
33
33
|
return stdout.strip
|
@@ -36,8 +36,8 @@ module AiChatbot
|
|
36
36
|
end
|
37
37
|
end
|
38
38
|
|
39
|
-
def self.
|
40
|
-
|
39
|
+
def self.update_or_delete_question(existing_question, new_question=nil)
|
40
|
+
stdout, stderr, status = new_question == nil ? Open3.capture3("python3", "#{__dir__}/ml_model.py", "update_or_delete_question", existing_question,"None") : Open3.capture3("python3", "#{__dir__}/ml_model.py", "update_or_delete_question", existing_question, new_question)
|
41
41
|
|
42
42
|
if status.success?
|
43
43
|
return stdout.strip
|
@@ -46,7 +46,7 @@ module AiChatbot
|
|
46
46
|
end
|
47
47
|
end
|
48
48
|
|
49
|
-
def self.
|
49
|
+
def self.list_questions
|
50
50
|
stdout, stderr, status = Open3.capture3("python3", "#{__dir__}/ml_model.py", "list_questions")
|
51
51
|
|
52
52
|
if status.success?
|
@@ -56,6 +56,16 @@ module AiChatbot
|
|
56
56
|
end
|
57
57
|
end
|
58
58
|
|
59
|
+
def self.list_answers
|
60
|
+
stdout, stderr, status = Open3.capture3("python3", "#{__dir__}/ml_model.py", "list_answers")
|
61
|
+
|
62
|
+
if status.success?
|
63
|
+
return stdout.strip
|
64
|
+
else
|
65
|
+
raise "Error: #{stderr}"
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
59
69
|
|
60
70
|
end
|
61
71
|
end
|
data/lib/ml_model.py
CHANGED
@@ -32,14 +32,17 @@ model.fit(questions, answers)
|
|
32
32
|
def main(action, query=None, new_answer=None):
|
33
33
|
if action == "predict":
|
34
34
|
return get_prediction(query)
|
35
|
-
elif action == "
|
35
|
+
elif action == "train_model":
|
36
36
|
return train_model(query, new_answer)
|
37
37
|
elif action == "update_answer":
|
38
38
|
return update_answer(query, new_answer)
|
39
|
-
elif action == "
|
40
|
-
return
|
39
|
+
elif action == "update_or_delete_question":
|
40
|
+
return update_or_delete_question(query, new_answer) # Corrected here, calling the right function
|
41
41
|
elif action == "list_questions":
|
42
42
|
return list_questions()
|
43
|
+
elif action == "list_answers":
|
44
|
+
return list_answers()
|
45
|
+
|
43
46
|
|
44
47
|
# Function to predict the response with confidence check
|
45
48
|
def get_prediction(query):
|
@@ -91,27 +94,46 @@ def update_answer(existing_question, new_answer):
|
|
91
94
|
else:
|
92
95
|
return "Question not found. Please provide a valid question."
|
93
96
|
|
94
|
-
def
|
97
|
+
def update_or_delete_question(existing_question, new_question):
|
95
98
|
global questions # Only 'questions' is global, not 'new_question'
|
96
|
-
|
99
|
+
if new_question=="None":
|
100
|
+
new_question = None
|
101
|
+
|
97
102
|
if existing_question in questions:
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
103
|
+
if new_question:
|
104
|
+
# Find the index of the existing question
|
105
|
+
index = questions.index(existing_question)
|
106
|
+
# Update the question
|
107
|
+
questions[index] = new_question
|
108
|
+
# Retrain the model with updated data
|
109
|
+
model.fit(questions, answers)
|
110
|
+
# Save the updated model and data
|
111
|
+
with open("qa_model.pkl", "wb") as f:
|
112
|
+
pickle.dump({"questions": questions, "answers": answers}, f)
|
113
|
+
return f"Question updated from '{existing_question}' to '{new_question}'"
|
114
|
+
else:
|
115
|
+
# Remove the question if no new question is provided
|
116
|
+
index = questions.index(existing_question)
|
117
|
+
del questions[index]
|
118
|
+
del answers[index] # Ensure you also delete the corresponding answer
|
119
|
+
# Retrain the model with updated data
|
120
|
+
model.fit(questions, answers)
|
121
|
+
# Save the updated model and data
|
122
|
+
with open("qa_model.pkl", "wb") as f:
|
123
|
+
pickle.dump({"questions": questions, "answers": answers}, f)
|
124
|
+
return f"Question '{existing_question}' deleted successfully."
|
108
125
|
else:
|
109
126
|
return "Question not found. Please provide a valid question."
|
110
127
|
|
128
|
+
|
111
129
|
def list_questions():
|
112
130
|
global questions
|
113
131
|
return questions
|
114
132
|
|
133
|
+
def list_answers():
|
134
|
+
global answers
|
135
|
+
return answers
|
136
|
+
|
115
137
|
if __name__ == "__main__":
|
116
138
|
# Expecting action (predict/train), question, and answer (if training)
|
117
139
|
action = sys.argv[1]
|
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
|
4
|
+
version: 0.1.6.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sanket
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-09-
|
11
|
+
date: 2024-09-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: open3
|