Skip to content

armgit / txtai

Public Imported from GitHub
0

💡 All-in-one AI framework for semantic search, LLM orchestration and language model workflows

Code cleanup

davidmezzetti3ea197468deb
diff --git a/src/python/txtai/agent/tool/factory.py b/src/python/txtai/agent/tool/factory.py
index 9474b99d..dfb89928 100644
--- a/src/python/txtai/agent/tool/factory.py
+++ b/src/python/txtai/agent/tool/factory.py
@@ -30,50 +30,22 @@ class ToolFactory:
Methods to create tools.
"""
- # Names in the default toolkit.
- DEFAULTS = (
- "bash",
- "edit",
- "glob",
- "grep",
- "python",
- "question",
- "read",
- "todowrite",
- "websearch",
- "webview",
- "write",
- )
-
- @staticmethod
- def default(name):
- """
- Creates a default tool by alias name.
-
- Args:
- name: default tool alias name
-
- Returns:
- Tool
- """
-
- tools = {
- "bash": BashTool,
- "edit": EditTool,
- "glob": GlobTool,
- "grep": GrepTool,
- "python": PythonInterpreterTool,
- "question": UserInputTool,
- "read": ReadTool,
- "webview": ReadTool,
- "todowrite": TodoWriteTool,
- "websearch": WebSearchTool,
- "write": WriteTool,
- }
- if name not in tools:
- raise KeyError(name)
-
- return tools[name]()
+ # Default toolkit
+ DEFAULTS = {
+ "bash": BashTool,
+ "edit": EditTool,
+ "glob": GlobTool,
+ "grep": GrepTool,
+ "python": PythonInterpreterTool,
+ "question": UserInputTool,
+ "read": ReadTool,
+ "todowrite": TodoWriteTool,
+ "websearch": WebSearchTool,
+ "write": WriteTool,
+ }
+
+ # Backwards compatible mappings
+ DEFAULTS["webview"] = DEFAULTS["read"]
@staticmethod
def create(config):
@@ -150,6 +122,20 @@ class ToolFactory:
except (TypeHintParsingException, TypeError):
return ToolFactory.fromdocs(target, config if config else {})
+ @staticmethod
+ def default(name):
+ """
+ Creates a default tool by alias name.
+
+ Args:
+ name: default tool alias name
+
+ Returns:
+ Tool
+ """
+
+ return ToolFactory.DEFAULTS[name]()
+
@staticmethod
def fromdocs(target, config):
"""
diff --git a/test/python/testagent.py b/test/python/testagent.py
index bf7d9542..d211db08 100644
--- a/test/python/testagent.py
+++ b/test/python/testagent.py
@@ -3,7 +3,6 @@ Agent module tests
"""
import os
-import subprocess
import tempfile
import unittest
@@ -185,6 +184,11 @@ class TestAgent(unittest.TestCase):
agent.tools["edit"](path, "hello", "goodbye")
self.assertEqual(agent.tools["read"](path), "goodbye world".strip())
+ # Test dynamic default tool creation
+ tool = ToolFactory.default("bash")
+ self.assertEqual(tool.name, "bash")
+ self.assertIsNot(tool, ToolFactory.default("bash"))
+
def testToolsEmbeddings(self):
"""
Test adding Embeddings as a tool
@@ -229,18 +233,3 @@ class TestAgent(unittest.TestCase):
agent = Agent(tools=["http://localhost:8000/mcp"], llm="hf-internal-testing/tiny-random-LlamaForCausalLM", max_steps=1)
self.assertEqual(len(agent.tools), 2)
-
-
-class TestToolFactory(unittest.TestCase):
- """
- ToolFactory tests.
- """
-
- def testDefaultToolCreation(self):
- """
- Test default tools are created on demand.
- """
-
- tool = ToolFactory.default("bash")
- self.assertEqual(tool.name, "bash")
- self.assertIsNot(tool, ToolFactory.default("bash"))
diff --git a/test/python/testapi/testopenai.py b/test/python/testapi/testopenai.py
index 6fe54276..4f7682e9 100644
--- a/test/python/testapi/testopenai.py
+++ b/test/python/testapi/testopenai.py
@@ -113,15 +113,6 @@ class TestOpenAI(unittest.TestCase):
self.assertEqual(response["choices"][0]["message"]["content"], "Hi")
- def testChatLLM(self):
- """
- Test a chat completion with a LLM
- """
-
- response = self.client.post("/v1/chat/completions", json={"messages": [{"role": "user", "content": "Hello"}], "model": "llm"}).json()
-
- self.assertIsNotNone(response["choices"][0]["message"]["content"])
-
def testChatLatestMessage(self):
"""
Test a chat completion with multiple messages
@@ -140,6 +131,15 @@ class TestOpenAI(unittest.TestCase):
self.assertEqual(response["choices"][0]["message"]["content"], "Hello")
+ def testChatLLM(self):
+ """
+ Test a chat completion with a LLM
+ """
+
+ response = self.client.post("/v1/chat/completions", json={"messages": [{"role": "user", "content": "Hello"}], "model": "llm"}).json()
+
+ self.assertIsNotNone(response["choices"][0]["message"]["content"])
+
def testChatPipeline(self):
"""
Test a chat completion with a pipeline