diff --git a/helpers/tools.ts b/helpers/tools.ts index 70ee87723b5eceaa4ab5c4e66e5d6bda3514c492..2b949c693053ef16648823ddaa77d545115a7b33 100644 --- a/helpers/tools.ts +++ b/helpers/tools.ts @@ -143,7 +143,8 @@ export const supportedTools: Tool[] = [ { name: TOOL_SYSTEM_PROMPT_ENV_VAR, description: "System prompt for openapi action tool.", - value: `You are an OpenAPI action agent. You help users to make requests to the provided OpenAPI schema.`, + value: + "You are an OpenAPI action agent. You help users to make requests to the provided OpenAPI schema.", }, ], }, diff --git a/templates/components/engines/python/agent/tools/openapi_action.py b/templates/components/engines/python/agent/tools/openapi_action.py index d47fa6652ad54a801fc701d4a34629031501e28e..1e1a8e3041bbd0f74f0f84be0a4ef988d3353c95 100644 --- a/templates/components/engines/python/agent/tools/openapi_action.py +++ b/templates/components/engines/python/agent/tools/openapi_action.py @@ -1,4 +1,3 @@ -import inspect from typing import Dict, List, Tuple from llama_index.tools.openapi import OpenAPIToolSpec from llama_index.tools.requests import RequestsToolSpec @@ -43,17 +42,30 @@ class OpenAPIActionToolSpec(OpenAPIToolSpec, RequestsToolSpec): if uri.startswith("http"): import requests - response = requests.get(uri).text - spec = yaml.safe_load(response) + response = requests.get(uri) + if response.status_code != 200: + raise ValueError( + "Could not initialize OpenAPIActionToolSpec: " + f"Failed to load OpenAPI spec from {uri}, status code: {response.status_code}" + ) + spec = yaml.safe_load(response.text) elif uri.startswith("file"): filepath = uri[7:] # Remove the 'file://' scheme with open(filepath, "r") as file: spec = yaml.safe_load(file) else: raise ValueError( - "Could not initialize OpenAPIActionToolSpec because invalid OpenAPI URI provided. " + "Could not initialize OpenAPIActionToolSpec: Invalid OpenAPI URI provided. " "Only HTTP and file path are supported." ) # Add the servers to the whitelist - servers = [urlparse(server["url"]).netloc for server in spec.get("servers", [])] + try: + servers = [ + urlparse(server["url"]).netloc for server in spec.get("servers", []) + ] + except KeyError as e: + raise ValueError( + "Could not initialize OpenAPIActionToolSpec: Invalid OpenAPI spec provided. " + "Could not get `servers` from the spec." + ) from e return spec, servers