OpenAI API – 异步 API 调用

huangapple go评论67阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年5月22日 18:23:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76305207.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定