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)
Parameters:
rag: An instance of the RAG system or API client used for backend communication.res_dict: A dictionary containing resource data used to initialize the instance.
Description:
Initializes aChatobject with default properties including a unique ID, assistant name, avatar path, and nestedLLMandPromptconfigurations.Usage Example:
chat = Chat(rag_instance, resource_data)
Nested Class: Chat.LLM
Represents the language model configuration for the Chat assistant.
Constructor: __init__(self, rag, res_dict)
Parameters:
rag: The RAG instance or API client.res_dict: Initialization data dictionary.
Attributes:
model_name(str or None): The name of the language model.temperature(float): Sampling temperature controlling randomness (default 0.1).top_p(float): Nucleus sampling parameter (default 0.3).presence_penalty(float): Penalty for new topic introduction (default 0.4).frequency_penalty(float): Penalty for word repetition (default 0.7).max_tokens(int): Maximum tokens allowed in the model output (default 512).
Description:
Configures and maintains parameters influencing response generation behavior of the language model.
Nested Class: Chat.Prompt
Represents prompt configuration and related settings for the assistant.
Constructor: __init__(self, rag, res_dict)
Parameters:
rag: The RAG instance or API client.res_dict: Initialization data dictionary.
Attributes:
similarity_threshold(float): Threshold to determine relevant knowledge base content (default 0.2).keywords_similarity_weight(float): Weight assigned to keyword similarity (default 0.7).top_n(int): Number of top results to consider (default 8).top_k(int): Max number of documents to search (default 1024).variables(list): List of variables used in prompt templating (default includes optional "knowledge").rerank_model(str): Model used for reranking search results (default empty).empty_response(any): Default response if no knowledge base content is relevant (default None).opener(str): Initial greeting message.show_quote(bool): Whether to display quoted knowledge base content (default True).prompt(str): The template string guiding the assistant’s response formulation.
Description:
Defines how the assistant constructs its queries and responses using the knowledge base and chat history.
Method: update(self, update_message: dict)
Parameters:
update_message(dict): A dictionary containing fields to update for the chat instance.
Returns: None
Raises: Exception if the update operation fails.
Description:
Sends a PUT request to update the chat's metadata on the backend server.Usage Example:
chat.update({"name": "New Assistant Name"})
Method: create_session(self, name: str = "New session") -> Session
Parameters:
name(str): The name for the new session (default "New session").
Returns: An instance of
Sessionrepresenting the newly created chat session.Raises: Exception if the creation fails.
Description:
Creates a new session under the current chat by sending a POST request.Usage Example:
new_session = chat.create_session("Support Chat")
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]
Parameters:
page(int): Page number for paginated results (default 1).page_size(int): Number of sessions per page (default 30).orderby(str): Field to order by (default "create_time").desc(bool): Whether to order descending (default True).id(str or None): Filter by session ID.name(str or None): Filter by session name.
Returns: List of
Sessioninstances matching the query.Raises: Exception if the retrieval fails.
Description:
Retrieves a paginated, optionally filtered and sorted list of chat sessions.Usage Example:
sessions = chat.list_sessions(page=2, page_size=10)
Method: delete_sessions(self, ids: list[str] | None = None)
Parameters:
ids(list of str or None): List of session IDs to delete. If None, behavior depends on backend implementation.
Returns: None
Raises: Exception if deletion fails.
Description:
Deletes specified chat sessions by sending a DELETE request to the server.Usage Example:
chat.delete_sessions(["session_id_1", "session_id_2"])
Important Implementation Details
The
Chatclass inherits from a base classBase, which likely provides HTTP methods (get,post,put,rm) to interact with the backend API.The nested classes
LLMandPromptalso inherit fromBase, implying they may also support backend synchronization or configuration fetching mechanisms.The design tightly couples the chat assistant’s configuration and session management with RESTful API endpoints, using JSON payloads for communication.
The prompt template in
Prompt.promptis carefully designed to instruct the assistant to summarize knowledge base content while handling cases where the knowledge base does not contain relevant information.
Interactions with Other System Components
Base (
base.py): Provides foundational methods for REST API operations (get,post,put,rm) thatChatand its nested classes utilize.Session (
session.py): Represents individual chat sessions. TheChatclass creates, lists, and deletesSessioninstances, facilitating user conversation management.RAG System: The
ragparameter passed around is presumed to be a client or handler for the retrieval-augmented generation system, enabling knowledge retrieval and language model integration.Backend API: The class methods communicate with backend REST endpoints (e.g.,
/chats/{id}/sessions) to persist and manipulate chat and session data.
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.