Skip to content

Commit

Permalink
fix: fix OpenAPITool notebook (#48)
Browse files Browse the repository at this point in the history
* fix notebook

* fix
  • Loading branch information
anakin87 authored Jul 29, 2024
1 parent 46726dd commit dc2bb50
Showing 1 changed file with 72 additions and 47 deletions.
119 changes: 72 additions & 47 deletions examples/openapitool.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 14,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
Expand All @@ -99,10 +99,10 @@
{
"data": {
"text/plain": [
"{'service_response': [ChatMessage(content='{\"latitude\": 37.763283, \"longitude\": -122.41286, \"generationtime_ms\": 0.07903575897216797, \"utc_offset_seconds\": 0, \"timezone\": \"GMT\", \"timezone_abbreviation\": \"GMT\", \"elevation\": 18.0, \"current_weather_units\": {\"time\": \"iso8601\", \"interval\": \"seconds\", \"temperature\": \"\\\\u00b0C\", \"windspeed\": \"km/h\", \"winddirection\": \"\\\\u00b0\", \"is_day\": \"\", \"weathercode\": \"wmo code\"}, \"current_weather\": {\"time\": \"2024-07-22T16:45\", \"interval\": 900, \"temperature\": 18.1, \"windspeed\": 3.4, \"winddirection\": 212, \"is_day\": 1, \"weathercode\": 1}}', role=<ChatRole.USER: 'user'>, name=None, meta={})]}"
"{'service_response': [ChatMessage(content='{\"latitude\": 37.763283, \"longitude\": -122.41286, \"generationtime_ms\": 0.07700920104980469, \"utc_offset_seconds\": -25200, \"timezone\": \"America/Los_Angeles\", \"timezone_abbreviation\": \"PDT\", \"elevation\": 18.0, \"current_weather_units\": {\"time\": \"iso8601\", \"interval\": \"seconds\", \"temperature\": \"\\\\u00b0C\", \"windspeed\": \"km/h\", \"winddirection\": \"\\\\u00b0\", \"is_day\": \"\", \"weathercode\": \"wmo code\"}, \"current_weather\": {\"time\": \"2024-07-29T06:45\", \"interval\": 900, \"temperature\": 13.2, \"windspeed\": 12.7, \"winddirection\": 263, \"is_day\": 1, \"weathercode\": 45}}', role=<ChatRole.USER: 'user'>, name=None, meta={})]}"
]
},
"execution_count": 2,
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -128,12 +128,12 @@
"\n",
"Next, let's create a simple Pipeline where the service response is translated into a human-understandable format using the Language Model.\n",
"\n",
"We use an [`OutputAdapter`](https://docs.haystack.deepset.ai/docs/outputadapter) to create a list of Chat Messages for the LM."
"We use a [`ChatPromptBuilder`](https://docs.haystack.deepset.ai/docs/chatpromptbuilder) to create a list of Chat Messages for the LM."
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 15,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
Expand All @@ -145,51 +145,54 @@
{
"data": {
"text/plain": [
"<haystack.core.pipeline.pipeline.Pipeline object at 0x7b8db7235720>\n",
"<haystack.core.pipeline.pipeline.Pipeline object at 0x7f8899207e50>\n",
"🚅 Components\n",
" - meteo: OpenAPITool\n",
" - prompt_adapter: OutputAdapter\n",
" - builder: ChatPromptBuilder\n",
" - llm: OpenAIChatGenerator\n",
"🛤️ Connections\n",
" - meteo.service_response -> prompt_adapter.service_response (List[ChatMessage])\n",
" - prompt_adapter.output -> llm.messages (List[ChatMessage])"
" - meteo.service_response -> builder.service_response (List[ChatMessage])\n",
" - builder.prompt -> llm.messages (List[ChatMessage])"
]
},
"execution_count": 3,
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from typing import List\n",
"from haystack import Pipeline\n",
"from haystack.components.converters import OutputAdapter\n",
"from haystack.components.builders import ChatPromptBuilder\n",
"from haystack.components.generators.chat import OpenAIChatGenerator\n",
"\n",
"messages = [ChatMessage.from_user(\"{{user_message}}\"), ChatMessage.from_user(\"{{service_response}}\")]\n",
"builder = ChatPromptBuilder(template=messages)\n",
"\n",
"pipe = Pipeline()\n",
"pipe.add_component(\"meteo\", tool)\n",
"pipe.add_component(\"prompt_adapter\", OutputAdapter(\"{{user_message + service_response}}\", List[ChatMessage]))\n",
"pipe.add_component(\"builder\", builder)\n",
"pipe.add_component(\"llm\", OpenAIChatGenerator(generation_kwargs={\"max_tokens\": 1024}))\n",
"\n",
"pipe.connect(\"meteo\", \"prompt_adapter.service_response\")\n",
"pipe.connect(\"prompt_adapter\", \"llm.messages\")"
"pipe.connect(\"meteo\", \"builder.service_response\")\n",
"pipe.connect(\"builder\", \"llm.messages\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 16,
"metadata": {
"id": "6yF2RyfQhAaV"
},
"outputs": [],
"source": [
"result = pipe.run(data={\"meteo\": {\"messages\": [ChatMessage.from_user(\"weather in San Francisco, US\")]},\n",
" \"prompt_adapter\": {\"user_message\": [ChatMessage.from_user(\"Explain the weather in San Francisco in a human understandable way\")]}})"
" \"builder\": {\"user_message\": [ChatMessage.from_user(\"Explain the weather in San Francisco in a human understandable way\")]}})"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 17,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
Expand All @@ -202,7 +205,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Currently in San Francisco, the weather can be described as mild with a temperature of 18.1°C (64.6°F). The wind is blowing at a speed of 3.4 km/h coming from the south-southwest direction. It is daytime in San Francisco at the moment.\n"
"The current weather in San Francisco is 13.2°C with a windspeed of 12.7 km/h coming from the west-southwest. It is currently daytime and the weather code indicates that it is partly cloudy.\n"
]
}
],
Expand All @@ -225,7 +228,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 9,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
Expand All @@ -237,31 +240,35 @@
{
"data": {
"text/plain": [
"<haystack.core.pipeline.pipeline.Pipeline object at 0x7b8d53d03c40>\n",
"<haystack.core.pipeline.pipeline.Pipeline object at 0x7f8899ea6ef0>\n",
"🚅 Components\n",
" - firecrawl: OpenAPITool\n",
" - prompt_adapter: OutputAdapter\n",
" - builder: ChatPromptBuilder\n",
" - llm: OpenAIChatGenerator\n",
"🛤️ Connections\n",
" - firecrawl.service_response -> prompt_adapter.service_response (List[ChatMessage])\n",
" - prompt_adapter.output -> llm.messages (List[ChatMessage])"
" - firecrawl.service_response -> builder.service_response (List[ChatMessage])\n",
" - builder.prompt -> llm.messages (List[ChatMessage])"
]
},
"execution_count": 6,
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"messages = [ChatMessage.from_user(\"{{user_message}}\"), ChatMessage.from_user(\"{{service_response}}\")]\n",
"builder = ChatPromptBuilder(template=messages)\n",
"\n",
"\n",
"pipe = Pipeline()\n",
"pipe.add_component(\"firecrawl\", OpenAPITool(generator_api=LLMProvider.OPENAI,\n",
" spec=\"https://raw.githubusercontent.com/mendableai/firecrawl/main/apps/api/openapi.json\",\n",
" credentials=Secret.from_env_var(\"FIRECRAWL_API_KEY\")))\n",
"pipe.add_component(\"prompt_adapter\", OutputAdapter(\"{{user_message + service_response}}\", List[ChatMessage]))\n",
"pipe.add_component(\"builder\", builder)\n",
"pipe.add_component(\"llm\", OpenAIChatGenerator(generation_kwargs={\"max_tokens\": 1024}))\n",
"\n",
"pipe.connect(\"firecrawl\", \"prompt_adapter.service_response\")\n",
"pipe.connect(\"prompt_adapter\", \"llm.messages\")"
"pipe.connect(\"firecrawl\", \"builder.service_response\")\n",
"pipe.connect(\"builder\", \"llm.messages\")"
]
},
{
Expand All @@ -275,7 +282,7 @@
"user_prompt = \"Given the article below, list the most important facts in a bulleted list. Do not include repetitions. Max 5 points.\"\n",
"\n",
"result = pipe.run(data={\"firecrawl\": {\"messages\": [ChatMessage.from_user(\"Scrape https://lite.cnn.com/2024/07/18/style/rome-ancient-papal-palace/index.html\")]},\n",
" \"prompt_adapter\": {\"user_message\": [ChatMessage.from_user(user_prompt)]}})"
" \"builder\": {\"user_message\": [ChatMessage.from_user(user_prompt)]}})"
]
},
{
Expand Down Expand Up @@ -320,7 +327,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 10,
"metadata": {
"id": "73ypFwhtKbH9"
},
Expand Down Expand Up @@ -368,22 +375,19 @@
" except:\n",
" return \"error\"\n",
"\n",
"def create_chat_message(query):\n",
" return [ChatMessage.from_user(query)]\n",
"\n",
"\n",
"routes = [\n",
" {\n",
" \"condition\": \"{{replies[0] | get_tool_name == 'search_web'}}\",\n",
" \"output\": \"{{query | create_chat_message}}\",\n",
" \"output\": \"{{query}}\",\n",
" \"output_name\": \"search_web\",\n",
" \"output_type\": List[ChatMessage],\n",
" \"output_type\": str,\n",
" },\n",
" {\n",
" \"condition\": \"{{replies[0] | get_tool_name == 'scrape_page'}}\",\n",
" \"output\": \"{{query | create_chat_message}}\",\n",
" \"output\": \"{{query}}\",\n",
" \"output_name\": \"scrape_page\",\n",
" \"output_type\": List[ChatMessage],\n",
" \"output_type\": str,\n",
" },\n",
" {\n",
" \"condition\": \"{{replies[0] | get_tool_name == 'none'}}\",\n",
Expand All @@ -402,7 +406,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 13,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
Expand All @@ -414,30 +418,40 @@
{
"data": {
"text/plain": [
"<haystack.core.pipeline.pipeline.Pipeline object at 0x7b8d53caa410>\n",
"<haystack.core.pipeline.pipeline.Pipeline object at 0x7f8899ea57e0>\n",
"🚅 Components\n",
" - prompt_builder: PromptBuilder\n",
" - llm: OpenAIGenerator\n",
" - router: ConditionalRouter\n",
" - search_web_chat_builder: ChatPromptBuilder\n",
" - scrape_page_chat_builder: ChatPromptBuilder\n",
" - search_web_tool: OpenAPITool\n",
" - scrape_page_tool: OpenAPITool\n",
"🛤️ Connections\n",
" - prompt_builder.prompt -> llm.prompt (str)\n",
" - llm.replies -> router.replies (List[str])\n",
" - router.search_web -> search_web_tool.messages (List[ChatMessage])\n",
" - router.scrape_page -> scrape_page_tool.messages (List[ChatMessage])"
" - router.search_web -> search_web_chat_builder.query (str)\n",
" - router.scrape_page -> scrape_page_chat_builder.query (str)\n",
" - search_web_chat_builder.prompt -> search_web_tool.messages (List[ChatMessage])\n",
" - scrape_page_chat_builder.prompt -> scrape_page_tool.messages (List[ChatMessage])"
]
},
"execution_count": 10,
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from haystack.components.builders.prompt_builder import PromptBuilder\n",
"from haystack.components.builders import PromptBuilder, ChatPromptBuilder\n",
"from haystack.components.routers import ConditionalRouter\n",
"from haystack.components.generators import OpenAIGenerator\n",
"\n",
"\n",
"messages = [ChatMessage.from_user(\"{{query}}\")]\n",
"\n",
"search_web_chat_builder = ChatPromptBuilder(template=messages)\n",
"scrape_page_chat_builder = ChatPromptBuilder(template=messages)\n",
"\n",
"search_web_tool = OpenAPITool(generator_api=LLMProvider.OPENAI,\n",
" spec=\"https://bit.ly/serper_dev_spec_yaml\",\n",
" credentials=Secret.from_env_var(\"SERPERDEV_API_KEY\"))\n",
Expand All @@ -449,15 +463,18 @@
"pipe = Pipeline()\n",
"pipe.add_component(\"prompt_builder\", PromptBuilder(template=decision_prompt_template))\n",
"pipe.add_component(\"llm\", OpenAIGenerator())\n",
"pipe.add_component(\"router\", ConditionalRouter(routes, custom_filters={\"get_tool_name\": get_tool_name,\n",
" \"create_chat_message\": create_chat_message}))\n",
"pipe.add_component(\"router\", ConditionalRouter(routes, custom_filters={\"get_tool_name\": get_tool_name}))\n",
"pipe.add_component(\"search_web_chat_builder\", search_web_chat_builder)\n",
"pipe.add_component(\"scrape_page_chat_builder\", scrape_page_chat_builder)\n",
"pipe.add_component(\"search_web_tool\", search_web_tool)\n",
"pipe.add_component(\"scrape_page_tool\", scrape_page_tool)\n",
"\n",
"pipe.connect(\"prompt_builder\", \"llm\")\n",
"pipe.connect(\"llm.replies\", \"router.replies\")\n",
"pipe.connect(\"router.search_web\", \"search_web_tool\")\n",
"pipe.connect(\"router.scrape_page\", \"scrape_page_tool\")"
"pipe.connect(\"router.search_web\", \"search_web_chat_builder\")\n",
"pipe.connect(\"router.scrape_page\", \"scrape_page_chat_builder\")\n",
"pipe.connect(\"search_web_chat_builder\", \"search_web_tool\")\n",
"pipe.connect(\"scrape_page_chat_builder\", \"scrape_page_tool\")"
]
},
{
Expand Down Expand Up @@ -538,8 +555,16 @@
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"version": "3.10.13"
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.12"
}
},
"nbformat": 4,
Expand Down

0 comments on commit dc2bb50

Please sign in to comment.