Skip to content
Closed
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
143 changes: 99 additions & 44 deletions ai_connection/models/ai_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,56 +51,111 @@ def _run(
)

def _run_ai(self, messages, tools=None, record=None, max_iterations=None):
client = getattr(self, f"_get_client_{self.kind}")(tools)
# Shallow copying messages to avoid edition of the messages
messages = list(messages)
self.ensure_one()
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"])
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"]
return self._run_ai_step(
messages=list(messages),
tools=tools,
record=record,
max_iterations=max_iterations,
iteration=0,
prompt_tokens=0,
completion_tokens=0,
)

def _run_ai_step(
self,
messages,
tools,
record,
max_iterations,
iteration,
prompt_tokens,
completion_tokens,
):
self.ensure_one()
if iteration >= max_iterations:
return self._run_ai_error(
self.env._(
"Iterations reached the maximum allowed (%s)", max_iterations
)
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,
)
iteration += 1

client = getattr(self, f"_get_client_{self.kind}")(tools)
response = client.handle_message(
messages=messages, temperature=self.temperature
)
messages = [*messages, response["message"]]
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 self._run_ai_finalize(
messages, 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)
)
raise UserError(
self.env._("Iterations reached the maximum allowed (%s)", max_iterations)
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,
)

return self._run_ai_next(
messages=messages,
tools=tools,
record=record,
max_iterations=max_iterations,
iteration=iteration,
prompt_tokens=prompt_tokens,
completion_tokens=completion_tokens,
)

def _run_ai_next(
self,
messages,
tools,
record,
max_iterations,
iteration,
prompt_tokens,
completion_tokens,
):
return self._run_ai_step(
messages=messages,
tools=tools,
record=record,
max_iterations=max_iterations,
iteration=iteration,
prompt_tokens=prompt_tokens,
completion_tokens=completion_tokens,
)

def _run_ai_finalize(self, messages, prompt_tokens, completion_tokens, iteration):
return (messages[-1]["content"], prompt_tokens, completion_tokens, iteration)

def _run_ai_error(self, message):
raise UserError(message)

def _process_tool_call(self, tool, tool_call, record):
tool_response = tool._execute_tool(**tool_call["arguments"], record=record)
return getattr(
Expand Down
103 changes: 103 additions & 0 deletions ai_connection_queue/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
.. image:: https://odoo-community.org/readme-banner-image
:target: https://odoo-community.org/get-involved?utm_source=readme
:alt: Odoo Community Association

===================
Ai Connection Queue
===================

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

.. |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/license-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_queue
: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_queue
: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 module runs ``ai.connection`` conversations through ``queue_job``
instead of blocking the calling worker until the AI provider produces a
final answer.

It does not change ``ai.connection``'s synchronous behaviour at all - it
only adds an opt-in, context-driven way to run a conversation
asynchronously and be notified of its progress.

**Table of contents**

.. contents::
:local:

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

This module runs ``ai.connection`` conversations through ``queue_job``,
one job per round, instead of blocking the calling worker until a final
answer comes back.

It is enabled per call through context, without changing
``ai.connection``'s own methods, and it can also notify a caller-chosen
record or model after every round, not only when the conversation
finishes.

Check ``ai.connection.run`` to follow the state of an asynchronous
conversation.

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_queue%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
-------

* SDi

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

- `SDi <https://sdi.es>`__

- Angel Moya

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_queue>`_ project on GitHub.

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

{
"name": "Ai Connection Queue",
"summary": """Run ai.connection conversations through queue_job""",
"version": "18.0.1.0.0",
"license": "AGPL-3",
"author": "SDi,Odoo Community Association (OCA)",
"website": "https://github.com/OCA/ai",
"depends": [
"ai_connection",
"queue_job",
],
"data": [
"security/ir.model.access.csv",
],
"demo": [],
}
2 changes: 2 additions & 0 deletions ai_connection_queue/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import ai_connection_run
from . import ai_connection
Loading
Loading