Commit
·
e4fe5a9
1
Parent(s):
315c2e3
Adding tool loading logic
Browse files- CEO/toolLoader.py +60 -0
- tools/weather_tool.py +49 -0
CEO/toolLoader.py
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import importlib
|
2 |
+
import importlib.util
|
3 |
+
import os
|
4 |
+
import pip
|
5 |
+
|
6 |
+
toolsImported = []
|
7 |
+
|
8 |
+
TOOLS_DIRECTORY = 'D:/Projects/AI/HASHIRU/tools'
|
9 |
+
|
10 |
+
class Tool:
|
11 |
+
def __init__(self, toolClass):
|
12 |
+
self.tool = toolClass()
|
13 |
+
self.inputSchema = self.tool.inputSchema
|
14 |
+
self.name = self.inputSchema["name"]
|
15 |
+
self.description = self.inputSchema["description"]
|
16 |
+
self.dependencies = self.tool.dependencies
|
17 |
+
for package in self.tool.dependencies:
|
18 |
+
try:
|
19 |
+
__import__(package.split('==')[0])
|
20 |
+
except ImportError:
|
21 |
+
print(f"Installing {package}")
|
22 |
+
if '==' in package:
|
23 |
+
package = package.split('==')[0]
|
24 |
+
pip.main(['install', package])
|
25 |
+
|
26 |
+
def run(self, query):
|
27 |
+
return self.tool.run(**query)
|
28 |
+
|
29 |
+
def load_tools():
|
30 |
+
for filename in os.listdir(TOOLS_DIRECTORY):
|
31 |
+
if filename.endswith(".py") and filename != "__init__.py":
|
32 |
+
module_name = filename[:-3]
|
33 |
+
spec = importlib.util.spec_from_file_location(module_name, f"{TOOLS_DIRECTORY}/{filename}")
|
34 |
+
foo = importlib.util.module_from_spec(spec)
|
35 |
+
spec.loader.exec_module(foo)
|
36 |
+
class_name = foo.__all__[0]
|
37 |
+
toolClass = getattr(foo, class_name)
|
38 |
+
toolObj = Tool(toolClass)
|
39 |
+
toolsImported.append(toolObj)
|
40 |
+
|
41 |
+
def runTool(toolName, query):
|
42 |
+
for tool in toolsImported:
|
43 |
+
if tool.name == toolName:
|
44 |
+
return tool.run(query)
|
45 |
+
return None
|
46 |
+
|
47 |
+
def getTools():
|
48 |
+
toolsList = []
|
49 |
+
for tool in toolsImported:
|
50 |
+
toolsList.append({
|
51 |
+
"type": "function",
|
52 |
+
"function": tool.inputSchema
|
53 |
+
})
|
54 |
+
return toolsList
|
55 |
+
|
56 |
+
load_tools()
|
57 |
+
|
58 |
+
# Example usage
|
59 |
+
print(getTools())
|
60 |
+
print(runTool("WeatherApi", {"location": "Davis"}))
|
tools/weather_tool.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import importlib
|
2 |
+
|
3 |
+
__all__ = ['WeatherApi']
|
4 |
+
|
5 |
+
|
6 |
+
class WeatherApi():
|
7 |
+
dependencies = ["requests==2.32.3"]
|
8 |
+
|
9 |
+
inputSchema = {
|
10 |
+
"name": "WeatherApi",
|
11 |
+
"description": "Returns weather information for a given location",
|
12 |
+
"parameters": {
|
13 |
+
"type": "object",
|
14 |
+
"properties": {
|
15 |
+
"location": {
|
16 |
+
"type": "string",
|
17 |
+
"description": "The location for which to get the weather information",
|
18 |
+
},
|
19 |
+
"required": ["location"],
|
20 |
+
},
|
21 |
+
}
|
22 |
+
}
|
23 |
+
|
24 |
+
def __init__(self):
|
25 |
+
pass
|
26 |
+
|
27 |
+
def run(self, **kwargs):
|
28 |
+
print("Running Weather API test tool")
|
29 |
+
location = kwargs.get("location")
|
30 |
+
print(f"Location: {location}")
|
31 |
+
|
32 |
+
requests = importlib.import_module("requests")
|
33 |
+
|
34 |
+
response = requests.get(
|
35 |
+
f"http://api.openweathermap.org/data/2.5/weather?q={location}&appid=ea50e63a3bea67adaf50fbecbe5b3c1e")
|
36 |
+
if response.status_code == 200:
|
37 |
+
return {
|
38 |
+
"status": "success",
|
39 |
+
"message": "Weather API test tool executed successfully",
|
40 |
+
"error": None,
|
41 |
+
"output": response.json()
|
42 |
+
}
|
43 |
+
else:
|
44 |
+
return {
|
45 |
+
"status": "error",
|
46 |
+
"message": "Weather API test tool failed",
|
47 |
+
"error": response.text,
|
48 |
+
"output": None
|
49 |
+
}
|