Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions docs-website/reference/haystack-api/hooks_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,17 +110,29 @@ paths, construct a `FunctionHook` directly with both `function` and `async_funct

```python
from haystack.components.agents import Agent
from haystack.components.generators.chat import OpenAIChatGenerator
from haystack.hooks import hook
from haystack.components.agents.state import State
from haystack.dataclasses import ChatMessage
from haystack.tools import tool

@tool
def weather_tool(city: str) -> str:
'''Get the current weather for a given city.'''
return f"The weather in {city} is sunny."

@tool
def save(content: str) -> str:
'''Save content to durable storage.'''
return "Saved."

@hook
def require_save(state: State) -> None:
if state.get("tool_call_counts", {}).get("save", 0) == 0:
state.set("messages", [ChatMessage.from_system("You must call `save` before finishing.")])
state.set("continue_run", True)

agent = Agent(chat_generator=..., tools=[...], hooks={"on_exit": [require_save]})
agent = Agent(chat_generator=OpenAIChatGenerator(), tools=[weather_tool, save], hooks={"on_exit": [require_save]})
```

**Parameters:**
Expand Down Expand Up @@ -199,6 +211,8 @@ Register it on an `Agent` to confirm, modify, or reject tool calls before they r

```python
from haystack.components.agents import Agent
from haystack.components.generators.chat import OpenAIChatGenerator
from haystack.tools import tool
from haystack.hooks.human_in_the_loop import (
AlwaysAskPolicy,
BlockingConfirmationStrategy,
Expand All @@ -208,14 +222,19 @@ from haystack.hooks.human_in_the_loop import (
SimpleConsoleUI,
)

@tool
def delete_file(path: str) -> str:
'''Delete the file at the given path.'''
return f"Deleted {path}."

hook = ConfirmationHook(
confirmation_strategies={
"my_tool": BlockingConfirmationStrategy(
"delete_file": BlockingConfirmationStrategy(
confirmation_policy=NeverAskPolicy(), confirmation_ui=SimpleConsoleUI()
)
}
)
agent = Agent(chat_generator=..., tools=[...], hooks={"before_tool": [hook]})
agent = Agent(chat_generator=OpenAIChatGenerator(), tools=[delete_file], hooks={"before_tool": [hook]})
```

A key may be a single tool name, a tuple of tool names sharing one strategy, or the wildcard `"*"` which applies
Expand Down Expand Up @@ -652,6 +671,8 @@ This `after_tool` Agent hook writes the full result to the store so the next LLM
the full result. Register it on an `Agent` under the `after_tool` hook point. Which tools offload, and under what
condition, is controlled per tool by `offload_strategies`:

<!-- test-concept -->

```python
from haystack.components.agents import Agent
from haystack.components.generators.chat import OpenAIChatGenerator
Expand Down
6 changes: 3 additions & 3 deletions docs-website/reference/haystack-api/joiners_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ p.add_component(instance=InMemoryEmbeddingRetriever(document_store=document_stor
p.add_component(instance=DocumentJoiner(), name="joiner")
p.connect("bm25_retriever", "joiner")
p.connect("embedding_retriever", "joiner")
p.connect("text_embedder", "embedding_retriever")
p.connect("text_embedder.embedding", "embedding_retriever.query_embedding")
query = "What is the capital of France?"
p.run(data={"query": query, "text": query, "top_k": 1})
```
Expand Down Expand Up @@ -452,8 +452,8 @@ pipe.connect("feedback_prompt_builder.prompt", "feedback_llm.messages")
pipe.connect("feedback_llm.replies", "list_joiner")

query = "What is nuclear physics?"
ans = pipe.run(data={"prompt_builder": {"template_variables":{"query": query}},
"feedback_prompt_builder": {"template_variables":{"query": query}}})
ans = pipe.run(data={"prompt_builder": {"query": query},
"feedback_prompt_builder": {"query": query}})

print(ans["list_joiner"]["values"])
```
Expand Down
2 changes: 1 addition & 1 deletion docs-website/reference/haystack-api/routers_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ This component can be used with general-purpose LLMs and with specialized LLMs f
### Usage example

```python
from haystack.components.generators.chat import HuggingFaceAPIChatGenerator
from haystack_integrations.components.generators.huggingface_api import HuggingFaceAPIChatGenerator
from haystack.components.routers.llm_messages_router import LLMMessagesRouter
from haystack.dataclasses import ChatMessage

Expand Down
2 changes: 2 additions & 0 deletions docs-website/reference/haystack-api/tools_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,8 @@ A skill is a directory (or equivalent storage unit) containing a `SKILL.md` file

### Usage example

<!-- test-concept -->

```python
from haystack.components.agents import Agent
from haystack.components.generators.chat import OpenAIChatGenerator
Expand Down