async openai calls with python

In the previous post we explored asyncio (link), here we build on that to create async openai calls

The uses the standard python “asyncio” library and the function asyncio.gather to concurrently call openai 3 times (you can add as many calls as you like).

Python
import asyncio
import getpass

from openai import AsyncOpenAI

# this will request the password from the user in a prompt
# in deployed settings you could get this from an environment variable
client = AsyncOpenAI(api_key=getpass.getpass())

async def openai_chat(prompt: str) -> str:
    chat_completion = await client.chat.completions.create(
        messages=[
            {
                "role": "user",
                "content": prompt,
            }
        ],
        model="gpt-3.5-turbo",
    )
    return chat_completion.choices[0].message.content.strip()

async def main() -> None:
    results = await asyncio.gather(
        openai_chat("Say this is test1"),
        openai_chat("Say this is test2"),
        openai_chat("Say this is test3")
    )
    for result in results:
        print(result)

# This is necessary if running in a jupyter notebook
loop = asyncio.get_running_loop()
await loop.create_task(main())

# Use this for standalone python script
# asyncio.run(main())