Hayhooks 让部署和提供 Haystack 管道 与 智能体 变得简单。
使用 Hayhooks,你可以:
pip install "hayhooks[chainlit]" 和 hayhooks run --with-chainlit —— 这是一个零配置的前端,支持流式传输、管道选择和自定义 UI 组件。📚 查看详细的指南、示例和 API 参考,请访问我们的 完整文档。
# 安装 Hayhooks
pip install hayhooks
hayhooks run
创建一个支持流式聊天和简单 HTTP POST API 的最小智能体包装器:
from typing import AsyncGenerator
from haystack.components.agents import Agent
from haystack.dataclasses import ChatMessage
from haystack.tools import Tool
from haystack.components.generators.chat import OpenAIChatGenerator
from hayhooks import BasePipelineWrapper, async_streaming_generator
# 定义一个提供给定位置天气信息的 Haystack 工具。
def weather_function(location):
return f"The weather in {location} is sunny."
weather_tool = Tool(
name="weather_tool",
description="Provides weather information for a given location.",
parameters={
"type": "object",
"properties": {"location": {"type": "string"}},
"required": ["location"],
},
function=weather_function,
)
class PipelineWrapper(BasePipelineWrapper):
def setup(self) -> None:
self.agent = Agent(
chat_generator=OpenAIChatGenerator(model="gpt-4o-mini"),
system_prompt="You're a helpful agent",
tools=[weather_tool],
)
# 这将创建一个 POST /my_agent/run 端点
# `question` 将是输入参数,并将由 Pydantic 模型自动验证
async def run_api_async(self, question: str) -> str:
result = await self.agent.run_async(messages=[ChatMessage.from_user(question)])
return result["last_message"].text
# 这将创建一个 OpenAI 兼容的 /chat/completions 端点
async def run_chat_completion_async(
self, model: str, messages: list[dict], body: dict
) -> AsyncGenerator[str, None]:
chat_messages = [
ChatMessage.from_openai_dict_format(message) for message in messages
]
return async_streaming_generator(
pipeline=self.agent,
pipeline_run_args={
"messages": chat_messages,
},
)
保存为 my_agent_dir/pipeline_wrapper.py。
hayhooks pipeline deploy-files -n my_agent ./my_agent_dir
调用 HTTP POST API (/my_agent/run):
curl -X POST http://localhost:1416/my_agent/run \
-H 'Content-Type: application/json' \
-d '{"question": "What can you do?"}'
调用 OpenAI 兼容的聊天补全 API(启用流式传输):
curl -X POST http://localhost:1416/chat/completions \
-H 'Content-Type: application/json' \
-d '{
"model": "my_agent",
"messages": [{"role": "user", "content": "What can you do?"}]
}'
或者在 嵌入式 Chainlit UI (hayhooks run --with-chainlit) 中与它聊天,或者 将其与 Open WebUI 集成!
Hayhooks 由 deepset 团队积极维护。