129 lines
4.5 KiB
Python
129 lines
4.5 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""Read / write the LLM provider configuration.
|
|
|
|
All settings live in ``ir.config_parameter`` so they can be changed without a
|
|
module upgrade, and so other modules can override them per company later.
|
|
"""
|
|
import logging
|
|
|
|
from odoo import api, models
|
|
|
|
from ..utils.openai_client import PROVIDER_PRESETS
|
|
|
|
_logger = logging.getLogger(__name__)
|
|
|
|
PARAM_PROVIDER = 'dn_ai.provider'
|
|
PARAM_BASE_URL = 'dn_ai.base_url'
|
|
PARAM_API_KEY = 'dn_ai.api_key'
|
|
PARAM_MODEL = 'dn_ai.model'
|
|
PARAM_TEMPERATURE = 'dn_ai.temperature'
|
|
PARAM_MAX_TOKENS = 'dn_ai.max_tokens'
|
|
PARAM_EMB_BASE_URL = 'dn_ai.embedding_base_url'
|
|
PARAM_EMB_API_KEY = 'dn_ai.embedding_api_key'
|
|
PARAM_EMB_MODEL = 'dn_ai.embedding_model'
|
|
PARAM_MAX_ROUNDS = 'dn_ai.max_rounds'
|
|
PARAM_MAX_TOOLS_PER_CALL = 'dn_ai.max_tools_per_call'
|
|
PARAM_TIMEOUT = 'dn_ai.timeout'
|
|
PARAM_VISION = 'dn_ai.vision'
|
|
|
|
DEFAULT_MAX_ROUNDS = 20
|
|
DEFAULT_MAX_TOOLS_PER_CALL = 20
|
|
DEFAULT_TIMEOUT = 120
|
|
|
|
|
|
class DNaiProvider(models.AbstractModel):
|
|
"""Stateless helper exposing the resolved provider configuration."""
|
|
_name = 'dn.ai.provider'
|
|
_description = 'DN AI Provider Configuration'
|
|
|
|
@api.model
|
|
def _params(self):
|
|
return self.env['ir.config_parameter'].sudo()
|
|
|
|
@api.model
|
|
def get_provider(self):
|
|
return self._params().get_str(PARAM_PROVIDER) or 'deepseek'
|
|
|
|
@api.model
|
|
def get_preset(self, provider=None):
|
|
provider = provider or self.get_provider()
|
|
return PROVIDER_PRESETS.get(provider) or PROVIDER_PRESETS['custom']
|
|
|
|
@api.model
|
|
def get_config(self):
|
|
"""Resolved chat configuration for the current company."""
|
|
params = self._params()
|
|
preset = self.get_preset()
|
|
base_url = params.get_str(PARAM_BASE_URL) or preset['base_url']
|
|
model = params.get_str(PARAM_MODEL) or preset['model']
|
|
# `res.config.settings` stores 0 for a cleared float/integer field, so
|
|
# treat 0 as "not set" rather than "temperature 0".
|
|
temperature = params.get_float(PARAM_TEMPERATURE, 0.0) or None
|
|
max_tokens = params.get_int(PARAM_MAX_TOKENS, 0) or None
|
|
timeout = params.get_int(PARAM_TIMEOUT, 0) or DEFAULT_TIMEOUT
|
|
return {
|
|
'provider': self.get_provider(),
|
|
'base_url': base_url,
|
|
'api_key': params.get_str(PARAM_API_KEY),
|
|
'model': model,
|
|
'temperature': temperature,
|
|
'max_tokens': max_tokens,
|
|
'timeout': timeout,
|
|
'vision': self.get_vision(),
|
|
'file_api': bool(preset.get('file_api')),
|
|
}
|
|
|
|
@api.model
|
|
def get_vision(self):
|
|
"""Whether uploaded images/PDFs may be sent as multimodal content.
|
|
|
|
Tri-state on purpose: an unset parameter falls back to the provider
|
|
preset, so switching to a text-only provider (DeepSeek, a custom
|
|
endpoint) stops sending image blocks that would be rejected with an
|
|
HTTP 400 — while still letting the admin override it per installation.
|
|
"""
|
|
raw = self._params().get_str(PARAM_VISION)
|
|
if raw in ('true', 'false'):
|
|
return raw == 'true'
|
|
return bool(self.get_preset().get('vision'))
|
|
|
|
@api.model
|
|
def get_embedding_config(self):
|
|
params = self._params()
|
|
preset = self.get_preset()
|
|
chat = self.get_config()
|
|
return {
|
|
'base_url': params.get_str(PARAM_EMB_BASE_URL) or chat['base_url'],
|
|
'api_key': params.get_str(PARAM_EMB_API_KEY) or chat['api_key'],
|
|
'embedding_model': params.get_str(PARAM_EMB_MODEL) or preset['embedding_model'],
|
|
}
|
|
|
|
@api.model
|
|
def is_configured(self):
|
|
cfg = self.get_config()
|
|
return bool(cfg['base_url'] and cfg['model'] and cfg['api_key'])
|
|
|
|
@api.model
|
|
def get_max_rounds(self):
|
|
return self._params().get_int(PARAM_MAX_ROUNDS) or DEFAULT_MAX_ROUNDS
|
|
|
|
@api.model
|
|
def get_max_tools_per_call(self):
|
|
return self._params().get_int(PARAM_MAX_TOOLS_PER_CALL) or DEFAULT_MAX_TOOLS_PER_CALL
|
|
|
|
@api.model
|
|
def action_test_connection(self):
|
|
"""Called from the settings page; returns a notification action."""
|
|
from ..utils.openai_client import probe
|
|
ok, message = probe(self.get_config())
|
|
return {
|
|
'type': 'ir.actions.client',
|
|
'tag': 'display_notification',
|
|
'params': {
|
|
'title': self.env._("Connection successful") if ok else self.env._("Connection failed"),
|
|
'message': message,
|
|
'type': 'success' if ok else 'danger',
|
|
'sticky': not ok,
|
|
},
|
|
}
|