test_llm.py 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. import json
  2. import os
  3. import time
  4. import uuid
  5. from collections.abc import Generator
  6. from unittest.mock import MagicMock
  7. import pytest
  8. from core.app.entities.app_invoke_entities import InvokeFrom, ModelConfigWithCredentialsEntity
  9. from core.entities.provider_configuration import ProviderConfiguration, ProviderModelBundle
  10. from core.entities.provider_entities import CustomConfiguration, CustomProviderConfiguration, SystemConfiguration
  11. from core.model_manager import ModelInstance
  12. from core.model_runtime.entities.model_entities import ModelType
  13. from core.model_runtime.model_providers import ModelProviderFactory
  14. from core.workflow.entities.variable_pool import VariablePool
  15. from core.workflow.enums import SystemVariableKey
  16. from core.workflow.graph_engine.entities.graph import Graph
  17. from core.workflow.graph_engine.entities.graph_init_params import GraphInitParams
  18. from core.workflow.graph_engine.entities.graph_runtime_state import GraphRuntimeState
  19. from core.workflow.nodes.event import RunCompletedEvent
  20. from core.workflow.nodes.llm.node import LLMNode
  21. from extensions.ext_database import db
  22. from models.enums import UserFrom
  23. from models.provider import ProviderType
  24. from models.workflow import WorkflowNodeExecutionStatus, WorkflowType
  25. """FOR MOCK FIXTURES, DO NOT REMOVE"""
  26. from tests.integration_tests.model_runtime.__mock.openai import setup_openai_mock
  27. from tests.integration_tests.workflow.nodes.__mock.code_executor import setup_code_executor_mock
  28. def init_llm_node(config: dict) -> LLMNode:
  29. graph_config = {
  30. "edges": [
  31. {
  32. "id": "start-source-next-target",
  33. "source": "start",
  34. "target": "llm",
  35. },
  36. ],
  37. "nodes": [{"data": {"type": "start"}, "id": "start"}, config],
  38. }
  39. graph = Graph.init(graph_config=graph_config)
  40. init_params = GraphInitParams(
  41. tenant_id="1",
  42. app_id="1",
  43. workflow_type=WorkflowType.WORKFLOW,
  44. workflow_id="1",
  45. graph_config=graph_config,
  46. user_id="1",
  47. user_from=UserFrom.ACCOUNT,
  48. invoke_from=InvokeFrom.DEBUGGER,
  49. call_depth=0,
  50. )
  51. # construct variable pool
  52. variable_pool = VariablePool(
  53. system_variables={
  54. SystemVariableKey.QUERY: "what's the weather today?",
  55. SystemVariableKey.FILES: [],
  56. SystemVariableKey.CONVERSATION_ID: "abababa",
  57. SystemVariableKey.USER_ID: "aaa",
  58. },
  59. user_inputs={},
  60. environment_variables=[],
  61. conversation_variables=[],
  62. )
  63. variable_pool.add(["abc", "output"], "sunny")
  64. node = LLMNode(
  65. id=str(uuid.uuid4()),
  66. graph_init_params=init_params,
  67. graph=graph,
  68. graph_runtime_state=GraphRuntimeState(variable_pool=variable_pool, start_at=time.perf_counter()),
  69. config=config,
  70. )
  71. return node
  72. @pytest.mark.parametrize("setup_openai_mock", [["chat"]], indirect=True)
  73. def test_execute_llm(setup_openai_mock):
  74. node = init_llm_node(
  75. config={
  76. "id": "llm",
  77. "data": {
  78. "title": "123",
  79. "type": "llm",
  80. "model": {"provider": "openai", "name": "gpt-3.5-turbo", "mode": "chat", "completion_params": {}},
  81. "prompt_template": [
  82. {"role": "system", "text": "you are a helpful assistant.\ntoday's weather is {{#abc.output#}}."},
  83. {"role": "user", "text": "{{#sys.query#}}"},
  84. ],
  85. "memory": None,
  86. "context": {"enabled": False},
  87. "vision": {"enabled": False},
  88. },
  89. },
  90. )
  91. credentials = {"openai_api_key": os.environ.get("OPENAI_API_KEY")}
  92. provider_instance = ModelProviderFactory().get_provider_instance("openai")
  93. model_type_instance = provider_instance.get_model_instance(ModelType.LLM)
  94. provider_model_bundle = ProviderModelBundle(
  95. configuration=ProviderConfiguration(
  96. tenant_id="1",
  97. provider=provider_instance.get_provider_schema(),
  98. preferred_provider_type=ProviderType.CUSTOM,
  99. using_provider_type=ProviderType.CUSTOM,
  100. system_configuration=SystemConfiguration(enabled=False),
  101. custom_configuration=CustomConfiguration(provider=CustomProviderConfiguration(credentials=credentials)),
  102. model_settings=[],
  103. ),
  104. model_type_instance=model_type_instance,
  105. )
  106. model_instance = ModelInstance(provider_model_bundle=provider_model_bundle, model="gpt-3.5-turbo")
  107. model_schema = model_type_instance.get_model_schema("gpt-3.5-turbo")
  108. assert model_schema is not None
  109. model_config = ModelConfigWithCredentialsEntity(
  110. model="gpt-3.5-turbo",
  111. provider="openai",
  112. mode="chat",
  113. credentials=credentials,
  114. parameters={},
  115. model_schema=model_schema,
  116. provider_model_bundle=provider_model_bundle,
  117. )
  118. # Mock db.session.close()
  119. db.session.close = MagicMock()
  120. node._fetch_model_config = MagicMock(return_value=(model_instance, model_config))
  121. # execute node
  122. result = node._run()
  123. assert isinstance(result, Generator)
  124. for item in result:
  125. if isinstance(item, RunCompletedEvent):
  126. assert item.run_result.status == WorkflowNodeExecutionStatus.SUCCEEDED
  127. assert item.run_result.process_data is not None
  128. assert item.run_result.outputs is not None
  129. assert item.run_result.outputs.get("text") is not None
  130. assert item.run_result.outputs.get("usage", {})["total_tokens"] > 0
  131. @pytest.mark.parametrize("setup_code_executor_mock", [["none"]], indirect=True)
  132. @pytest.mark.parametrize("setup_openai_mock", [["chat"]], indirect=True)
  133. def test_execute_llm_with_jinja2(setup_code_executor_mock, setup_openai_mock):
  134. """
  135. Test execute LLM node with jinja2
  136. """
  137. node = init_llm_node(
  138. config={
  139. "id": "llm",
  140. "data": {
  141. "title": "123",
  142. "type": "llm",
  143. "model": {"provider": "openai", "name": "gpt-3.5-turbo", "mode": "chat", "completion_params": {}},
  144. "prompt_config": {
  145. "jinja2_variables": [
  146. {"variable": "sys_query", "value_selector": ["sys", "query"]},
  147. {"variable": "output", "value_selector": ["abc", "output"]},
  148. ]
  149. },
  150. "prompt_template": [
  151. {
  152. "role": "system",
  153. "text": "you are a helpful assistant.\ntoday's weather is {{#abc.output#}}",
  154. "jinja2_text": "you are a helpful assistant.\ntoday's weather is {{output}}.",
  155. "edition_type": "jinja2",
  156. },
  157. {
  158. "role": "user",
  159. "text": "{{#sys.query#}}",
  160. "jinja2_text": "{{sys_query}}",
  161. "edition_type": "basic",
  162. },
  163. ],
  164. "memory": None,
  165. "context": {"enabled": False},
  166. "vision": {"enabled": False},
  167. },
  168. },
  169. )
  170. credentials = {"openai_api_key": os.environ.get("OPENAI_API_KEY")}
  171. provider_instance = ModelProviderFactory().get_provider_instance("openai")
  172. model_type_instance = provider_instance.get_model_instance(ModelType.LLM)
  173. provider_model_bundle = ProviderModelBundle(
  174. configuration=ProviderConfiguration(
  175. tenant_id="1",
  176. provider=provider_instance.get_provider_schema(),
  177. preferred_provider_type=ProviderType.CUSTOM,
  178. using_provider_type=ProviderType.CUSTOM,
  179. system_configuration=SystemConfiguration(enabled=False),
  180. custom_configuration=CustomConfiguration(provider=CustomProviderConfiguration(credentials=credentials)),
  181. model_settings=[],
  182. ),
  183. model_type_instance=model_type_instance,
  184. )
  185. model_instance = ModelInstance(provider_model_bundle=provider_model_bundle, model="gpt-3.5-turbo")
  186. model_schema = model_type_instance.get_model_schema("gpt-3.5-turbo")
  187. assert model_schema is not None
  188. model_config = ModelConfigWithCredentialsEntity(
  189. model="gpt-3.5-turbo",
  190. provider="openai",
  191. mode="chat",
  192. credentials=credentials,
  193. parameters={},
  194. model_schema=model_schema,
  195. provider_model_bundle=provider_model_bundle,
  196. )
  197. # Mock db.session.close()
  198. db.session.close = MagicMock()
  199. node._fetch_model_config = MagicMock(return_value=(model_instance, model_config))
  200. # execute node
  201. result = node._run()
  202. for item in result:
  203. if isinstance(item, RunCompletedEvent):
  204. assert item.run_result.status == WorkflowNodeExecutionStatus.SUCCEEDED
  205. assert item.run_result.process_data is not None
  206. assert "sunny" in json.dumps(item.run_result.process_data)
  207. assert "what's the weather today?" in json.dumps(item.run_result.process_data)