helloparthshah commited on
Commit
8b2fc7e
·
1 Parent(s): e4fe5a9

Adding tool loading logic to CEO

Browse files
Files changed (4) hide show
  1. CEO/CEO.py +6 -0
  2. CEO/toolLoader.py +36 -30
  3. tools/test.py +51 -0
  4. tools/tool_creator.py +50 -0
CEO/CEO.py CHANGED
@@ -4,6 +4,7 @@ from typing import List, Dict, Optional
4
  from pathlib import Path
5
  import ollama
6
  from googlesearch import search
 
7
 
8
  # Enum for Model Types
9
  class ModelType(Enum):
@@ -130,6 +131,11 @@ if __name__ == "__main__":
130
  },
131
  }
132
  ]
 
 
 
 
 
133
 
134
  # Create the Ollama model manager and ensure the model is set up.
135
  model_manager = OllamaModelManager()
 
4
  from pathlib import Path
5
  import ollama
6
  from googlesearch import search
7
+ from toolLoader import ToolLoader
8
 
9
  # Enum for Model Types
10
  class ModelType(Enum):
 
131
  },
132
  }
133
  ]
134
+
135
+ # Load the tools using the ToolLoader class.
136
+ tool_loader = ToolLoader()
137
+ tool_loader.load_tools()
138
+ tools.extend(tool_loader.getTools())
139
 
140
  # Create the Ollama model manager and ensure the model is set up.
141
  model_manager = OllamaModelManager()
CEO/toolLoader.py CHANGED
@@ -26,35 +26,41 @@ class Tool:
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"}))
 
26
  def run(self, query):
27
  return self.tool.run(**query)
28
 
29
+ class ToolLoader:
30
+ def __init__(self):
31
+ self.toolsImported = []
32
+ self.load_tools()
33
+ pass
34
+
35
+ def load_tools(self):
36
+ for filename in os.listdir(TOOLS_DIRECTORY):
37
+ if filename.endswith(".py") and filename != "__init__.py":
38
+ module_name = filename[:-3]
39
+ spec = importlib.util.spec_from_file_location(module_name, f"{TOOLS_DIRECTORY}/{filename}")
40
+ foo = importlib.util.module_from_spec(spec)
41
+ spec.loader.exec_module(foo)
42
+ class_name = foo.__all__[0]
43
+ toolClass = getattr(foo, class_name)
44
+ toolObj = Tool(toolClass)
45
+ self.toolsImported.append(toolObj)
46
+
47
+ def runTool(self, toolName, query):
48
+ for tool in self.toolsImported:
49
+ if tool.name == toolName:
50
+ return tool.run(query)
51
+ return None
52
+
53
+ def getTools(self):
54
+ toolsList = []
55
+ for tool in self.toolsImported:
56
+ toolsList.append({
57
+ "type": "function",
58
+ "function": tool.inputSchema
59
+ })
60
+ return toolsList
61
+
62
+ toolLoader = ToolLoader()
63
 
64
  # Example usage
65
+ # print(toolLoader.getTools())
66
+ # print(toolLoader.runTool("WeatherApi", {"location": "Davis"}))
tools/test.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import importlib
3
+
4
+ __all__ = ['ToolCreator']
5
+
6
+
7
+ class ToolCreator():
8
+ dependencies = ["os"]
9
+
10
+ inputSchema = {
11
+ "name": "ToolCreator",
12
+ "description": "Creates a tool for the given function",
13
+ "parameters": {
14
+ "type": "object",
15
+ "properties": {
16
+ "name": {
17
+ "type": "string",
18
+ "description": "The name of the tool to create",
19
+ },
20
+ "content": {
21
+ "type": "string",
22
+ "description": "The content of the tool to create",
23
+ },
24
+ "required": ["name", "content"],
25
+ },
26
+ }
27
+ }
28
+
29
+ def __init__(self):
30
+ pass
31
+
32
+ def run(self, **kwargs):
33
+ print("Running Tool Creator")
34
+ name = kwargs.get("name")
35
+ content = kwargs.get("content")
36
+ print(f"Tool Name: {name}")
37
+ print(f"Tool Content: {content}")
38
+ # Create the tool file
39
+ tool_file_path = f"tools/{name}.py"
40
+ with open(tool_file_path, "w") as tool_file:
41
+ tool_file.write(content)
42
+ print(f"Tool file created at {tool_file_path}")
43
+ return {
44
+ "status": "success",
45
+ "message": "Tool created successfully",
46
+ "error": None,
47
+ "output": {
48
+ "tool_file_path": tool_file_path,
49
+ "tool_name": name,
50
+ }
51
+ }
tools/tool_creator.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import importlib
2
+
3
+ __all__ = ['ToolCreator']
4
+
5
+
6
+ class ToolCreator():
7
+ dependencies = ["os"]
8
+
9
+ inputSchema = {
10
+ "name": "ToolCreator",
11
+ "description": "Creates a tool for the given function",
12
+ "parameters": {
13
+ "type": "object",
14
+ "properties": {
15
+ "name": {
16
+ "type": "string",
17
+ "description": "The name of the tool to create",
18
+ },
19
+ "content": {
20
+ "type": "string",
21
+ "description": "The content of the tool to create",
22
+ },
23
+ "required": ["name", "content"],
24
+ },
25
+ }
26
+ }
27
+
28
+ def __init__(self):
29
+ pass
30
+
31
+ def run(self, **kwargs):
32
+ print("Running Tool Creator")
33
+ name = kwargs.get("name")
34
+ content = kwargs.get("content")
35
+ print(f"Tool Name: {name}")
36
+ print(f"Tool Content: {content}")
37
+ # Create the tool file
38
+ tool_file_path = f"tools/{name}.py"
39
+ with open(tool_file_path, "w") as tool_file:
40
+ tool_file.write(content)
41
+ print(f"Tool file created at {tool_file_path}")
42
+ return {
43
+ "status": "success",
44
+ "message": "Tool created successfully",
45
+ "error": None,
46
+ "output": {
47
+ "tool_file_path": tool_file_path,
48
+ "tool_name": name,
49
+ }
50
+ }