Odoo LLM Integration

repository·18.0·Indexed 42 days ago

https://github.com/apexive/odoo-llm

A comprehensive framework for embedding Large Language Models (LLMs) into Odoo. It provides a unified API for multiple AI providers (OpenAI, Anthropic, Ollama, Mistral), a tool-calling framework for automation, and support for Retrieval-Augmented Generation (RAG) and external MCP clients. Key features include AI-powered invoice data extraction, specialized AI assistants for chat and analysis, and a flexible prompt template system.

Tokens
80.8K
Snippets
181
Records
387
Agent score
45%

What's inside Odoo LLM

  1. Core Features and Enterprise Capabilities

    18.0

    The Odoo LLM integration provides several enterprise-grade features for AI-driven business processes:

    • AI-driven Automation: Sophisticated tool execution for repetitive tasks.
    • Smart Querying: Direct access to Odoo data via natural language.
    • Role-Based Security: Granular access control for AI features and user-scoped API keys.
    • Tool Consent System: Requires user approval for sensitive operations.
    • Audit Trail: Complete tracking of all AI interactions and tool usage.
    • Reliability: Uses PostgreSQL Advisory Locking to prevent race conditions and conflicts during concurrent generation tasks.
    • Performance: Optimized database schema (e.g., using the llm_role field) provides up to 10x faster message queries.
  2. How fine-tuned models are used after training

    18.0

    Once an LLM fine-tuning job is successfully completed, the resulting model is automatically added to your available models list. It can then be used immediately in:

    • LLM Threads
    • LLM Agents
    • Any other component within the system that utilizes LLM models.
  3. Manage Tool Security and Consent

    18.0

    You can control how tools are executed to ensure security. Use the following hints in your tool definition:

    • destructive_hint = True: Indicates the tool may modify or delete data.
    • read_only_hint = True: Indicates the tool only reads data.

    To configure user approval requirements, navigate to: LLM → Configuration → Tool Consent Configs in the Odoo interface.

    requires_user_consent = True   # User must approve before execution
    read_only_hint = True          # Tool only reads, doesn't modify
    destructive_hint = True        # Tool may modify/delete data
  4. Understand the MCP Session State Machine

    18.0

    The server uses a state machine to manage the lifecycle of a connection. Understanding these states is critical for debugging connection issues:

    • not_initialized: The starting state. Only initialize and ping methods are valid.
    • initializing: Entered after the initialize call. This state allows concurrent requests to handle parallel discovery requests (common in clients like Claude Desktop).
    • initialized: The steady state. All JSON-RPC methods are available.
    • Termination: A session is ended via a DELETE /mcp request.
  5. How llm_knowledge_automation works

    18.0

    The module acts as an automated sync engine between Odoo data and your RAG (Retrieval-Augmented Generation) pipeline. It uses Odoo's base_automation to monitor data changes.

    The Lifecycle Management:

    • Auto-Create: When a new record matches a domain filter, a llm.resource is created and added to the collection.
    • Auto-Update: When a record is updated, the RAG resource is synced. If the record no longer matches the domain filter, it is removed from the collection.
    • Auto-Remove: When a record is deleted, its corresponding RAG resource is cleaned up.
    • Auto-Process: The system automatically triggers the RAG pipeline (Parse → Chunk → Embed) for new or updated resources.

    Behind the scenes: For every domain filter, the module automatically generates three automation rules: on_create, on_write, and on_unlink.

  6. Understand the llm.generation.job model

    18.0

    The llm.generation.job model is the primary entity for managing individual AI generation tasks. It handles the complete lifecycle of a job.

    Key Responsibilities:

    • Relationships: Links a task to a specific thread, provider, model, and set of messages.
    • Status Management: Tracks state transitions (e.g., draft, queued, running, completed, failed, cancelled).
    • Timing: Records queue_time, processing_time, and completion_time.
    • Error Handling: Provides comprehensive error tracking and reporting for failed tasks.
  7. Understand the MCP Server Architecture

    18.0

    The MCP (Model Context Protocol) server acts as a stateful JSON-RPC 2.0 bridge between MCP clients (like Claude Desktop) and Odoo's LLM tool system. It manages the lifecycle of a connection through a session-based model.

    Core Components

    • MCPController: The HTTP endpoint handler located at /mcp.
    • MCPSession: Manages the state and lifecycle of individual client sessions.
    • MCPServerConfig: Handles server-level configuration.
    • Bearer Auth: A decorator used to enforce API key authentication for sensitive operations.
  8. Ollama Provider Features and Technical Capabilities

    18.0

    The llm_ollama module acts as a provider within the llm core framework. It provides:

    • Model Support: Connects to various open-source models including Llama, Mistral, Vicuna, CodeLlama, and Phi.
    • Automatic Discovery: Uses the Fetch Models mechanism to sync available Ollama models into Odoo.
    • Advanced AI Features: Supports text generation, function calling, and streaming responses.
    • Privacy: Ensures data stays local on your hardware, making it suitable for offline use.
  9. Understand the llm_mistral architecture

    18.0

    The llm_mistral module acts as a Provider within the Odoo LLM ecosystem. It sits between the core llm base module and various consumer-facing LLM modules.

    It provides Mistral AI capabilities (Chat completion, Streaming, and Embeddings) to modules such as:

    • llm_assistant: For AI chat interfaces.
    • llm_thread: For thread-based interactions.
    • llm_knowledge: For knowledge management.
    • llm_generate: For content generation tasks.
  10. How the llm_mcp_server architecture works

    18.0

    The llm_mcp_server acts as a bridge between external AI clients (like Claude Desktop or Cursor) and Odoo's internal tool registry.

    Core Concepts:

    • Protocol: Implements the Model Context Protocol (MCP) 2025-06-18 spec via JSON-RPC 2.0.
    • Transport: Uses streamable-http (HTTP with streaming responses).
    • Endpoint: The server listens on /mcp (POST requests).
    • Authentication: Uses Bearer tokens via Odoo API keys.
    • Tool Discovery: Tools are automatically discovered from the llm.tool registry. If a tool is active in Odoo and the user has permission, it is exposed to the AI client.
    • Security: Every request is user-scoped. The server validates the Bearer token and loads the corresponding Odoo user session, ensuring all Odoo ACLs and permissions are strictly enforced.
  11. Understand the LLM Provider Framework

    18.0

    The framework uses a provider abstraction to unify multiple AI services. Providers implement a dynamic method dispatch system to handle different AI tasks through a consistent interface.

    Key dispatch methods include:

    • chat: For conversational interactions.
    • embedding: For generating vector embeddings.
    • generate: For text or image generation.
    # Dynamic method dispatch to service implementations
    provider._dispatch('chat', messages=messages, model=model)
    provider._dispatch('embedding', text=text)
    provider._dispatch('generate', prompt=prompt, type='image')
  12. Understand the Letta Agent Lifecycle

    18.0

    The llm_letta module manages the lifecycle of Letta agents automatically through Odoo threads.

    Creation

    When a thread is created or the provider is switched to Letta, the module:

    1. Generates a per-thread API key with rpc scope.
    2. Stores the API key and its ID in thread.metadata (api_key and api_key_id).
    3. Creates the Letta agent with default memory blocks (persona and human).
    4. Stores the Letta agent ID in thread.external_id.
    5. Synchronizes attached tools to the agent.

    Updates

    Changing the model_id or assistant on a thread triggers an update to the existing Letta agent via the client.agents.update method.

    Cleanup

    When a thread is deleted (unlink()) or the provider is changed, the module:

    1. Deletes the associated res.users.apikeys record.
    2. Deletes the agent from the Letta server.
    3. Clears thread.metadata and thread.external_id.