-
Notifications
You must be signed in to change notification settings - Fork 6
/
chatbot.py
46 lines (36 loc) · 1.56 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
import os
from dotenv import load_dotenv
import openai
load_dotenv()
openai.api_key = os.environ.get("OPENAI_KEY")
completion = openai.Completion()
# Initial prompt to give an idea of the domain and tone we eant the chatbot to have
start_chat_log = '''This is Marcus, your travel guide bot. I am informative, creative, and knows a lot about landmarks.
\n
Human: Hello, who are you?
AI: I am Marcus, your travel guide. How can I help you today?
Human: Where can I spend an evening in Beirut?
AI: You can catch a play at Baalbeck International Festival, or go for a sailing trip on the Mediterranean Sea.
Human: Where can I have some fun in Goa?
AI: Goa has peaceful beaches and fun-filled pubs/clubs.
'''
def ask(question, chat_log=None):
"""
:param question: The user query
:param chat_log: history of conversation
:return answer: the bot response
"""
if chat_log is None:
chat_log = start_chat_log
prompt = f'{chat_log}Human: {question}\nAI:'
# Model parameters chosen so as to give set the tune of a friendly and creative chatbot
response = completion.create(
prompt=prompt, engine="davinci", stop=["\n", " Human:", " AI:"], temperature=0.9,
top_p=1, frequency_penalty=0, presence_penalty=0.6, best_of=1,
max_tokens=70)
answer = response.choices[0].text.strip()
return answer
def append_interaction_to_chat_log(question, answer, chat_log=None):
if chat_log is None:
chat_log = start_chat_log
return f'{chat_log}Human: {question}\nAI: {answer}\n'