-
Notifications
You must be signed in to change notification settings - Fork 0
/
chatbot.py
84 lines (59 loc) · 3.49 KB
/
chatbot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import openai
from termcolor import colored
import streamlit as st
from database import get_redis_connection,get_redis_results
from config import CHAT_MODEL,COMPLETIONS_MODEL, INDEX_NAME
redis_client = get_redis_connection()
# A basic class to create a message as a dict for chat
class Message:
def __init__(self,role,content):
self.role = role
self.content = content
def message(self):
return {"role": self.role,"content": self.content}
# New Assistant class to add a vector database call to its responses
class RetrievalAssistant:
def __init__(self):
self.conversation_history = []
def _get_assistant_response(self, prompt):
try:
completion = openai.ChatCompletion.create(
model=CHAT_MODEL,
messages=prompt,
temperature=0.1
)
response_message = Message(completion['choices'][0]['message']['role'],completion['choices'][0]['message']['content'])
return response_message.message()
except Exception as e:
return f'Request failed with exception {e}'
# The function to retrieve Redis search results
def _get_search_results(self,prompt):
latest_question = prompt
search_content = get_redis_results(redis_client,latest_question,INDEX_NAME)['result'][0]
return search_content
def ask_assistant(self, next_user_prompt):
[self.conversation_history.append(x) for x in next_user_prompt]
assistant_response = self._get_assistant_response(self.conversation_history)
# Answer normally unless the trigger sequence is used "searching_for_answers"
if 'searching for answers' in assistant_response['content'].lower():
question_extract = openai.Completion.create(model=COMPLETIONS_MODEL,prompt=f"Extract the user's latest question and the year for that question from this conversation: {self.conversation_history}. Extract it as a sentence stating the Question and Year")
search_result = self._get_search_results(question_extract['choices'][0]['text'])
# We insert an extra system prompt here to give fresh context to the Chatbot on how to use the Redis results
# In this instance we add it to the conversation history, but in production it may be better to hide
self.conversation_history.insert(-1,{"role": 'system',"content": f"Answer the user's question using this content: {search_result}. If you cannot answer the question, say 'Sorry, I don't know the answer to this one'"})
assistant_response = self._get_assistant_response(self.conversation_history)
self.conversation_history.append(assistant_response)
return assistant_response
else:
self.conversation_history.append(assistant_response)
return assistant_response
def pretty_print_conversation_history(self, colorize_assistant_replies=True):
for entry in self.conversation_history:
if entry['role'] == 'system':
pass
else:
prefix = entry['role']
content = entry['content']
output = colored(prefix +':\n' + content, 'green') if colorize_assistant_replies and entry['role'] == 'assistant' else prefix +':\n' + content
#prefix = entry['role']
print(output)