英文:
OpenAI api - asynchronous API calls
问题
I understand your request. Here is the translated content:
我使用OpenAI API工作。我已经从PowerPoint演示文稿中提取了幻灯片文本,并为每张幻灯片编写了提示。现在,我想进行异步API调用,以便同时处理所有幻灯片。
这是异步主函数中的代码:
for prompt in prompted_slides_text:
task = asyncio.create_task(api_manager.generate_answer(prompt))
tasks.append(task)
results = await asyncio.gather(*tasks)
而这是generate_answer函数:
@staticmethod
async def generate_answer(prompt):
"""
向OpenAI API发送提示并获取答案。
:param prompt: 要发送的提示。
:return: 答案。
"""
completion = await openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": prompt}]
)
return completion.choices[0].message.content
问题是:
> 对象OpenAIObject不能在'await'表达式中使用
我不知道如何在generate_answer函数中等待响应。
非常感谢任何帮助!
英文:
I work with the OpenAI API. I have extracted slides text from a PowerPoint presentation, and written a prompt for each slide. Now, I want to make asynchronous API calls,
so that all the slides are processed at the same time.
this is the code from the async main function:
for prompt in prompted_slides_text:
task = asyncio.create_task(api_manager.generate_answer(prompt))
tasks.append(task)
results = await asyncio.gather(*tasks)
and this is generate_answer function:
@staticmethod
async def generate_answer(prompt):
"""
Send a prompt to OpenAI API and get the answer.
:param prompt: the prompt to send.
:return: the answer.
"""
completion = await openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": prompt}]
)
return completion.choices[0].message.content
the problem is:
> object OpenAIObject can't be used in 'await' expression
and I don't know how to await for the response in generate_answer function
Would appreciate any help!
答案1
得分: 12
你必须使用 openai.ChatCompletion.acreate
来异步使用API。
它在他们的Github上有文档 - https://github.com/openai/openai-python#async-api
英文:
You have to use openai.ChatCompletion.acreate
to use the api asynchronously.
It's documented on their Github - https://github.com/openai/openai-python#async-api
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论