conversation_app.py
Overview
conversation_app.py is a Flask-based RESTful API module that manages conversations and dialogs within the InfiniFlow platform. It provides endpoints for creating, updating, retrieving, deleting, and interacting with conversations and dialogs powered by LLM (Large Language Model) services. The file integrates with various service layers to handle user authentication, conversation persistence, dialog management, LLM-based chat completions, text-to-speech synthesis, and knowledge base queries.
Key functionalities include:
Conversation lifecycle management (create, update, list, delete).
Real-time chat completion streaming supporting multiple LLM configurations.
Text-to-speech (TTS) streaming.
Question answering and mindmap generation based on knowledge bases.
Feedback mechanisms like thumbs up/down on assistant responses.
Retrieval and authorization checks based on user and tenant associations.
This module depends heavily on services like ConversationService, DialogService, TenantLLMService, and others to abstract database interactions and LLM operations.
Classes and Functions
This file does not define any classes but contains a set of Flask route functions (view functions) registered on a Flask Blueprint or Flask app instance named manager (assumed imported or defined elsewhere). Each function corresponds to an endpoint handling conversation or dialog related operations.
Route Functions
1. set_conversation()
Route: /set
Methods:
POSTAuthentication: Required (
@login_required)Purpose: Create a new conversation or update an existing conversation.
Parameters (JSON payload):
conversation_id(str): ID of the conversation to update (ifis_newis False).is_new(bool): Indicates if the conversation is new or existing.name(str, optional): Name of the conversation (default: "New conversation").dialog_id(str): ID of the dialog linked with the conversation.Other fields used to update the conversation info.
Returns:
JSON response containing the conversation data or an error message.
Behavior:
If
is_newis False, attempts to update the existing conversation byconversation_id.If
is_newis True, creates a new conversation initialized with dialog prologue prompt.Truncates the conversation name to 255 characters if needed.
Associates the conversation with the current logged-in user.
Handles errors gracefully and returns appropriate API error responses.
Usage example:
# Example JSON payload to create a new conversation
{
"is_new": True,
"dialog_id": "dialog123",
"name": "My Conversation"
}
2. get()
Route:
/getMethods:
GETAuthentication: Required
Purpose: Retrieve a conversation by its
conversation_id.
Query Parameters:
conversation_id(str): The ID of the conversation to fetch.
Returns:
JSON response with the conversation details including messages and avatar icon.
Authorization check ensures only owners of the conversation's tenant dialogs can access.
Important details:
Retrieves user tenants and validates the user's ownership of the dialog.
Formats conversation references for display.
Returns error if conversation not found or unauthorized access.
3. getsse(dialog_id)
Route:
/getsse/<dialog_id>Methods:
GETAuthentication: None (relies on API token in headers)
Purpose: Retrieve dialog information via Server-Sent Events (SSE) pattern.
Headers:
Authorization: "Bearer "
Returns:
JSON response with dialog info if authorized by API token.
Errors if token invalid or dialog not found.
4. rm()
Route:
/rmMethods:
POSTAuthentication: Required
Purpose: Remove (delete) conversations by IDs.
JSON Payload:
conversation_ids(list of str): List of conversation IDs to delete.
Returns:
JSON boolean success flag or error messages.
Important details:
Checks ownership by verifying user's tenant dialogs before deletion.
Deletes conversations one by one.
Returns error if any conversation not found or unauthorized.
5. list_conversation()
Route:
/listMethods:
GETAuthentication: Required
Purpose: List all conversations for a given dialog.
Query Parameters:
dialog_id(str): ID of the dialog to list conversations for.
Returns:
JSON list of conversation objects sorted by creation time descending.
Authorization check to ensure user owns the dialog.
6. completion()
Route:
/completionMethods:
POSTAuthentication: Required
Validation: Requires JSON fields
conversation_idandmessagesPurpose: Generate or stream chat completions for a conversation using LLMs.
JSON Payload:
conversation_id(str): Conversation identifier.messages(list): List of messages with roles (user,assistant,system).Optional LLM parameters:
llm_id(str): Specific LLM model to use.temperature,top_p,frequency_penalty,presence_penalty,max_tokens(various LLM configs).
stream(bool, optional): Whether to stream responses or return one-shot.
Returns:
If streaming: Server-Sent Events response streaming partial answers.
Otherwise: JSON with final answer.
Important Implementation Details:
Filters system messages and initial empty assistant messages.
Validates user authorization to conversation and dialog.
Supports embedded LLM models via tenant API keys.
Appends new references to conversation metadata.
Updates conversation state in the DB if not streaming.
Example usage:
{
"conversation_id": "conv123",
"messages": [
{"role": "user", "content": "Hello"},
{"role": "assistant", "content": "Hi! How can I help?"}
],
"llm_id": "openai-gpt4",
"temperature": 0.7,
"stream": true
}
7. tts()
Route:
/ttsMethods:
POSTAuthentication: Required
Purpose: Stream text-to-speech audio for input text.
JSON Payload:
text(str): Text to synthesize to speech.
Returns:
Streaming audio response (
audio/mpeg) chunked from TTS model.
Important details:
Uses the tenant's default TTS model.
Splits input text on common Chinese and punctuation marks to chunk synthesis.
Streams chunks progressively to the client.
Handles errors by streaming error messages.
8. delete_msg()
Route:
/delete_msgMethods:
POSTAuthentication: Required
Validation: Requires
conversation_idandmessage_idPurpose: Delete a message and its associated assistant reply from a conversation.
JSON Payload:
conversation_id(str)message_id(str)
Returns:
Updated conversation JSON or error.
Important details:
Deletes the user message and the assistant message immediately following it.
Also removes corresponding references.
Updates the conversation record in DB.
9. thumbup()
Route:
/thumbupMethods:
POSTAuthentication: Required
Validation: Requires
conversation_idandmessage_idPurpose: Record user feedback (thumbs up or down) on assistant messages.
JSON Payload:
conversation_id(str)message_id(str)thumbup(bool): True for thumbs up, False for thumbs down.feedback(str, optional): Optional feedback text on thumbs down.
Returns:
Updated conversation JSON.
10. ask_about()
Route:
/askMethods:
POSTAuthentication: Required
Validation: Requires
questionandkb_idsPurpose: Ask a question about knowledge base(s) and stream answers.
JSON Payload:
question(str)kb_ids(list of str): Knowledge base IDs to query.search_id(str, optional): Search application context.
Returns:
SSE stream of answer data chunks.
Final chunk indicates completion.
Implementation:
Retrieves search configuration if
search_idspecified.Uses
ask()function to generate answer stream.Handles errors by sending error messages in stream.
11. mindmap()
Route:
/mindmapMethods:
POSTAuthentication: Required
Validation: Requires
questionandkb_idsPurpose: Generate a mindmap visualization based on a question and KBs.
JSON Payload:
question(str)kb_ids(list of str)search_id(str, optional)
Returns:
JSON mindmap data or error response.
Implementation:
Combines KB IDs from search config and request.
Calls
gen_mindmap()to create mindmap data.Returns error if generation fails.
12. related_questions()
Route:
/related_questionsMethods:
POSTAuthentication: Required
Validation: Requires
questionPurpose: Generate related search questions given a user query.
JSON Payload:
question(str)search_id(str, optional)
Returns:
JSON list of related questions (strings).
Implementation:
Loads prompt template
related_question.Uses chat LLM bundle with configured temperature.
Parses LLM output to extract numbered questions.
Important Implementation Details and Algorithms
Streaming with Server-Sent Events: The
/completionand/askendpoints use generator functions yielding JSON chunks with chat responses or error messages, streamed as SSE to clients. This enables real-time partial result delivery.Conversation and Dialog Ownership: Throughout the module, authorization checks ensure users can only access or modify conversations and dialogs they own, leveraging tenant and user associations.
Reference Management: Conversation references are maintained and updated on message deletions and completions, helping track context or document chunks linked to messages.
LLM Model Configuration: Supports dynamic selection and configuration of LLM models per tenant, including temperature, token limits, and penalties for generation control.
Text-to-Speech Chunking: Splits input text based on punctuation for smoother TTS streaming, improving performance and user experience.
Error Handling: Uses consistent error response helpers and streams error information when appropriate to maintain robust client communication.
System Interaction
Database Models: Uses
ConversationService,DialogService,APIToken,UserTenantService, andTenantServiceto interact with persistent storage of conversations, dialogs, users, tenants, and API tokens.LLM Integration: Via
LLMBundleandTenantLLMService, interacts with various LLM providers for chat and TTS functionality.Authentication: Leverages Flask-Login for session-based authentication (
current_user) and API token validation for certain endpoints.Prompt Management: Loads prompt templates and formats from
rag.promptsmodules to guide LLM responses.Search Services: Uses
SearchServiceto retrieve search application configurations affecting knowledge base queries and chat behavior.Utilities: Uses helper functions for JSON formatting, error handling, and request validation from
api.utils.api_utils.
Visual Diagram: Class Diagram of Core Services Used in conversation_app.py
classDiagram
class ConversationService {
+get_by_id(id)
+update_by_id(id, data)
+save(**kwargs)
+delete_by_id(id)
+query(**filters)
+model
}
class DialogService {
+get_by_id(id)
+query(**filters)
+ask(question, kb_ids, user_id, search_config)
+chat(dialog, messages, stream, **kwargs)
+gen_mindmap(question, kb_ids, tenant_id, search_config)
}
class TenantLLMService {
+get_api_key(tenant_id, model_name)
}
class LLMBundle {
+chat(prompt, messages, config)
+tts(text)
}
class UserTenantService {
+query(user_id)
}
class SearchService {
+get_detail(search_id)
}
ConversationService ..> DialogService : uses
ConversationService ..> UserTenantService : uses
DialogService ..> TenantLLMService : uses
DialogService ..> LLMBundle : uses
UserTenantService ..> TenantService : uses
SearchService ..> DialogService : uses
Summary
The conversation_app.py file is a critical API layer in InfiniFlow's conversational AI platform, managing conversations, dialogs, and interactions with LLM and TTS services. It implements secure, real-time, and configurable endpoints leveraging tenant-based authorization and modular service abstractions. The file follows Flask best practices, uses streaming for efficient data delivery, and integrates seamlessly with the broader system components to provide a robust conversational experience.