ant_ai.llm.protocol
ChatLLM
Bases: Protocol
Interface for a language model that generates chat responses.
Every backend is constructed as Backend(model, *, api_key=None,
api_base=None) and exposes the three as attributes. A caller can therefore
point any backend at its own deployment (a vLLM server, a proxy, …) without
knowing which one it holds, and keep the secret under its own name instead
of the one the provider's SDK reads from the environment.
Attributes:
| Name | Type | Description |
|---|---|---|
model |
str
|
Model identifier in the backend's own naming scheme. |
api_key |
str | None
|
Credential for the endpoint, or None to let the backend fall back to its provider's environment variable. |
api_base |
str | None
|
Endpoint URL, or None for the provider's default. |
Source code in src/ant_ai/llm/protocol.py
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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | |
invoke
invoke(
messages: list[Message],
*,
ctx: InvocationContext | None = None,
tools: list | None = None,
response_format: dict | type[BaseModel] | None = None,
) -> ChatLLMResponse
Send messages and return a complete response synchronously.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
messages
|
list[Message]
|
Conversation history to send to the model. |
required |
ctx
|
InvocationContext | None
|
Invocation context, or None if not available. |
None
|
tools
|
list | None
|
Tool schemas to expose to the model, or None for no tools. |
None
|
response_format
|
dict | type[BaseModel] | None
|
Constrain the output to a JSON schema or Pydantic model. |
None
|
Returns:
| Type | Description |
|---|---|
ChatLLMResponse
|
The complete model response. |
Source code in src/ant_ai/llm/protocol.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | |
ainvoke
async
ainvoke(
messages: list[Message],
*,
ctx: InvocationContext | None = None,
tools: list | None = None,
response_format: dict | type[BaseModel] | None = None,
) -> ChatLLMResponse
Send messages and return a complete response asynchronously.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
messages
|
list[Message]
|
Conversation history to send to the model. |
required |
ctx
|
InvocationContext | None
|
Invocation context, or None if not available. |
None
|
tools
|
list | None
|
Tool schemas to expose to the model, or None for no tools. |
None
|
response_format
|
dict | type[BaseModel] | None
|
Constrain the output to a JSON schema or Pydantic model. |
None
|
Returns:
| Type | Description |
|---|---|
ChatLLMResponse
|
The complete model response. |
Source code in src/ant_ai/llm/protocol.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | |
stream
stream(
messages: list[Message],
*,
ctx: InvocationContext | None = None,
tools: list | None = None,
response_format: dict | type[BaseModel] | None = None,
) -> AsyncIterator[ChatLLMStreamChunk]
Send messages and stream the response as chunks.
Backends that generate tokens incrementally (e.g. OpenAI, LiteLLM)
override this for true token-by-token delivery. The default here
falls back to ainvoke() and re-emits its result as a single chunk,
so any ChatLLM implementation — including test doubles and
backends with no incremental API — supports .stream() without
extra work, just without the live granularity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
messages
|
list[Message]
|
Conversation history to send to the model. |
required |
ctx
|
InvocationContext | None
|
Invocation context, or None if not available. |
None
|
tools
|
list | None
|
Tool schemas to expose to the model, or None for no tools. |
None
|
response_format
|
dict | type[BaseModel] | None
|
Constrain the output to a JSON schema or Pydantic model. |
None
|
Returns:
| Type | Description |
|---|---|
AsyncIterator[ChatLLMStreamChunk]
|
An async iterator of response chunks. |
Source code in src/ant_ai/llm/protocol.py
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | |