VPCSinfo's picture
[ADD]ADDED multiple llm porvider options with token and trial argument from fronend. added semanctic search with odoo docs as a context before going to code.
68114ba
raw
history blame
3.15 kB
from smolagents.tools import Tool
import requests
from typing import List, Dict
from bs4 import BeautifulSoup
from transformers import pipeline # Requires transformers
import tempfile
from tools.odoo_documentation_search import OdooDocumentationSearchTool
import yaml
from tools.final_answer import FinalAnswerTool
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):
# Load the summarization pipeline
self.summarizer = pipeline("summarization", model="google/flan-t5-small")
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/" # Specific to Odoo 16
try:
# Use the odoo_documentation_search tool to get relevant documentation snippets
odoo_docs = OdooDocumentationSearchTool().forward(query=query, version="16.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]
max_length = int(len(truncated_prompt) / 2)
code = self.summarizer(truncated_prompt, max_length=max_length, min_length=30, do_sample=False)[0]['summary_text']
return FinalAnswerTool(answer=f"```py\n{code}\n```", include_download=True)