Async client
AsyncAICPClient is the async counterpart to AICPClient. It uses httpx's async transport and exposes the same namespaces (auth, chat, providers, routing, projects, etc.) with async def methods.
Setup
from aicp import AsyncAICPClient
client = AsyncAICPClient(
api_key="aicp-...",
base_url="https://your-gateway",
timeout=30.0,
)
Context manager (recommended)
async with AsyncAICPClient(
api_key="aicp-...",
base_url="https://your-gateway",
) as client:
response = await client.chat.complete(
model="auto",
messages=[{"role": "user", "content": "Hello!"}],
)
Example: concurrent requests
import asyncio
from aicp import AsyncAICPClient
async def main():
async with AsyncAICPClient(api_key="aicp-...", base_url="https://your-gateway") as client:
prompts = [
"Summarise the French Revolution in one sentence.",
"What is the boiling point of water in Kelvin?",
"Name three uses of transformer models.",
]
tasks = [
client.chat.complete(
model="auto",
messages=[{"role": "user", "content": p}],
)
for p in prompts
]
responses = await asyncio.gather(*tasks)
for prompt, response in zip(prompts, responses):
print(f"Q: {prompt}")
print(f"A: {response['choices'][0]['message']['content']}\n")
asyncio.run(main())
FastAPI integration
from contextlib import asynccontextmanager
from fastapi import FastAPI
from aicp import AsyncAICPClient
client: AsyncAICPClient
@asynccontextmanager
async def lifespan(app: FastAPI):
global client
client = AsyncAICPClient(api_key="aicp-...", base_url="https://your-gateway")
yield
await client.close()
app = FastAPI(lifespan=lifespan)
@app.post("/ask")
async def ask(question: str):
response = await client.chat.complete(
model="auto",
messages=[{"role": "user", "content": question}],
)
return {"answer": response["choices"][0]["message"]["content"]}
Differences from the sync client
AICPClient | AsyncAICPClient | |
|---|---|---|
| Methods | def | async def |
| Transport | httpx.Client | httpx.AsyncClient |
chat.stream() | Iterator[str] | AsyncIterator[str] |
| Context manager | with | async with |
close() | sync | await client.close() |