llm_app.py


Overview

The llm_app.py file is a Flask-based REST API module responsible for managing Large Language Model (LLM) factories and tenant-specific LLM configurations within the InfiniFlow platform. It provides endpoints to retrieve available LLM factories, add or delete tenant-specific LLM configurations, set API keys for LLM access, and list the LLMs associated with a tenant.

The core functionality revolves around tenant-specific LLM management, including validating API keys by testing connectivity to various LLM models (embedding, chat, rerank, image-to-text, text-to-speech), handling special authentication schemes for different LLM providers, and querying/updating LLM-related data persisted in the database via service layers.

This module interacts with services that abstract database models for tenants’ LLMs (TenantLLMService), general LLM metadata (LLMService), and LLM factories (LLMFactoriesService). It also integrates with the RAG (Retrieval-Augmented Generation) library to instantiate and test different LLM model types.


Detailed Explanations of Endpoints and Functions

1. factories()

Route: /factories
Method: GET
Authentication: Required (@login_required)

Purpose

Retrieve and return a list of all available LLM factories excluding specific ones (Youdao, FastEmbed, BAAI), along with the types of models supported by each factory.

Functionality

Returns

Example Usage

GET /factories
Authorization: Bearer <token>

2. set_api_key()

Route: /set_api_key
Method: POST
Authentication: Required
Request Validation: Requires JSON fields llm_factory and api_key

Purpose

Set or update the API key for a tenant's LLM factory and validate the key by testing it against supported model types (embedding, chat, rerank).

Parameters

Functionality

Returns

Example Usage

POST /set_api_key
Content-Type: application/json
Authorization: Bearer <token>

{
  "llm_factory": "OpenAI",
  "api_key": "sk-xxxxxx",
  "base_url": "https://api.openai.com"
}

3. add_llm()

Route: /add_llm
Method: POST
Authentication: Required
Request Validation: Requires JSON field llm_factory

Purpose

Add a new tenant-specific LLM configuration, with support for a variety of factories and their special authentication schemes.

Parameters

Functionality

Returns

Example Usage

POST /add_llm
Content-Type: application/json
Authorization: Bearer <token>

{
  "llm_factory": "OpenAI",
  "model_type": "chat",
  "api_key": "sk-xxxx",
  "llm_name": "gpt-4",
  "max_tokens": 2048
}

4. delete_llm()

Route: /delete_llm
Method: POST
Authentication: Required
Request Validation: Requires JSON fields llm_factory and llm_name

Purpose

Delete a tenant-specific LLM configuration by factory and model name.

Parameters

Functionality

Returns


5. delete_factory()

Route: /delete_factory
Method: POST
Authentication: Required
Request Validation: Requires JSON field llm_factory

Purpose

Delete all tenant-specific LLMs belonging to a particular LLM factory.

Parameters

Functionality

Returns


6. my_llms()

Route: /my_llms
Method: GET
Authentication: Required

Purpose

Retrieve all LLMs configured for the current tenant, optionally including detailed information.

Parameters

Functionality

Returns


7. list_app()

Route: /list
Method: GET
Authentication: Required

Purpose

List all LLMs available to the current tenant, marking their availability and filtering by optional model type.

Parameters

Functionality

Returns


Important Implementation Details & Algorithms


Interaction with Other System Components


Visual Diagram

classDiagram
    class LLMApp {
        <<module>>
        +factories()
        +set_api_key()
        +add_llm()
        +delete_llm()
        +delete_factory()
        +my_llms()
        +list_app()
    }

    class LLMFactoriesService {
        +get_all()
        +query()
    }
    class TenantLLMService {
        +filter_update()
        +save()
        +filter_delete()
        +query()
        +get_my_llms()
    }
    class LLMService {
        +get_all()
        +query()
    }
    class EmbeddingModel {
        <<dict>>
    }
    class ChatModel {
        <<dict>>
    }
    class RerankModel {
        <<dict>>
    }
    class CvModel {
        <<dict>>
    }
    class TTSModel {
        <<dict>>
    }

    LLMApp --> LLMFactoriesService : uses
    LLMApp --> TenantLLMService : uses
    LLMApp --> LLMService : uses
    LLMApp --> EmbeddingModel : instantiates
    LLMApp --> ChatModel : instantiates
    LLMApp --> RerankModel : instantiates
    LLMApp --> CvModel : instantiates
    LLMApp --> TTSModel : instantiates

Summary

llm_app.py is a critical API component managing tenant-specific LLM configurations and factory metadata in InfiniFlow. It provides endpoints for CRUD operations on LLM configurations, API key validation through test calls, and retrieval of available LLMs and factories. The module ensures multi-tenant security and supports a variety of LLM providers with custom authentication schemes. It tightly integrates with database services and the RAG library for LLM interactions.

This file forms the backend management interface for tenants to configure and use LLMs safely and effectively within the InfiniFlow platform.