Skip to content

Commit

Permalink
Update anthropic.md (#254)
Browse files Browse the repository at this point in the history
- update examples to Haystack 2.3+
  • Loading branch information
vblagoje authored Aug 14, 2024
1 parent 58de760 commit 54b4856
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions integrations/anthropic.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ Before using, make sure to set the `ANTHROPIC_API_KEY` environment variable.
Below is an example RAG Pipeline where we answer a predefined question using the contents from the below mentioned URL pointing to Anthropic prompt engineering guide. We fetch the contents of the URL and generate an answer with the `AnthropicChatGenerator`.

```python
# To run this example, you will need to set a `ANTHROPIC_API_KEY` environment variable.

from haystack import Pipeline
from haystack.components.builders import ChatPromptBuilder
from haystack.components.converters import HTMLToDocument
Expand All @@ -69,18 +71,23 @@ from haystack_integrations.components.generators.anthropic import AnthropicChatG

messages = [
ChatMessage.from_system("You are a prompt expert who answers questions based on the given documents."),
ChatMessage.from_user("Here are the documents: {{documents}} \\n Answer: {{query}}"),
ChatMessage.from_user(
"Here are the documents:\n"
"{% for d in documents %} \n"
" {{d.content}} \n"
"{% endfor %}"
"\nAnswer: {{query}}"
),
]

rag_pipeline = Pipeline()
rag_pipeline.add_component("fetcher", LinkContentFetcher())
rag_pipeline.add_component("converter", HTMLToDocument())
rag_pipeline.add_component("prompt_builder", ChatPromptBuilder())
rag_pipeline.add_component("prompt_builder", ChatPromptBuilder(variables=["documents"]))
rag_pipeline.add_component(
"llm",
AnthropicChatGenerator(
api_key=Secret.from_env_var("ANTHROPIC_API_KEY"),
model="claude-3-sonnet-20240229",
streaming_callback=print_streaming_chunk,
),
)
Expand All @@ -90,10 +97,10 @@ rag_pipeline.connect("fetcher", "converter")
rag_pipeline.connect("converter", "prompt_builder")
rag_pipeline.connect("prompt_builder.prompt", "llm.messages")

question = "What are the best practices in prompt engineering?"
question = "When should we use prompt engineering and when should we fine-tune?"
rag_pipeline.run(
data={
"fetcher": {"urls": ["https://docs.anthropic.com/claude/docs/prompt-engineering"]},
"fetcher": {"urls": ["https://docs.anthropic.com/en/docs/build-with-claude/prompt-engineering/overview"]},
"prompt_builder": {"template_variables": {"query": question}, "template": messages},
}
)
Expand All @@ -106,7 +113,7 @@ Below is an example of using `AnthropicGenerator`:
```python
from haystack_integrations.components.generators.anthropic import AnthropicGenerator

client = AnthropicGenerator(model="claude-2.1")
client = AnthropicGenerator()
response = client.run("What's Natural Language Processing? Be brief.")
print(response)

Expand Down

0 comments on commit 54b4856

Please sign in to comment.