-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathassistant.py
More file actions
61 lines (52 loc) · 2.08 KB
/
assistant.py
File metadata and controls
61 lines (52 loc) · 2.08 KB
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
import logging
from configparser import ConfigParser
import openai
from hugchat import hugchat
class Assistant:
"""
This class represents the assistant with methods that handels the workflow
from asking the assistant (a user_text) to the response (assistant_text)
"""
def __init__(self, firstname) -> None:
self.firstname = firstname
self.usertext = ''
def setup_assistant_openai(self):
config = ConfigParser()
try:
config.read('config.ini')
openai.api_key = config.get('auth', 'OPENAI_API_KEY')
except FileNotFoundError as e:
logging.error(f"Config file not found: {e}")
except KeyError as e:
logging.error(f"Missing API key in config file: {e}")
def get_usertext(self, userinput):
self.usertext = userinput
logging.info(f'From user to assistant: {userinput}')
def response_openai(self, from_user):
self.get_usertext(from_user)
try:
response = openai.Completion.create(
model="text-davinci-003",
prompt=self.usertext,
temperature=0.7,
max_tokens=50,
top_p=1,
frequency_penalty=0,
presence_penalty=0)
response_text = response["choices"][0]["text"][2:] # type: ignore
logging.info(f'From assistant to user: {response_text}')
return response_text
except openai.error.AuthenticationError as e: # type: ignore
# Handle authentication error, e.g. check credentials or log
print(f"OpenAI API request was not authorized: {e}")
return None
def response_hugging_chat(self, from_user):
self.get_usertext(from_user)
chatbot = hugchat.ChatBot()
# id = chatbot.new_conversation()
# print("id: ", id)
# chatbot.change_conversation(id)
response_text = chatbot.chat(self.usertext)
logging.info(f'From assistant to user: {response_text}')
chatbot.get_conversation_list()
return response_text