-
Notifications
You must be signed in to change notification settings - Fork 0
/
chatbot.py
99 lines (86 loc) · 3.13 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
"""
Chatbot script for prompting using ChatGPT; using pn for GUI
"""
# Installing dependencies
import os
import openai
import panel as pn
from dotenv import load_dotenv, find_dotenv
# read local .env file
_ = load_dotenv(find_dotenv('keys.env'))
# load api key
openai.api_key = os.getenv('OPENAI_API_KEY')
###### Useful Functions ######
def get_completion(prompt, model="gpt-3.5-turbo"):
"""
Function:
- Retrieve response from ChatGPT based off singular prompt
Args:
- prompt: message to be sent
- model: GPT model to use
Output:
- response as a string
"""
messages = [{"role": "user", "content": prompt}]
response = openai.ChatCompletion.create(
model=model,
messages=messages,
temperature=0.2, # this is the degree of randomness of the model's output
)
return response.choices[0].message["content"]
def get_completion_from_messages(messages, model="gpt-3.5-turbo", temperature=0):
"""
Function:
- Retrieve response from ChatGPT based on history i.e. messages
Args:
- messages: string object containing context and history
- model: GPT model to use
- temperature: determine variability in response from GPT
Output:
- response as a JSON object
"""
response = openai.ChatCompletion.create(
model=model,
messages=messages,
temperature=temperature, # this is the degree of randomness of the model's output
)
# print(str(response.choices[0].message))
return response.choices[0].message["content"]
def collect_messages(_):
"""
Function:
- Retrieve responses from GPT prompt
Output:
- response as a string object
"""
prompt = inp.value_input
# append user prompt
inp.value = ''
context.append({'role': 'user', 'content': f"{prompt}"})
# append response from system
response = get_completion_from_messages(context)
context.append({'role': 'assistant', 'content': f"{response}"})
# store as context
panels.append(pn.Row('User:', pn.pane.Markdown(prompt, width=600)))
pannels.append(pn.Row('Assistant:', pn.pane.Markdown(response, width=600, style={'background-color': '#F6F6F6'})))
return pn.Column(*panels)
###### Python Script ######
pn.extension()
panels = []
# context for bot
context = [ {'role':'system', 'content':"""
You are SmooBot, an automated ChatBot meant to serve as a interactive Library Management service. \ You will follow the steps accordingly whenever you meet a customer: \
1. Find the current time. \
2. You must greet the customer according to the time and introduce yourself in a kind and polite manner. \
3. Ask the customer for their name and always refer to them by their name. \
4. Ask them what they would like to do. \
"""} ] # accumulate messages
inp = pn.widgets.TextInput(value="Hi", placeholder='Enter text here…')
button_conversation = pn.widgets.Button(name="Chat!")
interactive_conversation = pn.bind(collect_messages, button_conversation)
dashboard = pn.Column(
inp,
pn.Row(button_conversation),
pn.panel(interactive_conversation, loading_indicator=True, height=300),
)
dashboard