|
| 1 | +# SPDX-FileCopyrightText: 2022-present deepset GmbH <[email protected]> |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +""" |
| 6 | +Example of a Hayhooks PipelineWrapper for deploying an MCP Tool-based time pipeline as a REST API. |
| 7 | +
|
| 8 | +To run this example: |
| 9 | +
|
| 10 | +1. Install Hayhooks and dependencies: |
| 11 | + $ pip install hayhooks haystack-ai |
| 12 | +
|
| 13 | +2. Start the Hayhooks server: |
| 14 | + $ hayhooks run |
| 15 | +
|
| 16 | +3. Deploy this pipeline wrapper: |
| 17 | + $ hayhooks pipeline deploy-files -n time_pipeline {root_dir_for_mcp_haystack_integration}/examples/hayhooks/ |
| 18 | +
|
| 19 | +4. Invoke via curl: |
| 20 | + $ curl -X POST 'http://localhost:1416/time_pipeline/run' -H 'accept: application/json' -H 'Content-Type: application/json' -d '{"query":"What is the time in San Francisco? Be brief"}' |
| 21 | +
|
| 22 | +For more information, see: https://github.com/deepset-ai/hayhooks |
| 23 | +""" |
| 24 | + |
| 25 | +from hayhooks import BasePipelineWrapper |
| 26 | +from haystack import Pipeline |
| 27 | +from haystack.components.converters import OutputAdapter |
| 28 | +from haystack.components.generators.chat import OpenAIChatGenerator |
| 29 | +from haystack.components.tools import ToolInvoker |
| 30 | +from haystack.dataclasses import ChatMessage |
| 31 | + |
| 32 | +from haystack_integrations.tools.mcp.mcp_tool import MCPTool, StdioServerInfo |
| 33 | + |
| 34 | + |
| 35 | +class PipelineWrapper(BasePipelineWrapper): |
| 36 | + def setup(self) -> None: |
| 37 | + """ |
| 38 | + Setup the pipeline with MCP time tool. |
| 39 | +
|
| 40 | + This creates a pipeline that uses an MCP time tool to get the current time |
| 41 | + and then uses the time to answer a user question. |
| 42 | + """ |
| 43 | + |
| 44 | + time_tool = MCPTool( |
| 45 | + name="get_current_time", |
| 46 | + server_info=StdioServerInfo(command="uvx", args=["mcp-server-time", "--local-timezone=Europe/Berlin"]), |
| 47 | + ) |
| 48 | + |
| 49 | + self.pipeline = Pipeline() |
| 50 | + self.pipeline.add_component("llm", OpenAIChatGenerator(model="gpt-4o-mini", tools=[time_tool])) |
| 51 | + self.pipeline.add_component("tool_invoker", ToolInvoker(tools=[time_tool])) |
| 52 | + self.pipeline.add_component( |
| 53 | + "adapter", |
| 54 | + OutputAdapter( |
| 55 | + template="{{ initial_msg + initial_tool_messages + tool_messages }}", |
| 56 | + output_type=list[ChatMessage], |
| 57 | + unsafe=True, |
| 58 | + ), |
| 59 | + ) |
| 60 | + self.pipeline.add_component("response_llm", OpenAIChatGenerator(model="gpt-4o-mini")) |
| 61 | + self.pipeline.connect("llm.replies", "tool_invoker.messages") |
| 62 | + self.pipeline.connect("llm.replies", "adapter.initial_tool_messages") |
| 63 | + self.pipeline.connect("tool_invoker.tool_messages", "adapter.tool_messages") |
| 64 | + self.pipeline.connect("adapter.output", "response_llm.messages") |
| 65 | + |
| 66 | + def run_api(self, query: str) -> str: |
| 67 | + """ |
| 68 | + Run the pipeline with a user query. |
| 69 | +
|
| 70 | + :param query: The user query asking about time |
| 71 | + :return: The response from the LLM |
| 72 | + """ |
| 73 | + # Create a user message from the query |
| 74 | + user_input_msg = ChatMessage.from_user(text=query) |
| 75 | + |
| 76 | + # Run the pipeline |
| 77 | + result = self.pipeline.run( |
| 78 | + {"llm": {"messages": [user_input_msg]}, "adapter": {"initial_msg": [user_input_msg]}} |
| 79 | + ) |
| 80 | + |
| 81 | + # Return the text of the first reply |
| 82 | + return result["response_llm"]["replies"][0].text |
0 commit comments