|
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 OdooCodeAgent16(Tool): |
|
name = "odoo_code_agent_16" |
|
description = "Generates Odoo code for version 16 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 16 based on the user's query and Odoo documentation. |
|
""" |
|
base_url = "https://www.odoo.com/documentation/16.0/" |
|
|
|
try: |
|
|
|
odoo_docs = OdooDocumentationSearchTool().forward(query=query, version="16.0") |
|
|
|
|
|
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) |
|
""" |
|
|
|
|
|
|
|
|
|
|
|
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. |
|
""" |
|
|
|
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) |
|
|