-
Notifications
You must be signed in to change notification settings - Fork 0
/
chatbot.py
68 lines (54 loc) · 2.38 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
import google.generativeai as genai
class GenAIException(Exception):
"""GenAI Exception Base Class"""
class ChatBot:
""" chat can only have one candidate count """
CHATBOT_NAME = 'ByteAI'
def __init__(self, api_key):
self.genai = genai
self.genai.configure(api_key=api_key)
self.model = self.genai.GenerativeModel('gemini-pro')
self.conversation = None
self._conversation_history = []
self.preload_conversation()
def send_prompt(self, prompt, temperature=0.1):
if temperature < 0 or temperature > 1:
raise GenAIException('Temperature must be between 0 and 1')
if not prompt:
raise GenAIException('Prompt cannot be empty, Please enter a prompt')
try:
response = self.conversation.send_message(
content=prompt,
generation_config=self._generation_config(temperature),
)
response.resolve()
return f'{response.text}\n' + '___' * 20
except Exception as e:
raise GenAIException(e.message)
@property
def history(self):
conversation_history = [
{'role': message.role, 'text': message.parts[0].text} for message in self.conversation.history
]
return conversation_history
def clear_conversations(self):
self.conversation = self.model.start_chat(history=[])
def start_conversation(self):
self.conversation = self.model.start_chat(history=self._conversation_history)
def _generation_config(self, temperature):
return genai.types.GenerationConfig(
temperature=temperature
)
def _construct_message(self, text, role='user'):
return {
'role': role,
'parts': [text]
}
def preload_conversation(self, conversation_history=None):
if isinstance(conversation_history, list):
self._conversation_history = conversation_history
else:
self._conversation_history = [
self._construct_message('From now on, return the output as a JSON object that can be loaded in python with the key as \'text\'. For example, {"text": "<output goes here>"}'),
self._construct_message('Sure, I can return the output as a regular JSON object with the key as `text`. Here is an example: {"text": "Your output"}.')
]