Join us for Advent of Haystack this December 🌲
Maintained by deepset

Integration: Anthropic

Use Anthropic Models with Haystack


You can use Anhtropic Claude in your Haystack pipelines with the PromptNode, which can also be used with and Agent.


pip install farm-haystack[inference]


You can use Anthropic models in various ways:

Using Claude with PromptNode

To use Claude for prompting and generating answers, initialize a PromptNode with the model name, your Anthrpic API key and a prompt template. You can then use this PromptNode in a question answering pipeline to generate answers based on the given context.

Below is the example of a PromptNode that uses a custom PromptTemplate

from haystack.nodes import PromptTemplate, PromptNode

prompt_text = """
Answer the following question.
Question: {query}

prompt_template = PromptTemplate(prompt=prompt_text)

prompt_node = PromptNode(
    model_name_or_path = "claude-2",
    model_kwargs={"stream": True},

Using Claude for Agents

To use Calude for an Agent, simply provide a PromptNode that uses Claude to the Agent:

from haystack.agents import Agent
from haystack.nodes import PromptNode

prompt_node = PromptNode(model_name_or_path="YOUR_ANTHROPIC_API_KEY", api_key=anthropic_key, stop_words=["Observation:"])
agent = Agent(prompt_node=prompt_node)