Completion
Completion
any_llm.completion(model, messages, *, tools=None, tool_choice=None, max_turns=None, temperature=None, top_p=None, max_tokens=None, response_format=None, stream=None, n=None, stop=None, presence_penalty=None, frequency_penalty=None, seed=None, api_key=None, api_base=None, timeout=None, user=None, **kwargs)
Create a chat completion.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model
|
str
|
Model identifier in format 'provider/model' (e.g., 'mistral/mistral-small') |
required |
messages
|
list[dict[str, Any]]
|
List of messages for the conversation |
required |
tools
|
Optional[List[Union[dict[str, Any], Callable[..., Any]]]]
|
List of tools for tool calling. Can be Python callables or OpenAI tool format dicts |
None
|
tool_choice
|
Optional[Union[str, dict[str, Any]]]
|
Controls which tools the model can call |
None
|
max_turns
|
Optional[int]
|
Maximum number of tool execution turns |
None
|
temperature
|
Optional[float]
|
Controls randomness in the response (0.0 to 2.0) |
None
|
top_p
|
Optional[float]
|
Controls diversity via nucleus sampling (0.0 to 1.0) |
None
|
max_tokens
|
Optional[int]
|
Maximum number of tokens to generate |
None
|
response_format
|
dict[str, Any] | type[BaseModel] | None
|
Format specification for the response |
None
|
stream
|
Optional[bool]
|
Whether to stream the response |
None
|
n
|
Optional[int]
|
Number of completions to generate |
None
|
stop
|
Optional[Union[str, List[str]]]
|
Stop sequences for generation |
None
|
presence_penalty
|
Optional[float]
|
Penalize new tokens based on presence in text |
None
|
frequency_penalty
|
Optional[float]
|
Penalize new tokens based on frequency in text |
None
|
seed
|
Optional[int]
|
Random seed for reproducible results |
None
|
api_key
|
Optional[str]
|
API key for the provider |
None
|
api_base
|
Optional[str]
|
Base URL for the provider API |
None
|
timeout
|
Optional[Union[float, int]]
|
Request timeout in seconds |
None
|
user
|
Optional[str]
|
Unique identifier for the end user |
None
|
**kwargs
|
Any
|
Additional provider-specific parameters |
{}
|
Returns:
Type | Description |
---|---|
ChatCompletion | Stream[ChatCompletionChunk]
|
The completion response from the provider |
Source code in src/any_llm/api.py
any_llm.acompletion(model, messages, *, tools=None, tool_choice=None, max_turns=None, temperature=None, top_p=None, max_tokens=None, response_format=None, stream=None, n=None, stop=None, presence_penalty=None, frequency_penalty=None, seed=None, api_key=None, api_base=None, timeout=None, user=None, **kwargs)
async
Create a chat completion asynchronously.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model
|
str
|
Model identifier in format 'provider/model' (e.g., 'mistral/mistral-small') |
required |
messages
|
list[dict[str, Any]]
|
List of messages for the conversation |
required |
tools
|
Optional[List[Union[dict[str, Any], Callable[..., Any]]]]
|
List of tools for tool calling. Can be Python callables or OpenAI tool format dicts |
None
|
tool_choice
|
Optional[Union[str, dict[str, Any]]]
|
Controls which tools the model can call |
None
|
max_turns
|
Optional[int]
|
Maximum number of tool execution turns |
None
|
temperature
|
Optional[float]
|
Controls randomness in the response (0.0 to 2.0) |
None
|
top_p
|
Optional[float]
|
Controls diversity via nucleus sampling (0.0 to 1.0) |
None
|
max_tokens
|
Optional[int]
|
Maximum number of tokens to generate |
None
|
response_format
|
dict[str, Any] | type[BaseModel] | None
|
Format specification for the response |
None
|
stream
|
Optional[bool]
|
Whether to stream the response |
None
|
n
|
Optional[int]
|
Number of completions to generate |
None
|
stop
|
Optional[Union[str, List[str]]]
|
Stop sequences for generation |
None
|
presence_penalty
|
Optional[float]
|
Penalize new tokens based on presence in text |
None
|
frequency_penalty
|
Optional[float]
|
Penalize new tokens based on frequency in text |
None
|
seed
|
Optional[int]
|
Random seed for reproducible results |
None
|
api_key
|
Optional[str]
|
API key for the provider |
None
|
api_base
|
Optional[str]
|
Base URL for the provider API |
None
|
timeout
|
Optional[Union[float, int]]
|
Request timeout in seconds |
None
|
user
|
Optional[str]
|
Unique identifier for the end user |
None
|
**kwargs
|
Any
|
Additional provider-specific parameters |
{}
|
Returns:
Type | Description |
---|---|
ChatCompletion | Stream[ChatCompletionChunk]
|
The completion response from the provider |