|
|
|
@ -8,8 +8,6 @@ from unittest.mock import MagicMock, patch
|
|
|
|
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
|
|
|
from app_factory import create_app
|
|
|
|
|
|
|
|
from configs import dify_config
|
|
|
|
|
|
|
|
from core.app.entities.app_invoke_entities import InvokeFrom
|
|
|
|
from core.app.entities.app_invoke_entities import InvokeFrom
|
|
|
|
from core.model_runtime.entities.llm_entities import LLMResult, LLMUsage
|
|
|
|
from core.model_runtime.entities.llm_entities import LLMResult, LLMUsage
|
|
|
|
from core.model_runtime.entities.message_entities import AssistantPromptMessage
|
|
|
|
from core.model_runtime.entities.message_entities import AssistantPromptMessage
|
|
|
|
@ -30,21 +28,6 @@ from tests.integration_tests.model_runtime.__mock.plugin_daemon import setup_mod
|
|
|
|
from tests.integration_tests.workflow.nodes.__mock.code_executor import setup_code_executor_mock
|
|
|
|
from tests.integration_tests.workflow.nodes.__mock.code_executor import setup_code_executor_mock
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
|
|
|
|
|
|
def app():
|
|
|
|
|
|
|
|
# Set up storage configuration
|
|
|
|
|
|
|
|
os.environ["STORAGE_TYPE"] = "opendal"
|
|
|
|
|
|
|
|
os.environ["OPENDAL_SCHEME"] = "fs"
|
|
|
|
|
|
|
|
os.environ["OPENDAL_FS_ROOT"] = "storage"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Ensure storage directory exists
|
|
|
|
|
|
|
|
os.makedirs("storage", exist_ok=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app = create_app()
|
|
|
|
|
|
|
|
dify_config.LOGIN_DISABLED = True
|
|
|
|
|
|
|
|
return app
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def init_llm_node(config: dict) -> LLMNode:
|
|
|
|
def init_llm_node(config: dict) -> LLMNode:
|
|
|
|
graph_config = {
|
|
|
|
graph_config = {
|
|
|
|
"edges": [
|
|
|
|
"edges": [
|
|
|
|
@ -102,197 +85,195 @@ def init_llm_node(config: dict) -> LLMNode:
|
|
|
|
return node
|
|
|
|
return node
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_execute_llm(app):
|
|
|
|
def test_execute_llm(flask_req_ctx):
|
|
|
|
with app.app_context():
|
|
|
|
node = init_llm_node(
|
|
|
|
node = init_llm_node(
|
|
|
|
config={
|
|
|
|
config={
|
|
|
|
"id": "llm",
|
|
|
|
"id": "llm",
|
|
|
|
"data": {
|
|
|
|
"data": {
|
|
|
|
"title": "123",
|
|
|
|
"title": "123",
|
|
|
|
"type": "llm",
|
|
|
|
"type": "llm",
|
|
|
|
"model": {
|
|
|
|
"model": {
|
|
|
|
"provider": "langgenius/openai/openai",
|
|
|
|
"provider": "langgenius/openai/openai",
|
|
|
|
"name": "gpt-3.5-turbo",
|
|
|
|
"name": "gpt-3.5-turbo",
|
|
|
|
"mode": "chat",
|
|
|
|
"mode": "chat",
|
|
|
|
"completion_params": {},
|
|
|
|
"completion_params": {},
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
"prompt_template": [
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
"role": "system",
|
|
|
|
|
|
|
|
"text": "you are a helpful assistant.\ntoday's weather is {{#abc.output#}}.",
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
{"role": "user", "text": "{{#sys.query#}}"},
|
|
|
|
|
|
|
|
],
|
|
|
|
|
|
|
|
"memory": None,
|
|
|
|
|
|
|
|
"context": {"enabled": False},
|
|
|
|
|
|
|
|
"vision": {"enabled": False},
|
|
|
|
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
"prompt_template": [
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
"role": "system",
|
|
|
|
|
|
|
|
"text": "you are a helpful assistant.\ntoday's weather is {{#abc.output#}}.",
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
{"role": "user", "text": "{{#sys.query#}}"},
|
|
|
|
|
|
|
|
],
|
|
|
|
|
|
|
|
"memory": None,
|
|
|
|
|
|
|
|
"context": {"enabled": False},
|
|
|
|
|
|
|
|
"vision": {"enabled": False},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
)
|
|
|
|
credentials = {"openai_api_key": os.environ.get("OPENAI_API_KEY")}
|
|
|
|
|
|
|
|
|
|
|
|
credentials = {"openai_api_key": os.environ.get("OPENAI_API_KEY")}
|
|
|
|
# Create a proper LLM result with real entities
|
|
|
|
|
|
|
|
mock_usage = LLMUsage(
|
|
|
|
# Create a proper LLM result with real entities
|
|
|
|
prompt_tokens=30,
|
|
|
|
mock_usage = LLMUsage(
|
|
|
|
prompt_unit_price=Decimal("0.001"),
|
|
|
|
prompt_tokens=30,
|
|
|
|
prompt_price_unit=Decimal("1000"),
|
|
|
|
prompt_unit_price=Decimal("0.001"),
|
|
|
|
prompt_price=Decimal("0.00003"),
|
|
|
|
prompt_price_unit=Decimal("1000"),
|
|
|
|
completion_tokens=20,
|
|
|
|
prompt_price=Decimal("0.00003"),
|
|
|
|
completion_unit_price=Decimal("0.002"),
|
|
|
|
completion_tokens=20,
|
|
|
|
completion_price_unit=Decimal("1000"),
|
|
|
|
completion_unit_price=Decimal("0.002"),
|
|
|
|
completion_price=Decimal("0.00004"),
|
|
|
|
completion_price_unit=Decimal("1000"),
|
|
|
|
total_tokens=50,
|
|
|
|
completion_price=Decimal("0.00004"),
|
|
|
|
total_price=Decimal("0.00007"),
|
|
|
|
total_tokens=50,
|
|
|
|
currency="USD",
|
|
|
|
total_price=Decimal("0.00007"),
|
|
|
|
latency=0.5,
|
|
|
|
currency="USD",
|
|
|
|
)
|
|
|
|
latency=0.5,
|
|
|
|
|
|
|
|
)
|
|
|
|
mock_message = AssistantPromptMessage(content="This is a test response from the mocked LLM.")
|
|
|
|
|
|
|
|
|
|
|
|
mock_message = AssistantPromptMessage(content="This is a test response from the mocked LLM.")
|
|
|
|
mock_llm_result = LLMResult(
|
|
|
|
|
|
|
|
model="gpt-3.5-turbo",
|
|
|
|
mock_llm_result = LLMResult(
|
|
|
|
prompt_messages=[],
|
|
|
|
model="gpt-3.5-turbo",
|
|
|
|
message=mock_message,
|
|
|
|
prompt_messages=[],
|
|
|
|
usage=mock_usage,
|
|
|
|
message=mock_message,
|
|
|
|
)
|
|
|
|
usage=mock_usage,
|
|
|
|
|
|
|
|
)
|
|
|
|
# Create a simple mock model instance that doesn't call real providers
|
|
|
|
|
|
|
|
mock_model_instance = MagicMock()
|
|
|
|
# Create a simple mock model instance that doesn't call real providers
|
|
|
|
mock_model_instance.invoke_llm.return_value = mock_llm_result
|
|
|
|
mock_model_instance = MagicMock()
|
|
|
|
|
|
|
|
mock_model_instance.invoke_llm.return_value = mock_llm_result
|
|
|
|
# Create a simple mock model config with required attributes
|
|
|
|
|
|
|
|
mock_model_config = MagicMock()
|
|
|
|
# Create a simple mock model config with required attributes
|
|
|
|
mock_model_config.mode = "chat"
|
|
|
|
mock_model_config = MagicMock()
|
|
|
|
mock_model_config.provider = "langgenius/openai/openai"
|
|
|
|
mock_model_config.mode = "chat"
|
|
|
|
mock_model_config.model = "gpt-3.5-turbo"
|
|
|
|
mock_model_config.provider = "langgenius/openai/openai"
|
|
|
|
mock_model_config.provider_model_bundle.configuration.tenant_id = "9d2074fc-6f86-45a9-b09d-6ecc63b9056b"
|
|
|
|
mock_model_config.model = "gpt-3.5-turbo"
|
|
|
|
|
|
|
|
mock_model_config.provider_model_bundle.configuration.tenant_id = "9d2074fc-6f86-45a9-b09d-6ecc63b9056b"
|
|
|
|
# Mock the _fetch_model_config method
|
|
|
|
|
|
|
|
def mock_fetch_model_config_func(_node_data_model):
|
|
|
|
# Mock the _fetch_model_config method
|
|
|
|
return mock_model_instance, mock_model_config
|
|
|
|
def mock_fetch_model_config_func(_node_data_model):
|
|
|
|
|
|
|
|
return mock_model_instance, mock_model_config
|
|
|
|
# Also mock ModelManager.get_model_instance to avoid database calls
|
|
|
|
|
|
|
|
def mock_get_model_instance(_self, **kwargs):
|
|
|
|
# Also mock ModelManager.get_model_instance to avoid database calls
|
|
|
|
return mock_model_instance
|
|
|
|
def mock_get_model_instance(_self, **kwargs):
|
|
|
|
|
|
|
|
return mock_model_instance
|
|
|
|
with (
|
|
|
|
|
|
|
|
patch.object(node, "_fetch_model_config", mock_fetch_model_config_func),
|
|
|
|
with (
|
|
|
|
patch("core.model_manager.ModelManager.get_model_instance", mock_get_model_instance),
|
|
|
|
patch.object(node, "_fetch_model_config", mock_fetch_model_config_func),
|
|
|
|
):
|
|
|
|
patch("core.model_manager.ModelManager.get_model_instance", mock_get_model_instance),
|
|
|
|
# execute node
|
|
|
|
):
|
|
|
|
result = node._run()
|
|
|
|
# execute node
|
|
|
|
assert isinstance(result, Generator)
|
|
|
|
result = node._run()
|
|
|
|
|
|
|
|
assert isinstance(result, Generator)
|
|
|
|
for item in result:
|
|
|
|
|
|
|
|
if isinstance(item, RunCompletedEvent):
|
|
|
|
for item in result:
|
|
|
|
assert item.run_result.status == WorkflowNodeExecutionStatus.SUCCEEDED
|
|
|
|
if isinstance(item, RunCompletedEvent):
|
|
|
|
assert item.run_result.process_data is not None
|
|
|
|
assert item.run_result.status == WorkflowNodeExecutionStatus.SUCCEEDED
|
|
|
|
assert item.run_result.outputs is not None
|
|
|
|
assert item.run_result.process_data is not None
|
|
|
|
assert item.run_result.outputs.get("text") is not None
|
|
|
|
assert item.run_result.outputs is not None
|
|
|
|
assert item.run_result.outputs.get("usage", {})["total_tokens"] > 0
|
|
|
|
assert item.run_result.outputs.get("text") is not None
|
|
|
|
|
|
|
|
assert item.run_result.outputs.get("usage", {})["total_tokens"] > 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("setup_code_executor_mock", [["none"]], indirect=True)
|
|
|
|
@pytest.mark.parametrize("setup_code_executor_mock", [["none"]], indirect=True)
|
|
|
|
def test_execute_llm_with_jinja2(app, setup_code_executor_mock):
|
|
|
|
def test_execute_llm_with_jinja2(flask_req_ctx, setup_code_executor_mock):
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
Test execute LLM node with jinja2
|
|
|
|
Test execute LLM node with jinja2
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
with app.app_context():
|
|
|
|
node = init_llm_node(
|
|
|
|
node = init_llm_node(
|
|
|
|
config={
|
|
|
|
config={
|
|
|
|
"id": "llm",
|
|
|
|
"id": "llm",
|
|
|
|
"data": {
|
|
|
|
"data": {
|
|
|
|
"title": "123",
|
|
|
|
"title": "123",
|
|
|
|
"type": "llm",
|
|
|
|
"type": "llm",
|
|
|
|
"model": {"provider": "openai", "name": "gpt-3.5-turbo", "mode": "chat", "completion_params": {}},
|
|
|
|
"model": {"provider": "openai", "name": "gpt-3.5-turbo", "mode": "chat", "completion_params": {}},
|
|
|
|
"prompt_config": {
|
|
|
|
"prompt_config": {
|
|
|
|
"jinja2_variables": [
|
|
|
|
"jinja2_variables": [
|
|
|
|
{"variable": "sys_query", "value_selector": ["sys", "query"]},
|
|
|
|
{"variable": "sys_query", "value_selector": ["sys", "query"]},
|
|
|
|
{"variable": "output", "value_selector": ["abc", "output"]},
|
|
|
|
{"variable": "output", "value_selector": ["abc", "output"]},
|
|
|
|
]
|
|
|
|
]
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
"prompt_template": [
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
"role": "system",
|
|
|
|
|
|
|
|
"text": "you are a helpful assistant.\ntoday's weather is {{#abc.output#}}",
|
|
|
|
|
|
|
|
"jinja2_text": "you are a helpful assistant.\ntoday's weather is {{output}}.",
|
|
|
|
|
|
|
|
"edition_type": "jinja2",
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
"role": "user",
|
|
|
|
|
|
|
|
"text": "{{#sys.query#}}",
|
|
|
|
|
|
|
|
"jinja2_text": "{{sys_query}}",
|
|
|
|
|
|
|
|
"edition_type": "basic",
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
],
|
|
|
|
|
|
|
|
"memory": None,
|
|
|
|
|
|
|
|
"context": {"enabled": False},
|
|
|
|
|
|
|
|
"vision": {"enabled": False},
|
|
|
|
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
"prompt_template": [
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
"role": "system",
|
|
|
|
|
|
|
|
"text": "you are a helpful assistant.\ntoday's weather is {{#abc.output#}}",
|
|
|
|
|
|
|
|
"jinja2_text": "you are a helpful assistant.\ntoday's weather is {{output}}.",
|
|
|
|
|
|
|
|
"edition_type": "jinja2",
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
"role": "user",
|
|
|
|
|
|
|
|
"text": "{{#sys.query#}}",
|
|
|
|
|
|
|
|
"jinja2_text": "{{sys_query}}",
|
|
|
|
|
|
|
|
"edition_type": "basic",
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
],
|
|
|
|
|
|
|
|
"memory": None,
|
|
|
|
|
|
|
|
"context": {"enabled": False},
|
|
|
|
|
|
|
|
"vision": {"enabled": False},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
)
|
|
|
|
# Mock db.session.close()
|
|
|
|
|
|
|
|
db.session.close = MagicMock()
|
|
|
|
# Mock db.session.close()
|
|
|
|
|
|
|
|
db.session.close = MagicMock()
|
|
|
|
# Create a proper LLM result with real entities
|
|
|
|
|
|
|
|
mock_usage = LLMUsage(
|
|
|
|
# Create a proper LLM result with real entities
|
|
|
|
prompt_tokens=30,
|
|
|
|
mock_usage = LLMUsage(
|
|
|
|
prompt_unit_price=Decimal("0.001"),
|
|
|
|
prompt_tokens=30,
|
|
|
|
prompt_price_unit=Decimal("1000"),
|
|
|
|
prompt_unit_price=Decimal("0.001"),
|
|
|
|
prompt_price=Decimal("0.00003"),
|
|
|
|
prompt_price_unit=Decimal("1000"),
|
|
|
|
completion_tokens=20,
|
|
|
|
prompt_price=Decimal("0.00003"),
|
|
|
|
completion_unit_price=Decimal("0.002"),
|
|
|
|
completion_tokens=20,
|
|
|
|
completion_price_unit=Decimal("1000"),
|
|
|
|
completion_unit_price=Decimal("0.002"),
|
|
|
|
completion_price=Decimal("0.00004"),
|
|
|
|
completion_price_unit=Decimal("1000"),
|
|
|
|
total_tokens=50,
|
|
|
|
completion_price=Decimal("0.00004"),
|
|
|
|
total_price=Decimal("0.00007"),
|
|
|
|
total_tokens=50,
|
|
|
|
currency="USD",
|
|
|
|
total_price=Decimal("0.00007"),
|
|
|
|
latency=0.5,
|
|
|
|
currency="USD",
|
|
|
|
)
|
|
|
|
latency=0.5,
|
|
|
|
|
|
|
|
)
|
|
|
|
mock_message = AssistantPromptMessage(content="Test response: sunny weather and what's the weather today?")
|
|
|
|
|
|
|
|
|
|
|
|
mock_message = AssistantPromptMessage(content="Test response: sunny weather and what's the weather today?")
|
|
|
|
mock_llm_result = LLMResult(
|
|
|
|
|
|
|
|
model="gpt-3.5-turbo",
|
|
|
|
mock_llm_result = LLMResult(
|
|
|
|
prompt_messages=[],
|
|
|
|
model="gpt-3.5-turbo",
|
|
|
|
message=mock_message,
|
|
|
|
prompt_messages=[],
|
|
|
|
usage=mock_usage,
|
|
|
|
message=mock_message,
|
|
|
|
)
|
|
|
|
usage=mock_usage,
|
|
|
|
|
|
|
|
)
|
|
|
|
# Create a simple mock model instance that doesn't call real providers
|
|
|
|
|
|
|
|
mock_model_instance = MagicMock()
|
|
|
|
# Create a simple mock model instance that doesn't call real providers
|
|
|
|
mock_model_instance.invoke_llm.return_value = mock_llm_result
|
|
|
|
mock_model_instance = MagicMock()
|
|
|
|
|
|
|
|
mock_model_instance.invoke_llm.return_value = mock_llm_result
|
|
|
|
# Create a simple mock model config with required attributes
|
|
|
|
|
|
|
|
mock_model_config = MagicMock()
|
|
|
|
# Create a simple mock model config with required attributes
|
|
|
|
mock_model_config.mode = "chat"
|
|
|
|
mock_model_config = MagicMock()
|
|
|
|
mock_model_config.provider = "openai"
|
|
|
|
mock_model_config.mode = "chat"
|
|
|
|
mock_model_config.model = "gpt-3.5-turbo"
|
|
|
|
mock_model_config.provider = "openai"
|
|
|
|
mock_model_config.provider_model_bundle.configuration.tenant_id = "9d2074fc-6f86-45a9-b09d-6ecc63b9056b"
|
|
|
|
mock_model_config.model = "gpt-3.5-turbo"
|
|
|
|
|
|
|
|
mock_model_config.provider_model_bundle.configuration.tenant_id = "9d2074fc-6f86-45a9-b09d-6ecc63b9056b"
|
|
|
|
# Mock the _fetch_model_config method
|
|
|
|
|
|
|
|
def mock_fetch_model_config_func(_node_data_model):
|
|
|
|
# Mock the _fetch_model_config method
|
|
|
|
return mock_model_instance, mock_model_config
|
|
|
|
def mock_fetch_model_config_func(_node_data_model):
|
|
|
|
|
|
|
|
return mock_model_instance, mock_model_config
|
|
|
|
# Also mock ModelManager.get_model_instance to avoid database calls
|
|
|
|
|
|
|
|
def mock_get_model_instance(_self, **kwargs):
|
|
|
|
# Also mock ModelManager.get_model_instance to avoid database calls
|
|
|
|
return mock_model_instance
|
|
|
|
def mock_get_model_instance(_self, **kwargs):
|
|
|
|
|
|
|
|
return mock_model_instance
|
|
|
|
with (
|
|
|
|
|
|
|
|
patch.object(node, "_fetch_model_config", mock_fetch_model_config_func),
|
|
|
|
with (
|
|
|
|
patch("core.model_manager.ModelManager.get_model_instance", mock_get_model_instance),
|
|
|
|
patch.object(node, "_fetch_model_config", mock_fetch_model_config_func),
|
|
|
|
):
|
|
|
|
patch("core.model_manager.ModelManager.get_model_instance", mock_get_model_instance),
|
|
|
|
# execute node
|
|
|
|
):
|
|
|
|
result = node._run()
|
|
|
|
# execute node
|
|
|
|
|
|
|
|
result = node._run()
|
|
|
|
for item in result:
|
|
|
|
|
|
|
|
if isinstance(item, RunCompletedEvent):
|
|
|
|
for item in result:
|
|
|
|
assert item.run_result.status == WorkflowNodeExecutionStatus.SUCCEEDED
|
|
|
|
if isinstance(item, RunCompletedEvent):
|
|
|
|
assert item.run_result.process_data is not None
|
|
|
|
assert item.run_result.status == WorkflowNodeExecutionStatus.SUCCEEDED
|
|
|
|
assert "sunny" in json.dumps(item.run_result.process_data)
|
|
|
|
assert item.run_result.process_data is not None
|
|
|
|
assert "what's the weather today?" in json.dumps(item.run_result.process_data)
|
|
|
|
assert "sunny" in json.dumps(item.run_result.process_data)
|
|
|
|
|
|
|
|
assert "what's the weather today?" in json.dumps(item.run_result.process_data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_extract_json():
|
|
|
|
def test_extract_json():
|
|
|
|
|