chat.py

Overview

The chat.py file defines the Chat class, which represents a conversational assistant within the InfiniFlow system. This class encapsulates the configuration and management of a conversational agent, including its language model parameters (LLM), prompt settings (Prompt), and session handling capabilities.

The Chat class provides methods to update chat metadata, create new chat sessions, list existing sessions, and delete sessions. It acts as a high-level interface for managing chat interactions backed by a retrieval-augmented generation (RAG) system, leveraging an underlying RESTful API for persistent data operations.


Classes and Methods

Class: Chat

The main class representing a chat assistant instance.

Constructor: __init__(self, rag, res_dict)


Nested Class: Chat.LLM

Represents the language model configuration for the Chat assistant.

Constructor: __init__(self, rag, res_dict)


Nested Class: Chat.Prompt

Represents prompt configuration and related settings for the assistant.

Constructor: __init__(self, rag, res_dict)


Method: update(self, update_message: dict)


Method: create_session(self, name: str = "New session") -> Session


Method: list_sessions(self, page: int = 1, page_size: int = 30, orderby: str = "create_time", desc: bool = True, id: str = None, name: str = None) -> list[Session]


Method: delete_sessions(self, ids: list[str] | None = None)


Important Implementation Details


Interactions with Other System Components


Mermaid Class Diagram

classDiagram
    class Chat {
        +str id
        +str name
        +str avatar
        +LLM llm
        +Prompt prompt
        +__init__(rag, res_dict)
        +update(update_message: dict)
        +create_session(name: str) Session
        +list_sessions(page: int, page_size: int, orderby: str, desc: bool, id: str, name: str) list~Session~
        +delete_sessions(ids: list~str~)
    }

    class LLM {
        +str model_name
        +float temperature
        +float top_p
        +float presence_penalty
        +float frequency_penalty
        +int max_tokens
        +__init__(rag, res_dict)
    }

    class Prompt {
        +float similarity_threshold
        +float keywords_similarity_weight
        +int top_n
        +int top_k
        +list variables
        +str rerank_model
        +empty_response
        +str opener
        +bool show_quote
        +str prompt
        +__init__(rag, res_dict)
    }

    Chat "1" *-- "1" LLM : contains
    Chat "1" *-- "1" Prompt : contains
    Chat ..> Base : inherits
    LLM ..> Base : inherits
    Prompt ..> Base : inherits
    Chat ..> Session : uses (methods return Session instances)

Summary

The chat.py file defines the Chat class, a central component for managing a conversational AI assistant. It encapsulates language model parameters, prompt configuration, and session management, all integrated with a backend via RESTful API calls. The nested LLM and Prompt classes define specialized configurations to control the assistant's behavior and response generation. This file interacts closely with the Session class and the foundational Base class, forming a critical part of the InfiniFlow chat system.