Files
wehub-resource-sync c56bef871b
Sync docs with Docusaurus / sync (push) Waiting to run
Tests / Check if changed (push) Waiting to run
Tests / format (push) Blocked by required conditions
Tests / check-imports (push) Blocked by required conditions
Tests / Unit / macos-latest (push) Blocked by required conditions
Tests / Unit / ubuntu-latest (push) Blocked by required conditions
Tests / Unit / windows-latest (push) Blocked by required conditions
Tests / mypy (push) Blocked by required conditions
Tests / Integration / ubuntu-latest (push) Blocked by required conditions
Tests / Integration / macos-latest (push) Blocked by required conditions
Tests / Integration / windows-latest (push) Blocked by required conditions
Tests / notify-slack-on-failure (push) Blocked by required conditions
Tests / Mark tests as completed (push) Blocked by required conditions
Docker image release / Build base image (push) Waiting to run
CodeQL / Analyze (python) (push) Has been cancelled
Update Platform Components Table / update (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:22:28 +08:00

139 lines
4.6 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
title: "LiteLLM"
id: integrations-litellm
description: "LiteLLM integration for Haystack"
slug: "/integrations-litellm"
---
## haystack_integrations.components.generators.litellm.chat.chat_generator
### LiteLLMChatGenerator
Completes chats using any of 100+ LLM providers via LiteLLM.
LiteLLM routes to OpenAI, Anthropic, Google, AWS Bedrock, Azure, Cohere,
Mistral, Groq, and many more through a single unified interface.
Model names use LiteLLM format: `provider/model-name`, e.g.
`anthropic/claude-sonnet-4-20250514`, `openai/gpt-4o`,
`bedrock/anthropic.claude-3-5-sonnet-20241022-v2:0`.
See https://docs.litellm.ai/docs/providers for the full list.
Usage example:
```python
from haystack_integrations.components.generators.litellm import LiteLLMChatGenerator
from haystack.dataclasses import ChatMessage
generator = LiteLLMChatGenerator(
model="anthropic/claude-sonnet-4-20250514",
generation_kwargs={"max_tokens": 1024, "temperature": 0.7},
)
messages = [
ChatMessage.from_system("You are a helpful assistant"),
ChatMessage.from_user("What's Natural Language Processing?"),
]
result = generator.run(messages=messages)
print(result["replies"][0].text)
```
#### __init__
```python
__init__(
*,
api_key: Secret | None = None,
model: str = "openai/gpt-4o",
streaming_callback: StreamingCallbackT | None = None,
api_base_url: str | None = None,
generation_kwargs: dict[str, Any] | None = None,
tools: ToolsType | None = None
) -> None
```
Create a LiteLLMChatGenerator instance.
**Parameters:**
- **api_key** (<code>Secret | None</code>) The API key for the provider. Optional: when not set, LiteLLM resolves
credentials itself from the provider's standard environment variable
(e.g. `ANTHROPIC_API_KEY`, `OPENAI_API_KEY`). Pass a `Secret` only
when you want Haystack to manage and serialize the key explicitly.
- **model** (<code>str</code>) The model name in LiteLLM format (provider/model-name).
- **streaming_callback** (<code>StreamingCallbackT | None</code>) A callback function invoked with each new StreamingChunk.
- **api_base_url** (<code>str | None</code>) Custom API base URL (e.g. for a self-hosted LiteLLM proxy).
- **generation_kwargs** (<code>dict\[str, Any\] | None</code>) Additional parameters passed to litellm.completion().
See https://docs.litellm.ai/docs/completion/input for details.
- **tools** (<code>ToolsType | None</code>) A list of Tool / Toolset objects the model can prepare calls for.
#### run
```python
run(
messages: list[ChatMessage] | str,
streaming_callback: StreamingCallbackT | None = None,
generation_kwargs: dict[str, Any] | None = None,
*,
tools: ToolsType | None = None
) -> dict[str, list[ChatMessage]]
```
Invoke chat completion via LiteLLM.
**Parameters:**
- **messages** (<code>list\[ChatMessage\] | str</code>) Input messages as ChatMessage instances.
If a string is provided, it is converted to a list containing a ChatMessage with user role.
- **streaming_callback** (<code>StreamingCallbackT | None</code>) Override the streaming callback for this call.
- **generation_kwargs** (<code>dict\[str, Any\] | None</code>) Override generation parameters for this call.
- **tools** (<code>ToolsType | None</code>) Override tools for this call.
**Returns:**
- <code>dict\[str, list\[ChatMessage\]\]</code> A dict with key `replies` containing ChatMessage instances.
#### run_async
```python
run_async(
messages: list[ChatMessage] | str,
streaming_callback: StreamingCallbackT | None = None,
generation_kwargs: dict[str, Any] | None = None,
*,
tools: ToolsType | None = None
) -> dict[str, list[ChatMessage]]
```
Async version of run(). Invoke chat completion via LiteLLM.
**Parameters:**
- **messages** (<code>list\[ChatMessage\] | str</code>) Input messages as ChatMessage instances.
If a string is provided, it is converted to a list containing a ChatMessage with user role.
- **streaming_callback** (<code>StreamingCallbackT | None</code>) Override the streaming callback for this call.
- **generation_kwargs** (<code>dict\[str, Any\] | None</code>) Override generation parameters for this call.
- **tools** (<code>ToolsType | None</code>) Override tools for this call.
**Returns:**
- <code>dict\[str, list\[ChatMessage\]\]</code> A dict with key `replies` containing ChatMessage instances.
#### to_dict
```python
to_dict() -> dict[str, Any]
```
Serialize this component to a dictionary.
#### from_dict
```python
from_dict(data: dict[str, Any]) -> LiteLLMChatGenerator
```
Deserialize a component from a dictionary.