connectors_api.md
1 --- 2 title: "Connectors" 3 id: connectors-api 4 description: "Various connectors to integrate with external services." 5 slug: "/connectors-api" 6 --- 7 8 9 ## openapi 10 11 ### OpenAPIConnector 12 13 OpenAPIConnector enables direct invocation of REST endpoints defined in an OpenAPI specification. 14 15 The OpenAPIConnector serves as a bridge between Haystack pipelines and any REST API that follows 16 the OpenAPI(formerly Swagger) specification. It dynamically interprets the API specification and 17 provides an interface for executing API operations. It is usually invoked by passing input 18 arguments to it from a Haystack pipeline run method or by other components in a pipeline that 19 pass input arguments to this component. 20 21 Example: 22 23 ```python 24 from haystack.utils import Secret 25 from haystack.components.connectors.openapi import OpenAPIConnector 26 27 connector = OpenAPIConnector( 28 openapi_spec="https://bit.ly/serperdev_openapi", 29 credentials=Secret.from_env_var("SERPERDEV_API_KEY"), 30 service_kwargs={"config_factory": my_custom_config_factory} 31 ) 32 response = connector.run( 33 operation_id="search", 34 arguments={"q": "Who was Nikola Tesla?"} 35 ) 36 ``` 37 38 Note: 39 40 - The `parameters` argument is required for this component. 41 - The `service_kwargs` argument is optional, it can be used to pass additional options to the OpenAPIClient. 42 43 #### __init__ 44 45 ```python 46 __init__( 47 openapi_spec: str, 48 credentials: Secret | None = None, 49 service_kwargs: dict[str, Any] | None = None, 50 ) -> None 51 ``` 52 53 Initialize the OpenAPIConnector with a specification and optional credentials. 54 55 **Parameters:** 56 57 - **openapi_spec** (<code>str</code>) – URL, file path, or raw string of the OpenAPI specification 58 - **credentials** (<code>Secret | None</code>) – Optional API key or credentials for the service wrapped in a Secret 59 - **service_kwargs** (<code>dict\[str, Any\] | None</code>) – Additional keyword arguments passed to OpenAPIClient.from_spec() 60 For example, you can pass a custom config_factory or other configuration options. 61 62 #### to_dict 63 64 ```python 65 to_dict() -> dict[str, Any] 66 ``` 67 68 Serialize this component to a dictionary. 69 70 #### from_dict 71 72 ```python 73 from_dict(data: dict[str, Any]) -> OpenAPIConnector 74 ``` 75 76 Deserialize this component from a dictionary. 77 78 #### run 79 80 ```python 81 run( 82 operation_id: str, arguments: dict[str, Any] | None = None 83 ) -> dict[str, Any] 84 ``` 85 86 Invokes a REST endpoint specified in the OpenAPI specification. 87 88 **Parameters:** 89 90 - **operation_id** (<code>str</code>) – The operationId from the OpenAPI spec to invoke 91 - **arguments** (<code>dict\[str, Any\] | None</code>) – Optional parameters for the endpoint (query, path, or body parameters) 92 93 **Returns:** 94 95 - <code>dict\[str, Any\]</code> – Dictionary containing the service response 96 97 ## openapi_service 98 99 ### patch_request 100 101 ```python 102 patch_request( 103 self: Operation, 104 base_url: str, 105 *, 106 data: Any | None = None, 107 parameters: dict[str, Any] | None = None, 108 raw_response: bool = False, 109 security: dict[str, str] | None = None, 110 session: Any | None = None, 111 verify: bool | str = True 112 ) -> Any | None 113 ``` 114 115 Sends an HTTP request as described by this path. 116 117 **Parameters:** 118 119 - **base_url** (<code>str</code>) – The URL to append this operation's path to when making 120 the call. 121 - **data** (<code>Any | None</code>) – The request body to send. 122 - **parameters** (<code>dict\[str, Any\] | None</code>) – The parameters used to create the path. 123 - **raw_response** (<code>bool</code>) – If true, return the raw response instead of validating 124 and extrapolating it. 125 - **security** (<code>dict\[str, str\] | None</code>) – The security scheme to use, and the values it needs to 126 process successfully. 127 - **session** (<code>Any | None</code>) – A persistent request session. 128 - **verify** (<code>bool | str</code>) – If we should do an SSL verification on the request or not. 129 In case str was provided, will use that as the CA. 130 131 **Returns:** 132 133 - <code>Any | None</code> – The response data, either raw or processed depending on raw_response flag. 134 135 ### OpenAPIServiceConnector 136 137 A component which connects the Haystack framework to OpenAPI services. 138 139 The `OpenAPIServiceConnector` component connects the Haystack framework to OpenAPI services, enabling it to call 140 operations as defined in the OpenAPI specification of the service. 141 142 It integrates with `ChatMessage` dataclass, where the `ToolCall` entries in messages are used to determine the 143 method to be called and the parameters to be passed. The method name and parameters are then used to invoke the 144 method on the OpenAPI service. The response from the service is returned as a `ChatMessage`. 145 146 Before using this component, users usually resolve service endpoint parameters with a help of 147 `OpenAPIServiceToFunctions` component. 148 149 The example below demonstrates how to use the `OpenAPIServiceConnector` to invoke a method on a https://serper.dev/ 150 service specified via OpenAPI specification. 151 152 Note, however, that `OpenAPIServiceConnector` is usually not meant to be used directly, but rather as part of a 153 pipeline that includes the `OpenAPIServiceToFunctions` component and a Chat Generator component using an LLM 154 with tool calling capabilities. In the example below we use the tool call payload directly, but in a 155 real-world scenario, the tool calls would usually be generated by the Chat Generator component. 156 157 Usage example: 158 159 ```python 160 import json 161 import requests 162 163 from haystack.components.connectors import OpenAPIServiceConnector 164 from haystack.dataclasses import ChatMessage, ToolCall 165 166 167 tool_call = ToolCall( 168 tool_name="search", 169 arguments={"q": "Why was Sam Altman ousted from OpenAI?"}, 170 ) 171 message = ChatMessage.from_assistant(tool_calls=[tool_call]) 172 173 serper_token = "your_serper_dev_token" 174 serperdev_openapi_spec = json.loads(requests.get("https://bit.ly/serper_dev_spec").text) 175 service_connector = OpenAPIServiceConnector() 176 result = service_connector.run( 177 messages=[message], 178 service_openapi_spec=serperdev_openapi_spec, 179 service_credentials=serper_token, 180 ) 181 print(result) 182 183 # {'service_response': ChatMessage(_role=<ChatRole.USER: 'user'>, _content=[TextContent(text= 184 # '{"searchParameters": {"q": "Why was Sam Altman ousted from OpenAI?", 185 # "type": "search", "engine": "google"}, "answerBox": {"snippet": "Concerns over AI safety and OpenAI's role 186 # in protecting were at the center of Altman's brief ouster from the company."... 187 ``` 188 189 #### __init__ 190 191 ```python 192 __init__(ssl_verify: bool | str | None = None) -> None 193 ``` 194 195 Initializes the OpenAPIServiceConnector instance 196 197 **Parameters:** 198 199 - **ssl_verify** (<code>[bool | str | None</code>) – Decide if to use SSL verification to the requests or not, 200 in case a string is passed, will be used as the CA. 201 202 #### run 203 204 ```python 205 run( 206 messages: list[ChatMessage], 207 service_openapi_spec: dict[str, Any], 208 service_credentials: dict | str | None = None, 209 ) -> dict[str, list[ChatMessage]] 210 ``` 211 212 Processes a list of chat messages to invoke a method on an OpenAPI service. 213 214 It parses the last message in the list, expecting it to contain tool calls. 215 216 **Parameters:** 217 218 - **messages** (<code>list\[ChatMessage\]</code>) – A list of `ChatMessage` objects containing the messages to be processed. The last message 219 should contain the tool calls. 220 - **service_openapi_spec** (<code>dict\[str, Any\]</code>) – The OpenAPI JSON specification object of the service to be invoked. All the refs 221 should already be resolved. 222 - **service_credentials** (<code>dict | str | None</code>) – The credentials to be used for authentication with the service. 223 Currently, only the http and apiKey OpenAPI security schemes are supported. 224 225 **Returns:** 226 227 - <code>dict\[str, list\[ChatMessage\]\]</code> – A dictionary with the following keys: 228 - `service_response`: a list of `ChatMessage` objects, each containing the response from the service. The 229 response is in JSON format, and the `content` attribute of the `ChatMessage` contains 230 the JSON string. 231 232 **Raises:** 233 234 - <code>ValueError</code> – If the last message is not from the assistant or if it does not contain tool calls. 235 236 #### to_dict 237 238 ```python 239 to_dict() -> dict[str, Any] 240 ``` 241 242 Serializes the component to a dictionary. 243 244 **Returns:** 245 246 - <code>dict\[str, Any\]</code> – Dictionary with serialized data. 247 248 #### from_dict 249 250 ```python 251 from_dict(data: dict[str, Any]) -> OpenAPIServiceConnector 252 ``` 253 254 Deserializes the component from a dictionary. 255 256 **Parameters:** 257 258 - **data** (<code>dict\[str, Any\]</code>) – The dictionary to deserialize from. 259 260 **Returns:** 261 262 - <code>OpenAPIServiceConnector</code> – The deserialized component.