Allows Haystack to interact with OpenAPI specified services.
Module haystack_experimental.components.tools.openapi.openapi_tool
OpenAPITool
The OpenAPITool calls a RESTful endpoint of an OpenAPI service using payloads generated from human instructions.
Here is an example of how to use the OpenAPITool component to scrape a URL using the FireCrawl API:
from haystack.dataclasses import ChatMessage
from haystack_experimental.components.tools.openapi import OpenAPITool, LLMProvider
from haystack.utils import Secret
tool = OpenAPITool(generator_api=LLMProvider.OPENAI,
generator_api_params={"model":"gpt-3.5-turbo"},
spec="https://raw.githubusercontent.com/mendableai/firecrawl/main/apps/api/openapi.json",
credentials=Secret.from_env_var("FIRECRAWL_API_KEY"))
results = tool.run(messages=[ChatMessage.from_user("Scrape URL: https://news.ycombinator.com/")])
print(results)
Similarly, you can use the OpenAPITool component to invoke any OpenAPI service/tool by providing the OpenAPI specification and credentials.
OpenAPITool.__init__
def __init__(generator_api: LLMProvider,
generator_api_params: Optional[Dict[str, Any]] = None,
spec: Optional[Union[str, Path]] = None,
credentials: Optional[Secret] = None)
Initialize the OpenAPITool component.
Arguments:
generator_api
: The API provider for the chat generator.generator_api_params
: Parameters to pass for the chat generator creation.spec
: OpenAPI specification for the tool/service. This can be a URL, a local file path, or an OpenAPI service specification provided as a string.credentials
: Credentials for the tool/service.
OpenAPITool.run
@component.output_types(service_response=List[ChatMessage])
def run(messages: List[ChatMessage],
fc_generator_kwargs: Optional[Dict[str, Any]] = None,
spec: Optional[Union[str, Path]] = None,
credentials: Optional[Secret] = None) -> Dict[str, List[ChatMessage]]
Invokes the underlying OpenAPI service/tool with the function calling payload generated by the chat generator.
Arguments:
messages
: List of ChatMessages to generate function calling payload (e.g. human instructions). The last message should be human instruction containing enough information to generate the function calling payload suitable for the OpenAPI service/tool used. See the examples in the class docstring.fc_generator_kwargs
: Additional arguments for the function calling payload generation process.spec
: OpenAPI specification for the tool/service, overrides the one provided at initialization.credentials
: Credentials for the tool/service, overrides the one provided at initialization.
Returns:
a dictionary containing the service response with the following key:
service_response
: List of ChatMessages containing the service response. ChatMessages are generated based on the response from the OpenAPI service/tool and contains the JSON response from the service. If there is an error during the invocation, the response will be a ChatMessage with the error message under theerror
key.
OpenAPITool.to_dict
def to_dict() -> Dict[str, Any]
Serialize this component to a dictionary.
Returns:
The serialized component as a dictionary.
OpenAPITool.from_dict
@classmethod
def from_dict(cls, data: Dict[str, Any]) -> "OpenAPITool"
Deserialize this component from a dictionary.
Arguments:
data
: The dictionary representation of this component.
Returns:
The deserialized component instance.