-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchat.py
36 lines (30 loc) · 1.18 KB
/
chat.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
import openai
def open_file(filepath) :
with open(filepath, 'r', encoding='utf-8') as infile :
return infile.read()
openai.api_key=open_file('openaiapikey.txt')
def gpt3_completion(prompt, engine='text-davinci-003', temp=0.7, top_p = 1.0, tokens =400, freq_pen=0.0, pres_pen=0.0, stop=['JAX: ', 'USER: ']) :
prompt = prompt.encode(encoding='ASCII', errors='ignore').decode()
response = openai.Completion.create(
#engine=engine,
engine='text-ada-001',
prompt=prompt,
temperature=temp,
max_tokens=tokens,
top_p=top_p,
frequency_penalty=freq_pen,
presence_penalty=pres_pen,
stop=stop)
text = response['choices'][0]['text'].strip()
return text
if __name__ == '__main__' :
conversation = list()
while True:
user_input = input('USER: ')
conversation.append('USER: %s' % user_input)
text_block = '\n'.join(conversation)
prompt = open_file('prompt_chat.txt').replace('<<BLOCK>>', text_block)
prompt = prompt + '\JAX: '
response = gpt3_completion(prompt)
print('JAX: ', response)
conversation.append('JAX: %s' % response)