forked from daveshap/PythonGPT3Tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhello_world.py
30 lines (22 loc) · 843 Bytes
/
hello_world.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
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-002', temp=0.7, top_p=1.0, tokens=400, freq_pen=0.0, pres_pen=0.0, stop=['<<END>>']):
prompt = prompt.encode(encoding='ASCII',errors='ignore').decode()
response = openai.Completion.create(
engine=engine,
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__':
prompt = 'Write a list of famous American actors:'
response = gpt3_completion(prompt)
print(response)