Files
Enofirm-Temp/extra-addons/AI/llm/models/llm_model.py
T
2026-09-18 13:55:25 +07:00

89 lines
2.9 KiB
Python

from odoo import api, fields, models
class LLMModel(models.Model):
_name = "llm.model"
_description = "LLM Model"
_inherit = ["mail.thread"]
name = fields.Char(required=True)
provider_id = fields.Many2one("llm.provider", required=True, ondelete="cascade")
publisher_id = fields.Many2one(
"llm.publisher",
string="Publisher",
ondelete="restrict",
tracking=True,
help="The organization or entity that published this model",
)
model_use = fields.Selection(
selection="_get_available_model_usages",
required=True,
default="chat",
)
default = fields.Boolean(default=False)
active = fields.Boolean(default=True)
# Model details
details = fields.Json()
model_info = fields.Json()
parameters = fields.Text()
template = fields.Text()
@api.model
def _get_available_model_usages(self):
return [
("embedding", "Embedding"),
("completion", "Completion"),
("chat", "Chat"),
("multimodal", "Multimodal"),
("generation", "Generic binary generation"),
("image_generation", "Image Generation"),
]
@api.model_create_multi
def create(self, vals_list):
records = super().create(vals_list)
for record in records:
if record.default:
# Ensure only one default per provider/use combo
self.search(
[
("provider_id", "=", record.provider_id.id),
("model_use", "=", record.model_use),
("default", "=", True),
("id", "!=", record.id),
]
).write({"default": False})
return records
def chat(self, messages, stream=False, **kwargs):
"""Send chat messages using this model"""
return self.provider_id.chat(messages, model=self, stream=stream, **kwargs)
def embedding(self, texts):
"""Generate embeddings using this model"""
return self.provider_id.embedding(texts, model=self)
def generate(self, input_data, stream=False, **kwargs):
"""Generate content using this model
Args:
input_data: Input data for generation (could be text, prompt, or structured data)
stream: Whether to stream the response
**kwargs: Additional provider-specific parameters
Returns:
Generated content from the provider
"""
return self.provider_id.generate(
input_data, model=self, stream=stream, **kwargs
)
def action_open_fetch_this_model_wizard(self):
self.ensure_one()
# Call the provider's action_fetch_models with context for specific model
return self.provider_id.with_context(
default_model_to_fetch=self.name
).action_fetch_models()