Asynchronous use of the library

In the latest version of the OpenAI Python library, the acreate method has been removed. Instead, you can use the AsyncOpenAI class to make asynchronous calls. Here’s an example of how you can use it:

from openai import AsyncOpenAI

client = AsyncOpenAI()

response = await client.chat.completions.create(
            model="gpt-4",
            messages=messages,
            tools=functions,
            temperature=0.0,
            tool_choice=None
        )

This code was found in a forum post here.

Please note that you might encounter some issues when using the AsyncOpenAI class at scale. For example, creating new clients per request might lead to a memory leak, and reusing a global AsyncOpenAI client across requests might lead to httpx.PoolTimeout s. This information was found in another Github issue on the OpenAI Python API library.

5 Likes