diff --git a/backend/consts/const.py b/backend/consts/const.py index 2877a4b7a..7a01b1d45 100644 --- a/backend/consts/const.py +++ b/backend/consts/const.py @@ -271,3 +271,6 @@ # APP Version APP_VERSION = "v1.7.5.1" + +DEFAULT_ZH_TITLE = "新对话" +DEFAULT_EN_TITLE = "New Conversation" diff --git a/backend/services/conversation_management_service.py b/backend/services/conversation_management_service.py index c489c0fb9..a794598df 100644 --- a/backend/services/conversation_management_service.py +++ b/backend/services/conversation_management_service.py @@ -7,7 +7,7 @@ from jinja2 import StrictUndefined, Template from smolagents import OpenAIServerModel -from consts.const import LANGUAGE, MODEL_CONFIG_MAPPING, MESSAGE_ROLE +from consts.const import LANGUAGE, MODEL_CONFIG_MAPPING, MESSAGE_ROLE, DEFAULT_EN_TITLE, DEFAULT_ZH_TITLE from consts.model import AgentRequest, ConversationResponse, MessageRequest, MessageUnit from database.conversation_db import ( create_conversation, @@ -277,7 +277,8 @@ def call_llm_for_title(content: str, tenant_id: str, language: str = LANGUAGE["Z # Call the model response = llm(messages, max_tokens=10) - + if not response or not response.content or not response.content.strip(): + return DEFAULT_EN_TITLE if language == LANGUAGE["EN"] else DEFAULT_ZH_TITLE return remove_think_blocks(response.content.strip()) diff --git a/test/backend/services/test_conversation_management_service.py b/test/backend/services/test_conversation_management_service.py index 009f7a84a..8c582fb02 100644 --- a/test/backend/services/test_conversation_management_service.py +++ b/test/backend/services/test_conversation_management_service.py @@ -346,6 +346,68 @@ def test_call_llm_for_title(self, mock_get_model_config, mock_get_prompt_templat mock_llm_instance.assert_called_once() mock_get_prompt_template.assert_called_once_with(language='zh') + @patch('backend.services.conversation_management_service.OpenAIServerModel') + @patch('backend.services.conversation_management_service.get_generate_title_prompt_template') + @patch('backend.services.conversation_management_service.tenant_config_manager.get_model_config') + def test_call_llm_for_title_response_none_zh(self, mock_get_model_config, mock_get_prompt_template, mock_openai): + """Test call_llm_for_title returns default ZH title when response is None.""" + # Setup + mock_get_model_config.return_value = { + "model_name": "gpt-4", + "model_repo": "openai", + "base_url": "http://example.com", + "api_key": "fake-key" + } + + mock_prompt_template = { + "SYSTEM_PROMPT": "Generate a short title", + "USER_PROMPT": "Generate a title for: {{content}}" + } + mock_get_prompt_template.return_value = mock_prompt_template + + mock_llm_instance = mock_openai.return_value + mock_llm_instance.return_value = None + + # Execute + result = call_llm_for_title( + "What is AI?", tenant_id=self.tenant_id, language="zh") + + # Assert + self.assertEqual(result, "新对话") + mock_openai.assert_called_once() + mock_get_prompt_template.assert_called_once_with(language='zh') + + @patch('backend.services.conversation_management_service.OpenAIServerModel') + @patch('backend.services.conversation_management_service.get_generate_title_prompt_template') + @patch('backend.services.conversation_management_service.tenant_config_manager.get_model_config') + def test_call_llm_for_title_response_none_en(self, mock_get_model_config, mock_get_prompt_template, mock_openai): + """Test call_llm_for_title returns default EN title when response is None.""" + # Setup + mock_get_model_config.return_value = { + "model_name": "gpt-4", + "model_repo": "openai", + "base_url": "http://example.com", + "api_key": "fake-key" + } + + mock_prompt_template = { + "SYSTEM_PROMPT": "Generate a short title", + "USER_PROMPT": "Generate a title for: {{content}}" + } + mock_get_prompt_template.return_value = mock_prompt_template + + mock_llm_instance = mock_openai.return_value + mock_llm_instance.return_value = None + + # Execute + result = call_llm_for_title( + "What is AI?", tenant_id=self.tenant_id, language="en") + + # Assert + self.assertEqual(result, "New Conversation") + mock_openai.assert_called_once() + mock_get_prompt_template.assert_called_once_with(language='en') + @patch('backend.services.conversation_management_service.rename_conversation') def test_update_conversation_title(self, mock_rename_conversation): # Setup