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