diff --git a/application/tools/base_agent.py b/application/agents/base.py similarity index 96% rename from application/tools/base_agent.py rename to application/agents/base.py index bc8f61a4..93dcb4e2 100644 --- a/application/tools/base_agent.py +++ b/application/agents/base.py @@ -1,10 +1,11 @@ from typing import Dict, Generator +from application.agents.llm_handler import get_llm_handler +from application.agents.tools.tool_action_parser import ToolActionParser +from application.agents.tools.tool_manager import ToolManager + from application.core.mongo_db import MongoDB from application.llm.llm_creator import LLMCreator -from application.tools.llm_handler import get_llm_handler -from application.tools.tool_action_parser import ToolActionParser -from application.tools.tool_manager import ToolManager class BaseAgent: diff --git a/application/tools/agent.py b/application/agents/classic_agent.py similarity index 98% rename from application/tools/agent.py rename to application/agents/classic_agent.py index 95895e70..c7846a04 100644 --- a/application/tools/agent.py +++ b/application/agents/classic_agent.py @@ -1,8 +1,9 @@ import uuid from typing import Dict, Generator +from application.agents.base import BaseAgent + from application.retriever.base import BaseRetriever -from application.tools.base_agent import BaseAgent class ClassicAgent(BaseAgent): diff --git a/application/tools/llm_handler.py b/application/agents/llm_handler.py similarity index 100% rename from application/tools/llm_handler.py rename to application/agents/llm_handler.py diff --git a/application/tools/implementations/api_tool.py b/application/agents/tools/api_tool.py similarity index 98% rename from application/tools/implementations/api_tool.py rename to application/agents/tools/api_tool.py index 69026dc1..06d5fb7a 100644 --- a/application/tools/implementations/api_tool.py +++ b/application/agents/tools/api_tool.py @@ -1,7 +1,7 @@ import json import requests -from application.tools.base import Tool +from application.agents.tools.base import Tool class APITool(Tool): diff --git a/application/tools/base.py b/application/agents/tools/base.py similarity index 100% rename from application/tools/base.py rename to application/agents/tools/base.py diff --git a/application/tools/implementations/cryptoprice.py b/application/agents/tools/cryptoprice.py similarity index 98% rename from application/tools/implementations/cryptoprice.py rename to application/agents/tools/cryptoprice.py index 7b88c866..80d0c2fc 100644 --- a/application/tools/implementations/cryptoprice.py +++ b/application/agents/tools/cryptoprice.py @@ -1,5 +1,5 @@ import requests -from application.tools.base import Tool +from application.agents.tools.base import Tool class CryptoPriceTool(Tool): diff --git a/application/tools/implementations/telegram.py b/application/agents/tools/telegram.py similarity index 98% rename from application/tools/implementations/telegram.py rename to application/agents/tools/telegram.py index a32bbe88..06350ae9 100644 --- a/application/tools/implementations/telegram.py +++ b/application/agents/tools/telegram.py @@ -1,5 +1,5 @@ import requests -from application.tools.base import Tool +from application.agents.tools.base import Tool class TelegramTool(Tool): diff --git a/application/tools/tool_action_parser.py b/application/agents/tools/tool_action_parser.py similarity index 100% rename from application/tools/tool_action_parser.py rename to application/agents/tools/tool_action_parser.py diff --git a/application/tools/tool_manager.py b/application/agents/tools/tool_manager.py similarity index 79% rename from application/tools/tool_manager.py rename to application/agents/tools/tool_manager.py index 3e0766cf..ad71db28 100644 --- a/application/tools/tool_manager.py +++ b/application/agents/tools/tool_manager.py @@ -3,7 +3,7 @@ import inspect import os import pkgutil -from application.tools.base import Tool +from application.agents.tools.base import Tool class ToolManager: @@ -13,13 +13,11 @@ class ToolManager: self.load_tools() def load_tools(self): - tools_dir = os.path.join(os.path.dirname(__file__), "implementations") + tools_dir = os.path.join(os.path.dirname(__file__)) for finder, name, ispkg in pkgutil.iter_modules([tools_dir]): if name == "base" or name.startswith("__"): continue - module = importlib.import_module( - f"application.tools.implementations.{name}" - ) + module = importlib.import_module(f"application.agents.tools.{name}") for member_name, obj in inspect.getmembers(module, inspect.isclass): if issubclass(obj, Tool) and obj is not Tool: tool_config = self.config.get(name, {}) @@ -27,9 +25,7 @@ class ToolManager: def load_tool(self, tool_name, tool_config): self.config[tool_name] = tool_config - module = importlib.import_module( - f"application.tools.implementations.{tool_name}" - ) + module = importlib.import_module(f"application.agents.tools.{tool_name}") for member_name, obj in inspect.getmembers(module, inspect.isclass): if issubclass(obj, Tool) and obj is not Tool: return obj(tool_config) diff --git a/application/api/answer/routes.py b/application/api/answer/routes.py index f83f0c7e..d21c256e 100644 --- a/application/api/answer/routes.py +++ b/application/api/answer/routes.py @@ -1,15 +1,16 @@ import asyncio import datetime import json +import logging import os import traceback -import logging from bson.dbref import DBRef from bson.objectid import ObjectId from flask import Blueprint, make_response, request, Response from flask_restx import fields, Namespace, Resource +from application.agents.classic_agent import ClassicAgent from application.core.mongo_db import MongoDB from application.core.settings import settings @@ -17,7 +18,6 @@ from application.error import bad_request from application.extensions import api from application.llm.llm_creator import LLMCreator from application.retriever.retriever_creator import RetrieverCreator -from application.tools.agent import ClassicAgent from application.utils import check_required_fields, limit_chat_history logger = logging.getLogger(__name__) diff --git a/application/api/user/routes.py b/application/api/user/routes.py index f71ab3dc..dced8a82 100644 --- a/application/api/user/routes.py +++ b/application/api/user/routes.py @@ -1,9 +1,9 @@ import datetime +import json import math import os import shutil import uuid -import json from bson.binary import Binary, UuidRepresentation from bson.dbref import DBRef @@ -12,12 +12,13 @@ from flask import Blueprint, current_app, jsonify, make_response, redirect, requ from flask_restx import fields, inputs, Namespace, Resource from werkzeug.utils import secure_filename +from application.agents.tools.tool_manager import ToolManager + from application.api.user.tasks import ingest, ingest_remote from application.core.mongo_db import MongoDB from application.core.settings import settings from application.extensions import api -from application.tools.tool_manager import ToolManager from application.tts.google_tts import GoogleTTS from application.utils import check_required_fields, validate_function_name from application.vectorstore.vector_creator import VectorCreator @@ -429,22 +430,21 @@ class UploadRemote(Resource): return missing_fields try: - config = json.loads(data["data"]) - source_data = None + config = json.loads(data["data"]) + source_data = None - if data["source"] == "github": + if data["source"] == "github": source_data = config.get("repo_url") - elif data["source"] in ["crawler", "url"]: + elif data["source"] in ["crawler", "url"]: source_data = config.get("url") - elif data["source"] == "reddit": - source_data = config + elif data["source"] == "reddit": + source_data = config - - task = ingest_remote.delay( + task = ingest_remote.delay( source_data=source_data, job_name=data["name"], user=data["user"], - loader=data["source"] + loader=data["source"], ) except Exception as err: current_app.logger.error(f"Error uploading remote source: {err}") @@ -1936,11 +1936,14 @@ class UpdateTool(Resource): for action_name in list(data["config"]["actions"].keys()): if not validate_function_name(action_name): return make_response( - jsonify({ - "success": False, - "message": f"Invalid function name '{action_name}'. Function names must match pattern '^[a-zA-Z0-9_-]+$'.", - "param": "tools[].function.name" - }), 400 + jsonify( + { + "success": False, + "message": f"Invalid function name '{action_name}'. Function names must match pattern '^[a-zA-Z0-9_-]+$'.", + "param": "tools[].function.name", + } + ), + 400, ) update_data["config"] = data["config"] if "status" in data: diff --git a/application/retriever/classic_rag.py b/application/retriever/classic_rag.py index 8a1406f4..5c74878c 100644 --- a/application/retriever/classic_rag.py +++ b/application/retriever/classic_rag.py @@ -51,10 +51,12 @@ class ClassicRAG(BaseRetriever): Rephrase the following user question to be a standalone search query that captures all relevant context from the conversation: - {self.original_question} """ - messages = [{"role": "system", "content": prompt}] + messages = [ + {"role": "system", "content": prompt}, + {"role": "user", "content": self.original_question}, + ] try: rephrased_query = self.llm.gen(model=self.gpt_model, messages=messages)