dialog_service.py


Overview

dialog_service.py is a core service module in the InfiniFlow system that manages dialog-related functionalities, including dialog record management, chat interactions with large language models (LLMs), knowledge base retrieval, SQL generation for question answering, and mind map generation from knowledge bases. This file integrates several subsystems such as database models, LLM bundles, knowledge retrieval, and Langfuse tracing to provide a comprehensive dialog experience for tenants.

Key responsibilities include:


Classes and Functions

Class: DialogService

A subclass of CommonService managing database operations on the Dialog model.

Methods


Function: chat_solo(dialog, messages, stream=True)

Performs a chat interaction with an LLM without knowledge base integration.


Function: get_models(dialog)

Fetches and prepares various LLM bundles and related knowledge bases for a dialog.


Function: repair_bad_citation_formats(answer: str, kbinfos: dict, idx: set)

Normalizes and repairs citation patterns in the generated answers.


Function: convert_conditions(metadata_condition)

Converts metadata filter conditions from UI format to internal operator notation.


Function: meta_filter(metas: dict, filters: list[dict])

Filters document IDs based on metadata filters.


Function: chat(dialog, messages, stream=True, **kwargs)

Main chatbot function integrating dialogs, knowledge bases, and LLMs.


Function: use_sql(question, field_map, tenant_id, chat_mdl, quota=True, kb_ids=None)

Generates and executes SQL queries based on user questions and table metadata.


Function: tts(tts_mdl, text)

Generates TTS audio in hex string format.


Function: ask(question, kb_ids, tenant_id, chat_llm_name=None, search_config={})

Simple Q&A function using knowledge base retrieval and chat model.


Function: gen_mindmap(question, kb_ids, tenant_id, search_config={})

Generates a mind map visualization based on knowledge base documents relevant to a question.


Important Implementation Details and Algorithms


Interaction with Other System Components


Visual Diagram

classDiagram
    class DialogService {
        +save(**kwargs)
        +update_many_by_id(data_list)
        +get_list(tenant_id, page, per_page, orderby, desc, id, name)
        +get_by_tenant_ids(joined_tenant_ids, user_id, page, per_page, orderby, desc, keywords, parser_id)
    }
    class LLMBundle {
        +chat(system_prompt, messages, settings)
        +chat_streamly(system_prompt, messages, settings)
        +tts(text)
        +bind_tools(session, tools)
    }
    class KnowledgebaseService {
        +get_by_ids(kb_ids)
        +get_field_map(kb_ids)
    }
    class TenantLLMService {
        +llm_id2llm_type(llm_id)
        +get_model_config(tenant_id, llm_type, llm_id)
    }
    class Langfuse {
        +auth_check()
        +create_trace_id()
        +start_generation(...)
    }
    class DeepResearcher {
        +thinking(kbinfos, question)
    }
    class MindMapExtractor {
        +__call__(contents)
        +output
    }
    DialogService ..> Dialog : model
    DialogService ..> LLMBundle : uses
    DialogService ..> KnowledgebaseService : uses
    DialogService ..> TenantLLMService : uses
    DialogService ..> Langfuse : uses
    DialogService ..> DeepResearcher : uses
    DialogService ..> MindMapExtractor : uses

    class chat {
        +chat(dialog, messages, stream=True, **kwargs)
    }
    class chat_solo {
        +chat_solo(dialog, messages, stream=True)
    }
    class ask {
        +ask(question, kb_ids, tenant_id, chat_llm_name, search_config)
    }
    class gen_mindmap {
        +gen_mindmap(question, kb_ids, tenant_id, search_config)
    }
    DialogService ..> chat
    DialogService ..> chat_solo
    DialogService ..> ask
    DialogService ..> gen_mindmap

Summary

The dialog_service.py file orchestrates dialog management, question answering, and knowledge retrieval by interfacing with LLMs, knowledge bases, and external services. It supports advanced features like streaming chat, SQL-based retrieval, citation management, metadata filtering, and mind map generation. The integration with Langfuse for tracing and the modular design with LLM bundles and retrieval services make it a central and sophisticated component of the InfiniFlow platform.


End of Documentation for dialog_service.py