- Initial agent development, starting by PDF

fix:
- Fixed test discovery on subdirectories

test:
- Started tests for PDF agent
This commit is contained in:
2025-03-19 21:57:08 -03:00
parent 5238d73942
commit a7b292f437
12 changed files with 110 additions and 16 deletions

View File

@@ -7,5 +7,8 @@
"test_*.py" "test_*.py"
], ],
"python.testing.pytestEnabled": false, "python.testing.pytestEnabled": false,
"python.testing.unittestEnabled": true "python.testing.unittestEnabled": true,
"python.analysis.extraPaths": [
"./src/service"
]
} }

0
__init__.py Normal file
View File

View File

@@ -1 +0,0 @@
# Este arquivo transforma o diretório em um pacote Python

View File

@@ -1,7 +1,46 @@
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from json import tool
from typing import List, Dict, Any from typing import List, Dict, Any
from langchain.tools import BaseTool
from sqlalchemy import desc
class Agent(ABC): class Agent(ABC):
name: str
description: str
tools: List[BaseTool]
@abstractmethod
def list_tools(self) -> List[BaseTool]:
"""
List all available tools.
Returns:
List[BaseTool]: A list of tools available for the agent.
"""
return self.tools
@abstractmethod
def add_tool(self, tool: BaseTool) -> None:
"""
Adds a new tool to the agent's toolset.
Args:
tool (BaseTool): LangChain tool to be added
"""
pass
@abstractmethod
def remove_tool(self, tool_name: str) -> None:
"""
Removes a tool from the agent's toolset.
Args:
tool_name (str): Name of the tool to be removed
"""
pass
@abstractmethod @abstractmethod
def get_agent_tools(self) -> List[Dict[str, Any]]: def get_agent_tools(self) -> List[Dict[str, Any]]:
""" """
@@ -21,3 +60,17 @@ class Agent(ABC):
Dict[str, Any]: A dictionary containing agent details. Dict[str, Any]: A dictionary containing agent details.
""" """
pass pass
@abstractmethod
def create_agent(self, agent_name: str, agent_description: str) -> Dict[str, Any]:
"""
Create a new agent.
Args:
agent_name (str): The name of the agent.
agent_description (str): The description of the agent.
Returns:
Dict[str, Any]: A dictionary containing the created agent's details.
"""
pass

View File

@@ -1,9 +1,20 @@
from service.ollama.ollamaModelManager import OllamaModelManager
# Utilizando nossa classe para obter os modelos # Utilizando nossa classe para obter os modelos
ollama_manager = OllamaModelManager() import sys
models = ollama_manager.get_available_models() import os
# Adiciona o diretório raiz ao path
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
from src.service.ollama.ollamaModelManager import OllamaModelManager
# ollama_manager = OllamaModelManager()
# models = ollama_manager.get_available_models()
# print(models) # print(models)
details = ollama_manager.get_model_details("qwen2.5:1.5b")
print(details)
# details = ollama_manager.get_model_details("qwen2.5:1.5b")
# print(details)

View File

@@ -8,6 +8,10 @@ from src.interfaces.agents.agent import Agent
class PDFAgent(Agent): class PDFAgent(Agent):
# Class properties
name = "PDF Agent"
description = "Agent to handle PDF operations"
tools = []
def __init__(self): def __init__(self):
""" """
@@ -18,10 +22,10 @@ class PDFAgent(Agent):
Attributes: Attributes:
agent: The agent implementation for PDF operations. agent: The agent implementation for PDF operations.
""" """
self.agent = None
self.tools = []
self.agent
def get_agent_details(self): def get_agent_details(self):
return super().get_agent_details() return super().get_agent_details()

View File

@@ -1,5 +1,5 @@
import fitz import fitz
from langchain_community.tools import BaseTool from langchain.tools import BaseTool
class PDFMetadataTool(BaseTool): class PDFMetadataTool(BaseTool):
name="pdf_metadata" name="pdf_metadata"
@@ -11,5 +11,8 @@ class PDFMetadataTool(BaseTool):
metadata = { metadata = {
"páginas": len(doc) "páginas": len(doc)
} }
except Exception as e: metadata.update(doc.metadata)
return f"Error: {e}" return metadata
except FileNotFoundError:
return f"Error: File {path} not found."

0
src/service/__init__.py Normal file
View File

View File

0
test/agent/__init__.py Normal file
View File

View File

@@ -0,0 +1,20 @@
from math import e
import unittest
from src.interfaces.agents.agent import Agent
from src.modules.pdf.pdfAgent import PDFAgent
class TestPDFAgent(unittest.TestCase):
def setUp(self):
self.agent: Agent
return super().setUp()
def createAgent(self):
self.agent = PDFAgent()
self.agent.createAgent()
self.assertIsNone(self.agent)
self.assertEqual(self.agent.name, "PDF Agent")
self.assertEqual(self.agent.description, "Agent to handle PDF operations")
self.assertIsInstance(self.agent.tools, list)

View File

@@ -3,13 +3,14 @@ from unittest.mock import patch, MagicMock
from src.service.ollama.ollamaModelManager import OllamaModelManager from src.service.ollama.ollamaModelManager import OllamaModelManager
class TestOllamaModelManager(unittest.TestCase): class TestOllamaModelManager(unittest.TestCase):
def setUp(self): def setUp(self):
self.model_manager = OllamaModelManager() self.model_manager = OllamaModelManager()
self.model_manager.base_url = "http://test-url:11434" self.model_manager.base_url = "http://test-url:11434"
@patch('service.ollama.ollamaModelManager.requests.get') @patch('src.service.ollama.ollamaModelManager.requests.get')
def test_get_available_models_success(self, mock_get): def test_get_available_models_success(self, mock_get):
# Setup mock response # Setup mock response
mock_response = MagicMock() mock_response = MagicMock()
@@ -29,7 +30,7 @@ class TestOllamaModelManager(unittest.TestCase):
self.assertEqual(len(result), 2) self.assertEqual(len(result), 2)
self.assertEqual(result, [{'name': 'model1'}, {'name': 'model2'}]) self.assertEqual(result, [{'name': 'model1'}, {'name': 'model2'}])
@patch('service.ollama.ollamaModelManager.requests.get') @patch('src.service.ollama.ollamaModelManager.requests.get')
def test_get_available_models_exception(self, mock_get): def test_get_available_models_exception(self, mock_get):
# Setup mock to raise exception # Setup mock to raise exception
mock_get.side_effect = Exception("Connection error") mock_get.side_effect = Exception("Connection error")
@@ -40,7 +41,7 @@ class TestOllamaModelManager(unittest.TestCase):
# Assertions # Assertions
self.assertEqual(result, []) self.assertEqual(result, [])
@patch('service.ollama.ollamaModelManager.requests.post') @patch('src.service.ollama.ollamaModelManager.requests.post')
def test_get_model_details_success(self, mock_post): def test_get_model_details_success(self, mock_post):
# Setup mock response # Setup mock response
mock_response = MagicMock() mock_response = MagicMock()
@@ -63,7 +64,7 @@ class TestOllamaModelManager(unittest.TestCase):
self.assertEqual(result['context_size'], 4096) self.assertEqual(result['context_size'], 4096)
self.assertEqual(result['license'], 'Apache 2.0') self.assertEqual(result['license'], 'Apache 2.0')
@patch('service.ollama.ollamaModelManager.requests.post') @patch('src.service.ollama.ollamaModelManager.requests.post')
def test_get_model_details_exception(self, mock_post): def test_get_model_details_exception(self, mock_post):
# Setup mock to raise exception # Setup mock to raise exception
mock_post.side_effect = Exception("API error") mock_post.side_effect = Exception("API error")