Calling Chatgpt api
Ref: https://platform.openai.com/docs/guides/gpt/chat-completions-api
def call_api(prompt):
try:
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo-0613",
messages=[
{
"role": "user",
"content": prompt
}
],
temperature=0.,
max_tokens=30,
top_p=1,
stop=[" Human:", " AI:", "\n", "\n\n"]
)
return response['choices'][0]['message']['content']
except:
return None
Devide iterative datas in to chunks
def chunkwise_iterate(data, chunk_size):
for i in range(0, len(data), chunk_size):
yield data[i:i + chunk_size]
chunk_datas = chunkwise_iterate(all_hyp_datas, 100)
chunks = chunkwise_iterate(all_hyp_datas, 100)
for chunk_i, sub_datas in enumerate(chunks):
for data in tqdm(sub_datas):
prompt = "Give me a recipe to make applepie."
while True:
time.sleep(0.5)
response = call_api(prompt)
if response is not None:
break
else:
time.sleep(3)
print(response)