MCPcopy Create free account
hub / github.com/OpenBMB/BMTools / ToolServer

Class ToolServer

bmtools/tools/serve.py:63–101  ·  view source on GitHub ↗

This class host your own API backend.

Source from the content-addressed store, hash-verified

61 }
62
63class ToolServer:
64 """ This class host your own API backend.
65 """
66 def __init__(self) -> None:
67 # define the root API server
68 self.api = fastapi.FastAPI(
69 title="BMTools",
70 description="Tools for bigmodels",
71 )
72 self.loaded_tools = dict()
73 self.retriever = Retriever()
74 _bind_tool_server(self)
75
76 def load_tool(self, name: str, config: Dict = {}):
77 if self.is_loaded(name):
78 raise ValueError(f"Tool {name} is already loaded")
79 try:
80 tool = build_tool(name, config)
81 except BaseException as e:
82 print(f"Cannot load tool {name}: {repr(e)}")
83 return
84 self.loaded_tools[name] = tool.api_info
85 self.retriever.add_tool(name, tool.api_info)
86
87 # mount sub API server to the root API server, thus can mount all urls of sub API server to /tools/{name} route
88 self.api.mount(f"/tools/{name}", tool, name)
89 return
90
91 def is_loaded(self, name : str):
92 return name in self.loaded_tools
93
94 def serve(self, host : str = "0.0.0.0", port : int = 8079):
95 uvicorn.run(self.api, host=host, port=port)
96
97 def list_tools(self) -> List[str]:
98 return list_tools()
99
100 def retrieve(self, query: str, topk: int = 3) -> List[str]:
101 return self.retriever.query(query, topk)

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected