Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions ai_connection/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
=============
Ai Connection
=============

..
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:55879f38672930678ed61631886590084592fb3717aea06b117f3b816e35e826
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
:target: https://odoo-community.org/page/development-status
:alt: Beta
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fai-lightgray.png?logo=github
:target: https://github.com/OCA/ai/tree/18.0/ai_connection
:alt: OCA/ai
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/ai-18-0/ai-18-0-ai_connection
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
:target: https://runboat.odoo-community.org/builds?repo=OCA/ai&target_branch=18.0
:alt: Try me on Runboat

|badge1| |badge2| |badge3| |badge4| |badge5|

This is a module that defines the basic structure of AI Connections.

However, it does not include any extra configurations.

**Table of contents**

.. contents::
:local:

Use Cases / Context
===================

This module allows to create basic configuration of AI connections. It
is left for child modules the specific connection, however, it creates a
basic structure that will be used from it. Check on existent modules to
know how to handle it.

Bug Tracker
===========

Bugs are tracked on `GitHub Issues <https://github.com/OCA/ai/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us to smash it by providing a detailed and welcomed
`feedback <https://github.com/OCA/ai/issues/new?body=module:%20ai_connection%0Aversion:%2018.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

Do not contact contributors directly about support or help with technical issues.

Credits
=======

Authors
-------

* Dixmit

Contributors
------------

- `Dixmit <https://www.dixmit.com>`__

- Enric Tobella

Maintainers
-----------

This module is maintained by the OCA.

.. image:: https://odoo-community.org/logo.png
:alt: Odoo Community Association
:target: https://odoo-community.org

OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.

This module is part of the `OCA/ai <https://github.com/OCA/ai/tree/18.0/ai_connection>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
2 changes: 2 additions & 0 deletions ai_connection/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import models
from .client import AiConnectionClient
19 changes: 19 additions & 0 deletions ai_connection/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright 2026 Dixmit
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

{
"name": "Ai Connection",
"summary": """Creates connections to AI systems""",
"version": "18.0.1.0.0",
"license": "AGPL-3",
"author": "Dixmit,Odoo Community Association (OCA)",
"website": "https://github.com/OCA/ai",
"depends": [
"ai_tool",
],
"data": [
"security/ir.model.access.csv",
"views/ai_connection.xml",
],
"demo": [],
}
19 changes: 19 additions & 0 deletions ai_connection/client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright 2026 Dixmit
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).


class AiConnectionClient:
def handle_message(self, messages=None, **kwargs):
"""Handle a message from the AI system.
Will return a dict with the following information:
{
"message": "",
"tool_calls": [
{
"name": "",
"arguments": {},
}
]
}
"""
raise NotImplementedError("Subclasses must implement this method")
1 change: 1 addition & 0 deletions ai_connection/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import ai_connection
118 changes: 118 additions & 0 deletions ai_connection/models/ai_connection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
# Copyright 2026 Dixmit
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).


import json

from odoo import fields, models
from odoo.exceptions import UserError


class AiConnection(models.Model):
_name = "ai.connection"
_description = "AI Connection"
_max_iterations = 50

name = fields.Char(required=True)
kind = fields.Selection([], required=True)
active = fields.Boolean(default=True)
url = fields.Char(groups="base.group_system")
model = fields.Char(groups="base.group_system")
temperature = fields.Float(default=0.8)

def _run(
self,
prompt=None,
tools=None,
record=None,
system_prompt=None,
messages=None,
max_iterations=None,
attachments=None,
):
if messages is None:
messages = []
if system_prompt:
messages.append({"role": "system", "content": system_prompt})
if prompt or attachments:
message = {"role": "user", "content": prompt or ""}
if attachments:
message["files"] = [
{
"name": attachment.name,
"content": attachment.datas.decode("utf-8"),
"mimetype": attachment.mimetype,
}
for attachment in attachments
]
messages.append(message)
return self._run_ai(
messages=messages, tools=tools, record=record, max_iterations=max_iterations
)

def _run_ai(self, messages, tools=None, record=None, max_iterations=None):
client = getattr(self, f"_get_client_{self.kind}")(tools)
Comment thread
etobella marked this conversation as resolved.
# Shallow copying messages to avoid edition of the messages
messages = list(messages)
if max_iterations is None:
max_iterations = self._max_iterations
iteration = 0
prompt_tokens = 0
completion_tokens = 0
while iteration < max_iterations:
iteration += 1
response = client.handle_message(
messages=messages, temperature=self.temperature
)
messages.append(response["message"])
Comment thread
etobella marked this conversation as resolved.
prompt_tokens += response.get("usage", {}).get("prompt_tokens", 0)
completion_tokens += response.get("usage", {}).get("completion_tokens", 0)
if not response.get("tool_calls"):
return (
response["message"]["content"],
prompt_tokens,
completion_tokens,
iteration,
)
for tool_call in response["tool_calls"]:
tool = tools.filtered(
lambda t, tool_call=tool_call: t.name == tool_call["name"]
)
if tool:
try:
with self.env.cr.savepoint():
messages.append(
self._process_tool_call(tool, tool_call, record)
)
except Exception as e:
getattr(
self,
f"_process_tool_call_result_{self.kind}",
self._process_tool_call_result,
)(
tool,
{
"error": str(e),
"type": type(e).__name__,
},
tool_call,
)
raise UserError(
self.env._("Iterations reached the maximum allowed (%s)", max_iterations)
)

def _process_tool_call(self, tool, tool_call, record):
tool_response = tool._execute_tool(**tool_call["arguments"], record=record)
return getattr(
self,
f"_process_tool_call_result_{self.kind}",
self._process_tool_call_result,
)(tool, tool_response, tool_call)

def _process_tool_call_result(self, tool, tool_response, tool_call):
return {
"role": "tool",
"name": tool.name,
"tool_call_id": tool_call.get("id"),
"content": json.dumps(tool_response),
}
3 changes: 3 additions & 0 deletions ai_connection/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[build-system]
requires = ["whool"]
build-backend = "whool.buildapi"
3 changes: 3 additions & 0 deletions ai_connection/readme/CONTEXT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
This module allows to create basic configuration of AI connections.
It is left for child modules the specific connection, however, it creates a basic structure that will be used from it.
Check on existent modules to know how to handle it.
2 changes: 2 additions & 0 deletions ai_connection/readme/CONTRIBUTORS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- [Dixmit](https://www.dixmit.com)
- Enric Tobella
3 changes: 3 additions & 0 deletions ai_connection/readme/DESCRIPTION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
This is a module that defines the basic structure of AI Connections.

However, it does not include any extra configurations.
3 changes: 3 additions & 0 deletions ai_connection/security/ir.model.access.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
access_ai_connection,access_ai_connection,model_ai_connection,base.group_user,1,0,0,0
manage_ai_connection,manage_ai_connection,model_ai_connection,base.group_system,1,1,1,0
Binary file added ai_connection/static/description/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading