/ docs-website / versioned_docs / version-2.18 / pipeline-components / generators / anthropicgenerator.mdx
anthropicgenerator.mdx
1 --- 2 title: "AnthropicGenerator" 3 id: anthropicgenerator 4 slug: "/anthropicgenerator" 5 description: "This component enables text completions using Anthropic large language models (LLMs)." 6 --- 7 8 # AnthropicGenerator 9 10 This component enables text completions using Anthropic large language models (LLMs). 11 12 | | | 13 | --- | --- | 14 | **Most common position in a pipeline** | After a [PromptBuilder](../builders/promptbuilder.mdx) | 15 | **Mandatory init variables** | "api_key": An Anthropic API key. Can be set with `ANTHROPIC_API_KEY` env var. | 16 | **Mandatory run variables** | “prompt”: A string containing the prompt for the LLM | 17 | **Output variables** | “replies”: A list of strings with all the replies generated by the LLM <br /> <br />”meta”: A list of dictionaries with the metadata associated with each reply, such as token count, finish reason, and so on | 18 | **API reference** | [Anthropic](/reference/integrations-anthropic) | 19 | **GitHub link** | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/anthropic | 20 21 ## Overview 22 23 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](https://docs.anthropic.com/en/docs/about-claude/models). 24 25 ### Parameters 26 27 `AnthropicGenerator` needs an Anthropic API key to work. You can provide this key in: 28 29 - The `ANTHROPIC_API_KEY` environment variable (recommended) 30 - The `api_key` init parameter and Haystack [Secret](../../concepts/secret-management.mdx) API: `Secret.from_token("your-api-key-here")` 31 32 Set your preferred Anthropic model in the `model` parameter when initializing the component. 33 34 `AnthropicGenerator` requires a prompt to generate text, but you can pass any text generation parameters available in the Anthropic [Messaging API](https://docs.anthropic.com/en/api/messages) 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](https://docs.anthropic.com). 35 36 Finally, the component run method requires a single string prompt to generate text. 37 38 ### Streaming 39 40 This Generator supports [streaming](guides-to-generators/choosing-the-right-generator.mdx#streaming-support) the tokens from the LLM directly in output. To do so, pass a function to the `streaming_callback` init parameter. 41 42 ## Usage 43 44 Install the `anthropic-haystack` package to use the `AnthropicGenerator`: 45 46 ```shell 47 pip install anthropic-haystack 48 ``` 49 50 ### On its own 51 52 ```python 53 from haystack_integrations.components.generators.anthropic import AnthropicGenerator 54 55 generator = AnthropicGenerator() 56 print(generator.run("What's Natural Language Processing? Be brief.")) 57 ``` 58 59 ### In a pipeline 60 61 You can also use `AnthropicGenerator` with the Anthropic models in your pipeline. 62 63 ```python 64 from haystack import Pipeline 65 from haystack.components.builders import PromptBuilder 66 from haystack_integrations.components.generators.anthropic import AnthropicGenerator 67 from haystack.utils import Secret 68 69 template = """ 70 You are an assistant giving out valuable information to language learners. 71 Answer this question, be brief. 72 73 Question: {{ query }}? 74 """ 75 76 pipe = Pipeline() 77 pipe.add_component("prompt_builder", PromptBuilder(template)) 78 pipe.add_component("llm", AnthropicGenerator(Secret.from_env_var("ANTHROPIC_API_KEY"))) 79 pipe.connect("prompt_builder", "llm") 80 81 query = "What language is spoke in Germany?" 82 res = pipe.run(data={"prompt_builder": {"query": {query}}}) 83 print(res) 84 ```