-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathdemo_async_client_httpx.py
More file actions
62 lines (45 loc) · 1.92 KB
/
demo_async_client_httpx.py
File metadata and controls
62 lines (45 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
"""
Run chat twice: once with the SDK default async HTTP stack (HttpxAiohttpClient),
once with an explicit httpx.AsyncClient.
Requires CO_API_KEY in the environment. Optional: CO_MODEL (defaults to command-r-plus).
"""
from __future__ import annotations
import asyncio
import os
import sys
import httpx
from cohere import AsyncClient
def _api_key() -> str:
key = os.getenv("CO_API_KEY")
if not key:
print("Set CO_API_KEY to your Cohere API key.", file=sys.stderr)
sys.exit(1)
return key
def _model() -> str:
return os.getenv("CO_MODEL", "command-a-03-2025")
async def chat_default_httpx_aiohttp() -> None:
"""Uses the SDK default (aiohttp-backed HttpxAiohttpClient)."""
async with AsyncClient(api_key=_api_key()) as client:
inner = client._client_wrapper.httpx_client.httpx_client
print(f"default inner client: {type(inner).__module__}.{type(inner).__name__}")
response = await client.chat(message="Hello", model=_model())
print("default — reply:", (response.text or "")[:200])
async def chat_custom_httpx() -> None:
"""Uses a caller-provided httpx.AsyncClient."""
custom = httpx.AsyncClient(timeout=120.0)
try:
async with AsyncClient(api_key=_api_key(), httpx_client=custom) as client:
inner = client._client_wrapper.httpx_client.httpx_client
print(f"custom inner client: {type(inner).__module__}.{type(inner).__name__}")
assert inner is custom
response = await client.chat(message="Hello", model=_model())
print("custom — reply:", (response.text or "")[:200])
finally:
# Context manager already closed `custom` when using httpx_client=; this is a no-op if closed.
if not custom.is_closed:
await custom.aclose()
async def main() -> None:
await chat_default_httpx_aiohttp()
await chat_custom_httpx()
if __name__ == "__main__":
asyncio.run(main())