ai_chatbot 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/ai_chatbot/version.rb +5 -0
- data/lib/ai_chatbot.rb +27 -0
- data/lib/ml_model.py +76 -0
- metadata +61 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: bde3d724e066978fd481402f7aaa899041d85044b8b3ecc10574410ee79946f2
|
4
|
+
data.tar.gz: 8cc52ee8ec6b7e061fa6c009fdac5886b6b8e1a6e785d50abf70146bf04135ab
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 115664a820b9b5c3da45d3fbb382785bd24dc0a840ad707fd8d1f0e7612d498c377ed12e86e7f997191cc83585a4245cfedda79cc22ee8ecfd44e01c131bb10b
|
7
|
+
data.tar.gz: f94763913a5cf902b9f940855c9f5a608a47495f776ddd3ab59e223ebad03be2bb0320c9d57317db2435a2f0662e08aca18f3143e56265a3fb7d7e86e018a6ca
|
data/lib/ai_chatbot.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'open3'
|
2
|
+
|
3
|
+
module AiChatbot
|
4
|
+
class Chatbot
|
5
|
+
# Method to ask a question and get prediction from Python
|
6
|
+
def self.ask_question(question)
|
7
|
+
stdout, stderr, status = Open3.capture3("python3", "#{__dir__}/ml_model.py", "predict", question)
|
8
|
+
|
9
|
+
if status.success?
|
10
|
+
return stdout.strip
|
11
|
+
else
|
12
|
+
raise "Error: #{stderr}"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
# Method to train the model with a new question-answer pair
|
17
|
+
def self.train_model(new_question, new_answer)
|
18
|
+
stdout, stderr, status = Open3.capture3("python3", "#{__dir__}/ml_model.py", "train", new_question, new_answer)
|
19
|
+
|
20
|
+
if status.success?
|
21
|
+
return stdout.strip
|
22
|
+
else
|
23
|
+
raise "Error: #{stderr}"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/lib/ml_model.py
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
import sys
|
2
|
+
from sklearn.feature_extraction.text import TfidfVectorizer
|
3
|
+
from sklearn.naive_bayes import MultinomialNB
|
4
|
+
from sklearn.pipeline import make_pipeline
|
5
|
+
from sklearn.metrics.pairwise import cosine_similarity
|
6
|
+
import pickle
|
7
|
+
import os
|
8
|
+
|
9
|
+
# Load or initialize the dataset
|
10
|
+
if os.path.exists("qa_model.pkl"):
|
11
|
+
with open("qa_model.pkl", "rb") as f:
|
12
|
+
model_data = pickle.load(f)
|
13
|
+
questions = model_data['questions']
|
14
|
+
answers = model_data['answers']
|
15
|
+
else:
|
16
|
+
questions = [
|
17
|
+
"How to create a new model in Rails?",
|
18
|
+
"What is migration?",
|
19
|
+
"How to add a route?"
|
20
|
+
]
|
21
|
+
answers = [
|
22
|
+
"You can create a model using 'rails generate model'.",
|
23
|
+
"Migration is a database schema change.",
|
24
|
+
"You can add a route in the config/routes.rb file."
|
25
|
+
]
|
26
|
+
|
27
|
+
# Create a pipeline (TF-IDF + MultinomialNB)
|
28
|
+
model = make_pipeline(TfidfVectorizer(), MultinomialNB())
|
29
|
+
model.fit(questions, answers)
|
30
|
+
|
31
|
+
# Function to predict or retrain the model
|
32
|
+
def main(action, query=None, new_answer=None):
|
33
|
+
if action == "predict":
|
34
|
+
return get_prediction(query)
|
35
|
+
elif action == "train":
|
36
|
+
return train_model(query, new_answer)
|
37
|
+
|
38
|
+
# Function to predict the response with confidence check
|
39
|
+
def get_prediction(query):
|
40
|
+
query_vec = model.named_steps['tfidfvectorizer'].transform([query])
|
41
|
+
question_vecs = model.named_steps['tfidfvectorizer'].transform(questions)
|
42
|
+
|
43
|
+
# Calculate cosine similarity between query and known questions
|
44
|
+
similarities = cosine_similarity(query_vec, question_vecs)
|
45
|
+
max_similarity = similarities.max()
|
46
|
+
|
47
|
+
threshold = 0.85
|
48
|
+
if max_similarity < threshold:
|
49
|
+
return "No good match found. Please provide the correct answer."
|
50
|
+
else:
|
51
|
+
prediction = model.predict([query])
|
52
|
+
return prediction[0]
|
53
|
+
|
54
|
+
# Function to train the model with a new question and answer
|
55
|
+
def train_model(new_question, new_answer):
|
56
|
+
global questions, answers
|
57
|
+
|
58
|
+
# Append new question-answer pair to the dataset
|
59
|
+
questions.append(new_question)
|
60
|
+
answers.append(new_answer)
|
61
|
+
|
62
|
+
# Retrain the model with updated data
|
63
|
+
model.fit(questions, answers)
|
64
|
+
|
65
|
+
# Save the updated model and data
|
66
|
+
with open("qa_model.pkl", "wb") as f:
|
67
|
+
pickle.dump({"questions": questions, "answers": answers}, f)
|
68
|
+
|
69
|
+
return f"Model retrained with the new question: '{new_question}' and answer: '{new_answer}'"
|
70
|
+
|
71
|
+
if __name__ == "__main__":
|
72
|
+
# Expecting action (predict/train), question, and answer (if training)
|
73
|
+
action = sys.argv[1]
|
74
|
+
question = sys.argv[2] if len(sys.argv) > 2 else None
|
75
|
+
answer = sys.argv[3] if len(sys.argv) > 3 else None
|
76
|
+
print(main(action, question, answer))
|
metadata
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ai_chatbot
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sanet
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-09-14 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: open3
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description: Integrates a chatbot using Python for predictions and training in a Rails
|
28
|
+
application.
|
29
|
+
email:
|
30
|
+
- sanket.tikhande@gmail.com
|
31
|
+
executables: []
|
32
|
+
extensions: []
|
33
|
+
extra_rdoc_files: []
|
34
|
+
files:
|
35
|
+
- lib/ai_chatbot.rb
|
36
|
+
- lib/ai_chatbot/version.rb
|
37
|
+
- lib/ml_model.py
|
38
|
+
homepage:
|
39
|
+
licenses:
|
40
|
+
- MIT
|
41
|
+
metadata: {}
|
42
|
+
post_install_message:
|
43
|
+
rdoc_options: []
|
44
|
+
require_paths:
|
45
|
+
- lib
|
46
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: '0'
|
51
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
requirements: []
|
57
|
+
rubygems_version: 3.3.7
|
58
|
+
signing_key:
|
59
|
+
specification_version: 4
|
60
|
+
summary: A chatbot for Rails integration with AI model using Python
|
61
|
+
test_files: []
|