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
1 change: 1 addition & 0 deletions .github/workflows/docs-website-test-docs-snippets.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ jobs:
- name: Run snippet tests (verbose)
shell: bash
run: |
hatch -e test env run -- pip install huggingface-api-haystack # required by LLMMessagesRouter docstring example
hatch -e test env run -- python docs-website/scripts/test_python_snippets.py --verbose tmp_api_reference/

notify-slack-on-failure:
Expand Down
2 changes: 1 addition & 1 deletion haystack/components/joiners/document_joiner.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class DocumentJoiner:
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
4 changes: 2 additions & 2 deletions haystack/components/joiners/list_joiner.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ class ListJoiner:
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 haystack/components/routers/llm_messages_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class LLMMessagesRouter:
### 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
14 changes: 13 additions & 1 deletion haystack/hooks/from_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,17 +140,29 @@ def hook(function: Callable[[State], None | Awaitable[None]]) -> FunctionHook:

```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]})
```

:param function: A callable taking the Agent's `State` and returning `None` (sync or async).
Expand Down
11 changes: 9 additions & 2 deletions haystack/hooks/human_in_the_loop/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class ConfirmationHook:

```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 @@ -33,14 +35,19 @@ class ConfirmationHook:
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
1 change: 1 addition & 0 deletions haystack/hooks/tool_result_offloading/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ class ToolResultOffloadHook:
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
1 change: 1 addition & 0 deletions haystack/tools/skills/skill_toolset.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class SkillToolset(Toolset):

### Usage example

<!-- test-concept -->
```python
from haystack.components.agents import Agent
from haystack.components.generators.chat import OpenAIChatGenerator
Expand Down
Loading