Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

refactor(api): remove legacy Baichuan prompt handling (#42779)

非法操作0c36f4a104ac
diff --git a/api/controllers/console/app/advanced_prompt_template.py b/api/controllers/console/app/advanced_prompt_template.py
index bc96b4f422e..267ac25c0c9 100644
--- a/api/controllers/console/app/advanced_prompt_template.py
+++ b/api/controllers/console/app/advanced_prompt_template.py
@@ -52,7 +52,6 @@ class AdvancedPromptTemplateList(Resource):
result = application_services().advanced_prompt_templates.get_prompt(
app_mode=req_data.app_mode,
model_mode=req_data.model_mode,
- model_name=req_data.model_name,
has_context=req_data.has_context,
)
return AdvancedPromptTemplateResponse.model_validate(result).model_dump(mode="json", exclude_unset=True)
diff --git a/api/core/prompt/prompt_templates/advanced_prompt_templates.py b/api/core/prompt/prompt_templates/advanced_prompt_templates.py
index cfe38bfc6b3..9200dad4568 100644
--- a/api/core/prompt/prompt_templates/advanced_prompt_templates.py
+++ b/api/core/prompt/prompt_templates/advanced_prompt_templates.py
@@ -1,7 +1,5 @@
CONTEXT = "Use the following context as your learned knowledge, inside <context></context> XML tags.\n\n<context>\n{{#context#}}\n</context>\n\nWhen answer to user:\n- If you don't know, just say that you don't know.\n- If you don't know when you are not sure, ask for clarification.\nAvoid mentioning that you obtained the information from the context.\nAnd answer according to the language of the user's question.\n" # noqa: E501
-BAICHUAN_CONTEXT = "用户在与一个客观的助手对话。助手会尊重找到的材料,给出全面专业的解释,但不会过度演绎。同时回答中不会暴露引用的材料:\n\n```\n{{#context#}}\n```\n\n" # noqa: E501
-
CHAT_APP_COMPLETION_PROMPT_CONFIG = {
"completion_prompt_config": {
"prompt": {
@@ -20,26 +18,3 @@ COMPLETION_APP_COMPLETION_PROMPT_CONFIG = {
"completion_prompt_config": {"prompt": {"text": "{{#pre_prompt#}}"}},
"stop": ["Human:"],
}
-
-BAICHUAN_CHAT_APP_COMPLETION_PROMPT_CONFIG = {
- "completion_prompt_config": {
- "prompt": {
- "text": "{{#pre_prompt#}}\n\n用户和助手的历史对话内容如下:\n```\n{{#histories#}}\n```\n\n\n\n用户:{{#query#}}" # noqa: E501
- },
- "conversation_histories_role": {"user_prefix": "用户", "assistant_prefix": "助手"},
- },
- "stop": ["用户:"],
-}
-
-BAICHUAN_CHAT_APP_CHAT_PROMPT_CONFIG = {
- "chat_prompt_config": {"prompt": [{"role": "system", "text": "{{#pre_prompt#}}"}]}
-}
-
-BAICHUAN_COMPLETION_APP_CHAT_PROMPT_CONFIG = {
- "chat_prompt_config": {"prompt": [{"role": "user", "text": "{{#pre_prompt#}}"}]}
-}
-
-BAICHUAN_COMPLETION_APP_COMPLETION_PROMPT_CONFIG = {
- "completion_prompt_config": {"prompt": {"text": "{{#pre_prompt#}}"}},
- "stop": ["用户:"],
-}
diff --git a/api/core/prompt/prompt_templates/baichuan_chat.json b/api/core/prompt/prompt_templates/baichuan_chat.json
deleted file mode 100644
index b3f7cdaa18b..00000000000
--- a/api/core/prompt/prompt_templates/baichuan_chat.json
+++ /dev/null
@@ -1,13 +0,0 @@
-{
- "human_prefix": "用户",
- "assistant_prefix": "助手",
- "context_prompt": "用户在与一个客观的助手对话。助手会尊重找到的材料,给出全面专业的解释,但不会过度演绎。同时回答中不会暴露引用的材料:\n\n```\n{{#context#}}\n```\n\n",
- "histories_prompt": "用户和助手的历史对话内容如下:\n```\n{{#histories#}}\n```\n\n",
- "system_prompt_orders": [
- "context_prompt",
- "pre_prompt",
- "histories_prompt"
- ],
- "query_prompt": "\n\n用户:{{#query#}}",
- "stops": ["用户:"]
-}
diff --git a/api/core/prompt/prompt_templates/baichuan_completion.json b/api/core/prompt/prompt_templates/baichuan_completion.json
deleted file mode 100644
index cee9ea47cd8..00000000000
--- a/api/core/prompt/prompt_templates/baichuan_completion.json
+++ /dev/null
@@ -1,9 +0,0 @@
-{
- "context_prompt": "用户在与一个客观的助手对话。助手会尊重找到的材料,给出全面专业的解释,但不会过度演绎。同时回答中不会暴露引用的材料:\n\n```\n{{#context#}}\n```\n",
- "system_prompt_orders": [
- "context_prompt",
- "pre_prompt"
- ],
- "query_prompt": "{{#query#}}",
- "stops": null
-}
diff --git a/api/core/prompt/simple_prompt_transform.py b/api/core/prompt/simple_prompt_transform.py
index e836554ca04..2042e1bef2a 100644
--- a/api/core/prompt/simple_prompt_transform.py
+++ b/api/core/prompt/simple_prompt_transform.py
@@ -93,7 +93,6 @@ class SimplePromptTransform(PromptTransform):
def _get_prompt_str_and_rules(
self,
app_mode: AppMode,
- model_config: ModelConfigWithCredentialsEntity,
pre_prompt: str,
inputs: dict[str, Any],
query: str | None = None,
@@ -103,8 +102,6 @@ class SimplePromptTransform(PromptTransform):
# get prompt template
prompt_template_config = self.get_prompt_template(
app_mode=app_mode,
- provider=model_config.provider,
- model=model_config.model,
pre_prompt=pre_prompt,
has_context=context is not None,
query_in_prompt=query is not None,
@@ -148,14 +145,12 @@ class SimplePromptTransform(PromptTransform):
def get_prompt_template(
self,
app_mode: AppMode,
- provider: str,
- model: str,
pre_prompt: str,
has_context: bool,
query_in_prompt: bool,
with_memory_prompt: bool = False,
) -> PromptTemplateConfigDict:
- prompt_rules = self._get_prompt_rule(app_mode=app_mode, provider=provider, model=model)
+ prompt_rules = self._get_prompt_rule(app_mode=app_mode)
custom_variable_keys: list[str] = []
special_variable_keys: list[str] = []
@@ -203,7 +198,6 @@ class SimplePromptTransform(PromptTransform):
# get prompt
prompt, _ = self._get_prompt_str_and_rules(
app_mode=app_mode,
- model_config=model_config,
pre_prompt=pre_prompt,
inputs=inputs,
query=None,
@@ -248,7 +242,6 @@ class SimplePromptTransform(PromptTransform):
# get prompt
prompt, prompt_rules = self._get_prompt_str_and_rules(
app_mode=app_mode,
- model_config=model_config,
pre_prompt=pre_prompt,
inputs=inputs,
query=query,
@@ -274,7 +267,6 @@ class SimplePromptTransform(PromptTransform):
# get prompt
prompt, prompt_rules = self._get_prompt_str_and_rules(
app_mode=app_mode,
- model_config=model_config,
pre_prompt=pre_prompt,
inputs=inputs,
query=query,
@@ -315,15 +307,13 @@ class SimplePromptTransform(PromptTransform):
return prompt_message
- def _get_prompt_rule(self, app_mode: AppMode, provider: str, model: str) -> dict[str, Any]:
+ def _get_prompt_rule(self, app_mode: AppMode) -> dict[str, Any]:
"""
Get simple prompt rule.
:param app_mode: app mode
- :param provider: model provider
- :param model: model name
:return:
"""
- prompt_file_name = self._prompt_file_name(app_mode=app_mode, provider=provider, model=model)
+ prompt_file_name = "common_completion" if app_mode == AppMode.COMPLETION else "common_chat"
# Check if the prompt file is already loaded
if prompt_file_name in prompt_file_contents:
@@ -341,25 +331,3 @@ class SimplePromptTransform(PromptTransform):
prompt_file_contents[prompt_file_name] = content
return cast(dict[str, Any], content)
-
- def _prompt_file_name(self, app_mode: AppMode, provider: str, model: str) -> str:
- # baichuan
- is_baichuan = False
- if provider == "baichuan":
- is_baichuan = True
- else:
- baichuan_supported_providers = ["huggingface_hub", "openllm", "xinference"]
- if provider in baichuan_supported_providers and "baichuan" in model.lower():
- is_baichuan = True
-
- if is_baichuan:
- if app_mode == AppMode.COMPLETION:
- return "baichuan_completion"
- else:
- return "baichuan_chat"
-
- # common
- if app_mode == AppMode.COMPLETION:
- return "common_completion"
- else:
- return "common_chat"
diff --git a/api/services/app/advanced_prompt_template_service.py b/api/services/app/advanced_prompt_template_service.py
index cf6990938c2..157e9dae9e0 100644
--- a/api/services/app/advanced_prompt_template_service.py
+++ b/api/services/app/advanced_prompt_template_service.py
@@ -2,11 +2,6 @@ import copy
from typing import Any
from core.prompt.prompt_templates.advanced_prompt_templates import (
- BAICHUAN_CHAT_APP_CHAT_PROMPT_CONFIG,
- BAICHUAN_CHAT_APP_COMPLETION_PROMPT_CONFIG,
- BAICHUAN_COMPLETION_APP_CHAT_PROMPT_CONFIG,
- BAICHUAN_COMPLETION_APP_COMPLETION_PROMPT_CONFIG,
- BAICHUAN_CONTEXT,
CHAT_APP_CHAT_PROMPT_CONFIG,
CHAT_APP_COMPLETION_PROMPT_CONFIG,
COMPLETION_APP_CHAT_PROMPT_CONFIG,
@@ -18,14 +13,7 @@ from models.model import AppMode
class AdvancedPromptTemplateService:
@classmethod
- def get_prompt(cls, *, app_mode: str, model_mode: str, model_name: str, has_context: str) -> dict[str, Any]:
- if "baichuan" in model_name.lower():
- return cls.get_baichuan_prompt(app_mode, model_mode, has_context)
- else:
- return cls.get_common_prompt(app_mode, model_mode, has_context)
-
- @classmethod
- def get_common_prompt(cls, app_mode: str, model_mode: str, has_context: str) -> dict[str, Any]:
+ def get_prompt(cls, *, app_mode: str, model_mode: str, has_context: str) -> dict[str, Any]:
context_prompt = copy.deepcopy(CONTEXT)
match app_mode:
@@ -75,43 +63,3 @@ class AdvancedPromptTemplateService:
)
return prompt_template
-
- @classmethod
- def get_baichuan_prompt(cls, app_mode: str, model_mode: str, has_context: str) -> dict[str, Any]:
- baichuan_context_prompt = copy.deepcopy(BAICHUAN_CONTEXT)
-
- match app_mode:
- case AppMode.CHAT:
- match model_mode:
- case "completion":
- return cls.get_completion_prompt(
- copy.deepcopy(BAICHUAN_CHAT_APP_COMPLETION_PROMPT_CONFIG),
- has_context,
- baichuan_context_prompt,
- )
- case "chat":
- return cls.get_chat_prompt(
- copy.deepcopy(BAICHUAN_CHAT_APP_CHAT_PROMPT_CONFIG), has_context, baichuan_context_prompt
- )
- case _:
- pass
- case AppMode.COMPLETION:
- match model_mode:
- case "completion":
- return cls.get_completion_prompt(
- copy.deepcopy(BAICHUAN_COMPLETION_APP_COMPLETION_PROMPT_CONFIG),
- has_context,
- baichuan_context_prompt,
- )
- case "chat":
- return cls.get_chat_prompt(
- copy.deepcopy(BAICHUAN_COMPLETION_APP_CHAT_PROMPT_CONFIG),
- has_context,
- baichuan_context_prompt,
- )
- case _:
- pass
- case _:
- pass
- # default return empty dict
- return {}
diff --git a/api/services/workflow/workflow_converter.py b/api/services/workflow/workflow_converter.py
index c9d1d0fa46c..c36edca335a 100644
--- a/api/services/workflow/workflow_converter.py
+++ b/api/services/workflow/workflow_converter.py
@@ -476,8 +476,6 @@ class WorkflowConverter:
prompt_transform = SimplePromptTransform()
prompt_template_config = prompt_transform.get_prompt_template(
app_mode=original_app_mode,
- provider=model_config.provider,
- model=model_config.model,
pre_prompt=prompt_template.simple_prompt_template,
has_context=knowledge_retrieval_node is not None,
query_in_prompt=False,
@@ -517,8 +515,6 @@ class WorkflowConverter:
prompt_transform = SimplePromptTransform()
prompt_template_config = prompt_transform.get_prompt_template(
app_mode=original_app_mode,
- provider=model_config.provider,
- model=model_config.model,
pre_prompt=prompt_template.simple_prompt_template,
has_context=knowledge_retrieval_node is not None,
query_in_prompt=False,
diff --git a/api/tests/test_containers_integration_tests/services/test_advanced_prompt_template_service.py b/api/tests/test_containers_integration_tests/services/test_advanced_prompt_template_service.py
index 5ea3c06e77d..144cba280f4 100644
--- a/api/tests/test_containers_integration_tests/services/test_advanced_prompt_template_service.py
+++ b/api/tests/test_containers_integration_tests/services/test_advanced_prompt_template_service.py
@@ -5,11 +5,6 @@ from faker import Faker
from sqlalchemy.orm import Session
from core.prompt.prompt_templates.advanced_prompt_templates import (
- BAICHUAN_CHAT_APP_CHAT_PROMPT_CONFIG,
- BAICHUAN_CHAT_APP_COMPLETION_PROMPT_CONFIG,
- BAICHUAN_COMPLETION_APP_CHAT_PROMPT_CONFIG,
- BAICHUAN_COMPLETION_APP_COMPLETION_PROMPT_CONFIG,
- BAICHUAN_CONTEXT,
CHAT_APP_CHAT_PROMPT_CONFIG,
CHAT_APP_COMPLETION_PROMPT_CONFIG,
COMPLETION_APP_CHAT_PROMPT_CONFIG,
@@ -30,24 +25,19 @@ class TestAdvancedPromptTemplateService:
# for consistency with other test files
return {}
- def test_get_prompt_baichuan_model_success(
- self, db_session_with_containers: Session, mock_external_service_dependencies
- ):
+ def test_get_prompt_success(self, db_session_with_containers: Session, mock_external_service_dependencies):
"""
- Test successful prompt generation for Baichuan model.
+ Test successful prompt generation.
This test verifies:
- - Proper prompt generation for Baichuan models
- - Correct model detection logic
+ - Proper prompt generation
- Appropriate prompt template selection
"""
fake = Faker()
- # Test data for Baichuan model
args = {
"app_mode": AppMode.CHAT,
"model_mode": "completion",
- "model_name": "baichuan-13b-chat",
"has_context": "true",
}
@@ -60,86 +50,18 @@ class TestAdvancedPromptTemplateService:
assert "prompt" in result["completion_prompt_config"]
assert "text" in result["completion_prompt_config"]["prompt"]
- # Verify context is included for Baichuan model
- prompt_text = result["completion_prompt_config"]["prompt"]["text"]
- assert BAICHUAN_CONTEXT in prompt_text
- assert "{{#pre_prompt#}}" in prompt_text
- assert "{{#histories#}}" in prompt_text
- assert "{{#query#}}" in prompt_text
-
- def test_get_prompt_common_model_success(
- self, db_session_with_containers: Session, mock_external_service_dependencies
- ):
- """
- Test successful prompt generation for common models.
-
- This test verifies:
- - Proper prompt generation for non-Baichuan models
- - Correct model detection logic
- - Appropriate prompt template selection
- """
- fake = Faker()
-
- # Test data for common model
- args = {
- "app_mode": AppMode.CHAT,
- "model_mode": "completion",
- "model_name": "gpt-3.5-turbo",
- "has_context": "true",
- }
-
- # Act: Execute the method under test
- result = AdvancedPromptTemplateService.get_prompt(**args)
-
- # Assert: Verify the expected outcomes
- assert result is not None
- assert "completion_prompt_config" in result
- assert "prompt" in result["completion_prompt_config"]
- assert "text" in result["completion_prompt_config"]["prompt"]
-
- # Verify context is included for common model
+ # Verify context is included
prompt_text = result["completion_prompt_config"]["prompt"]["text"]
assert CONTEXT in prompt_text
assert "{{#pre_prompt#}}" in prompt_text
assert "{{#histories#}}" in prompt_text
assert "{{#query#}}" in prompt_text
- def test_get_prompt_case_insensitive_baichuan_detection(
- self, db_session_with_containers: Session, mock_external_service_dependencies
- ):
- """
- Test Baichuan model detection is case insensitive.
-
- This test verifies:
- - Model name detection works regardless of case
- - Proper prompt template selection for different case variations
- """
- fake = Faker()
-
- # Test different case variations
- test_cases = ["Baichuan-13B-Chat", "BAICHUAN-13B-CHAT", "baichuan-13b-chat", "BaiChuan-13B-Chat"]
-
- for model_name in test_cases:
- args = {
- "app_mode": AppMode.CHAT,
- "model_mode": "completion",
- "model_name": model_name,
- "has_context": "true",
- }
-
- # Act: Execute the method under test
- result = AdvancedPromptTemplateService.get_prompt(**args)
-
- # Assert: Verify Baichuan template is used
- assert result is not None
- prompt_text = result["completion_prompt_config"]["prompt"]["text"]
- assert BAICHUAN_CONTEXT in prompt_text
-
- def test_get_common_prompt_chat_app_completion_mode(
+ def test_get_prompt_chat_app_completion_mode(
self, db_session_with_containers: Session, mock_external_service_dependencies
):
"""
- Test common prompt generation for chat app with completion mode.
+ Test prompt generation for chat app with completion mode.
This test verifies:
- Correct prompt template selection for chat app + completion mode
@@ -149,7 +71,9 @@ class TestAdvancedPromptTemplateService:
fake = Faker()
# Act: Execute the method under test
- result = AdvancedPromptTemplateService.get_common_prompt(AppMode.CHAT, "completion", "true")
+ result = AdvancedPromptTemplateService.get_prompt(
+ app_mode=AppMode.CHAT, model_mode="completion", has_context="true"
+ )
# Assert: Verify the expected outcomes
assert result is not None
@@ -166,11 +90,11 @@ class TestAdvancedPromptTemplateService:
assert "{{#histories#}}" in prompt_text
assert "{{#query#}}" in prompt_text
- def test_get_common_prompt_chat_app_chat_mode(
+ def test_get_prompt_chat_app_chat_mode(
self, db_session_with_containers: Session, mock_external_service_dependencies
):
"""
- Test common prompt generation for chat app with chat mode.
+ Test prompt generation for chat app with chat mode.
This test verifies:
- Correct prompt template selection for chat app + chat mode
@@ -180,7 +104,7 @@ class TestAdvancedPromptTemplateService:
fake = Faker()
# Act: Execute the method under test
- result = AdvancedPromptTemplateService.get_common_prompt(AppMode.CHAT, "chat", "true")
+ result = AdvancedPromptTemplateService.get_prompt(app_mode=AppMode.CHAT, model_mode="chat", has_context="true")
# Assert: Verify the expected outcomes
assert result is not None
@@ -195,11 +119,11 @@ class TestAdvancedPromptTemplateService:
assert CONTEXT in prompt_text
assert "{{#pre_prompt#}}" in prompt_text
- def test_get_common_prompt_completion_app_completion_mode(
+ def test_get_prompt_completion_app_completion_mode(
self, db_session_with_containers: Session, mock_external_service_dependencies
):
"""
- Test common prompt generation for completion app with completion mode.
+ Test prompt generation for completion app with completion mode.
This test verifies:
- Correct prompt template selection for completion app + completion mode
@@ -209,7 +133,9 @@ class TestAdvancedPromptTemplateService:
fake = Faker()
# Act: Execute the method under test
- result = AdvancedPromptTemplateService.get_common_prompt(AppMode.COMPLETION, "completion", "true")
+ result = AdvancedPromptTemplateService.get_prompt(
+ app_mode=AppMode.COMPLETION, model_mode="completion", has_context="true"
+ )
# Assert: Verify the expected outcomes
assert result is not None
@@ -223,11 +149,11 @@ class TestAdvancedPromptTemplateService:
assert CONTEXT in prompt_text
assert "{{#pre_prompt#}}" in prompt_text
- def test_get_common_prompt_completion_app_chat_mode(
+ def test_get_prompt_completion_app_chat_mode(
self, db_session_with_containers: Session, mock_external_service_dependencies
):
"""
- Test common prompt generation for completion app with chat mode.
+ Test prompt generation for completion app with chat mode.
This test verifies:
- Correct prompt template selection for completion app + chat mode
@@ -237,7 +163,9 @@ class TestAdvancedPromptTemplateService:
fake = Faker()
# Act: Execute the method under test
- result = AdvancedPromptTemplateService.get_common_prompt(AppMode.COMPLETION, "chat", "true")
+ result = AdvancedPromptTemplateService.get_prompt(
+ app_mode=AppMode.COMPLETION, model_mode="chat", has_context="true"
+ )
# Assert: Verify the expected outcomes
assert result is not None
@@ -252,11 +180,9 @@ class TestAdvancedPromptTemplateService:
assert CONTEXT in prompt_text
assert "{{#pre_prompt#}}" in prompt_text
- def test_get_common_prompt_no_context(
- self, db_session_with_containers: Session, mock_external_service_dependencies
- ):
+ def test_get_prompt_no_context(self, db_session_with_containers: Session, mock_external_service_dependencies):
"""
- Test common prompt generation without context.
+ Test prompt generation without context.
This test verifies:
- Correct handling when has_context is "false"
@@ -266,7 +192,9 @@ class TestAdvancedPromptTemplateService:
fake = Faker()
# Act: Execute the method under test
- result = AdvancedPromptTemplateService.get_common_prompt(AppMode.CHAT, "completion", "false")
+ result = AdvancedPromptTemplateService.get_prompt(
+ app_mode=AppMode.CHAT, model_mode="completion", has_context="false"
+ )
# Assert: Verify the expected outcomes
assert result is not None
@@ -281,11 +209,11 @@ class TestAdvancedPromptTemplateService:
assert "{{#histories#}}" in prompt_text
assert "{{#query#}}" in prompt_text
- def test_get_common_prompt_unsupported_app_mode(
+ def test_get_prompt_unsupported_app_mode(
self, db_session_with_containers: Session, mock_external_service_dependencies
):
"""
- Test common prompt generation with unsupported app mode.
+ Test prompt generation with unsupported app mode.
This test verifies:
- Proper handling of unsupported app modes
@@ -294,16 +222,18 @@ class TestAdvancedPromptTemplateService:
fake = Faker()
# Act: Execute the method under test
- result = AdvancedPromptTemplateService.get_common_prompt("unsupported_mode", "completion", "true")
+ result = AdvancedPromptTemplateService.get_prompt(
+ app_mode="unsupported_mode", model_mode="completion", has_context="true"
+ )
# Assert: Verify empty dict is returned
assert result == {}
- def test_get_common_prompt_unsupported_model_mode(
+ def test_get_prompt_unsupported_model_mode(
self, db_session_with_containers: Session, mock_external_service_dependencies
):
"""
- Test common prompt generation with unsupported model mode.
+ Test prompt generation with unsupported model mode.
This test verifies:
- Proper handling of unsupported model modes
@@ -312,7 +242,9 @@ class TestAdvancedPromptTemplateService:
fake = Faker()
# Act: Execute the method under test
- result = AdvancedPromptTemplateService.get_common_prompt(AppMode.CHAT, "unsupported_mode", "true")
+ result = AdvancedPromptTemplateService.get_prompt(
+ app_mode=AppMode.CHAT, model_mode="unsupported_mode", has_context="true"
+ )
# Assert: Verify empty dict is returned
assert result == {}
@@ -443,196 +375,12 @@ class TestAdvancedPromptTemplateService:
assert result_text == original_text
assert CONTEXT not in result_text
- def test_get_baichuan_prompt_chat_app_completion_mode(
- self, db_session_with_containers: Session, mock_external_service_dependencies
- ):
- """
- Test Baichuan prompt generation for chat app with completion mode.
-
- This test verifies:
- - Correct Baichuan prompt template selection for chat app + completion mode
- - Proper Baichuan context integration
- - Template structure validation
- """
- fake = Faker()
-
- # Act: Execute the method under test
- result = AdvancedPromptTemplateService.get_baichuan_prompt(AppMode.CHAT, "completion", "true")
-
- # Assert: Verify the expected outcomes
- assert result is not None
- assert "completion_prompt_config" in result
- assert "prompt" in result["completion_prompt_config"]
- assert "text" in result["completion_prompt_config"]["prompt"]
- assert "conversation_histories_role" in result["completion_prompt_config"]
- assert "stop" in result
-
- # Verify Baichuan context is included
- prompt_text = result["completion_prompt_config"]["prompt"]["text"]
- assert BAICHUAN_CONTEXT in prompt_text
- assert "{{#pre_prompt#}}" in prompt_text
- assert "{{#histories#}}" in prompt_text
- assert "{{#query#}}" in prompt_text
-
- def test_get_baichuan_prompt_chat_app_chat_mode(
- self, db_session_with_containers: Session, mock_external_service_dependencies
- ):
- """
- Test Baichuan prompt generation for chat app with chat mode.
-
- This test verifies:
- - Correct Baichuan prompt template selection for chat app + chat mode
- - Proper Baichuan context integration
- - Template structure validation
- """
- fake = Faker()
-
- # Act: Execute the method under test
- result = AdvancedPromptTemplateService.get_baichuan_prompt(AppMode.CHAT, "chat", "true")
-
- # Assert: Verify the expected outcomes
- assert result is not None
- assert "chat_prompt_config" in result
- assert "prompt" in result["chat_prompt_config"]
- assert len(result["chat_prompt_config"]["prompt"]) > 0
- assert "role" in result["chat_prompt_config"]["prompt"][0]
- assert "text" in result["chat_prompt_config"]["prompt"][0]
-
- # Verify Baichuan context is included
- prompt_text = result["chat_prompt_config"]["prompt"][0]["text"]
- assert BAICHUAN_CONTEXT in prompt_text
- assert "{{#pre_prompt#}}" in prompt_text
-
- def test_get_baichuan_prompt_completion_app_completion_mode(
- self, db_session_with_containers: Session, mock_external_service_dependencies
- ):
- """
- Test Baichuan prompt generation for completion app with completion mode.
-
- This test verifies:
- - Correct Baichuan prompt template selection for completion app + completion mode
- - Proper Baichuan context integration
- - Template structure validation
- """
- fake = Faker()
-
- # Act: Execute the method under test
- result = AdvancedPromptTemplateService.get_baichuan_prompt(AppMode.COMPLETION, "completion", "true")
-
- # Assert: Verify the expected outcomes
- assert result is not None
- assert "completion_prompt_config" in result
- assert "prompt" in result["completion_prompt_config"]
- assert "text" in result["completion_prompt_config"]["prompt"]
- assert "stop" in result
-
- # Verify Baichuan context is included
- prompt_text = result["completion_prompt_config"]["prompt"]["text"]
- assert BAICHUAN_CONTEXT in prompt_text
- assert "{{#pre_prompt#}}" in prompt_text
-
- def test_get_baichuan_prompt_completion_app_chat_mode(
- self, db_session_with_containers: Session, mock_external_service_dependencies
- ):
+ def test_get_prompt_all_app_modes(self, db_session_with_containers: Session, mock_external_service_dependencies):
"""
- Test Baichuan prompt generation for completion app with chat mode.
+ Test prompt generation for all app modes.
This test verifies:
- - Correct Baichuan prompt template selection for completion app + chat mode
- - Proper Baichuan context integration
- - Template structure validation
- """
- fake = Faker()
-
- # Act: Execute the method under test
- result = AdvancedPromptTemplateService.get_baichuan_prompt(AppMode.COMPLETION, "chat", "true")
-
- # Assert: Verify the expected outcomes
- assert result is not None
- assert "chat_prompt_config" in result
- assert "prompt" in result["chat_prompt_config"]
- assert len(result["chat_prompt_config"]["prompt"]) > 0
- assert "role" in result["chat_prompt_config"]["prompt"][0]
- assert "text" in result["chat_prompt_config"]["prompt"][0]
-
- # Verify Baichuan context is included
- prompt_text = result["chat_prompt_config"]["prompt"][0]["text"]
- assert BAICHUAN_CONTEXT in prompt_text
- assert "{{#pre_prompt#}}" in prompt_text
-
- def test_get_baichuan_prompt_no_context(
- self, db_session_with_containers: Session, mock_external_service_dependencies
- ):
- """
- Test Baichuan prompt generation without context.
-
- This test verifies:
- - Correct handling when has_context is "false"
- - Baichuan context is not included in prompt
- - Template structure remains intact
- """
- fake = Faker()
-
- # Act: Execute the method under test
- result = AdvancedPromptTemplateService.get_baichuan_prompt(AppMode.CHAT, "completion", "false")
-
- # Assert: Verify the expected outcomes
- assert result is not None
- assert "completion_prompt_config" in result
- assert "prompt" in result["completion_prompt_config"]
- assert "text" in result["completion_prompt_config"]["prompt"]
-
- # Verify Baichuan context is NOT included
- prompt_text = result["completion_prompt_config"]["prompt"]["text"]
- assert BAICHUAN_CONTEXT not in prompt_text
- assert "{{#pre_prompt#}}" in prompt_text
- assert "{{#histories#}}" in prompt_text
- assert "{{#query#}}" in prompt_text
-
- def test_get_baichuan_prompt_unsupported_app_mode(
- self, db_session_with_containers: Session, mock_external_service_dependencies
- ):
- """
- Test Baichuan prompt generation with unsupported app mode.
-
- This test verifies:
- - Proper handling of unsupported app modes
- - Default empty dict return
- """
- fake = Faker()
-
- # Act: Execute the method under test
- result = AdvancedPromptTemplateService.get_baichuan_prompt("unsupported_mode", "completion", "true")
-
- # Assert: Verify empty dict is returned
- assert result == {}
-
- def test_get_baichuan_prompt_unsupported_model_mode(
- self, db_session_with_containers: Session, mock_external_service_dependencies
- ):
- """
- Test Baichuan prompt generation with unsupported model mode.
-
- This test verifies:
- - Proper handling of unsupported model modes
- - Default empty dict return
- """
- fake = Faker()
-
- # Act: Execute the method under test
- result = AdvancedPromptTemplateService.get_baichuan_prompt(AppMode.CHAT, "unsupported_mode", "true")
-
- # Assert: Verify empty dict is returned
- assert result == {}
-
- def test_get_prompt_all_app_modes_common_model(
- self, db_session_with_containers: Session, mock_external_service_dependencies
- ):
- """
- Test prompt generation for all app modes with common model.
-
- This test verifies:
- - All app modes work correctly with common models
+ - All app modes work correctly
- Proper template selection for each combination
"""
fake = Faker()
@@ -646,39 +394,6 @@ class TestAdvancedPromptTemplateService:
args = {
"app_mode": app_mode,
"model_mode": model_mode,
- "model_name": "gpt-3.5-turbo",
- "has_context": "true",
- }
-
- # Act: Execute the method under test
- result = AdvancedPromptTemplateService.get_prompt(**args)
-
- # Assert: Verify result is not empty
- assert result is not None
- assert result != {}
-
- def test_get_prompt_all_app_modes_baichuan_model(
- self, db_session_with_containers: Session, mock_external_service_dependencies
- ):
- """
- Test prompt generation for all app modes with Baichuan model.
-
- This test verifies:
- - All app modes work correctly with Baichuan models
- - Proper template selection for each combination
- """
- fake = Faker()
-
- # Test all app modes
- app_modes = [AppMode.CHAT, AppMode.COMPLETION]
- model_modes = ["completion", "chat"]
-
- for app_mode in app_modes:
- for model_mode in model_modes:
- args = {
- "app_mode": app_mode,
- "model_mode": model_mode,
- "model_name": "baichuan-13b-chat",
"has_context": "true",
}
@@ -702,13 +417,11 @@ class TestAdvancedPromptTemplateService:
# Test edge cases
edge_cases = [
- {"app_mode": "", "model_mode": "completion", "model_name": "gpt-3.5-turbo", "has_context": "true"},
- {"app_mode": AppMode.CHAT, "model_mode": "", "model_name": "gpt-3.5-turbo", "has_context": "true"},
- {"app_mode": AppMode.CHAT, "model_mode": "completion", "model_name": "", "has_context": "true"},
+ {"app_mode": "", "model_mode": "completion", "has_context": "true"},
+ {"app_mode": AppMode.CHAT, "model_mode": "", "has_context": "true"},
{
"app_mode": AppMode.CHAT,
"model_mode": "completion",
- "model_name": "gpt-3.5-turbo",
"has_context": "",
},
]
@@ -742,7 +455,6 @@ class TestAdvancedPromptTemplateService:
args = {
"app_mode": AppMode.CHAT,
"model_mode": "completion",
- "model_name": "gpt-3.5-turbo",
"has_context": "true",
}
@@ -755,42 +467,6 @@ class TestAdvancedPromptTemplateService:
assert original_completion_completion == COMPLETION_APP_COMPLETION_PROMPT_CONFIG
assert original_completion_chat == COMPLETION_APP_CHAT_PROMPT_CONFIG
- def test_baichuan_template_immutability(
- self, db_session_with_containers: Session, mock_external_service_dependencies
- ):
- """
- Test that original Baichuan templates are not modified.
-
- This test verifies:
- - Original Baichuan template constants are not modified
- - Deep copy is used properly
- - Template immutability is maintained
- """
- fake = Faker()
-
- # Store original templates
- original_baichuan_chat_completion = copy.deepcopy(BAICHUAN_CHAT_APP_COMPLETION_PROMPT_CONFIG)
- original_baichuan_chat_chat = copy.deepcopy(BAICHUAN_CHAT_APP_CHAT_PROMPT_CONFIG)
- original_baichuan_completion_completion = copy.deepcopy(BAICHUAN_COMPLETION_APP_COMPLETION_PROMPT_CONFIG)
- original_baichuan_completion_chat = copy.deepcopy(BAICHUAN_COMPLETION_APP_CHAT_PROMPT_CONFIG)
-
- # Test with context
- args = {
- "app_mode": AppMode.CHAT,
- "model_mode": "completion",
- "model_name": "baichuan-13b-chat",
- "has_context": "true",
- }
-
- # Act: Execute the method under test
- result = AdvancedPromptTemplateService.get_prompt(**args)
-
- # Assert: Verify original templates are unchanged
- assert original_baichuan_chat_completion == BAICHUAN_CHAT_APP_COMPLETION_PROMPT_CONFIG
- assert original_baichuan_chat_chat == BAICHUAN_CHAT_APP_CHAT_PROMPT_CONFIG
- assert original_baichuan_completion_completion == BAICHUAN_COMPLETION_APP_COMPLETION_PROMPT_CONFIG
- assert original_baichuan_completion_chat == BAICHUAN_COMPLETION_APP_CHAT_PROMPT_CONFIG
-
def test_context_integration_consistency(
self, db_session_with_containers: Session, mock_external_service_dependencies
):
@@ -809,25 +485,21 @@ class TestAdvancedPromptTemplateService:
{
"app_mode": AppMode.CHAT,
"model_mode": "completion",
- "model_name": "gpt-3.5-turbo",
"has_context": "true",
},
{
"app_mode": AppMode.CHAT,
"model_mode": "chat",
- "model_name": "gpt-3.5-turbo",
"has_context": "true",
},
{
"app_mode": AppMode.COMPLETION,
"model_mode": "completion",
- "model_name": "gpt-3.5-turbo",
"has_context": "true",
},
{
"app_mode": AppMode.COMPLETION,
"model_mode": "chat",
- "model_name": "gpt-3.5-turbo",
"has_context": "true",
},
]
@@ -847,60 +519,3 @@ class TestAdvancedPromptTemplateService:
elif "chat_prompt_config" in result:
prompt_text = result["chat_prompt_config"]["prompt"][0]["text"]
assert prompt_text.startswith(CONTEXT)
-
- def test_baichuan_context_integration_consistency(
- self, db_session_with_containers: Session, mock_external_service_dependencies
- ):
- """
- Test consistency of Baichuan context integration across different scenarios.
-
- This test verifies:
- - Baichuan context is always prepended correctly
- - Context integration is consistent across different templates
- - No context duplication or corruption
- """
- fake = Faker()
-
- # Test different scenarios
- test_scenarios = [
- {
- "app_mode": AppMode.CHAT,
- "model_mode": "completion",
- "model_name": "baichuan-13b-chat",
- "has_context": "true",
- },
- {
- "app_mode": AppMode.CHAT,
- "model_mode": "chat",
- "model_name": "baichuan-13b-chat",
- "has_context": "true",
- },
- {
- "app_mode": AppMode.COMPLETION,
- "model_mode": "completion",
- "model_name": "baichuan-13b-chat",
- "has_context": "true",
- },
- {
- "app_mode": AppMode.COMPLETION,
- "model_mode": "chat",
- "model_name": "baichuan-13b-chat",
- "has_context": "true",
- },
- ]
-
- for args in test_scenarios:
- # Act: Execute the method under test
- result = AdvancedPromptTemplateService.get_prompt(**args)
-
- # Assert: Verify context integration is consistent
- assert result is not None
- assert result != {}
-
- # Check that Baichuan context is properly integrated
- if "completion_prompt_config" in result:
- prompt_text = result["completion_prompt_config"]["prompt"]["text"]
- assert prompt_text.startswith(BAICHUAN_CONTEXT)
- elif "chat_prompt_config" in result:
- prompt_text = result["chat_prompt_config"]["prompt"][0]["text"]
- assert prompt_text.startswith(BAICHUAN_CONTEXT)
diff --git a/api/tests/unit_tests/core/prompt/test_simple_prompt_transform.py b/api/tests/unit_tests/core/prompt/test_simple_prompt_transform.py
index 0dc74b33dfb..e25aa10073c 100644
--- a/api/tests/unit_tests/core/prompt/test_simple_prompt_transform.py
+++ b/api/tests/unit_tests/core/prompt/test_simple_prompt_transform.py
@@ -6,11 +6,6 @@ import pytest
from core.app.entities.app_invoke_entities import ModelConfigWithCredentialsEntity
from core.memory.token_buffer_memory import TokenBufferMemory
from core.prompt.prompt_templates.advanced_prompt_templates import (
- BAICHUAN_CHAT_APP_CHAT_PROMPT_CONFIG,
- BAICHUAN_CHAT_APP_COMPLETION_PROMPT_CONFIG,
- BAICHUAN_COMPLETION_APP_CHAT_PROMPT_CONFIG,
- BAICHUAN_COMPLETION_APP_COMPLETION_PROMPT_CONFIG,
- BAICHUAN_CONTEXT,
CHAT_APP_CHAT_PROMPT_CONFIG,
CHAT_APP_COMPLETION_PROMPT_CONFIG,
COMPLETION_APP_CHAT_PROMPT_CONFIG,
@@ -32,31 +27,6 @@ def test_get_common_chat_app_prompt_template_with_pcqm():
pre_prompt = "You are a helpful assistant."
prompt_template = prompt_transform.get_prompt_template(
app_mode=AppMode.CHAT,
- provider="openai",
- model="gpt-4",
- pre_prompt=pre_prompt,
- has_context=True,
- query_in_prompt=True,
- with_memory_prompt=True,
- )
- prompt_rules = prompt_template["prompt_rules"]
- assert prompt_template["prompt_template"].template == (
- prompt_rules["context_prompt"]
- + pre_prompt
- + "\n"
- + prompt_rules["histories_prompt"]
- + prompt_rules["query_prompt"]
- )
- assert prompt_template["special_variable_keys"] == ["#context#", "#histories#", "#query#"]
-
-
-def test_get_baichuan_chat_app_prompt_template_with_pcqm():
- prompt_transform = SimplePromptTransform()
- pre_prompt = "You are a helpful assistant."
- prompt_template = prompt_transform.get_prompt_template(
- app_mode=AppMode.CHAT,
- provider="baichuan",
- model="Baichuan2-53B",
pre_prompt=pre_prompt,
has_context=True,
query_in_prompt=True,
@@ -78,27 +48,6 @@ def test_get_common_completion_app_prompt_template_with_pcq():
pre_prompt = "You are a helpful assistant."
prompt_template = prompt_transform.get_prompt_template(
app_mode=AppMode.WORKFLOW,
- provider="openai",
- model="gpt-4",
- pre_prompt=pre_prompt,
- has_context=True,
- query_in_prompt=True,
- with_memory_prompt=False,
- )
- prompt_rules = prompt_template["prompt_rules"]
- assert prompt_template["prompt_template"].template == (
- prompt_rules["context_prompt"] + pre_prompt + "\n" + prompt_rules["query_prompt"]
- )
- assert prompt_template["special_variable_keys"] == ["#context#", "#query#"]
-
-
-def test_get_baichuan_completion_app_prompt_template_with_pcq():
- prompt_transform = SimplePromptTransform()
- pre_prompt = "You are a helpful assistant."
- prompt_template = prompt_transform.get_prompt_template(
- app_mode=AppMode.WORKFLOW,
- provider="baichuan",
- model="Baichuan2-53B",
pre_prompt=pre_prompt,
has_context=True,
query_in_prompt=True,
@@ -116,8 +65,6 @@ def test_get_common_chat_app_prompt_template_with_q():
pre_prompt = ""
prompt_template = prompt_transform.get_prompt_template(
app_mode=AppMode.CHAT,
- provider="openai",
- model="gpt-4",
pre_prompt=pre_prompt,
has_context=False,
query_in_prompt=True,
@@ -133,8 +80,6 @@ def test_get_common_chat_app_prompt_template_with_cq():
pre_prompt = ""
prompt_template = prompt_transform.get_prompt_template(
app_mode=AppMode.CHAT,
- provider="openai",
- model="gpt-4",
pre_prompt=pre_prompt,
has_context=True,
query_in_prompt=True,
@@ -152,8 +97,6 @@ def test_get_common_chat_app_prompt_template_with_p():
pre_prompt = "you are {{name}}"
prompt_template = prompt_transform.get_prompt_template(
app_mode=AppMode.CHAT,
- provider="openai",
- model="gpt-4",
pre_prompt=pre_prompt,
has_context=False,
query_in_prompt=False,
@@ -193,8 +136,6 @@ def test__get_chat_model_prompt_messages():
prompt_template = prompt_transform.get_prompt_template(
app_mode=AppMode.CHAT,
- provider=model_config_mock.provider,
- model=model_config_mock.model,
pre_prompt=pre_prompt,
has_context=True,
query_in_prompt=False,
@@ -240,8 +181,6 @@ def test__get_completion_model_prompt_messages():
prompt_template = prompt_transform.get_prompt_template(
app_mode=AppMode.CHAT,
- provider=model_config_mock.provider,
- model=model_config_mock.model,
pre_prompt=pre_prompt,
has_context=True,
query_in_prompt=True,
@@ -306,12 +245,9 @@ def test_get_prompt_dispatches_chat_and_completion():
def test_get_prompt_str_and_rules_type_validation_errors():
transform = SimplePromptTransform()
- model_config = MagicMock(spec=ModelConfigWithCredentialsEntity)
- model_config.provider = "openai"
- model_config.model = "gpt-4"
- valid_prompt_template = SimplePromptTransform().get_prompt_template(
- AppMode.CHAT, "openai", "gpt-4", "", False, False
- )["prompt_template"]
+ valid_prompt_template = SimplePromptTransform().get_prompt_template(AppMode.CHAT, "", False, False)[
+ "prompt_template"
+ ]
bad_custom_keys = {
"prompt_template": valid_prompt_template,
@@ -321,7 +257,7 @@ def test_get_prompt_str_and_rules_type_validation_errors():
}
transform.get_prompt_template = MagicMock(return_value=bad_custom_keys)
with pytest.raises(TypeError, match="custom_variable_keys"):
- transform._get_prompt_str_and_rules(AppMode.CHAT, model_config, "", {}, query=None, context=None)
+ transform._get_prompt_str_and_rules(AppMode.CHAT, "", {}, query=None, context=None)
bad_special_keys = {
**bad_custom_keys,
@@ -330,7 +266,7 @@ def test_get_prompt_str_and_rules_type_validation_errors():
}
transform.get_prompt_template = MagicMock(return_value=bad_special_keys)
with pytest.raises(TypeError, match="special_variable_keys"):
- transform._get_prompt_str_and_rules(AppMode.CHAT, model_config, "", {}, query=None, context=None)
+ transform._get_prompt_str_and_rules(AppMode.CHAT, "", {}, query=None, context=None)
bad_prompt_template = {
**bad_custom_keys,
@@ -340,7 +276,7 @@ def test_get_prompt_str_and_rules_type_validation_errors():
}
transform.get_prompt_template = MagicMock(return_value=bad_prompt_template)
with pytest.raises(TypeError, match="PromptTemplateParser"):
- transform._get_prompt_str_and_rules(AppMode.CHAT, model_config, "", {}, query=None, context=None)
+ transform._get_prompt_str_and_rules(AppMode.CHAT, "", {}, query=None, context=None)
bad_prompt_rules = {
**bad_custom_keys,
@@ -351,7 +287,7 @@ def test_get_prompt_str_and_rules_type_validation_errors():
}
transform.get_prompt_template = MagicMock(return_value=bad_prompt_rules)
with pytest.raises(TypeError, match="prompt_rules"):
- transform._get_prompt_str_and_rules(AppMode.CHAT, model_config, "", {}, query=None, context=None)
+ transform._get_prompt_str_and_rules(AppMode.CHAT, "", {}, query=None, context=None)
def test_chat_model_prompt_messages_uses_prompt_when_query_empty():
@@ -419,23 +355,9 @@ def test_get_last_user_message_with_files_and_context_files():
assert message.content[2].data == "hello"
-def test_prompt_file_name_branches():
- transform = SimplePromptTransform()
-
- assert transform._prompt_file_name(AppMode.CHAT, "openai", "gpt-4") == "common_chat"
- assert transform._prompt_file_name(AppMode.COMPLETION, "openai", "gpt-4") == "common_completion"
- assert transform._prompt_file_name(AppMode.COMPLETION, "baichuan", "Baichuan2") == "baichuan_completion"
- assert transform._prompt_file_name(AppMode.CHAT, "huggingface_hub", "baichuan-13b") == "baichuan_chat"
-
-
def test_advanced_prompt_templates_constants_are_importable():
assert isinstance(CONTEXT, str)
- assert isinstance(BAICHUAN_CONTEXT, str)
assert "completion_prompt_config" in CHAT_APP_COMPLETION_PROMPT_CONFIG
assert "chat_prompt_config" in CHAT_APP_CHAT_PROMPT_CONFIG
assert "chat_prompt_config" in COMPLETION_APP_CHAT_PROMPT_CONFIG
assert "completion_prompt_config" in COMPLETION_APP_COMPLETION_PROMPT_CONFIG
- assert "completion_prompt_config" in BAICHUAN_CHAT_APP_COMPLETION_PROMPT_CONFIG
- assert "chat_prompt_config" in BAICHUAN_CHAT_APP_CHAT_PROMPT_CONFIG
- assert "chat_prompt_config" in BAICHUAN_COMPLETION_APP_CHAT_PROMPT_CONFIG
- assert "completion_prompt_config" in BAICHUAN_COMPLETION_APP_COMPLETION_PROMPT_CONFIG