Skip to content

Commit 2618dae

Browse files
docs: fix failing python code snippets (#12002)
Co-authored-by: David S. Batista <dsbatista@gmail.com>
1 parent bcb2867 commit 2618dae

8 files changed

Lines changed: 29 additions & 7 deletions

File tree

.github/workflows/docs-website-test-docs-snippets.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ jobs:
6161
- name: Run snippet tests (verbose)
6262
shell: bash
6363
run: |
64+
hatch -e test env run -- pip install huggingface-api-haystack # required by LLMMessagesRouter docstring example
6465
hatch -e test env run -- python docs-website/scripts/test_python_snippets.py --verbose tmp_api_reference/
6566
6667
notify-slack-on-failure:

haystack/components/joiners/document_joiner.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class DocumentJoiner:
7979
p.add_component(instance=DocumentJoiner(), name="joiner")
8080
p.connect("bm25_retriever", "joiner")
8181
p.connect("embedding_retriever", "joiner")
82-
p.connect("text_embedder", "embedding_retriever")
82+
p.connect("text_embedder.embedding", "embedding_retriever.query_embedding")
8383
query = "What is the capital of France?"
8484
p.run(data={"query": query, "text": query, "top_k": 1})
8585
```

haystack/components/joiners/list_joiner.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ class ListJoiner:
5757
pipe.connect("feedback_llm.replies", "list_joiner")
5858
5959
query = "What is nuclear physics?"
60-
ans = pipe.run(data={"prompt_builder": {"template_variables":{"query": query}},
61-
"feedback_prompt_builder": {"template_variables":{"query": query}}})
60+
ans = pipe.run(data={"prompt_builder": {"query": query},
61+
"feedback_prompt_builder": {"query": query}})
6262
6363
print(ans["list_joiner"]["values"])
6464
```

haystack/components/routers/llm_messages_router.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class LLMMessagesRouter:
2323
### Usage example
2424
2525
```python
26-
from haystack.components.generators.chat import HuggingFaceAPIChatGenerator
26+
from haystack_integrations.components.generators.huggingface_api import HuggingFaceAPIChatGenerator
2727
from haystack.components.routers.llm_messages_router import LLMMessagesRouter
2828
from haystack.dataclasses import ChatMessage
2929

haystack/hooks/from_function.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,17 +140,29 @@ def hook(function: Callable[[State], None | Awaitable[None]]) -> FunctionHook:
140140
141141
```python
142142
from haystack.components.agents import Agent
143+
from haystack.components.generators.chat import OpenAIChatGenerator
143144
from haystack.hooks import hook
144145
from haystack.components.agents.state import State
145146
from haystack.dataclasses import ChatMessage
147+
from haystack.tools import tool
148+
149+
@tool
150+
def weather_tool(city: str) -> str:
151+
'''Get the current weather for a given city.'''
152+
return f"The weather in {city} is sunny."
153+
154+
@tool
155+
def save(content: str) -> str:
156+
'''Save content to durable storage.'''
157+
return "Saved."
146158
147159
@hook
148160
def require_save(state: State) -> None:
149161
if state.get("tool_call_counts", {}).get("save", 0) == 0:
150162
state.set("messages", [ChatMessage.from_system("You must call `save` before finishing.")])
151163
state.set("continue_run", True)
152164
153-
agent = Agent(chat_generator=..., tools=[...], hooks={"on_exit": [require_save]})
165+
agent = Agent(chat_generator=OpenAIChatGenerator(), tools=[weather_tool, save], hooks={"on_exit": [require_save]})
154166
```
155167
156168
:param function: A callable taking the Agent's `State` and returning `None` (sync or async).

haystack/hooks/human_in_the_loop/hooks.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ class ConfirmationHook:
2424
2525
```python
2626
from haystack.components.agents import Agent
27+
from haystack.components.generators.chat import OpenAIChatGenerator
28+
from haystack.tools import tool
2729
from haystack.hooks.human_in_the_loop import (
2830
AlwaysAskPolicy,
2931
BlockingConfirmationStrategy,
@@ -33,14 +35,19 @@ class ConfirmationHook:
3335
SimpleConsoleUI,
3436
)
3537
38+
@tool
39+
def delete_file(path: str) -> str:
40+
'''Delete the file at the given path.'''
41+
return f"Deleted {path}."
42+
3643
hook = ConfirmationHook(
3744
confirmation_strategies={
38-
"my_tool": BlockingConfirmationStrategy(
45+
"delete_file": BlockingConfirmationStrategy(
3946
confirmation_policy=NeverAskPolicy(), confirmation_ui=SimpleConsoleUI()
4047
)
4148
}
4249
)
43-
agent = Agent(chat_generator=..., tools=[...], hooks={"before_tool": [hook]})
50+
agent = Agent(chat_generator=OpenAIChatGenerator(), tools=[delete_file], hooks={"before_tool": [hook]})
4451
```
4552
4653
A key may be a single tool name, a tuple of tool names sharing one strategy, or the wildcard `"*"` which applies

haystack/hooks/tool_result_offloading/hooks.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ class ToolResultOffloadHook:
126126
the full result. Register it on an `Agent` under the `after_tool` hook point. Which tools offload, and under what
127127
condition, is controlled per tool by `offload_strategies`:
128128
129+
<!-- test-concept -->
129130
```python
130131
from haystack.components.agents import Agent
131132
from haystack.components.generators.chat import OpenAIChatGenerator

haystack/tools/skills/skill_toolset.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class SkillToolset(Toolset):
3030
3131
### Usage example
3232
33+
<!-- test-concept -->
3334
```python
3435
from haystack.components.agents import Agent
3536
from haystack.components.generators.chat import OpenAIChatGenerator

0 commit comments

Comments
 (0)