deepset-ai--haystack
c56bef871b
CodeQL / Analyze (python) (push) Has been cancelled
Update Platform Components Table / update (push) Has been cancelled
Docker image release / Build base image (push) Has been cancelled
Sync docs with Docusaurus / sync (push) Has been cancelled
Tests / Check if changed (push) Has been cancelled
Tests / format (push) Has been cancelled
Tests / check-imports (push) Has been cancelled
Tests / Unit / macos-latest (push) Has been cancelled
Tests / Unit / ubuntu-latest (push) Has been cancelled
Tests / Unit / windows-latest (push) Has been cancelled
Tests / mypy (push) Has been cancelled
Tests / Integration / ubuntu-latest (push) Has been cancelled
Tests / Integration / macos-latest (push) Has been cancelled
Tests / Integration / windows-latest (push) Has been cancelled
Tests / notify-slack-on-failure (push) Has been cancelled
Tests / Mark tests as completed (push) Has been cancelled
114 行
4.1 KiB
Plaintext
114 行
4.1 KiB
Plaintext
---
|
|
title: "OpenAPIConnector"
|
|
id: openapiconnector
|
|
slug: "/openapiconnector"
|
|
description: "`OpenAPIConnector` is a component that acts as an interface between the Haystack ecosystem and OpenAPI services."
|
|
---
|
|
|
|
# OpenAPIConnector
|
|
|
|
`OpenAPIConnector` is a component that acts as an interface between the Haystack ecosystem and OpenAPI services.
|
|
|
|
:::tip[Consider using MCP instead]
|
|
|
|
These OpenAPI components are a legacy way to connect Haystack to external APIs. For most use cases, we recommend the [`MCPTool`](../../tools/mcptool.mdx) instead: it is the modern, standardized way to give your pipelines and agents access to external tools and services.
|
|
|
|
:::
|
|
|
|
<div className="key-value-table">
|
|
|
|
| | |
|
|
| --- | --- |
|
|
| **Most common position in a pipeline** | Anywhere, after components providing input for its run parameters |
|
|
| **Mandatory init variables** | `openapi_spec`: The OpenAPI specification for the service. Can be a URL, file path, or raw string. |
|
|
| **Mandatory run variables** | `operation_id`: The operationId from the OpenAPI spec to invoke. |
|
|
| **Output variables** | `response`: A REST service response |
|
|
| **API reference** | [OpenAPI](/reference/integrations-openapi) |
|
|
| **GitHub link** | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/openapi |
|
|
| **Package name** | `openapi-haystack` |
|
|
|
|
</div>
|
|
|
|
## Overview
|
|
|
|
The `OpenAPIConnector` is a component within the Haystack ecosystem that allows direct invocation of REST endpoints defined in an OpenAPI (formerly Swagger) specification. It acts as a bridge between Haystack pipelines and any REST API that follows the OpenAPI standard, enabling dynamic method calls, authentication, and parameter handling.
|
|
|
|
To use the `OpenAPIConnector`, ensure that you have the `openapi-haystack` package installed:
|
|
|
|
```shell
|
|
pip install openapi-haystack
|
|
```
|
|
|
|
Unlike [OpenAPIServiceConnector](openapiserviceconnector.mdx), which works with LLMs, `OpenAPIConnector` directly calls REST endpoints using explicit input arguments.
|
|
|
|
## Usage
|
|
|
|
### On its own
|
|
|
|
You can initialize and use the `OpenAPIConnector` on its own by passing an OpenAPI specification and other parameters:
|
|
|
|
```python
|
|
from haystack.utils import Secret
|
|
from haystack_integrations.components.connectors.openapi import OpenAPIConnector
|
|
|
|
connector = OpenAPIConnector(
|
|
openapi_spec="https://bit.ly/serperdev_openapi",
|
|
credentials=Secret.from_env_var("SERPERDEV_API_KEY"),
|
|
service_kwargs={"config_factory": my_custom_config_factory},
|
|
)
|
|
|
|
response = connector.run(
|
|
operation_id="search",
|
|
arguments={"q": "Who was Nikola Tesla?"},
|
|
)
|
|
```
|
|
|
|
#### Output
|
|
|
|
The `OpenAPIConnector` returns a dictionary containing the service response:
|
|
|
|
```json
|
|
{
|
|
"response": { // here goes REST endpoint response JSON
|
|
}
|
|
}
|
|
```
|
|
|
|
### In a pipeline
|
|
|
|
The `OpenAPIConnector` can be integrated into a Haystack pipeline to interact with OpenAPI services. For example, here’s how you can link the `OpenAPIConnector` to a pipeline:
|
|
|
|
```python
|
|
from haystack import Pipeline
|
|
from haystack_integrations.components.connectors.openapi import OpenAPIConnector
|
|
from haystack.dataclasses.chat_message import ChatMessage
|
|
from haystack.utils import Secret
|
|
|
|
# Initialize the OpenAPIConnector
|
|
connector = OpenAPIConnector(
|
|
openapi_spec="https://bit.ly/serperdev_openapi",
|
|
credentials=Secret.from_env_var("SERPERDEV_API_KEY"),
|
|
)
|
|
|
|
# Create a ChatMessage from the user
|
|
user_message = ChatMessage.from_user(text="Who was Nikola Tesla?")
|
|
|
|
# Define the pipeline
|
|
pipeline = Pipeline()
|
|
pipeline.add_component("openapi_connector", connector)
|
|
|
|
# Run the pipeline
|
|
response = pipeline.run(
|
|
data={
|
|
"openapi_connector": {
|
|
"operation_id": "search",
|
|
"arguments": {"q": user_message.text},
|
|
},
|
|
},
|
|
)
|
|
|
|
# Extract the answer from the response
|
|
answer = response.get("openapi_connector", {}).get("response", {})
|
|
print(answer)
|
|
```
|