from smolagents.tools import Tool import requests from typing import List, Dict from bs4 import BeautifulSoup import tempfile from tools.odoo_documentation_search import OdooDocumentationSearchTool import yaml from tools.final_answer import FinalAnswerTool from tools.summarizer_service import SummarizerService class OdooCodeAgent18(Tool): name = "odoo_code_agent_18" description = "Generates Odoo code for version 18 based on the user's query and Odoo documentation, using prompt templates." def __init__(self, system_prompt: str): self.summarizer_service = SummarizerService() self.is_initialized = True self.system_prompt = system_prompt inputs = { "query": {"type": "string", "description": "The search query (e.g., 'create a new module')"} } output_type = "array" def forward(self, query: str) -> List[Dict]: """ Generates Odoo code for version 18 based on the user's query and Odoo documentation. """ base_url = "https://www.odoo.com/documentation/18.0/" # Specific to Odoo 18 try: # Use the odoo_documentation_search tool to get relevant documentation snippets odoo_docs = OdooDocumentationSearchTool().forward(query=query, version="18.0") # Generate Odoo code based on the search results (Placeholder - Replace with actual implementation) generated_code = self.generate_code(query, odoo_docs) return [{"Code": generated_code}] except requests.exceptions.RequestException as e: return [{"Error": f"Error fetching Odoo documentation: {str(e)}"}] def perform_search(self, query: str, documentation: str) -> str: """ Performs semantic, keyword, and reranking search on the documentation. (Placeholder - Replace with actual implementation) """ # Placeholder implementation - Replace with actual search logic # This could involve using libraries like Sentence Transformers for semantic search, # keyword extraction techniques, and a reranking algorithm to prioritize relevant results. # For now, just return the first 500 characters of the documentation return documentation[:500] def generate_code(self, query: str, odoo_docs: List[Dict]) -> str: """ Generates Odoo code based on the search results and the user's query. """ # Use the summarization pipeline to generate code based on the prompt, query, and documentation prompt = f"{self.system_prompt}\n\nUser query: {query}\n\nOdoo documentation:\n" for doc in odoo_docs: prompt += f"# {doc['Result']}\n" if not odoo_docs: prompt += "# No documentation found.\n" truncated_prompt = prompt[:500] code = self.summarizer_service.summarize(truncated_prompt) return FinalAnswerTool(answer=f"```py\n{code}\n```", include_download=True)