diff --git a/openwebui_integration/models/openwebui_bot.py b/openwebui_integration/models/openwebui_bot.py index ba16f10..855a337 100644 --- a/openwebui_integration/models/openwebui_bot.py +++ b/openwebui_integration/models/openwebui_bot.py @@ -304,9 +304,30 @@ class OpenWebUIBot(models.Model): self.model_id.identifier, bool(self.context), bool(self.instructions) ) + # Get conversation history from the channel + domain = [ + ('model', '=', 'discuss.channel'), + ('res_id', '=', channel.id), + ('message_type', '=', 'comment'), + ('id', '<=', message.id) # Only get messages up to current message + ] + + # Limit to last 10 messages for context + history_messages = self.env['mail.message'].search(domain, limit=10, order='id desc') + + # Build message history in the format expected by the API + message_history = [] + for msg in reversed(history_messages[:-1]): # Exclude current message + role = 'assistant' if msg.author_id == self.partner_id else 'user' + message_history.append({ + 'role': role, + 'content': msg.body + }) + # Process message with OpenWebUI response = self.model_id.send_message( message=message.body, + message_history=message_history, context=self.context, instructions=self.instructions ) diff --git a/openwebui_integration/models/openwebui_model.py b/openwebui_integration/models/openwebui_model.py index d18539d..40d3869 100644 --- a/openwebui_integration/models/openwebui_model.py +++ b/openwebui_integration/models/openwebui_model.py @@ -219,13 +219,32 @@ class OpenWebUIModel(models.Model): _logger.error(f"Error synchronizing models: {str(e)}") return False, str(e) - def send_message(self, message, context=None, instructions=None): - """Sends a message to the model and returns its response""" + def send_message(self, message, message_history=None, context=None, instructions=None): + """Sends a message to the model and returns its response + + Args: + message (str): The message to send + message_history (list): Optional list of previous messages in the format + [{'role': 'user'|'assistant', 'content': 'message'}, ...] + context (dict): Optional context to pass to the model + instructions (str): Optional system instructions + + Returns: + str: The model's response or error message + """ self.ensure_one() + # Initialize messages list with history if provided + messages = [] + if message_history: + messages.extend(message_history) + + # Add the current message + messages.append({'role': 'user', 'content': message}) + data = { 'model': self.identifier, - 'messages': [{'role': 'user', 'content': message}], + 'messages': messages, } if context: