The AsyncClient provides the same functionality as the regular Client but is designed for use in asynchronous applications. Here are the key differences:
All methods return coroutines that must be awaited
Streaming responses use async for instead of regular for loops
The AsyncClient provides async versions of all the methods available in the regular Client. The parameters and return types are the same, but the methods must be awaited.
import asyncioimport goodfire# Initialize the async clientclient = goodfire.AsyncClient('{YOUR_API_KEY}')# Create a non-streaming completionasync def main(): response = await client.chat.completions.create( messages=[ {"role": "user", "content": "What is the capital of France?"} ], model="meta-llama/Llama-3.3-70B-Instruct" ) print(response.choices[0].message["content"])# Run the async functionawait main()
import goodfireimport asynciovariant = goodfire.Variant("meta-llama/Meta-Llama-3.1-8B-Instruct")client = goodfire.AsyncClient(api_key=GOODFIRE_API_KEY)async def chat_completion(message: str, index: int) -> str: response = await client.chat.completions.create( messages=[ {"role": "user", "content": message}, ], model=variant, ) print(f"Response {index}: {response.choices[0].message['content']}") return responseasync def main(): # Create multiple tasks to run concurrently messages = [ "Who is this?", "What is your favorite color?", "Tell me a joke", "Write a haiku about programming", "What's the capital of France?", "Explain quantum computing in one sentence", "Give me a random fact about elephants", "Write a short limerick", "What's the meaning of life?", "Tell me about the Renaissance period", "Give me a cooking tip", "What's your favorite book?", "Explain photosynthesis simply", "Tell me an interesting space fact", "Give me a riddle" ] tasks = [chat_completion(msg, i) for i, msg in enumerate(messages)] results = await asyncio.gather(*tasks) # # Print results # for msg, response in zip(messages, results): # print(f"Question: {msg}") # print(f"Answer: {response}\n") asyncio.run(main())
Remember to always run your async code within an async context using asyncio.run() or within an existing async application framework.