DocumentationAPI ReferenceπŸ““ TutorialsπŸ§‘β€πŸ³ Cookbook🀝 IntegrationsπŸ’œ Discord

AnthropicChatGenerator

This component enables chat completions using Anthropic large language models (LLMs).

NameAnthropicChatGenerator
Sourcehttps://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/anthropic
Most common position in a pipelineAfter aΒ ChatPromptBuilder
Mandatory input variablesβ€œmessages” A list ofΒ ChatMessageΒ objects
Output variables"replies": A list of ChatMessage objects

”meta”: A list of dictionaries with the metadata associated with each reply, such as token count, finish reason, and so on

Overview

This integration supports AnthropicΒ chatΒ models such asΒ claude-3-5-sonnet-20240620,claude-3-opus-20240229,Β claude-3-haiku-20240307, and similar. Check out the most recent full list inΒ Anthropic documentation.

Parameters

AnthropicChatGeneratorΒ needs an Anthropic API key to work. You can provide this key in:

  • TheΒ ANTHROPIC_API_KEYΒ environment variable (recommended)
  • TheΒ api_keyΒ init parameter and Haystack Secret API: Secret.from_token("your-api-key-here")

AnthropicChatGenerator requires a prompt to generate text, but you can pass any text generation parameters available in the Anthropic Messaging API method directly to this component using the generation_kwargs parameter, both at initialization and when running the component. For more details on the parameters supported by the Anthropic API, see theΒ Anthropic documentation.

Finally, the component needs a list ofΒ ChatMessageΒ objects to operate.Β ChatMessageΒ is a data class that contains a message, a role (who generated the message, such asΒ user,Β assistant,Β system,Β function), and optional metadata.

Only text input modality is supported at this time.

Streaming

AnthropicChatGeneratorΒ supports streaming the tokens from the LLM directly in output. To do so, pass a function to theΒ streaming_callbackΒ init parameter.

Usage

InstallΒ theanthropic-haystackΒ package to use theΒ AnthropicChatGenerator:

pip install anthropic-haystack

On its own

from haystack_integrations.components.generators.anthropic import AnthropicChatGenerator
from haystack.dataclasses import ChatMessage

generator = AnthropicChatGenerator()
message = ChatMessage.from_user("What's Natural Language Processing? Be brief.")
print(generator.run([message]))

In a pipeline

You can also useΒ AnthropicChatGeneratorwith the Anthropic chat models in your pipeline.

from haystack import Pipeline
from haystack.components.builders import ChatPromptBuilder
from haystack.dataclasses import ChatMessage
from haystack_integrations.components.generators.anthropic import AnthropicChatGenerator
from haystack.utils import Secret

pipe = Pipeline()
pipe.add_component("prompt_builder", ChatPromptBuilder())
pipe.add_component("llm", AnthropicChatGenerator(Secret.from_env_var("ANTHROPIC_API_KEY")))
pipe.connect("prompt_builder", "llm")

country = "Germany"
system_message = ChatMessage.from_system("You are an assistant giving out valuable information to language learners.")
messages = [system_message, ChatMessage.from_user("What's the official language of {{ country }}?")]

res = pipe.run(data={"prompt_builder": {"template_variables": {"country": country}, "template": messages}})
print(res)


Related Links

Check out the API reference in the GitHub repo or in our docs: