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

AnthropicGenerator

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

NameAnthropicGenerator
Sourcehttps://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/anthropic
Most common position in a pipelineAfter aΒ PromptBuilder
Mandatory input variablesβ€œprompt”: A string containing the prompt for the LLM
Output variablesβ€œreplies”: A list of strings with all the replies generated by the LLM

”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Β models such asΒ claude-3-5-sonnet-20240620,claude-3-opus-20240229,Β claude-3-haiku-20240307, and similar. Although these LLMs are called chat models, the main prompt interface works with the string prompts. Check out the most recent full list in theΒ Anthropic documentation.

Parameters

AnthropicGeneratorΒ 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")

AnthropicGenerator 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 to run() method. For more details on the parameters supported by the Anthropic API, seeΒ Anthropic documentation.

Finally, the component run method requiresΒ a single string prompt to generate text.

Streaming

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

Usage

InstallΒ the anthropic-haystackΒ package to use theΒ AnthropicGenerator:

pip install anthropic-haystack

On its own

from haystack_integrations.components.generators.anthropic import AnthropicGenerator

generator = AnthropicGenerator()
print(generator.run("What's Natural Language Processing? Be brief."))

In a pipeline

You can also useΒ AnthropicGeneratorΒ with the Anthropic models in your pipeline.

from haystack import Pipeline
from haystack.components.builders import PromptBuilder
from haystack_integrations.components.generators.anthropic import AnthropicGenerator
from haystack.utils import Secret

template = """
You are an assistant giving out valuable information to language learners.
Answer this question, be brief.

Question: {{ query }}?
"""

pipe = Pipeline()
pipe.add_component("prompt_builder", PromptBuilder(template))
pipe.add_component("llm", AnthropicGenerator(Secret.from_env_var("ANTHROPIC_API_KEY")))
pipe.connect("prompt_builder", "llm")

query = "What language is spoke in Germany?"
res = pipe.run(data={"prompt_builder": {"query": {query}}})
print(res)


Related Links

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