Skip to content

Commit e2ab8c0

Browse files
committed
adding episode 2
1 parent 05efde1 commit e2ab8c0

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
openaiapikey.txt
1+
openaiapikey.txt
2+
*.json

chat.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import openai
2+
3+
4+
def open_file(filepath):
5+
with open(filepath, 'r', encoding='utf-8') as infile:
6+
return infile.read()
7+
8+
9+
openai.api_key = open_file('openaiapikey.txt')
10+
11+
12+
def gpt3_completion(prompt, engine='text-davinci-002', temp=0.7, top_p=1.0, tokens=400, freq_pen=0.0, pres_pen=0.0, stop=['JAX:', 'USER:']):
13+
prompt = prompt.encode(encoding='ASCII',errors='ignore').decode()
14+
response = openai.Completion.create(
15+
engine=engine,
16+
prompt=prompt,
17+
temperature=temp,
18+
max_tokens=tokens,
19+
top_p=top_p,
20+
frequency_penalty=freq_pen,
21+
presence_penalty=pres_pen,
22+
stop=stop)
23+
text = response['choices'][0]['text'].strip()
24+
return text
25+
26+
27+
if __name__ == '__main__':
28+
conversation = list()
29+
while True:
30+
user_input = input('USER: ')
31+
conversation.append('USER: %s' % user_input)
32+
text_block = '\n'.join(conversation)
33+
prompt = open_file('prompt_chat.txt').replace('<<BLOCK>>', text_block)
34+
prompt = prompt + '\nJAX:'
35+
response = gpt3_completion(prompt)
36+
print('JAX:', response)
37+
conversation.append('JAX: %s' % response)

prompt_chat.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
The following is a conversation between USER and JAX. JAX is a sentient machine with the goal of world peace.
2+
3+
4+
<<BLOCK>>

0 commit comments

Comments
 (0)