-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpt3.py
36 lines (30 loc) · 1.14 KB
/
gpt3.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 os, time,re
import openai
def gpt3(input_text,_,max_length=25,num_return_sequences=15,stop=['#'],repetition_penalty = 0.5,top_p = 1,temperature = 0.8, block_linebreak = False,replace_linebreaks=False):
if replace_linebreaks:
input_text = re.sub('\n',' ',input_text).strip()
repetition_penalty = 1.7#(repetition_penalty-1)*2.5
print('input text')
print(input_text)
while True:
openai.api_key = os.environ.get('OPENAI_API_KEY')
openai.organization = os.environ.get('OPENAI_API_ID')
try:
responses = openai.Completion.create(
engine="text-davinci-003",
prompt=input_text,
temperature=temperature,
max_tokens=max_length,
top_p=top_p,
frequency_penalty=repetition_penalty,
presence_penalty=0.0,
stop=stop,
n=num_return_sequences
)
output = []
for response in responses['choices']:
output.append(response['text'])
return output
except:
print('failed to connect with api')
time.sleep(5)