-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatbot.py
26 lines (22 loc) · 887 Bytes
/
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
import os
import openai
# Based on a this article https://www.twilio.com/blog/openai-gpt-3-chatbot-python-twilio-sms
openai.api_key = 'add-your-key-here'
completion = openai.Completion()
start_chat_log = '''Human: Hello, who are you?
AI: I am doing great. How can I help you today?
'''
def ask(question, chat_log=None):
if chat_log is None:
chat_log = start_chat_log
prompt = f'{chat_log}Human: {question}\nAI:'
response = completion.create(
prompt=prompt, engine="davinci", stop=['\nHuman'], temperature=0.9,
top_p=1, frequency_penalty=0, presence_penalty=0.6, best_of=1,
max_tokens=150)
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'