关于 Hayhooks
Hayhooks 将 Haystack 流水线和 Agent 部署为 REST、OpenAI 兼容聊天、MCP 工具或 A2A 接口,面向构建与运行 AI 流程的开发者。它还支持 Redis 检查点恢复和执行追踪。
主要功能
- 01部署流水线和 Agent 为 REST API
- 02提供 OpenAI 兼容聊天接口
- 03暴露 MCP 工具和 A2A Agent
- 04通过 Redis 检查点恢复长任务
- 05查看执行与组件追踪
适用场景
- 01将 Haystack 流水线发布为带类型校验的 HTTP 端点
- 02为聊天界面提供流式响应
- 03让 MCP 客户端调用流水线或 Agent
- 04暂停任务等待人工批准并在重启后恢复
MCP 相关
可运行 MCP 服务,将 Haystack 流水线和 Agent 暴露为工具,供 Cursor、Claude Desktop 等 MCP 客户端使用。
配置
本地服务的配置在各客户端中通用,复制后粘贴到客户端的 MCP 配置文件即可。
我们还没有获取到这个服务的安装包或远程地址,请按项目 README 中的说明安装。
工具
本地服务需要在你的电脑上运行后才能读取工具,维护者暂未提供工具列表。
详细介绍
为管道和 Agent 提供服务
为管道或 Agent 编写 Python 封装。选择所需的接口和运行时功能:
- REST API。 将 Python 封装或 YAML 管道部署为经过类型检查和验证的端点,支持 OpenAPI 文档、文件上传和自定义路由。
- 兼容 OpenAI 的 API。 提供流式 Chat Completions 和 Responses 端点。连接 Open WebUI,或使用内置的 Chainlit UI。
- MCP 支持。 运行 MCP 服务,将管道和 Agent 作为工具提供给 Cursor、Claude Desktop 和其他 Model Context Protocol 客户端。
- A2A 支持。 通过 Agent2Agent 协议公开具备聊天功能的管道和 Agent,并生成 Agent 卡片以供发现和任务委派。
- 持久化执行。 在 Redis 中为长时间运行的工作创建检查点,等待人工审批,重试失败任务,并在重启后恢复。
- 追踪与可观测性。 在终端仪表板中检查执行情况和组件追踪,或使用
hayhooks[tracing]将跨度导出到 OpenTelemetry 后端。
终端仪表板
在终端中检查部署、跟踪执行情况并找出运行缓慢的组件。
使用演示数据的实际终端 UI。 探索仪表板。
快速入门
通过类型化 HTTP 端点提供 Haystack Agent 服务。你需要 Python 3.10 至 3.14 和 OpenAI API 密钥。
安装并启动
在 Python 环境中安装 Hayhooks,并在启动服务前设置 API 密钥:
pip install hayhooks
export OPENAI_API_KEY="your-api-key"
hayhooks run
保持服务运行。使用另一个终端执行以下命令。
创建 Agent
创建名为 my_agent_dir 的目录,并将以下内容保存为 my_agent_dir/pipeline_wrapper.py:
from haystack.components.agents import Agent
from haystack.components.generators.chat import OpenAIResponsesChatGenerator
from haystack.dataclasses import ChatMessage
from haystack.tools import tool
from hayhooks import BasePipelineWrapper
@tool
def search(query: str) -> str:
"""Search the knowledge base."""
return f"This is a demo tool."
class PipelineWrapper(BasePipelineWrapper):
def setup(self) -> None:
self.pipeline = Agent(
chat_generator=OpenAIResponsesChatGenerator(model="gpt-4o-mini"),
tools=[search],
)
async def run_api_async(self, question: str) -> str:
result = await self.pipeline.run_async(messages=[ChatMessage.from_user(question)])
return result["last_message"].text
Hayhooks 使用方法的类型提示来验证请求并生成 API 架构。
部署和运行
hayhooks pipeline deploy-files -n my_agent ./my_agent_dir
curl http://localhost:1416/my_agent/run \
-H 'Content-Type: application/json' \
-d '{"question": "What can you do?"}'
响应中包含 Agent 的回答。打开API 文档,查看并试用该端点。
添加流式聊天
实现 run_chat_completion_async,通过兼容 OpenAI 的 Chat Completions API公开 Agent。
在 pipeline_wrapper.py 顶部添加以下导入:
from collections.abc import AsyncGenerator
from hayhooks import async_streaming_generator
在 PipelineWrapper 中添加此方法:
async def run_chat_completion_async(
self, model: str, messages: list[dict], body: dict
) -> AsyncGenerator[str, None]:
return async_streaming_generator(
pipeline=self.pipeline,
pipeline_run_args={
"messages": [ChatMessage.from_openai_dict_format(m) for m in messages],
},
)
重新部署包装器,然后请求流式响应:
hayhooks pipeline deploy-files -n my_agent ./my_agent_dir --overwrite
curl -N http://localhost:1416/chat/completions \
-H 'Content-Type: application/json' \
-d '{
"model": "my_agent",
"messages": [{"role": "user", "content": "What can you do?"}],
"stream": true
}'
响应将以服务器发送事件的形式到达。连接 Open WebUI,或使用嵌入式 Chainlit UI与 Agent 聊天。
在仪表板中检查运行情况
在 macOS 或 Linux 上,使用与服务端相同的操作系统账户,在第二个终端中打开仪表板:
pip install "hayhooks[tui]"
hayhooks dashboard
按 e 查看执行记录,或按 v 查看追踪,然后按 Enter 查看详情。默认启用本地捕获。最近的普通运行记录会一直保留,直到服务端重启或缓冲区将其淘汰。
如需在 Windows 上操作或远程访问,请参阅仪表板连接指南。如需将跨度导出到外部 OpenTelemetry 后端,请参阅追踪与 OpenTelemetry。
持久化执行
运行时间超出请求生命周期的任务。Hayhooks 会将流水线和 Agent 的检查点保存到 Redis,以便其他工作进程在重启后恢复执行。
暂停运行以等待人工审批、重试失败任务,或从任意副本重新连接到输出流。
stateDiagram-v2
direction LR
[*] --> queued: submit (202)
queued --> running: worker claims
running --> waiting: suspend for a human
waiting --> queued: resume
running --> queued: retry or crash recovery
running --> completed
running --> failed
running --> canceled
运行需要人工审批的 Agent
此示例会在调用演示搜索工具前等待审批。将包装器保存为 pipelines/research_agent/pipeline_wrapper.py。
from haystack.components.agents import Agent
from haystack.components.generators.chat import OpenAIResponsesChatGenerator
from haystack.dataclasses import ChatMessage
from haystack.tools import tool
from pydantic import BaseModel
from hayhooks import BasePipelineWrapper, DurableContext
@tool
def search(query: str) -> str:
"""Search the knowledge base."""
return f"Hayhooks serves Haystack pipelines and agents; results for {query!r}."
class Task(BaseModel):
question: str
class Approval(BaseModel):
approved: bool
class Report(BaseModel):
answer: str
class PipelineWrapper(BasePipelineWrapper):
durable_revision = "research-agent-v1" # compatibility key for persisted inputs and checkpoints
durable_resume_model = Approval # typed input for human-in-the-loop waits
def setup(self) -> None:
generator = OpenAIResponsesChatGenerator(model="gpt-4o-mini")
self.pipeline = Agent(chat_generator=generator, tools=[search])
async def run_durable_async(self, context: DurableContext, task: Task) -> Report:
# Persist the wait in Redis so a worker serving this revision can resume it.
if not context.state.get("approved"):
decision = context.resume_input # consumed on first read
if decision is None:
await context.suspend({"kind": "approval", "message": f"Research {task.question!r}?"})
if not Approval.model_validate(decision).approved:
raise ValueError("research was rejected")
context.state["approved"] = True
# Agent state is checkpointed after tool calls: recovery continues the loop.
result = await context.run_agent_async(messages=[ChatMessage.from_user(task.question)])
return Report(answer=result["last_message"].text)
如果快速入门服务仍在运行,请将其停止。安装额外依赖,启动 Redis,并以持久化模式提供流水线目录:
pip install "hayhooks[durable]"
docker run -d -p 6379:6379 redis
export OPENAI_API_KEY="your-api-key"
HAYHOOKS_DURABLE_MODE=true hayhooks run --pipelines-dir ./pipelines
在第二个终端中提交运行任务:
curl -i http://localhost:1416/research_agent/run-durable \
-H 'Content-Type: application/json' -d '{"question": "What does Hayhooks do?"}'
服务端会返回 202 Accepted、执行 ID 和链接。将下方的 EXECUTION_ID 替换为该 ID,然后检查运行状态:
curl http://localhost:1416/research_agent/executions/EXECUTION_ID
状态变为 waiting 后,你可以测试恢复功能:停止服务端,然后使用相同命令重新启动。等待状态会在重启后保留。
恢复需要有工作进程提供相同的修订版本。部署不兼容的更改时,请让旧修订版本继续运行,直到其任务完成。请参阅修订版本兼容性。
批准运行任务并跟踪其输出:
curl -X POST http://localhost:1416/research_agent/executions/EXECUTION_ID/resume \
-H 'Content-Type: application/json' -d '{"approved": true}'
curl -N http://localhost:1416/research_agent/executions/EXECUTION_ID/stream
请阅读持久化执行指南,了解检查点行为;阅读运维指南,了解部署和恢复。相同的引擎也可以嵌入任何 FastAPI 应用。
后续步骤
- 快速入门指南 - 开始使用 Hayhooks
- 安装 - 安装 Hayhooks 和依赖项
- 配置 - 根据需要配置 Hayhooks
- 终端仪表板 - 检查部署、执行记录和组件追踪
- 持久化执行 - 在请求结束后继续运行流水线和 Agent
- 示例 - 浏览示例实现
社区与支持
- GitHub:deepset-ai/hayhooks
- 问题反馈:GitHub Issues
- 文档:完整文档
Hayhooks 由 deepset 团队积极维护。
评论
一个开放协议,规定了 AI 应用如何以统一方式连接外部工具与数据源。客户端负责与模型对话,MCP 服务负责提供工具、资源和提示模板,两者通过标准消息通信,同一个服务可以被不同客户端复用。
