mirror of
https://github.com/run-llama/create-llama.git
synced 2026-07-16 03:04:21 -04:00
Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 099f626586 | |||
| 956538eeb0 | |||
| 555f6b2905 | |||
| d8bc271a21 | |||
| f29561cde2 | |||
| 442abae8ac | |||
| 1f5a26f3a8 |
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"create-llama": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Add OpenAPI action tool
|
||||||
@@ -117,6 +117,37 @@ export const supportedTools: Tool[] = [
|
|||||||
},
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
display: "OpenAPI action",
|
||||||
|
name: "openapi_action.OpenAPIActionToolSpec",
|
||||||
|
dependencies: [
|
||||||
|
{
|
||||||
|
name: "llama-index-tools-openapi",
|
||||||
|
version: "0.1.3",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "jsonschema",
|
||||||
|
version: "^4.22.0",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "llama-index-tools-requests",
|
||||||
|
version: "0.1.3",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
config: {
|
||||||
|
openapi_uri: "The URL or file path of the OpenAPI schema",
|
||||||
|
},
|
||||||
|
supportedFrameworks: ["fastapi"],
|
||||||
|
type: ToolType.LOCAL,
|
||||||
|
envVars: [
|
||||||
|
{
|
||||||
|
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.",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
export const getTool = (toolName: string): Tool | undefined => {
|
export const getTool = (toolName: string): Tool | undefined => {
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
import os
|
import os
|
||||||
import yaml
|
import yaml
|
||||||
|
import json
|
||||||
import importlib
|
import importlib
|
||||||
|
from cachetools import cached, LRUCache
|
||||||
from llama_index.core.tools.tool_spec.base import BaseToolSpec
|
from llama_index.core.tools.tool_spec.base import BaseToolSpec
|
||||||
from llama_index.core.tools.function_tool import FunctionTool
|
from llama_index.core.tools.function_tool import FunctionTool
|
||||||
|
|
||||||
@@ -19,6 +20,14 @@ class ToolFactory:
|
|||||||
}
|
}
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
@cached(
|
||||||
|
LRUCache(maxsize=100),
|
||||||
|
key=lambda tool_type, tool_name, config: (
|
||||||
|
tool_type,
|
||||||
|
tool_name,
|
||||||
|
json.dumps(config, sort_keys=True),
|
||||||
|
),
|
||||||
|
)
|
||||||
def load_tools(tool_type: str, tool_name: str, config: dict) -> list[FunctionTool]:
|
def load_tools(tool_type: str, tool_name: str, config: dict) -> list[FunctionTool]:
|
||||||
source_package = ToolFactory.TOOL_SOURCE_PACKAGE_MAP[tool_type]
|
source_package = ToolFactory.TOOL_SOURCE_PACKAGE_MAP[tool_type]
|
||||||
try:
|
try:
|
||||||
|
|||||||
@@ -0,0 +1,71 @@
|
|||||||
|
from typing import Dict, List, Tuple
|
||||||
|
from llama_index.tools.openapi import OpenAPIToolSpec
|
||||||
|
from llama_index.tools.requests import RequestsToolSpec
|
||||||
|
|
||||||
|
|
||||||
|
class OpenAPIActionToolSpec(OpenAPIToolSpec, RequestsToolSpec):
|
||||||
|
"""
|
||||||
|
A combination of OpenAPI and Requests tool specs that can parse OpenAPI specs and make requests.
|
||||||
|
|
||||||
|
openapi_uri: str: The file path or URL to the OpenAPI spec.
|
||||||
|
domain_headers: dict: Whitelist domains and the headers to use.
|
||||||
|
"""
|
||||||
|
|
||||||
|
spec_functions = OpenAPIToolSpec.spec_functions + RequestsToolSpec.spec_functions
|
||||||
|
|
||||||
|
def __init__(self, openapi_uri: str, domain_headers: dict = {}, **kwargs):
|
||||||
|
# Load the OpenAPI spec
|
||||||
|
openapi_spec, servers = self.load_openapi_spec(openapi_uri)
|
||||||
|
|
||||||
|
# Add the servers to the domain headers if they are not already present
|
||||||
|
for server in servers:
|
||||||
|
if server not in domain_headers:
|
||||||
|
domain_headers[server] = {}
|
||||||
|
|
||||||
|
OpenAPIToolSpec.__init__(self, spec=openapi_spec)
|
||||||
|
RequestsToolSpec.__init__(self, domain_headers)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def load_openapi_spec(uri: str) -> Tuple[Dict, List[str]]:
|
||||||
|
"""
|
||||||
|
Load an OpenAPI spec from a URI.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
uri (str): A file path or URL to the OpenAPI spec.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
List[Document]: A list of Document objects.
|
||||||
|
"""
|
||||||
|
import yaml
|
||||||
|
from urllib.parse import urlparse
|
||||||
|
|
||||||
|
if uri.startswith("http"):
|
||||||
|
import requests
|
||||||
|
|
||||||
|
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 = urlparse(uri).path
|
||||||
|
with open(filepath, "r") as file:
|
||||||
|
spec = yaml.safe_load(file)
|
||||||
|
else:
|
||||||
|
raise ValueError(
|
||||||
|
"Could not initialize OpenAPIActionToolSpec: Invalid OpenAPI URI provided. "
|
||||||
|
"Only HTTP and file path are supported."
|
||||||
|
)
|
||||||
|
# Add the servers to the whitelist
|
||||||
|
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
|
||||||
Reference in New Issue
Block a user