Add virtual_api_server.py"
Browse files
pipeline/virtual_api_server.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Virtual API server for StableToolBench.
|
| 2 |
+
|
| 3 |
+
Cache-only server. For cached requests returns cached response.
|
| 4 |
+
For uncached requests returns simulated response.
|
| 5 |
+
For full simulation, deploy stabletoolbench/MirrorAPI.
|
| 6 |
+
"""
|
| 7 |
+
import json, os, re
|
| 8 |
+
from typing import Union, Optional
|
| 9 |
+
from fastapi import FastAPI
|
| 10 |
+
from pydantic import BaseModel
|
| 11 |
+
import uvicorn
|
| 12 |
+
|
| 13 |
+
app = FastAPI()
|
| 14 |
+
|
| 15 |
+
def standardize(name):
|
| 16 |
+
res = re.compile(r"[^\u4e00-\u9fa5a-zA-Z0-9_]")
|
| 17 |
+
name = res.sub("_", name)
|
| 18 |
+
name = re.sub(r"(_)\1+", "_", name).lower().strip("_")
|
| 19 |
+
if name and name[0].isdigit(): name = "get_" + name
|
| 20 |
+
return name
|
| 21 |
+
|
| 22 |
+
def change_name(name):
|
| 23 |
+
if name in ["from", "class", "return", "false", "true", "id", "and"]: return "is_" + name
|
| 24 |
+
return name
|
| 25 |
+
|
| 26 |
+
CACHE_FOLDER = os.environ.get("CACHE_FOLDER", os.path.join(os.path.dirname(__file__), "api_cache"))
|
| 27 |
+
TOOLS_FOLDER = os.environ.get("TOOLS_FOLDER", os.path.join(os.path.dirname(__file__), "..", "toolenv2404_filtered"))
|
| 28 |
+
|
| 29 |
+
class Info(BaseModel):
|
| 30 |
+
category: str
|
| 31 |
+
tool_name: str
|
| 32 |
+
api_name: str
|
| 33 |
+
tool_input: Union[str, dict]
|
| 34 |
+
strip: str
|
| 35 |
+
toolbench_key: str = ""
|
| 36 |
+
|
| 37 |
+
def prepare_tool_name_and_url(info):
|
| 38 |
+
category = info.category.replace(" ", "_").replace(",", "_").replace("/", "_").replace("__", "_")
|
| 39 |
+
tool_name = info.tool_name
|
| 40 |
+
api_name = change_name(standardize(info.api_name)).split(f"_for_{tool_name}")[0]
|
| 41 |
+
if not tool_name.endswith(f"_for_{category}"):
|
| 42 |
+
tool_name = standardize(info.tool_name) + f"_for_{category}"
|
| 43 |
+
return tool_name, category, api_name
|
| 44 |
+
|
| 45 |
+
@app.post('/virtual')
|
| 46 |
+
def get_virtual_response(info: Info):
|
| 47 |
+
tool_name, standard_category, api_name = prepare_tool_name_and_url(info)
|
| 48 |
+
tool_input = info.tool_input
|
| 49 |
+
try:
|
| 50 |
+
if isinstance(tool_input, str): tool_input = json.loads(tool_input) if tool_input else {}
|
| 51 |
+
except: tool_input = {}
|
| 52 |
+
cache_path = os.path.join(CACHE_FOLDER, standard_category, tool_name, api_name + ".json")
|
| 53 |
+
if os.path.exists(cache_path):
|
| 54 |
+
try:
|
| 55 |
+
with open(cache_path) as f: cache = json.load(f)
|
| 56 |
+
if str(tool_input) in cache: return cache[str(tool_input)]
|
| 57 |
+
except: pass
|
| 58 |
+
api_doc = _load_api_doc(standard_category, info.tool_name, api_name)
|
| 59 |
+
if api_doc:
|
| 60 |
+
return {"error": "", "response": json.dumps({"message": f"Simulated response for {api_name}", "input": tool_input, "status": "success"})}
|
| 61 |
+
return {"error": "API not working error...", "response": ""}
|
| 62 |
+
|
| 63 |
+
def _load_api_doc(category, tool_name, api_name):
|
| 64 |
+
json_path = os.path.join(TOOLS_FOLDER, category, standardize(tool_name) + ".json")
|
| 65 |
+
if not os.path.exists(json_path): return None
|
| 66 |
+
try:
|
| 67 |
+
with open(json_path) as f: return json.load(f)
|
| 68 |
+
except: return None
|
| 69 |
+
|
| 70 |
+
@app.get('/health')
|
| 71 |
+
def health(): return {"status": "ok"}
|
| 72 |
+
|
| 73 |
+
def main():
|
| 74 |
+
port = int(os.environ.get("API_SERVER_PORT", 8080))
|
| 75 |
+
print(f"Starting virtual API server on port {port}\nCache: {CACHE_FOLDER}\nTools: {TOOLS_FOLDER}")
|
| 76 |
+
uvicorn.run(app, host="0.0.0.0", port=port)
|
| 77 |
+
|
| 78 |
+
if __name__ == "__main__": main()
|