# -*- coding: utf-8 -*- from odoo import api, fields, models from ..utils.openai_client import PROVIDER_PRESETS PROVIDER_SELECTION = [(key, preset['label']) for key, preset in PROVIDER_PRESETS.items()] class ResConfigSettings(models.TransientModel): _inherit = 'res.config.settings' dn_ai_provider = fields.Selection( PROVIDER_SELECTION, string="AI Provider", default='deepseek', config_parameter='dn_ai.provider', ) dn_ai_base_url = fields.Char( string="API Base URL", config_parameter='dn_ai.base_url', help="Leave empty to use the default endpoint of the selected provider.", ) dn_ai_api_key = fields.Char( string="API Key", config_parameter='dn_ai.api_key', help="Stored in a system parameter; never shown to non-admin users.", ) dn_ai_model = fields.Char( string="Model", config_parameter='dn_ai.model', help="e.g. deepseek-chat, qwen-plus, gpt-4o-mini.", ) dn_ai_temperature = fields.Float( string="Temperature", config_parameter='dn_ai.temperature', help="Leave empty for the provider default.", ) dn_ai_max_tokens = fields.Integer( string="Max Tokens", config_parameter='dn_ai.max_tokens', ) dn_ai_embedding_model = fields.Char( string="Embedding Model", config_parameter='dn_ai.embedding_model', help="Required for knowledge sources (RAG).", ) dn_ai_max_rounds = fields.Integer( string="Max Tool Rounds", config_parameter='dn_ai.max_rounds', default=20, ) dn_ai_max_tools_per_call = fields.Integer( string="Max Tools per Round", config_parameter='dn_ai.max_tools_per_call', default=20, ) dn_ai_timeout = fields.Integer( string="Request Timeout (s)", config_parameter='dn_ai.timeout', default=120, ) dn_ai_vision = fields.Selection( [('true', "Yes"), ('false', "No")], string="Send Images to the Model", config_parameter='dn_ai.vision', help="When enabled, uploaded images and PDFs are sent to the provider " "as multimodal content (or uploaded through its Files API). Turn " "it off for text-only endpoints: PDFs are then read locally and " "sent as text instead. Leave empty to follow the provider default.", ) dn_ai_attachment_max_chars = fields.Integer( string="Attachment Text Limit", config_parameter='dn_ai.attachment_max_chars', default=40000, help="Maximum number of characters extracted from one uploaded file. " "Longer documents are truncated.", ) @api.model def default_get(self, fields_list): values = super().default_get(fields_list) # ``ir.config_parameter`` may hold a value that is not one of the keys # below -- an empty string written by an older build or by hand is the # common case. ``Selection.convert_to_cache`` raises on such a value and # the whole settings page dies with "Wrong value for # res.config.settings.dn_ai_vision". Fall back to "unset" instead, which # makes ``ai.provider.get_vision`` follow the provider preset. # # Note the membership test: ``False`` is the legitimate "unset" value and # an empty string is *not* falsy-insignificant here -- it is exactly the # value that used to crash the form. if 'dn_ai_vision' in values: allowed = self._fields['dn_ai_vision'].get_values(self.env) if values['dn_ai_vision'] not in allowed: values['dn_ai_vision'] = False return values @api.onchange('dn_ai_provider') def _onchange_dn_ai_provider(self): preset = PROVIDER_PRESETS.get(self.dn_ai_provider) if preset: self.dn_ai_base_url = preset['base_url'] self.dn_ai_model = preset['model'] self.dn_ai_embedding_model = preset['embedding_model'] # Keep the multimodal switch in sync with the chosen provider: # sending image blocks to a text-only endpoint fails every request. self.dn_ai_vision = 'true' if preset.get('vision') else 'false' def action_dn_ai_test_connection(self): return self.env['dn.ai.provider'].action_test_connection()