session.py

Overview

The session.py file defines classes and methods for managing interactive conversational sessions within the InfiniFlow system. It primarily provides an abstraction for a Session entity that supports multi-turn question-answer interactions with either a "chat" or "agent" backend model. The file encapsulates the logic for sending user queries, streaming or synchronous response handling, and managing session state and messages. Additionally, it defines a Message class representing individual assistant messages returned during the session.

This module acts as a client-side interface for interacting with remote chat or agent completion APIs, handling the network requests and response parsing transparently. It is designed to be extendable and integrates with a base class (Base) which likely provides HTTP request utilities and shared functionality.


Classes and Methods

Class: Session

Represents a conversational session with either a chat or agent backend. Supports sending questions, receiving responses (streamed or complete), and updating session metadata.

Constructor: __init__(self, rag, res_dict)

Initializes a new Session instance.


Method: ask(self, question="", stream=True, **kwargs)

Sends a question to the session backend and yields or returns the assistant's responses.


Method: _structure_answer(self, json_data)

Internal helper to parse JSON response data and create a Message instance.


Method: _ask_chat(self, question: str, stream: bool, **kwargs)

Sends a question to the chat backend API.


Method: _ask_agent(self, question: str, stream: bool)

Sends a question to the agent backend API.


Method: update(self, update_message)

Updates the session metadata on the backend.


Class: Message

Represents an individual assistant message in the session conversation.

Constructor: __init__(self, rag, res_dict)

Initializes a new Message.


Implementation Details and Algorithms


Interaction with Other System Components


Visual Diagram

classDiagram
    class Session {
        +id: str
        +name: str
        +messages: list
        +ask(question: str, stream: bool, **kwargs) Message | generator
        +update(update_message: dict) void
        -_structure_answer(json_data: dict) Message
        -_ask_chat(question: str, stream: bool, **kwargs) Response
        -_ask_agent(question: str, stream: bool) Response
    }
    class Message {
        +content: str
        +reference: dict | None
        +role: str
        +prompt: str | None
        +id: str | None
    }
    class Base {
        <<abstract>>
        +post(endpoint: str, data: dict, stream: bool) Response
        +put(endpoint: str, data: dict) Response
    }

    Session --|> Base
    Message --|> Base

Summary

The session.py file provides a robust interface for managing conversational sessions with chat or agent AI backends. It abstracts API communication, supports real-time streaming of assistant responses, and encapsulates message data consistently. This module is fundamental for building interactive, turn-based assistant features within the InfiniFlow application, enabling flexible session handling and extensibility.


End of Documentation for session.py