llm.py

Overview

The llm.py file is a core component module of the InfiniFlow system designed to provide interaction with large language models (LLMs). It defines the LLM component and its parameter class LLMParam, enabling the system to generate text completions or structured outputs by querying configured LLM backends. This module encapsulates prompt preparation, streaming and non-streaming generation, error handling, and integration with tenant-specific LLM configurations and multi-modal inputs (e.g., images). It is a key part of the agent framework facilitating advanced language understanding, reasoning, and response generation functionalities.


Classes and Their Detailed Descriptions

1. LLMParam (inherits from ComponentParamBase)

Purpose

Encapsulates all configuration parameters for the LLM component, including LLM model identifiers, prompt templates, generation parameters, and output formatting preferences.

Attributes

Methods

Usage Example

param = LLMParam()
param.llm_id = "gpt-4"
param.sys_prompt = "You are a helpful assistant."
param.prompts = [{"role": "user", "content": "Hello, world!"}]
param.max_tokens = 150
param.temperature = 0.7
param.check()  # validates parameters
conf = param.gen_conf()  # get generation config dictionary

2. LLM (inherits from ComponentBase)

Purpose

Main component class representing an LLM interaction node within the agent pipeline. Manages prompt preparation, generation calls (both streaming and batch), parsing, and output formatting. Supports multi-modal inputs and structured output enforcement.

Attributes

Constructor

def __init__(self, canvas, id, param: ComponentParamBase)

Initializes the component with references to the canvas (execution environment), component ID, and parameters. Sets up the LLM client bundle and initializes image list.


Public Methods


Core Internal Methods and Workflows

_prepare_prompt_variables() -> tuple[str, list[dict], dict]
_extract_prompts(sys_prompt: str) -> tuple[dict, str]
_generate(msg: list[dict], **kwargs) -> str
_generate_streamly(msg: list[dict], **kwargs) -> Generator[str, None, None]
_invoke(**kwargs) -> None
_stream_output(prompt: str, msg: list[dict]) -> Generator[str, None, None]

Important Implementation Details & Algorithms


Interaction with Other System Parts


Example Usage Scenario

Suppose a developer wants to instantiate an LLM component to answer user queries with GPT-4 using a custom system prompt and structured JSON output.

param = LLMParam()
param.llm_id = "gpt-4"
param.sys_prompt = "You are a helpful assistant. <TASK_ANALYSIS>Analyze the input carefully.</TASK_ANALYSIS>"
param.prompts = [{"role": "user", "content": "{user_query}"}]
param.max_tokens = 200
param.temperature = 0.5
param.output_structure = {"answer": "", "sources": []}
param.check()

llm_component = LLM(canvas, "llm1", param)
llm_component.set_debug_inputs({"user_query": {"value": "Explain quantum computing."}})
llm_component._invoke()
result = llm_component.get_output("structured_content")
print(result)

Visual Diagram

classDiagram
    class LLMParam {
        +llm_id: str
        +sys_prompt: str
        +prompts: list
        +max_tokens: int
        +temperature: float
        +top_p: float
        +presence_penalty: float
        +frequency_penalty: float
        +output_structure: dict
        +cite: bool
        +visual_files_var: str
        +check()
        +gen_conf() dict
    }

    class LLM {
        +component_name: str
        -chat_mdl: LLMBundle
        -imgs: list
        +__init__(canvas, id, param)
        +get_input_form() dict
        +get_input_elements() dict
        +set_debug_inputs(inputs)
        +add2system_prompt(txt)
        -_prepare_prompt_variables() tuple
        -_extract_prompts(sys_prompt) tuple
        -_generate(msg, kwargs) str
        -_generate_streamly(msg, kwargs) Generator
        -_invoke(kwargs)
        -_stream_output(prompt, msg) Generator
        +add_memory(user, assist, func_name, params, results, user_defined_prompt)
        +thoughts() str
    }

    LLMParam <|-- LLM

Summary

The llm.py file is a sophisticated module implementing an LLM integration component within an AI agent framework. It handles prompt management, generation calls to tenant-configured LLM backends, supports structured and streaming outputs, and manages multi-modal inputs. Its design facilitates extensible and robust usage of LLMs in multi-turn conversational or reasoning tasks, with built-in error handling and flexible output formatting. This component interacts closely with the canvas execution environment, tenant services, and prompt utilities to deliver tailored LLM functionalities in InfiniFlow’s pipeline.