openapiservicetofunctions.mdx
1 --- 2 title: "OpenAPIServiceToFunctions" 3 id: openapiservicetofunctions 4 slug: "/openapiservicetofunctions" 5 description: "`OpenAPIServiceToFunctions` is a component that transforms OpenAPI service specifications into a format compatible with LLM tool calling." 6 --- 7 8 # OpenAPIServiceToFunctions 9 10 `OpenAPIServiceToFunctions` is a component that transforms OpenAPI service specifications into a format compatible with LLM tool calling. 11 12 <div className="key-value-table"> 13 14 | | | 15 | --- | --- | 16 | **Most common position in a pipeline** | Flexible | 17 | **Mandatory run variables** | `sources`: A list of OpenAPI specification sources, which can be file paths or [`ByteStream`](../../concepts/data-classes.mdx#bytestream) objects | 18 | **Output variables** | `functions`: A list of JSON function definitions objects. For each path definition in OpenAPI specification, a corresponding function definition is generated. <br /> <br />`openapi_specs`: A list of JSON/YAML objects with references resolved. Such OpenAPI spec (with references resolved) can, in turn, be used as input to OpenAPIServiceConnector. | 19 | **API reference** | [Converters](/reference/converters-api) | 20 | **GitHub link** | https://github.com/deepset-ai/haystack/blob/main/haystack/components/converters/openapi_functions.py | 21 22 </div> 23 24 ## Overview 25 26 `OpenAPIServiceToFunctions` transforms OpenAPI service specifications into a function calling format suitable for LLM tool calling. It takes an OpenAPI specification, processes it to extract function definitions, and formats these definitions to be compatible with LLM tool calling. 27 28 `OpenAPIServiceToFunctions` is valuable when used together with [`OpenAPIServiceConnector`](../connectors/openapiserviceconnector.mdx) component. It converts OpenAPI specifications into function definitions, allowing `OpenAPIServiceConnector` to handle input parameters for the OpenAPI specification and facilitate their use in REST API calls through `OpenAPIServiceConnector`. 29 30 To use `OpenAPIServiceToFunctions`, you need to install an optional `jsonref` dependency with: 31 32 ```shell 33 pip install jsonref 34 ``` 35 36 `OpenAPIServiceToFunctions` component doesn’t have any init parameters. 37 38 ## Usage 39 40 ### On its own 41 42 This component is primarily meant to be used in pipelines. Using this component alone is useful when you want to convert OpenAPI specification into function definitions and then perhaps save them in a file and subsequently use them for tool calling. 43 44 ### In a pipeline 45 46 In a pipeline context, `OpenAPIServiceToFunctions` is most valuable when used alongside `OpenAPIServiceConnector`. For instance, let’s consider integrating [serper.dev](http://serper.dev/) search engine bridge into a pipeline. `OpenAPIServiceToFunctions` retrieves the OpenAPI specification of Serper from https://bit.ly/serper_dev_spec, converts this specification into function definitions that an LLM with tool calling capabilities can understand, and then seamlessly passes these definitions as `generation_kwargs` to the Chat Generator component. 47 48 :::info 49 To run the following code snippet, note that you have to have your own Serper and OpenAI API keys. 50 ::: 51 52 ```python 53 import json 54 import requests 55 56 from typing import Any 57 58 from haystack import Pipeline 59 from haystack.components.connectors import OpenAPIServiceConnector 60 from haystack.components.converters import OpenAPIServiceToFunctions, OutputAdapter 61 from haystack.components.generators.chat import OpenAIChatGenerator 62 from haystack.dataclasses import ChatMessage 63 from haystack.dataclasses.byte_stream import ByteStream 64 65 66 def prepare_fc_params(openai_functions_schema: dict[str, Any]) -> dict[str, Any]: 67 return { 68 "tools": [{"type": "function", "function": openai_functions_schema}], 69 "tool_choice": { 70 "type": "function", 71 "function": {"name": openai_functions_schema["name"]}, 72 }, 73 } 74 75 76 serperdev_spec = requests.get("https://bit.ly/serper_dev_spec").json() 77 system_prompt = requests.get("https://bit.ly/serper_dev_system").text 78 user_prompt = "Why was Sam Altman ousted from OpenAI?" 79 80 pipe = Pipeline() 81 pipe.add_component("spec_to_functions", OpenAPIServiceToFunctions()) 82 pipe.add_component( 83 "prepare_fc_adapter", 84 OutputAdapter( 85 "{{functions[0] | prepare_fc}}", 86 dict[str, Any], 87 {"prepare_fc": prepare_fc_params}, 88 ), 89 ) 90 pipe.add_component("functions_llm", OpenAIChatGenerator()) 91 pipe.add_component("openapi_connector", OpenAPIServiceConnector()) 92 pipe.add_component( 93 "message_adapter", 94 OutputAdapter( 95 "{{system_message + service_response}}", 96 list[ChatMessage], 97 unsafe=True, 98 ), 99 ) 100 pipe.add_component("llm", OpenAIChatGenerator()) 101 102 pipe.connect("spec_to_functions.functions", "prepare_fc_adapter.functions") 103 pipe.connect( 104 "spec_to_functions.openapi_specs", 105 "openapi_connector.service_openapi_spec", 106 ) 107 pipe.connect("prepare_fc_adapter", "functions_llm.generation_kwargs") 108 pipe.connect("functions_llm.replies", "openapi_connector.messages") 109 pipe.connect("openapi_connector.service_response", "message_adapter.service_response") 110 pipe.connect("message_adapter", "llm.messages") 111 112 result = pipe.run( 113 data={ 114 "functions_llm": { 115 "messages": [ 116 ChatMessage.from_system("Only do tool/function calling"), 117 ChatMessage.from_user(user_prompt), 118 ], 119 }, 120 "openapi_connector": { 121 "service_credentials": serper_dev_key, 122 }, 123 "spec_to_functions": { 124 "sources": [ByteStream.from_string(json.dumps(serperdev_spec))], 125 }, 126 "message_adapter": { 127 "system_message": [ChatMessage.from_system(system_prompt)], 128 }, 129 }, 130 ) 131 132 print(result["llm"]["replies"][0].text) 133 134 # Sam Altman was ousted from OpenAI on November 17, 2023, following 135 # a "deliberative review process" by the board of directors. The board concluded 136 # that he was not "consistently candid in his communications". However, he 137 # returned as CEO just days after his ouster. 138 ```