-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopen_ai_bot_experiment.py
69 lines (52 loc) · 1.81 KB
/
open_ai_bot_experiment.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
from random import choice
import os
import openai
import time
from credentials import openai_key
#https://beta.openai.com/account/api-keys
openai.api_key = openai_key["key"]
completion = openai.Completion()
start_sequence = '\nfanbot:'
restart_sequence = '\n\nyou:'
# f1 = open('extracted_tanu_whatsapp.txt', 'r+', encoding='UTF-8')
# session_prompt = f1.readlines()
# session_prompt = session_prompt[0:2100]
session_prompt = """
The following is a conversation with an AI assistant. The assistant is helpful, creative, clever, and very friendly.
"""
#print(session_prompt)
print("length of session prompt: ", len(session_prompt), '. Open ai bot initialised.')
chat_log = session_prompt
def ask(question):
global chat_log
prompt_text = f'{chat_log}{restart_sequence}: {question}{start_sequence}:'
response = openai.Completion.create(
engine='davinci',
prompt=prompt_text,
temperature=0.8,
max_tokens=150,
top_p=1,
frequency_penalty=0,
presence_penalty=0.3,
stop=['\n', 'fanbot:', 'You: '],
)
story = response['choices'][0]['text']
return str(story)
def append_interaction_to_chat_log(question, answer, chat_log=None):
if chat_log is None:
chat_log = session_prompt
return f'{chat_log}{restart_sequence} {question}{start_sequence}{answer}'
def append_to_session_prompt(question, answer):
global session_prompt
session_prompt += session_prompt +". you: " + question + ". fanbot: " + answer
def call_bot(session_prompt):
while(1):
input_text = input("You: ")
incoming_msg = input_text
answer = ask(incoming_msg)
print(answer, session_prompt, incoming_msg)
time.sleep(2)
session_prompt = append_to_session_prompt(incoming_msg, answer)
print(f"fanbot: {answer}")
if __name__ == 'main':
call_bot(session_prompt)