Co-authored-by: -LAN- <laipz8200@outlook.com>
This commit is contained in:
@@ -7,25 +7,22 @@ from jinja2 import Template
|
||||
|
||||
from core.helper.code_executor.code_executor import CodeExecutor, CodeLanguage
|
||||
|
||||
MOCK = os.getenv('MOCK_SWITCH', 'false') == 'true'
|
||||
MOCK = os.getenv("MOCK_SWITCH", "false") == "true"
|
||||
|
||||
|
||||
class MockedCodeExecutor:
|
||||
@classmethod
|
||||
def invoke(cls, language: Literal['python3', 'javascript', 'jinja2'],
|
||||
code: str, inputs: dict) -> dict:
|
||||
def invoke(cls, language: Literal["python3", "javascript", "jinja2"], code: str, inputs: dict) -> dict:
|
||||
# invoke directly
|
||||
match language:
|
||||
case CodeLanguage.PYTHON3:
|
||||
return {
|
||||
"result": 3
|
||||
}
|
||||
return {"result": 3}
|
||||
case CodeLanguage.JINJA2:
|
||||
return {
|
||||
"result": Template(code).render(inputs)
|
||||
}
|
||||
return {"result": Template(code).render(inputs)}
|
||||
case _:
|
||||
raise Exception("Language not supported")
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def setup_code_executor_mock(request, monkeypatch: MonkeyPatch):
|
||||
if not MOCK:
|
||||
|
||||
@@ -6,38 +6,32 @@ import httpx
|
||||
import pytest
|
||||
from _pytest.monkeypatch import MonkeyPatch
|
||||
|
||||
MOCK = os.getenv('MOCK_SWITCH', 'false') == 'true'
|
||||
MOCK = os.getenv("MOCK_SWITCH", "false") == "true"
|
||||
|
||||
|
||||
class MockedHttp:
|
||||
def httpx_request(method: Literal['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD'],
|
||||
url: str, **kwargs) -> httpx.Response:
|
||||
def httpx_request(
|
||||
method: Literal["GET", "POST", "PUT", "DELETE", "PATCH", "HEAD"], url: str, **kwargs
|
||||
) -> httpx.Response:
|
||||
"""
|
||||
Mocked httpx.request
|
||||
"""
|
||||
if url == 'http://404.com':
|
||||
response = httpx.Response(
|
||||
status_code=404,
|
||||
request=httpx.Request(method, url),
|
||||
content=b'Not Found'
|
||||
)
|
||||
if url == "http://404.com":
|
||||
response = httpx.Response(status_code=404, request=httpx.Request(method, url), content=b"Not Found")
|
||||
return response
|
||||
|
||||
# get data, files
|
||||
data = kwargs.get('data', None)
|
||||
files = kwargs.get('files', None)
|
||||
data = kwargs.get("data", None)
|
||||
files = kwargs.get("files", None)
|
||||
if data is not None:
|
||||
resp = dumps(data).encode('utf-8')
|
||||
resp = dumps(data).encode("utf-8")
|
||||
elif files is not None:
|
||||
resp = dumps(files).encode('utf-8')
|
||||
resp = dumps(files).encode("utf-8")
|
||||
else:
|
||||
resp = b'OK'
|
||||
resp = b"OK"
|
||||
|
||||
response = httpx.Response(
|
||||
status_code=200,
|
||||
request=httpx.Request(method, url),
|
||||
headers=kwargs.get('headers', {}),
|
||||
content=resp
|
||||
status_code=200, request=httpx.Request(method, url), headers=kwargs.get("headers", {}), content=resp
|
||||
)
|
||||
return response
|
||||
|
||||
|
||||
@@ -2,10 +2,10 @@ import pytest
|
||||
|
||||
from core.helper.code_executor.code_executor import CodeExecutionException, CodeExecutor
|
||||
|
||||
CODE_LANGUAGE = 'unsupported_language'
|
||||
CODE_LANGUAGE = "unsupported_language"
|
||||
|
||||
|
||||
def test_unsupported_with_code_template():
|
||||
with pytest.raises(CodeExecutionException) as e:
|
||||
CodeExecutor.execute_workflow_code_template(language=CODE_LANGUAGE, code='', inputs={})
|
||||
assert str(e.value) == f'Unsupported language {CODE_LANGUAGE}'
|
||||
CodeExecutor.execute_workflow_code_template(language=CODE_LANGUAGE, code="", inputs={})
|
||||
assert str(e.value) == f"Unsupported language {CODE_LANGUAGE}"
|
||||
|
||||
@@ -9,8 +9,8 @@ CODE_LANGUAGE = CodeLanguage.JAVASCRIPT
|
||||
|
||||
def test_javascript_plain():
|
||||
code = 'console.log("Hello World")'
|
||||
result_message = CodeExecutor.execute_code(language=CODE_LANGUAGE, preload='', code=code)
|
||||
assert result_message == 'Hello World\n'
|
||||
result_message = CodeExecutor.execute_code(language=CODE_LANGUAGE, preload="", code=code)
|
||||
assert result_message == "Hello World\n"
|
||||
|
||||
|
||||
def test_javascript_json():
|
||||
@@ -18,15 +18,18 @@ def test_javascript_json():
|
||||
obj = {'Hello': 'World'}
|
||||
console.log(JSON.stringify(obj))
|
||||
""")
|
||||
result = CodeExecutor.execute_code(language=CODE_LANGUAGE, preload='', code=code)
|
||||
result = CodeExecutor.execute_code(language=CODE_LANGUAGE, preload="", code=code)
|
||||
assert result == '{"Hello":"World"}\n'
|
||||
|
||||
|
||||
def test_javascript_with_code_template():
|
||||
result = CodeExecutor.execute_workflow_code_template(
|
||||
language=CODE_LANGUAGE, code=JavascriptCodeProvider.get_default_code(),
|
||||
inputs={'arg1': 'Hello', 'arg2': 'World'})
|
||||
assert result == {'result': 'HelloWorld'}
|
||||
language=CODE_LANGUAGE,
|
||||
code=JavascriptCodeProvider.get_default_code(),
|
||||
inputs={"arg1": "Hello", "arg2": "World"},
|
||||
)
|
||||
assert result == {"result": "HelloWorld"}
|
||||
|
||||
|
||||
def test_javascript_get_runner_script():
|
||||
runner_script = NodeJsTemplateTransformer.get_runner_script()
|
||||
|
||||
@@ -7,21 +7,24 @@ CODE_LANGUAGE = CodeLanguage.JINJA2
|
||||
|
||||
|
||||
def test_jinja2():
|
||||
template = 'Hello {{template}}'
|
||||
inputs = base64.b64encode(b'{"template": "World"}').decode('utf-8')
|
||||
code = (Jinja2TemplateTransformer.get_runner_script()
|
||||
.replace(Jinja2TemplateTransformer._code_placeholder, template)
|
||||
.replace(Jinja2TemplateTransformer._inputs_placeholder, inputs))
|
||||
result = CodeExecutor.execute_code(language=CODE_LANGUAGE,
|
||||
preload=Jinja2TemplateTransformer.get_preload_script(),
|
||||
code=code)
|
||||
assert result == '<<RESULT>>Hello World<<RESULT>>\n'
|
||||
template = "Hello {{template}}"
|
||||
inputs = base64.b64encode(b'{"template": "World"}').decode("utf-8")
|
||||
code = (
|
||||
Jinja2TemplateTransformer.get_runner_script()
|
||||
.replace(Jinja2TemplateTransformer._code_placeholder, template)
|
||||
.replace(Jinja2TemplateTransformer._inputs_placeholder, inputs)
|
||||
)
|
||||
result = CodeExecutor.execute_code(
|
||||
language=CODE_LANGUAGE, preload=Jinja2TemplateTransformer.get_preload_script(), code=code
|
||||
)
|
||||
assert result == "<<RESULT>>Hello World<<RESULT>>\n"
|
||||
|
||||
|
||||
def test_jinja2_with_code_template():
|
||||
result = CodeExecutor.execute_workflow_code_template(
|
||||
language=CODE_LANGUAGE, code='Hello {{template}}', inputs={'template': 'World'})
|
||||
assert result == {'result': 'Hello World'}
|
||||
language=CODE_LANGUAGE, code="Hello {{template}}", inputs={"template": "World"}
|
||||
)
|
||||
assert result == {"result": "Hello World"}
|
||||
|
||||
|
||||
def test_jinja2_get_runner_script():
|
||||
|
||||
@@ -10,8 +10,8 @@ CODE_LANGUAGE = CodeLanguage.PYTHON3
|
||||
|
||||
def test_python3_plain():
|
||||
code = 'print("Hello World")'
|
||||
result = CodeExecutor.execute_code(language=CODE_LANGUAGE, preload='', code=code)
|
||||
assert result == 'Hello World\n'
|
||||
result = CodeExecutor.execute_code(language=CODE_LANGUAGE, preload="", code=code)
|
||||
assert result == "Hello World\n"
|
||||
|
||||
|
||||
def test_python3_json():
|
||||
@@ -19,14 +19,15 @@ def test_python3_json():
|
||||
import json
|
||||
print(json.dumps({'Hello': 'World'}))
|
||||
""")
|
||||
result = CodeExecutor.execute_code(language=CODE_LANGUAGE, preload='', code=code)
|
||||
result = CodeExecutor.execute_code(language=CODE_LANGUAGE, preload="", code=code)
|
||||
assert result == '{"Hello": "World"}\n'
|
||||
|
||||
|
||||
def test_python3_with_code_template():
|
||||
result = CodeExecutor.execute_workflow_code_template(
|
||||
language=CODE_LANGUAGE, code=Python3CodeProvider.get_default_code(), inputs={'arg1': 'Hello', 'arg2': 'World'})
|
||||
assert result == {'result': 'HelloWorld'}
|
||||
language=CODE_LANGUAGE, code=Python3CodeProvider.get_default_code(), inputs={"arg1": "Hello", "arg2": "World"}
|
||||
)
|
||||
assert result == {"result": "HelloWorld"}
|
||||
|
||||
|
||||
def test_python3_get_runner_script():
|
||||
|
||||
@@ -9,137 +9,134 @@ from core.workflow.nodes.code.code_node import CodeNode
|
||||
from models.workflow import WorkflowNodeExecutionStatus
|
||||
from tests.integration_tests.workflow.nodes.__mock.code_executor import setup_code_executor_mock
|
||||
|
||||
CODE_MAX_STRING_LENGTH = int(getenv('CODE_MAX_STRING_LENGTH', '10000'))
|
||||
CODE_MAX_STRING_LENGTH = int(getenv("CODE_MAX_STRING_LENGTH", "10000"))
|
||||
|
||||
@pytest.mark.parametrize('setup_code_executor_mock', [['none']], indirect=True)
|
||||
|
||||
@pytest.mark.parametrize("setup_code_executor_mock", [["none"]], indirect=True)
|
||||
def test_execute_code(setup_code_executor_mock):
|
||||
code = '''
|
||||
code = """
|
||||
def main(args1: int, args2: int) -> dict:
|
||||
return {
|
||||
"result": args1 + args2,
|
||||
}
|
||||
'''
|
||||
"""
|
||||
# trim first 4 spaces at the beginning of each line
|
||||
code = '\n'.join([line[4:] for line in code.split('\n')])
|
||||
code = "\n".join([line[4:] for line in code.split("\n")])
|
||||
node = CodeNode(
|
||||
tenant_id='1',
|
||||
app_id='1',
|
||||
workflow_id='1',
|
||||
user_id='1',
|
||||
tenant_id="1",
|
||||
app_id="1",
|
||||
workflow_id="1",
|
||||
user_id="1",
|
||||
user_from=UserFrom.ACCOUNT,
|
||||
invoke_from=InvokeFrom.WEB_APP,
|
||||
config={
|
||||
'id': '1',
|
||||
'data': {
|
||||
'outputs': {
|
||||
'result': {
|
||||
'type': 'number',
|
||||
"id": "1",
|
||||
"data": {
|
||||
"outputs": {
|
||||
"result": {
|
||||
"type": "number",
|
||||
},
|
||||
},
|
||||
'title': '123',
|
||||
'variables': [
|
||||
"title": "123",
|
||||
"variables": [
|
||||
{
|
||||
'variable': 'args1',
|
||||
'value_selector': ['1', '123', 'args1'],
|
||||
"variable": "args1",
|
||||
"value_selector": ["1", "123", "args1"],
|
||||
},
|
||||
{
|
||||
'variable': 'args2',
|
||||
'value_selector': ['1', '123', 'args2']
|
||||
}
|
||||
{"variable": "args2", "value_selector": ["1", "123", "args2"]},
|
||||
],
|
||||
'answer': '123',
|
||||
'code_language': 'python3',
|
||||
'code': code
|
||||
}
|
||||
}
|
||||
"answer": "123",
|
||||
"code_language": "python3",
|
||||
"code": code,
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
# construct variable pool
|
||||
pool = VariablePool(system_variables={}, user_inputs={}, environment_variables=[])
|
||||
pool.add(['1', '123', 'args1'], 1)
|
||||
pool.add(['1', '123', 'args2'], 2)
|
||||
|
||||
pool.add(["1", "123", "args1"], 1)
|
||||
pool.add(["1", "123", "args2"], 2)
|
||||
|
||||
# execute node
|
||||
result = node.run(pool)
|
||||
assert result.status == WorkflowNodeExecutionStatus.SUCCEEDED
|
||||
assert result.outputs['result'] == 3
|
||||
assert result.outputs["result"] == 3
|
||||
assert result.error is None
|
||||
|
||||
@pytest.mark.parametrize('setup_code_executor_mock', [['none']], indirect=True)
|
||||
|
||||
@pytest.mark.parametrize("setup_code_executor_mock", [["none"]], indirect=True)
|
||||
def test_execute_code_output_validator(setup_code_executor_mock):
|
||||
code = '''
|
||||
code = """
|
||||
def main(args1: int, args2: int) -> dict:
|
||||
return {
|
||||
"result": args1 + args2,
|
||||
}
|
||||
'''
|
||||
"""
|
||||
# trim first 4 spaces at the beginning of each line
|
||||
code = '\n'.join([line[4:] for line in code.split('\n')])
|
||||
code = "\n".join([line[4:] for line in code.split("\n")])
|
||||
node = CodeNode(
|
||||
tenant_id='1',
|
||||
app_id='1',
|
||||
workflow_id='1',
|
||||
user_id='1',
|
||||
tenant_id="1",
|
||||
app_id="1",
|
||||
workflow_id="1",
|
||||
user_id="1",
|
||||
user_from=UserFrom.ACCOUNT,
|
||||
invoke_from=InvokeFrom.WEB_APP,
|
||||
config={
|
||||
'id': '1',
|
||||
'data': {
|
||||
"id": "1",
|
||||
"data": {
|
||||
"outputs": {
|
||||
"result": {
|
||||
"type": "string",
|
||||
},
|
||||
},
|
||||
'title': '123',
|
||||
'variables': [
|
||||
"title": "123",
|
||||
"variables": [
|
||||
{
|
||||
'variable': 'args1',
|
||||
'value_selector': ['1', '123', 'args1'],
|
||||
"variable": "args1",
|
||||
"value_selector": ["1", "123", "args1"],
|
||||
},
|
||||
{
|
||||
'variable': 'args2',
|
||||
'value_selector': ['1', '123', 'args2']
|
||||
}
|
||||
{"variable": "args2", "value_selector": ["1", "123", "args2"]},
|
||||
],
|
||||
'answer': '123',
|
||||
'code_language': 'python3',
|
||||
'code': code
|
||||
}
|
||||
}
|
||||
"answer": "123",
|
||||
"code_language": "python3",
|
||||
"code": code,
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
# construct variable pool
|
||||
pool = VariablePool(system_variables={}, user_inputs={}, environment_variables=[])
|
||||
pool.add(['1', '123', 'args1'], 1)
|
||||
pool.add(['1', '123', 'args2'], 2)
|
||||
|
||||
pool.add(["1", "123", "args1"], 1)
|
||||
pool.add(["1", "123", "args2"], 2)
|
||||
|
||||
# execute node
|
||||
result = node.run(pool)
|
||||
|
||||
assert result.status == WorkflowNodeExecutionStatus.FAILED
|
||||
assert result.error == 'Output variable `result` must be a string'
|
||||
assert result.error == "Output variable `result` must be a string"
|
||||
|
||||
|
||||
def test_execute_code_output_validator_depth():
|
||||
code = '''
|
||||
code = """
|
||||
def main(args1: int, args2: int) -> dict:
|
||||
return {
|
||||
"result": {
|
||||
"result": args1 + args2,
|
||||
}
|
||||
}
|
||||
'''
|
||||
"""
|
||||
# trim first 4 spaces at the beginning of each line
|
||||
code = '\n'.join([line[4:] for line in code.split('\n')])
|
||||
code = "\n".join([line[4:] for line in code.split("\n")])
|
||||
node = CodeNode(
|
||||
tenant_id='1',
|
||||
app_id='1',
|
||||
workflow_id='1',
|
||||
user_id='1',
|
||||
tenant_id="1",
|
||||
app_id="1",
|
||||
workflow_id="1",
|
||||
user_id="1",
|
||||
user_from=UserFrom.ACCOUNT,
|
||||
invoke_from=InvokeFrom.WEB_APP,
|
||||
config={
|
||||
'id': '1',
|
||||
'data': {
|
||||
"id": "1",
|
||||
"data": {
|
||||
"outputs": {
|
||||
"string_validator": {
|
||||
"type": "string",
|
||||
@@ -168,29 +165,26 @@ def test_execute_code_output_validator_depth():
|
||||
"depth": {
|
||||
"type": "number",
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
'title': '123',
|
||||
'variables': [
|
||||
"title": "123",
|
||||
"variables": [
|
||||
{
|
||||
'variable': 'args1',
|
||||
'value_selector': ['1', '123', 'args1'],
|
||||
"variable": "args1",
|
||||
"value_selector": ["1", "123", "args1"],
|
||||
},
|
||||
{
|
||||
'variable': 'args2',
|
||||
'value_selector': ['1', '123', 'args2']
|
||||
}
|
||||
{"variable": "args2", "value_selector": ["1", "123", "args2"]},
|
||||
],
|
||||
'answer': '123',
|
||||
'code_language': 'python3',
|
||||
'code': code
|
||||
}
|
||||
}
|
||||
"answer": "123",
|
||||
"code_language": "python3",
|
||||
"code": code,
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
# construct result
|
||||
@@ -199,14 +193,7 @@ def test_execute_code_output_validator_depth():
|
||||
"string_validator": "1",
|
||||
"number_array_validator": [1, 2, 3, 3.333],
|
||||
"string_array_validator": ["1", "2", "3"],
|
||||
"object_validator": {
|
||||
"result": 1,
|
||||
"depth": {
|
||||
"depth": {
|
||||
"depth": 1
|
||||
}
|
||||
}
|
||||
}
|
||||
"object_validator": {"result": 1, "depth": {"depth": {"depth": 1}}},
|
||||
}
|
||||
|
||||
# validate
|
||||
@@ -218,14 +205,7 @@ def test_execute_code_output_validator_depth():
|
||||
"string_validator": 1,
|
||||
"number_array_validator": ["1", "2", "3", "3.333"],
|
||||
"string_array_validator": [1, 2, 3],
|
||||
"object_validator": {
|
||||
"result": "1",
|
||||
"depth": {
|
||||
"depth": {
|
||||
"depth": "1"
|
||||
}
|
||||
}
|
||||
}
|
||||
"object_validator": {"result": "1", "depth": {"depth": {"depth": "1"}}},
|
||||
}
|
||||
|
||||
# validate
|
||||
@@ -238,34 +218,20 @@ def test_execute_code_output_validator_depth():
|
||||
"string_validator": (CODE_MAX_STRING_LENGTH + 1) * "1",
|
||||
"number_array_validator": [1, 2, 3, 3.333],
|
||||
"string_array_validator": ["1", "2", "3"],
|
||||
"object_validator": {
|
||||
"result": 1,
|
||||
"depth": {
|
||||
"depth": {
|
||||
"depth": 1
|
||||
}
|
||||
}
|
||||
}
|
||||
"object_validator": {"result": 1, "depth": {"depth": {"depth": 1}}},
|
||||
}
|
||||
|
||||
# validate
|
||||
with pytest.raises(ValueError):
|
||||
node._transform_result(result, node.node_data.outputs)
|
||||
|
||||
|
||||
# construct result
|
||||
result = {
|
||||
"number_validator": 1,
|
||||
"string_validator": "1",
|
||||
"number_array_validator": [1, 2, 3, 3.333] * 2000,
|
||||
"string_array_validator": ["1", "2", "3"],
|
||||
"object_validator": {
|
||||
"result": 1,
|
||||
"depth": {
|
||||
"depth": {
|
||||
"depth": 1
|
||||
}
|
||||
}
|
||||
}
|
||||
"object_validator": {"result": 1, "depth": {"depth": {"depth": 1}}},
|
||||
}
|
||||
|
||||
# validate
|
||||
@@ -274,58 +240,59 @@ def test_execute_code_output_validator_depth():
|
||||
|
||||
|
||||
def test_execute_code_output_object_list():
|
||||
code = '''
|
||||
code = """
|
||||
def main(args1: int, args2: int) -> dict:
|
||||
return {
|
||||
"result": {
|
||||
"result": args1 + args2,
|
||||
}
|
||||
}
|
||||
'''
|
||||
"""
|
||||
# trim first 4 spaces at the beginning of each line
|
||||
code = '\n'.join([line[4:] for line in code.split('\n')])
|
||||
code = "\n".join([line[4:] for line in code.split("\n")])
|
||||
node = CodeNode(
|
||||
tenant_id='1',
|
||||
app_id='1',
|
||||
workflow_id='1',
|
||||
user_id='1',
|
||||
tenant_id="1",
|
||||
app_id="1",
|
||||
workflow_id="1",
|
||||
user_id="1",
|
||||
invoke_from=InvokeFrom.WEB_APP,
|
||||
user_from=UserFrom.ACCOUNT,
|
||||
config={
|
||||
'id': '1',
|
||||
'data': {
|
||||
"id": "1",
|
||||
"data": {
|
||||
"outputs": {
|
||||
"object_list": {
|
||||
"type": "array[object]",
|
||||
},
|
||||
},
|
||||
'title': '123',
|
||||
'variables': [
|
||||
"title": "123",
|
||||
"variables": [
|
||||
{
|
||||
'variable': 'args1',
|
||||
'value_selector': ['1', '123', 'args1'],
|
||||
"variable": "args1",
|
||||
"value_selector": ["1", "123", "args1"],
|
||||
},
|
||||
{
|
||||
'variable': 'args2',
|
||||
'value_selector': ['1', '123', 'args2']
|
||||
}
|
||||
{"variable": "args2", "value_selector": ["1", "123", "args2"]},
|
||||
],
|
||||
'answer': '123',
|
||||
'code_language': 'python3',
|
||||
'code': code
|
||||
}
|
||||
}
|
||||
"answer": "123",
|
||||
"code_language": "python3",
|
||||
"code": code,
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
# construct result
|
||||
result = {
|
||||
"object_list": [{
|
||||
"result": 1,
|
||||
}, {
|
||||
"result": 2,
|
||||
}, {
|
||||
"result": [1, 2, 3],
|
||||
}]
|
||||
"object_list": [
|
||||
{
|
||||
"result": 1,
|
||||
},
|
||||
{
|
||||
"result": 2,
|
||||
},
|
||||
{
|
||||
"result": [1, 2, 3],
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
# validate
|
||||
@@ -333,13 +300,18 @@ def test_execute_code_output_object_list():
|
||||
|
||||
# construct result
|
||||
result = {
|
||||
"object_list": [{
|
||||
"result": 1,
|
||||
}, {
|
||||
"result": 2,
|
||||
}, {
|
||||
"result": [1, 2, 3],
|
||||
}, 1]
|
||||
"object_list": [
|
||||
{
|
||||
"result": 1,
|
||||
},
|
||||
{
|
||||
"result": 2,
|
||||
},
|
||||
{
|
||||
"result": [1, 2, 3],
|
||||
},
|
||||
1,
|
||||
]
|
||||
}
|
||||
|
||||
# validate
|
||||
|
||||
@@ -9,322 +9,337 @@ from core.workflow.nodes.http_request.http_request_node import HttpRequestNode
|
||||
from tests.integration_tests.workflow.nodes.__mock.http import setup_http_mock
|
||||
|
||||
BASIC_NODE_DATA = {
|
||||
'tenant_id': '1',
|
||||
'app_id': '1',
|
||||
'workflow_id': '1',
|
||||
'user_id': '1',
|
||||
'user_from': UserFrom.ACCOUNT,
|
||||
'invoke_from': InvokeFrom.WEB_APP,
|
||||
"tenant_id": "1",
|
||||
"app_id": "1",
|
||||
"workflow_id": "1",
|
||||
"user_id": "1",
|
||||
"user_from": UserFrom.ACCOUNT,
|
||||
"invoke_from": InvokeFrom.WEB_APP,
|
||||
}
|
||||
|
||||
# construct variable pool
|
||||
pool = VariablePool(system_variables={}, user_inputs={}, environment_variables=[])
|
||||
pool.add(['a', 'b123', 'args1'], 1)
|
||||
pool.add(['a', 'b123', 'args2'], 2)
|
||||
pool.add(["a", "b123", "args1"], 1)
|
||||
pool.add(["a", "b123", "args2"], 2)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('setup_http_mock', [['none']], indirect=True)
|
||||
@pytest.mark.parametrize("setup_http_mock", [["none"]], indirect=True)
|
||||
def test_get(setup_http_mock):
|
||||
node = HttpRequestNode(config={
|
||||
'id': '1',
|
||||
'data': {
|
||||
'title': 'http',
|
||||
'desc': '',
|
||||
'method': 'get',
|
||||
'url': 'http://example.com',
|
||||
'authorization': {
|
||||
'type': 'api-key',
|
||||
'config': {
|
||||
'type': 'basic',
|
||||
'api_key': 'ak-xxx',
|
||||
'header': 'api-key',
|
||||
}
|
||||
},
|
||||
'headers': 'X-Header:123',
|
||||
'params': 'A:b',
|
||||
'body': None,
|
||||
}
|
||||
}, **BASIC_NODE_DATA)
|
||||
|
||||
result = node.run(pool)
|
||||
|
||||
data = result.process_data.get('request', '')
|
||||
|
||||
assert '?A=b' in data
|
||||
assert 'X-Header: 123' in data
|
||||
|
||||
|
||||
@pytest.mark.parametrize('setup_http_mock', [['none']], indirect=True)
|
||||
def test_no_auth(setup_http_mock):
|
||||
node = HttpRequestNode(config={
|
||||
'id': '1',
|
||||
'data': {
|
||||
'title': 'http',
|
||||
'desc': '',
|
||||
'method': 'get',
|
||||
'url': 'http://example.com',
|
||||
'authorization': {
|
||||
'type': 'no-auth',
|
||||
'config': None,
|
||||
},
|
||||
'headers': 'X-Header:123',
|
||||
'params': 'A:b',
|
||||
'body': None,
|
||||
}
|
||||
}, **BASIC_NODE_DATA)
|
||||
|
||||
result = node.run(pool)
|
||||
|
||||
data = result.process_data.get('request', '')
|
||||
|
||||
assert '?A=b' in data
|
||||
assert 'X-Header: 123' in data
|
||||
|
||||
|
||||
@pytest.mark.parametrize('setup_http_mock', [['none']], indirect=True)
|
||||
def test_custom_authorization_header(setup_http_mock):
|
||||
node = HttpRequestNode(config={
|
||||
'id': '1',
|
||||
'data': {
|
||||
'title': 'http',
|
||||
'desc': '',
|
||||
'method': 'get',
|
||||
'url': 'http://example.com',
|
||||
'authorization': {
|
||||
'type': 'api-key',
|
||||
'config': {
|
||||
'type': 'custom',
|
||||
'api_key': 'Auth',
|
||||
'header': 'X-Auth',
|
||||
node = HttpRequestNode(
|
||||
config={
|
||||
"id": "1",
|
||||
"data": {
|
||||
"title": "http",
|
||||
"desc": "",
|
||||
"method": "get",
|
||||
"url": "http://example.com",
|
||||
"authorization": {
|
||||
"type": "api-key",
|
||||
"config": {
|
||||
"type": "basic",
|
||||
"api_key": "ak-xxx",
|
||||
"header": "api-key",
|
||||
},
|
||||
},
|
||||
"headers": "X-Header:123",
|
||||
"params": "A:b",
|
||||
"body": None,
|
||||
},
|
||||
'headers': 'X-Header:123',
|
||||
'params': 'A:b',
|
||||
'body': None,
|
||||
}
|
||||
}, **BASIC_NODE_DATA)
|
||||
},
|
||||
**BASIC_NODE_DATA,
|
||||
)
|
||||
|
||||
result = node.run(pool)
|
||||
|
||||
data = result.process_data.get('request', '')
|
||||
data = result.process_data.get("request", "")
|
||||
|
||||
assert '?A=b' in data
|
||||
assert 'X-Header: 123' in data
|
||||
assert "?A=b" in data
|
||||
assert "X-Header: 123" in data
|
||||
|
||||
|
||||
@pytest.mark.parametrize('setup_http_mock', [['none']], indirect=True)
|
||||
@pytest.mark.parametrize("setup_http_mock", [["none"]], indirect=True)
|
||||
def test_no_auth(setup_http_mock):
|
||||
node = HttpRequestNode(
|
||||
config={
|
||||
"id": "1",
|
||||
"data": {
|
||||
"title": "http",
|
||||
"desc": "",
|
||||
"method": "get",
|
||||
"url": "http://example.com",
|
||||
"authorization": {
|
||||
"type": "no-auth",
|
||||
"config": None,
|
||||
},
|
||||
"headers": "X-Header:123",
|
||||
"params": "A:b",
|
||||
"body": None,
|
||||
},
|
||||
},
|
||||
**BASIC_NODE_DATA,
|
||||
)
|
||||
|
||||
result = node.run(pool)
|
||||
|
||||
data = result.process_data.get("request", "")
|
||||
|
||||
assert "?A=b" in data
|
||||
assert "X-Header: 123" in data
|
||||
|
||||
|
||||
@pytest.mark.parametrize("setup_http_mock", [["none"]], indirect=True)
|
||||
def test_custom_authorization_header(setup_http_mock):
|
||||
node = HttpRequestNode(
|
||||
config={
|
||||
"id": "1",
|
||||
"data": {
|
||||
"title": "http",
|
||||
"desc": "",
|
||||
"method": "get",
|
||||
"url": "http://example.com",
|
||||
"authorization": {
|
||||
"type": "api-key",
|
||||
"config": {
|
||||
"type": "custom",
|
||||
"api_key": "Auth",
|
||||
"header": "X-Auth",
|
||||
},
|
||||
},
|
||||
"headers": "X-Header:123",
|
||||
"params": "A:b",
|
||||
"body": None,
|
||||
},
|
||||
},
|
||||
**BASIC_NODE_DATA,
|
||||
)
|
||||
|
||||
result = node.run(pool)
|
||||
|
||||
data = result.process_data.get("request", "")
|
||||
|
||||
assert "?A=b" in data
|
||||
assert "X-Header: 123" in data
|
||||
|
||||
|
||||
@pytest.mark.parametrize("setup_http_mock", [["none"]], indirect=True)
|
||||
def test_template(setup_http_mock):
|
||||
node = HttpRequestNode(config={
|
||||
'id': '1',
|
||||
'data': {
|
||||
'title': 'http',
|
||||
'desc': '',
|
||||
'method': 'get',
|
||||
'url': 'http://example.com/{{#a.b123.args2#}}',
|
||||
'authorization': {
|
||||
'type': 'api-key',
|
||||
'config': {
|
||||
'type': 'basic',
|
||||
'api_key': 'ak-xxx',
|
||||
'header': 'api-key',
|
||||
}
|
||||
node = HttpRequestNode(
|
||||
config={
|
||||
"id": "1",
|
||||
"data": {
|
||||
"title": "http",
|
||||
"desc": "",
|
||||
"method": "get",
|
||||
"url": "http://example.com/{{#a.b123.args2#}}",
|
||||
"authorization": {
|
||||
"type": "api-key",
|
||||
"config": {
|
||||
"type": "basic",
|
||||
"api_key": "ak-xxx",
|
||||
"header": "api-key",
|
||||
},
|
||||
},
|
||||
"headers": "X-Header:123\nX-Header2:{{#a.b123.args2#}}",
|
||||
"params": "A:b\nTemplate:{{#a.b123.args2#}}",
|
||||
"body": None,
|
||||
},
|
||||
'headers': 'X-Header:123\nX-Header2:{{#a.b123.args2#}}',
|
||||
'params': 'A:b\nTemplate:{{#a.b123.args2#}}',
|
||||
'body': None,
|
||||
}
|
||||
}, **BASIC_NODE_DATA)
|
||||
},
|
||||
**BASIC_NODE_DATA,
|
||||
)
|
||||
|
||||
result = node.run(pool)
|
||||
data = result.process_data.get('request', '')
|
||||
data = result.process_data.get("request", "")
|
||||
|
||||
assert '?A=b' in data
|
||||
assert 'Template=2' in data
|
||||
assert 'X-Header: 123' in data
|
||||
assert 'X-Header2: 2' in data
|
||||
assert "?A=b" in data
|
||||
assert "Template=2" in data
|
||||
assert "X-Header: 123" in data
|
||||
assert "X-Header2: 2" in data
|
||||
|
||||
|
||||
@pytest.mark.parametrize('setup_http_mock', [['none']], indirect=True)
|
||||
@pytest.mark.parametrize("setup_http_mock", [["none"]], indirect=True)
|
||||
def test_json(setup_http_mock):
|
||||
node = HttpRequestNode(config={
|
||||
'id': '1',
|
||||
'data': {
|
||||
'title': 'http',
|
||||
'desc': '',
|
||||
'method': 'post',
|
||||
'url': 'http://example.com',
|
||||
'authorization': {
|
||||
'type': 'api-key',
|
||||
'config': {
|
||||
'type': 'basic',
|
||||
'api_key': 'ak-xxx',
|
||||
'header': 'api-key',
|
||||
}
|
||||
node = HttpRequestNode(
|
||||
config={
|
||||
"id": "1",
|
||||
"data": {
|
||||
"title": "http",
|
||||
"desc": "",
|
||||
"method": "post",
|
||||
"url": "http://example.com",
|
||||
"authorization": {
|
||||
"type": "api-key",
|
||||
"config": {
|
||||
"type": "basic",
|
||||
"api_key": "ak-xxx",
|
||||
"header": "api-key",
|
||||
},
|
||||
},
|
||||
"headers": "X-Header:123",
|
||||
"params": "A:b",
|
||||
"body": {"type": "json", "data": '{"a": "{{#a.b123.args1#}}"}'},
|
||||
},
|
||||
'headers': 'X-Header:123',
|
||||
'params': 'A:b',
|
||||
'body': {
|
||||
'type': 'json',
|
||||
'data': '{"a": "{{#a.b123.args1#}}"}'
|
||||
},
|
||||
}
|
||||
}, **BASIC_NODE_DATA)
|
||||
},
|
||||
**BASIC_NODE_DATA,
|
||||
)
|
||||
|
||||
result = node.run(pool)
|
||||
data = result.process_data.get('request', '')
|
||||
data = result.process_data.get("request", "")
|
||||
|
||||
assert '{"a": "1"}' in data
|
||||
assert 'X-Header: 123' in data
|
||||
assert "X-Header: 123" in data
|
||||
|
||||
|
||||
def test_x_www_form_urlencoded(setup_http_mock):
|
||||
node = HttpRequestNode(config={
|
||||
'id': '1',
|
||||
'data': {
|
||||
'title': 'http',
|
||||
'desc': '',
|
||||
'method': 'post',
|
||||
'url': 'http://example.com',
|
||||
'authorization': {
|
||||
'type': 'api-key',
|
||||
'config': {
|
||||
'type': 'basic',
|
||||
'api_key': 'ak-xxx',
|
||||
'header': 'api-key',
|
||||
}
|
||||
node = HttpRequestNode(
|
||||
config={
|
||||
"id": "1",
|
||||
"data": {
|
||||
"title": "http",
|
||||
"desc": "",
|
||||
"method": "post",
|
||||
"url": "http://example.com",
|
||||
"authorization": {
|
||||
"type": "api-key",
|
||||
"config": {
|
||||
"type": "basic",
|
||||
"api_key": "ak-xxx",
|
||||
"header": "api-key",
|
||||
},
|
||||
},
|
||||
"headers": "X-Header:123",
|
||||
"params": "A:b",
|
||||
"body": {"type": "x-www-form-urlencoded", "data": "a:{{#a.b123.args1#}}\nb:{{#a.b123.args2#}}"},
|
||||
},
|
||||
'headers': 'X-Header:123',
|
||||
'params': 'A:b',
|
||||
'body': {
|
||||
'type': 'x-www-form-urlencoded',
|
||||
'data': 'a:{{#a.b123.args1#}}\nb:{{#a.b123.args2#}}'
|
||||
},
|
||||
}
|
||||
}, **BASIC_NODE_DATA)
|
||||
},
|
||||
**BASIC_NODE_DATA,
|
||||
)
|
||||
|
||||
result = node.run(pool)
|
||||
data = result.process_data.get('request', '')
|
||||
data = result.process_data.get("request", "")
|
||||
|
||||
assert 'a=1&b=2' in data
|
||||
assert 'X-Header: 123' in data
|
||||
assert "a=1&b=2" in data
|
||||
assert "X-Header: 123" in data
|
||||
|
||||
|
||||
def test_form_data(setup_http_mock):
|
||||
node = HttpRequestNode(config={
|
||||
'id': '1',
|
||||
'data': {
|
||||
'title': 'http',
|
||||
'desc': '',
|
||||
'method': 'post',
|
||||
'url': 'http://example.com',
|
||||
'authorization': {
|
||||
'type': 'api-key',
|
||||
'config': {
|
||||
'type': 'basic',
|
||||
'api_key': 'ak-xxx',
|
||||
'header': 'api-key',
|
||||
}
|
||||
node = HttpRequestNode(
|
||||
config={
|
||||
"id": "1",
|
||||
"data": {
|
||||
"title": "http",
|
||||
"desc": "",
|
||||
"method": "post",
|
||||
"url": "http://example.com",
|
||||
"authorization": {
|
||||
"type": "api-key",
|
||||
"config": {
|
||||
"type": "basic",
|
||||
"api_key": "ak-xxx",
|
||||
"header": "api-key",
|
||||
},
|
||||
},
|
||||
"headers": "X-Header:123",
|
||||
"params": "A:b",
|
||||
"body": {"type": "form-data", "data": "a:{{#a.b123.args1#}}\nb:{{#a.b123.args2#}}"},
|
||||
},
|
||||
'headers': 'X-Header:123',
|
||||
'params': 'A:b',
|
||||
'body': {
|
||||
'type': 'form-data',
|
||||
'data': 'a:{{#a.b123.args1#}}\nb:{{#a.b123.args2#}}'
|
||||
},
|
||||
}
|
||||
}, **BASIC_NODE_DATA)
|
||||
},
|
||||
**BASIC_NODE_DATA,
|
||||
)
|
||||
|
||||
result = node.run(pool)
|
||||
data = result.process_data.get('request', '')
|
||||
data = result.process_data.get("request", "")
|
||||
|
||||
assert 'form-data; name="a"' in data
|
||||
assert '1' in data
|
||||
assert "1" in data
|
||||
assert 'form-data; name="b"' in data
|
||||
assert '2' in data
|
||||
assert 'X-Header: 123' in data
|
||||
assert "2" in data
|
||||
assert "X-Header: 123" in data
|
||||
|
||||
|
||||
def test_none_data(setup_http_mock):
|
||||
node = HttpRequestNode(config={
|
||||
'id': '1',
|
||||
'data': {
|
||||
'title': 'http',
|
||||
'desc': '',
|
||||
'method': 'post',
|
||||
'url': 'http://example.com',
|
||||
'authorization': {
|
||||
'type': 'api-key',
|
||||
'config': {
|
||||
'type': 'basic',
|
||||
'api_key': 'ak-xxx',
|
||||
'header': 'api-key',
|
||||
}
|
||||
node = HttpRequestNode(
|
||||
config={
|
||||
"id": "1",
|
||||
"data": {
|
||||
"title": "http",
|
||||
"desc": "",
|
||||
"method": "post",
|
||||
"url": "http://example.com",
|
||||
"authorization": {
|
||||
"type": "api-key",
|
||||
"config": {
|
||||
"type": "basic",
|
||||
"api_key": "ak-xxx",
|
||||
"header": "api-key",
|
||||
},
|
||||
},
|
||||
"headers": "X-Header:123",
|
||||
"params": "A:b",
|
||||
"body": {"type": "none", "data": "123123123"},
|
||||
},
|
||||
'headers': 'X-Header:123',
|
||||
'params': 'A:b',
|
||||
'body': {
|
||||
'type': 'none',
|
||||
'data': '123123123'
|
||||
},
|
||||
}
|
||||
}, **BASIC_NODE_DATA)
|
||||
},
|
||||
**BASIC_NODE_DATA,
|
||||
)
|
||||
|
||||
result = node.run(pool)
|
||||
data = result.process_data.get('request', '')
|
||||
data = result.process_data.get("request", "")
|
||||
|
||||
assert 'X-Header: 123' in data
|
||||
assert '123123123' not in data
|
||||
assert "X-Header: 123" in data
|
||||
assert "123123123" not in data
|
||||
|
||||
|
||||
def test_mock_404(setup_http_mock):
|
||||
node = HttpRequestNode(config={
|
||||
'id': '1',
|
||||
'data': {
|
||||
'title': 'http',
|
||||
'desc': '',
|
||||
'method': 'get',
|
||||
'url': 'http://404.com',
|
||||
'authorization': {
|
||||
'type': 'no-auth',
|
||||
'config': None,
|
||||
node = HttpRequestNode(
|
||||
config={
|
||||
"id": "1",
|
||||
"data": {
|
||||
"title": "http",
|
||||
"desc": "",
|
||||
"method": "get",
|
||||
"url": "http://404.com",
|
||||
"authorization": {
|
||||
"type": "no-auth",
|
||||
"config": None,
|
||||
},
|
||||
"body": None,
|
||||
"params": "",
|
||||
"headers": "X-Header:123",
|
||||
},
|
||||
'body': None,
|
||||
'params': '',
|
||||
'headers': 'X-Header:123',
|
||||
}
|
||||
}, **BASIC_NODE_DATA)
|
||||
},
|
||||
**BASIC_NODE_DATA,
|
||||
)
|
||||
|
||||
result = node.run(pool)
|
||||
resp = result.outputs
|
||||
|
||||
assert 404 == resp.get('status_code')
|
||||
assert 'Not Found' in resp.get('body')
|
||||
assert 404 == resp.get("status_code")
|
||||
assert "Not Found" in resp.get("body")
|
||||
|
||||
|
||||
def test_multi_colons_parse(setup_http_mock):
|
||||
node = HttpRequestNode(config={
|
||||
'id': '1',
|
||||
'data': {
|
||||
'title': 'http',
|
||||
'desc': '',
|
||||
'method': 'get',
|
||||
'url': 'http://example.com',
|
||||
'authorization': {
|
||||
'type': 'no-auth',
|
||||
'config': None,
|
||||
node = HttpRequestNode(
|
||||
config={
|
||||
"id": "1",
|
||||
"data": {
|
||||
"title": "http",
|
||||
"desc": "",
|
||||
"method": "get",
|
||||
"url": "http://example.com",
|
||||
"authorization": {
|
||||
"type": "no-auth",
|
||||
"config": None,
|
||||
},
|
||||
"params": "Referer:http://example1.com\nRedirect:http://example2.com",
|
||||
"headers": "Referer:http://example3.com\nRedirect:http://example4.com",
|
||||
"body": {"type": "form-data", "data": "Referer:http://example5.com\nRedirect:http://example6.com"},
|
||||
},
|
||||
'params': 'Referer:http://example1.com\nRedirect:http://example2.com',
|
||||
'headers': 'Referer:http://example3.com\nRedirect:http://example4.com',
|
||||
'body': {
|
||||
'type': 'form-data',
|
||||
'data': 'Referer:http://example5.com\nRedirect:http://example6.com'
|
||||
},
|
||||
}
|
||||
}, **BASIC_NODE_DATA)
|
||||
},
|
||||
**BASIC_NODE_DATA,
|
||||
)
|
||||
|
||||
result = node.run(pool)
|
||||
resp = result.outputs
|
||||
|
||||
assert urlencode({'Redirect': 'http://example2.com'}) in result.process_data.get('request')
|
||||
assert 'form-data; name="Redirect"\n\nhttp://example6.com' in result.process_data.get('request')
|
||||
assert 'http://example3.com' == resp.get('headers').get('referer')
|
||||
assert urlencode({"Redirect": "http://example2.com"}) in result.process_data.get("request")
|
||||
assert 'form-data; name="Redirect"\n\nhttp://example6.com' in result.process_data.get("request")
|
||||
assert "http://example3.com" == resp.get("headers").get("referer")
|
||||
|
||||
@@ -23,90 +23,71 @@ from tests.integration_tests.model_runtime.__mock.openai import setup_openai_moc
|
||||
from tests.integration_tests.workflow.nodes.__mock.code_executor import setup_code_executor_mock
|
||||
|
||||
|
||||
@pytest.mark.parametrize('setup_openai_mock', [['chat']], indirect=True)
|
||||
@pytest.mark.parametrize("setup_openai_mock", [["chat"]], indirect=True)
|
||||
def test_execute_llm(setup_openai_mock):
|
||||
node = LLMNode(
|
||||
tenant_id='1',
|
||||
app_id='1',
|
||||
workflow_id='1',
|
||||
user_id='1',
|
||||
tenant_id="1",
|
||||
app_id="1",
|
||||
workflow_id="1",
|
||||
user_id="1",
|
||||
invoke_from=InvokeFrom.WEB_APP,
|
||||
user_from=UserFrom.ACCOUNT,
|
||||
config={
|
||||
'id': 'llm',
|
||||
'data': {
|
||||
'title': '123',
|
||||
'type': 'llm',
|
||||
'model': {
|
||||
'provider': 'openai',
|
||||
'name': 'gpt-3.5-turbo',
|
||||
'mode': 'chat',
|
||||
'completion_params': {}
|
||||
},
|
||||
'prompt_template': [
|
||||
{
|
||||
'role': 'system',
|
||||
'text': 'you are a helpful assistant.\ntoday\'s weather is {{#abc.output#}}.'
|
||||
},
|
||||
{
|
||||
'role': 'user',
|
||||
'text': '{{#sys.query#}}'
|
||||
}
|
||||
"id": "llm",
|
||||
"data": {
|
||||
"title": "123",
|
||||
"type": "llm",
|
||||
"model": {"provider": "openai", "name": "gpt-3.5-turbo", "mode": "chat", "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
|
||||
}
|
||||
}
|
||||
}
|
||||
"memory": None,
|
||||
"context": {"enabled": False},
|
||||
"vision": {"enabled": False},
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
# construct variable pool
|
||||
pool = VariablePool(system_variables={
|
||||
SystemVariableKey.QUERY: 'what\'s the weather today?',
|
||||
SystemVariableKey.FILES: [],
|
||||
SystemVariableKey.CONVERSATION_ID: 'abababa',
|
||||
SystemVariableKey.USER_ID: 'aaa'
|
||||
}, user_inputs={}, environment_variables=[])
|
||||
pool.add(['abc', 'output'], 'sunny')
|
||||
pool = VariablePool(
|
||||
system_variables={
|
||||
SystemVariableKey.QUERY: "what's the weather today?",
|
||||
SystemVariableKey.FILES: [],
|
||||
SystemVariableKey.CONVERSATION_ID: "abababa",
|
||||
SystemVariableKey.USER_ID: "aaa",
|
||||
},
|
||||
user_inputs={},
|
||||
environment_variables=[],
|
||||
)
|
||||
pool.add(["abc", "output"], "sunny")
|
||||
|
||||
credentials = {
|
||||
'openai_api_key': os.environ.get('OPENAI_API_KEY')
|
||||
}
|
||||
credentials = {"openai_api_key": os.environ.get("OPENAI_API_KEY")}
|
||||
|
||||
provider_instance = ModelProviderFactory().get_provider_instance('openai')
|
||||
provider_instance = ModelProviderFactory().get_provider_instance("openai")
|
||||
model_type_instance = provider_instance.get_model_instance(ModelType.LLM)
|
||||
provider_model_bundle = ProviderModelBundle(
|
||||
configuration=ProviderConfiguration(
|
||||
tenant_id='1',
|
||||
tenant_id="1",
|
||||
provider=provider_instance.get_provider_schema(),
|
||||
preferred_provider_type=ProviderType.CUSTOM,
|
||||
using_provider_type=ProviderType.CUSTOM,
|
||||
system_configuration=SystemConfiguration(
|
||||
enabled=False
|
||||
),
|
||||
custom_configuration=CustomConfiguration(
|
||||
provider=CustomProviderConfiguration(
|
||||
credentials=credentials
|
||||
)
|
||||
),
|
||||
model_settings=[]
|
||||
system_configuration=SystemConfiguration(enabled=False),
|
||||
custom_configuration=CustomConfiguration(provider=CustomProviderConfiguration(credentials=credentials)),
|
||||
model_settings=[],
|
||||
),
|
||||
provider_instance=provider_instance,
|
||||
model_type_instance=model_type_instance
|
||||
model_type_instance=model_type_instance,
|
||||
)
|
||||
model_instance = ModelInstance(provider_model_bundle=provider_model_bundle, model='gpt-3.5-turbo')
|
||||
model_instance = ModelInstance(provider_model_bundle=provider_model_bundle, model="gpt-3.5-turbo")
|
||||
model_config = ModelConfigWithCredentialsEntity(
|
||||
model='gpt-3.5-turbo',
|
||||
provider='openai',
|
||||
mode='chat',
|
||||
model="gpt-3.5-turbo",
|
||||
provider="openai",
|
||||
mode="chat",
|
||||
credentials=credentials,
|
||||
parameters={},
|
||||
model_schema=model_type_instance.get_model_schema('gpt-3.5-turbo'),
|
||||
provider_model_bundle=provider_model_bundle
|
||||
model_schema=model_type_instance.get_model_schema("gpt-3.5-turbo"),
|
||||
provider_model_bundle=provider_model_bundle,
|
||||
)
|
||||
|
||||
# Mock db.session.close()
|
||||
@@ -118,112 +99,97 @@ def test_execute_llm(setup_openai_mock):
|
||||
result = node.run(pool)
|
||||
|
||||
assert result.status == WorkflowNodeExecutionStatus.SUCCEEDED
|
||||
assert result.outputs['text'] is not None
|
||||
assert result.outputs['usage']['total_tokens'] > 0
|
||||
assert result.outputs["text"] is not None
|
||||
assert result.outputs["usage"]["total_tokens"] > 0
|
||||
|
||||
@pytest.mark.parametrize('setup_code_executor_mock', [['none']], indirect=True)
|
||||
@pytest.mark.parametrize('setup_openai_mock', [['chat']], indirect=True)
|
||||
|
||||
@pytest.mark.parametrize("setup_code_executor_mock", [["none"]], indirect=True)
|
||||
@pytest.mark.parametrize("setup_openai_mock", [["chat"]], indirect=True)
|
||||
def test_execute_llm_with_jinja2(setup_code_executor_mock, setup_openai_mock):
|
||||
"""
|
||||
Test execute LLM node with jinja2
|
||||
"""
|
||||
node = LLMNode(
|
||||
tenant_id='1',
|
||||
app_id='1',
|
||||
workflow_id='1',
|
||||
user_id='1',
|
||||
tenant_id="1",
|
||||
app_id="1",
|
||||
workflow_id="1",
|
||||
user_id="1",
|
||||
invoke_from=InvokeFrom.WEB_APP,
|
||||
user_from=UserFrom.ACCOUNT,
|
||||
config={
|
||||
'id': 'llm',
|
||||
'data': {
|
||||
'title': '123',
|
||||
'type': 'llm',
|
||||
'model': {
|
||||
'provider': 'openai',
|
||||
'name': 'gpt-3.5-turbo',
|
||||
'mode': 'chat',
|
||||
'completion_params': {}
|
||||
"id": "llm",
|
||||
"data": {
|
||||
"title": "123",
|
||||
"type": "llm",
|
||||
"model": {"provider": "openai", "name": "gpt-3.5-turbo", "mode": "chat", "completion_params": {}},
|
||||
"prompt_config": {
|
||||
"jinja2_variables": [
|
||||
{"variable": "sys_query", "value_selector": ["sys", "query"]},
|
||||
{"variable": "output", "value_selector": ["abc", "output"]},
|
||||
]
|
||||
},
|
||||
'prompt_config': {
|
||||
'jinja2_variables': [{
|
||||
'variable': 'sys_query',
|
||||
'value_selector': ['sys', 'query']
|
||||
}, {
|
||||
'variable': 'output',
|
||||
'value_selector': ['abc', 'output']
|
||||
}]
|
||||
},
|
||||
'prompt_template': [
|
||||
"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": "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'
|
||||
}
|
||||
"role": "user",
|
||||
"text": "{{#sys.query#}}",
|
||||
"jinja2_text": "{{sys_query}}",
|
||||
"edition_type": "basic",
|
||||
},
|
||||
],
|
||||
'memory': None,
|
||||
'context': {
|
||||
'enabled': False
|
||||
},
|
||||
'vision': {
|
||||
'enabled': False
|
||||
}
|
||||
}
|
||||
}
|
||||
"memory": None,
|
||||
"context": {"enabled": False},
|
||||
"vision": {"enabled": False},
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
# construct variable pool
|
||||
pool = VariablePool(system_variables={
|
||||
SystemVariableKey.QUERY: 'what\'s the weather today?',
|
||||
SystemVariableKey.FILES: [],
|
||||
SystemVariableKey.CONVERSATION_ID: 'abababa',
|
||||
SystemVariableKey.USER_ID: 'aaa'
|
||||
}, user_inputs={}, environment_variables=[])
|
||||
pool.add(['abc', 'output'], 'sunny')
|
||||
pool = VariablePool(
|
||||
system_variables={
|
||||
SystemVariableKey.QUERY: "what's the weather today?",
|
||||
SystemVariableKey.FILES: [],
|
||||
SystemVariableKey.CONVERSATION_ID: "abababa",
|
||||
SystemVariableKey.USER_ID: "aaa",
|
||||
},
|
||||
user_inputs={},
|
||||
environment_variables=[],
|
||||
)
|
||||
pool.add(["abc", "output"], "sunny")
|
||||
|
||||
credentials = {
|
||||
'openai_api_key': os.environ.get('OPENAI_API_KEY')
|
||||
}
|
||||
credentials = {"openai_api_key": os.environ.get("OPENAI_API_KEY")}
|
||||
|
||||
provider_instance = ModelProviderFactory().get_provider_instance('openai')
|
||||
provider_instance = ModelProviderFactory().get_provider_instance("openai")
|
||||
model_type_instance = provider_instance.get_model_instance(ModelType.LLM)
|
||||
provider_model_bundle = ProviderModelBundle(
|
||||
configuration=ProviderConfiguration(
|
||||
tenant_id='1',
|
||||
tenant_id="1",
|
||||
provider=provider_instance.get_provider_schema(),
|
||||
preferred_provider_type=ProviderType.CUSTOM,
|
||||
using_provider_type=ProviderType.CUSTOM,
|
||||
system_configuration=SystemConfiguration(
|
||||
enabled=False
|
||||
),
|
||||
custom_configuration=CustomConfiguration(
|
||||
provider=CustomProviderConfiguration(
|
||||
credentials=credentials
|
||||
)
|
||||
),
|
||||
model_settings=[]
|
||||
system_configuration=SystemConfiguration(enabled=False),
|
||||
custom_configuration=CustomConfiguration(provider=CustomProviderConfiguration(credentials=credentials)),
|
||||
model_settings=[],
|
||||
),
|
||||
provider_instance=provider_instance,
|
||||
model_type_instance=model_type_instance,
|
||||
)
|
||||
|
||||
model_instance = ModelInstance(provider_model_bundle=provider_model_bundle, model='gpt-3.5-turbo')
|
||||
model_instance = ModelInstance(provider_model_bundle=provider_model_bundle, model="gpt-3.5-turbo")
|
||||
|
||||
model_config = ModelConfigWithCredentialsEntity(
|
||||
model='gpt-3.5-turbo',
|
||||
provider='openai',
|
||||
mode='chat',
|
||||
model="gpt-3.5-turbo",
|
||||
provider="openai",
|
||||
mode="chat",
|
||||
credentials=credentials,
|
||||
parameters={},
|
||||
model_schema=model_type_instance.get_model_schema('gpt-3.5-turbo'),
|
||||
provider_model_bundle=provider_model_bundle
|
||||
model_schema=model_type_instance.get_model_schema("gpt-3.5-turbo"),
|
||||
provider_model_bundle=provider_model_bundle,
|
||||
)
|
||||
|
||||
# Mock db.session.close()
|
||||
@@ -235,5 +201,5 @@ def test_execute_llm_with_jinja2(setup_code_executor_mock, setup_openai_mock):
|
||||
result = node.run(pool)
|
||||
|
||||
assert result.status == WorkflowNodeExecutionStatus.SUCCEEDED
|
||||
assert 'sunny' in json.dumps(result.process_data)
|
||||
assert 'what\'s the weather today?' in json.dumps(result.process_data)
|
||||
assert "sunny" in json.dumps(result.process_data)
|
||||
assert "what's the weather today?" in json.dumps(result.process_data)
|
||||
|
||||
@@ -26,29 +26,25 @@ from tests.integration_tests.model_runtime.__mock.openai import setup_openai_moc
|
||||
|
||||
|
||||
def get_mocked_fetch_model_config(
|
||||
provider: str, model: str, mode: str,
|
||||
provider: str,
|
||||
model: str,
|
||||
mode: str,
|
||||
credentials: dict,
|
||||
):
|
||||
provider_instance = ModelProviderFactory().get_provider_instance(provider)
|
||||
model_type_instance = provider_instance.get_model_instance(ModelType.LLM)
|
||||
provider_model_bundle = ProviderModelBundle(
|
||||
configuration=ProviderConfiguration(
|
||||
tenant_id='1',
|
||||
tenant_id="1",
|
||||
provider=provider_instance.get_provider_schema(),
|
||||
preferred_provider_type=ProviderType.CUSTOM,
|
||||
using_provider_type=ProviderType.CUSTOM,
|
||||
system_configuration=SystemConfiguration(
|
||||
enabled=False
|
||||
),
|
||||
custom_configuration=CustomConfiguration(
|
||||
provider=CustomProviderConfiguration(
|
||||
credentials=credentials
|
||||
)
|
||||
),
|
||||
model_settings=[]
|
||||
system_configuration=SystemConfiguration(enabled=False),
|
||||
custom_configuration=CustomConfiguration(provider=CustomProviderConfiguration(credentials=credentials)),
|
||||
model_settings=[],
|
||||
),
|
||||
provider_instance=provider_instance,
|
||||
model_type_instance=model_type_instance
|
||||
model_type_instance=model_type_instance,
|
||||
)
|
||||
model_instance = ModelInstance(provider_model_bundle=provider_model_bundle, model=model)
|
||||
model_config = ModelConfigWithCredentialsEntity(
|
||||
@@ -58,268 +54,268 @@ def get_mocked_fetch_model_config(
|
||||
credentials=credentials,
|
||||
parameters={},
|
||||
model_schema=model_type_instance.get_model_schema(model),
|
||||
provider_model_bundle=provider_model_bundle
|
||||
provider_model_bundle=provider_model_bundle,
|
||||
)
|
||||
|
||||
return MagicMock(return_value=(model_instance, model_config))
|
||||
|
||||
|
||||
def get_mocked_fetch_memory(memory_text: str):
|
||||
class MemoryMock:
|
||||
def get_history_prompt_text(self, human_prefix: str = "Human",
|
||||
ai_prefix: str = "Assistant",
|
||||
max_token_limit: int = 2000,
|
||||
message_limit: Optional[int] = None):
|
||||
def get_history_prompt_text(
|
||||
self,
|
||||
human_prefix: str = "Human",
|
||||
ai_prefix: str = "Assistant",
|
||||
max_token_limit: int = 2000,
|
||||
message_limit: Optional[int] = None,
|
||||
):
|
||||
return memory_text
|
||||
|
||||
return MagicMock(return_value=MemoryMock())
|
||||
|
||||
@pytest.mark.parametrize('setup_openai_mock', [['chat']], indirect=True)
|
||||
|
||||
@pytest.mark.parametrize("setup_openai_mock", [["chat"]], indirect=True)
|
||||
def test_function_calling_parameter_extractor(setup_openai_mock):
|
||||
"""
|
||||
Test function calling for parameter extractor.
|
||||
"""
|
||||
node = ParameterExtractorNode(
|
||||
tenant_id='1',
|
||||
app_id='1',
|
||||
workflow_id='1',
|
||||
user_id='1',
|
||||
tenant_id="1",
|
||||
app_id="1",
|
||||
workflow_id="1",
|
||||
user_id="1",
|
||||
invoke_from=InvokeFrom.WEB_APP,
|
||||
user_from=UserFrom.ACCOUNT,
|
||||
config={
|
||||
'id': 'llm',
|
||||
'data': {
|
||||
'title': '123',
|
||||
'type': 'parameter-extractor',
|
||||
'model': {
|
||||
'provider': 'openai',
|
||||
'name': 'gpt-3.5-turbo',
|
||||
'mode': 'chat',
|
||||
'completion_params': {}
|
||||
},
|
||||
'query': ['sys', 'query'],
|
||||
'parameters': [{
|
||||
'name': 'location',
|
||||
'type': 'string',
|
||||
'description': 'location',
|
||||
'required': True
|
||||
}],
|
||||
'instruction': '',
|
||||
'reasoning_mode': 'function_call',
|
||||
'memory': None,
|
||||
}
|
||||
}
|
||||
"id": "llm",
|
||||
"data": {
|
||||
"title": "123",
|
||||
"type": "parameter-extractor",
|
||||
"model": {"provider": "openai", "name": "gpt-3.5-turbo", "mode": "chat", "completion_params": {}},
|
||||
"query": ["sys", "query"],
|
||||
"parameters": [{"name": "location", "type": "string", "description": "location", "required": True}],
|
||||
"instruction": "",
|
||||
"reasoning_mode": "function_call",
|
||||
"memory": None,
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
node._fetch_model_config = get_mocked_fetch_model_config(
|
||||
provider='openai', model='gpt-3.5-turbo', mode='chat', credentials={
|
||||
'openai_api_key': os.environ.get('OPENAI_API_KEY')
|
||||
}
|
||||
provider="openai",
|
||||
model="gpt-3.5-turbo",
|
||||
mode="chat",
|
||||
credentials={"openai_api_key": os.environ.get("OPENAI_API_KEY")},
|
||||
)
|
||||
db.session.close = MagicMock()
|
||||
|
||||
# construct variable pool
|
||||
pool = VariablePool(system_variables={
|
||||
SystemVariableKey.QUERY: 'what\'s the weather in SF',
|
||||
SystemVariableKey.FILES: [],
|
||||
SystemVariableKey.CONVERSATION_ID: 'abababa',
|
||||
SystemVariableKey.USER_ID: 'aaa'
|
||||
}, user_inputs={}, environment_variables=[])
|
||||
pool = VariablePool(
|
||||
system_variables={
|
||||
SystemVariableKey.QUERY: "what's the weather in SF",
|
||||
SystemVariableKey.FILES: [],
|
||||
SystemVariableKey.CONVERSATION_ID: "abababa",
|
||||
SystemVariableKey.USER_ID: "aaa",
|
||||
},
|
||||
user_inputs={},
|
||||
environment_variables=[],
|
||||
)
|
||||
|
||||
result = node.run(pool)
|
||||
|
||||
assert result.status == WorkflowNodeExecutionStatus.SUCCEEDED
|
||||
assert result.outputs.get('location') == 'kawaii'
|
||||
assert result.outputs.get('__reason') == None
|
||||
assert result.outputs.get("location") == "kawaii"
|
||||
assert result.outputs.get("__reason") == None
|
||||
|
||||
@pytest.mark.parametrize('setup_openai_mock', [['chat']], indirect=True)
|
||||
|
||||
@pytest.mark.parametrize("setup_openai_mock", [["chat"]], indirect=True)
|
||||
def test_instructions(setup_openai_mock):
|
||||
"""
|
||||
Test chat parameter extractor.
|
||||
"""
|
||||
node = ParameterExtractorNode(
|
||||
tenant_id='1',
|
||||
app_id='1',
|
||||
workflow_id='1',
|
||||
user_id='1',
|
||||
tenant_id="1",
|
||||
app_id="1",
|
||||
workflow_id="1",
|
||||
user_id="1",
|
||||
invoke_from=InvokeFrom.WEB_APP,
|
||||
user_from=UserFrom.ACCOUNT,
|
||||
config={
|
||||
'id': 'llm',
|
||||
'data': {
|
||||
'title': '123',
|
||||
'type': 'parameter-extractor',
|
||||
'model': {
|
||||
'provider': 'openai',
|
||||
'name': 'gpt-3.5-turbo',
|
||||
'mode': 'chat',
|
||||
'completion_params': {}
|
||||
},
|
||||
'query': ['sys', 'query'],
|
||||
'parameters': [{
|
||||
'name': 'location',
|
||||
'type': 'string',
|
||||
'description': 'location',
|
||||
'required': True
|
||||
}],
|
||||
'reasoning_mode': 'function_call',
|
||||
'instruction': '{{#sys.query#}}',
|
||||
'memory': None,
|
||||
}
|
||||
}
|
||||
"id": "llm",
|
||||
"data": {
|
||||
"title": "123",
|
||||
"type": "parameter-extractor",
|
||||
"model": {"provider": "openai", "name": "gpt-3.5-turbo", "mode": "chat", "completion_params": {}},
|
||||
"query": ["sys", "query"],
|
||||
"parameters": [{"name": "location", "type": "string", "description": "location", "required": True}],
|
||||
"reasoning_mode": "function_call",
|
||||
"instruction": "{{#sys.query#}}",
|
||||
"memory": None,
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
node._fetch_model_config = get_mocked_fetch_model_config(
|
||||
provider='openai', model='gpt-3.5-turbo', mode='chat', credentials={
|
||||
'openai_api_key': os.environ.get('OPENAI_API_KEY')
|
||||
}
|
||||
provider="openai",
|
||||
model="gpt-3.5-turbo",
|
||||
mode="chat",
|
||||
credentials={"openai_api_key": os.environ.get("OPENAI_API_KEY")},
|
||||
)
|
||||
db.session.close = MagicMock()
|
||||
|
||||
# construct variable pool
|
||||
pool = VariablePool(system_variables={
|
||||
SystemVariableKey.QUERY: 'what\'s the weather in SF',
|
||||
SystemVariableKey.FILES: [],
|
||||
SystemVariableKey.CONVERSATION_ID: 'abababa',
|
||||
SystemVariableKey.USER_ID: 'aaa'
|
||||
}, user_inputs={}, environment_variables=[])
|
||||
pool = VariablePool(
|
||||
system_variables={
|
||||
SystemVariableKey.QUERY: "what's the weather in SF",
|
||||
SystemVariableKey.FILES: [],
|
||||
SystemVariableKey.CONVERSATION_ID: "abababa",
|
||||
SystemVariableKey.USER_ID: "aaa",
|
||||
},
|
||||
user_inputs={},
|
||||
environment_variables=[],
|
||||
)
|
||||
|
||||
result = node.run(pool)
|
||||
|
||||
assert result.status == WorkflowNodeExecutionStatus.SUCCEEDED
|
||||
assert result.outputs.get('location') == 'kawaii'
|
||||
assert result.outputs.get('__reason') == None
|
||||
assert result.outputs.get("location") == "kawaii"
|
||||
assert result.outputs.get("__reason") == None
|
||||
|
||||
process_data = result.process_data
|
||||
|
||||
process_data.get('prompts')
|
||||
process_data.get("prompts")
|
||||
|
||||
for prompt in process_data.get('prompts'):
|
||||
if prompt.get('role') == 'system':
|
||||
assert 'what\'s the weather in SF' in prompt.get('text')
|
||||
for prompt in process_data.get("prompts"):
|
||||
if prompt.get("role") == "system":
|
||||
assert "what's the weather in SF" in prompt.get("text")
|
||||
|
||||
@pytest.mark.parametrize('setup_anthropic_mock', [['none']], indirect=True)
|
||||
|
||||
@pytest.mark.parametrize("setup_anthropic_mock", [["none"]], indirect=True)
|
||||
def test_chat_parameter_extractor(setup_anthropic_mock):
|
||||
"""
|
||||
Test chat parameter extractor.
|
||||
"""
|
||||
node = ParameterExtractorNode(
|
||||
tenant_id='1',
|
||||
app_id='1',
|
||||
workflow_id='1',
|
||||
user_id='1',
|
||||
tenant_id="1",
|
||||
app_id="1",
|
||||
workflow_id="1",
|
||||
user_id="1",
|
||||
invoke_from=InvokeFrom.WEB_APP,
|
||||
user_from=UserFrom.ACCOUNT,
|
||||
config={
|
||||
'id': 'llm',
|
||||
'data': {
|
||||
'title': '123',
|
||||
'type': 'parameter-extractor',
|
||||
'model': {
|
||||
'provider': 'anthropic',
|
||||
'name': 'claude-2',
|
||||
'mode': 'chat',
|
||||
'completion_params': {}
|
||||
},
|
||||
'query': ['sys', 'query'],
|
||||
'parameters': [{
|
||||
'name': 'location',
|
||||
'type': 'string',
|
||||
'description': 'location',
|
||||
'required': True
|
||||
}],
|
||||
'reasoning_mode': 'prompt',
|
||||
'instruction': '',
|
||||
'memory': None,
|
||||
}
|
||||
}
|
||||
"id": "llm",
|
||||
"data": {
|
||||
"title": "123",
|
||||
"type": "parameter-extractor",
|
||||
"model": {"provider": "anthropic", "name": "claude-2", "mode": "chat", "completion_params": {}},
|
||||
"query": ["sys", "query"],
|
||||
"parameters": [{"name": "location", "type": "string", "description": "location", "required": True}],
|
||||
"reasoning_mode": "prompt",
|
||||
"instruction": "",
|
||||
"memory": None,
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
node._fetch_model_config = get_mocked_fetch_model_config(
|
||||
provider='anthropic', model='claude-2', mode='chat', credentials={
|
||||
'anthropic_api_key': os.environ.get('ANTHROPIC_API_KEY')
|
||||
}
|
||||
provider="anthropic",
|
||||
model="claude-2",
|
||||
mode="chat",
|
||||
credentials={"anthropic_api_key": os.environ.get("ANTHROPIC_API_KEY")},
|
||||
)
|
||||
db.session.close = MagicMock()
|
||||
|
||||
# construct variable pool
|
||||
pool = VariablePool(system_variables={
|
||||
SystemVariableKey.QUERY: 'what\'s the weather in SF',
|
||||
SystemVariableKey.FILES: [],
|
||||
SystemVariableKey.CONVERSATION_ID: 'abababa',
|
||||
SystemVariableKey.USER_ID: 'aaa'
|
||||
}, user_inputs={}, environment_variables=[])
|
||||
pool = VariablePool(
|
||||
system_variables={
|
||||
SystemVariableKey.QUERY: "what's the weather in SF",
|
||||
SystemVariableKey.FILES: [],
|
||||
SystemVariableKey.CONVERSATION_ID: "abababa",
|
||||
SystemVariableKey.USER_ID: "aaa",
|
||||
},
|
||||
user_inputs={},
|
||||
environment_variables=[],
|
||||
)
|
||||
|
||||
result = node.run(pool)
|
||||
|
||||
assert result.status == WorkflowNodeExecutionStatus.SUCCEEDED
|
||||
assert result.outputs.get('location') == ''
|
||||
assert result.outputs.get('__reason') == 'Failed to extract result from function call or text response, using empty result.'
|
||||
prompts = result.process_data.get('prompts')
|
||||
assert result.outputs.get("location") == ""
|
||||
assert (
|
||||
result.outputs.get("__reason")
|
||||
== "Failed to extract result from function call or text response, using empty result."
|
||||
)
|
||||
prompts = result.process_data.get("prompts")
|
||||
|
||||
for prompt in prompts:
|
||||
if prompt.get('role') == 'user':
|
||||
if '<structure>' in prompt.get('text'):
|
||||
assert '<structure>\n{"type": "object"' in prompt.get('text')
|
||||
if prompt.get("role") == "user":
|
||||
if "<structure>" in prompt.get("text"):
|
||||
assert '<structure>\n{"type": "object"' in prompt.get("text")
|
||||
|
||||
@pytest.mark.parametrize('setup_openai_mock', [['completion']], indirect=True)
|
||||
|
||||
@pytest.mark.parametrize("setup_openai_mock", [["completion"]], indirect=True)
|
||||
def test_completion_parameter_extractor(setup_openai_mock):
|
||||
"""
|
||||
Test completion parameter extractor.
|
||||
"""
|
||||
node = ParameterExtractorNode(
|
||||
tenant_id='1',
|
||||
app_id='1',
|
||||
workflow_id='1',
|
||||
user_id='1',
|
||||
tenant_id="1",
|
||||
app_id="1",
|
||||
workflow_id="1",
|
||||
user_id="1",
|
||||
invoke_from=InvokeFrom.WEB_APP,
|
||||
user_from=UserFrom.ACCOUNT,
|
||||
config={
|
||||
'id': 'llm',
|
||||
'data': {
|
||||
'title': '123',
|
||||
'type': 'parameter-extractor',
|
||||
'model': {
|
||||
'provider': 'openai',
|
||||
'name': 'gpt-3.5-turbo-instruct',
|
||||
'mode': 'completion',
|
||||
'completion_params': {}
|
||||
"id": "llm",
|
||||
"data": {
|
||||
"title": "123",
|
||||
"type": "parameter-extractor",
|
||||
"model": {
|
||||
"provider": "openai",
|
||||
"name": "gpt-3.5-turbo-instruct",
|
||||
"mode": "completion",
|
||||
"completion_params": {},
|
||||
},
|
||||
'query': ['sys', 'query'],
|
||||
'parameters': [{
|
||||
'name': 'location',
|
||||
'type': 'string',
|
||||
'description': 'location',
|
||||
'required': True
|
||||
}],
|
||||
'reasoning_mode': 'prompt',
|
||||
'instruction': '{{#sys.query#}}',
|
||||
'memory': None,
|
||||
}
|
||||
}
|
||||
"query": ["sys", "query"],
|
||||
"parameters": [{"name": "location", "type": "string", "description": "location", "required": True}],
|
||||
"reasoning_mode": "prompt",
|
||||
"instruction": "{{#sys.query#}}",
|
||||
"memory": None,
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
node._fetch_model_config = get_mocked_fetch_model_config(
|
||||
provider='openai', model='gpt-3.5-turbo-instruct', mode='completion', credentials={
|
||||
'openai_api_key': os.environ.get('OPENAI_API_KEY')
|
||||
}
|
||||
provider="openai",
|
||||
model="gpt-3.5-turbo-instruct",
|
||||
mode="completion",
|
||||
credentials={"openai_api_key": os.environ.get("OPENAI_API_KEY")},
|
||||
)
|
||||
db.session.close = MagicMock()
|
||||
|
||||
# construct variable pool
|
||||
pool = VariablePool(system_variables={
|
||||
SystemVariableKey.QUERY: 'what\'s the weather in SF',
|
||||
SystemVariableKey.FILES: [],
|
||||
SystemVariableKey.CONVERSATION_ID: 'abababa',
|
||||
SystemVariableKey.USER_ID: 'aaa'
|
||||
}, user_inputs={}, environment_variables=[])
|
||||
pool = VariablePool(
|
||||
system_variables={
|
||||
SystemVariableKey.QUERY: "what's the weather in SF",
|
||||
SystemVariableKey.FILES: [],
|
||||
SystemVariableKey.CONVERSATION_ID: "abababa",
|
||||
SystemVariableKey.USER_ID: "aaa",
|
||||
},
|
||||
user_inputs={},
|
||||
environment_variables=[],
|
||||
)
|
||||
|
||||
result = node.run(pool)
|
||||
|
||||
assert result.status == WorkflowNodeExecutionStatus.SUCCEEDED
|
||||
assert result.outputs.get('location') == ''
|
||||
assert result.outputs.get('__reason') == 'Failed to extract result from function call or text response, using empty result.'
|
||||
assert len(result.process_data.get('prompts')) == 1
|
||||
assert 'SF' in result.process_data.get('prompts')[0].get('text')
|
||||
assert result.outputs.get("location") == ""
|
||||
assert (
|
||||
result.outputs.get("__reason")
|
||||
== "Failed to extract result from function call or text response, using empty result."
|
||||
)
|
||||
assert len(result.process_data.get("prompts")) == 1
|
||||
assert "SF" in result.process_data.get("prompts")[0].get("text")
|
||||
|
||||
|
||||
def test_extract_json_response():
|
||||
"""
|
||||
@@ -327,35 +323,30 @@ def test_extract_json_response():
|
||||
"""
|
||||
|
||||
node = ParameterExtractorNode(
|
||||
tenant_id='1',
|
||||
app_id='1',
|
||||
workflow_id='1',
|
||||
user_id='1',
|
||||
tenant_id="1",
|
||||
app_id="1",
|
||||
workflow_id="1",
|
||||
user_id="1",
|
||||
invoke_from=InvokeFrom.WEB_APP,
|
||||
user_from=UserFrom.ACCOUNT,
|
||||
config={
|
||||
'id': 'llm',
|
||||
'data': {
|
||||
'title': '123',
|
||||
'type': 'parameter-extractor',
|
||||
'model': {
|
||||
'provider': 'openai',
|
||||
'name': 'gpt-3.5-turbo-instruct',
|
||||
'mode': 'completion',
|
||||
'completion_params': {}
|
||||
"id": "llm",
|
||||
"data": {
|
||||
"title": "123",
|
||||
"type": "parameter-extractor",
|
||||
"model": {
|
||||
"provider": "openai",
|
||||
"name": "gpt-3.5-turbo-instruct",
|
||||
"mode": "completion",
|
||||
"completion_params": {},
|
||||
},
|
||||
'query': ['sys', 'query'],
|
||||
'parameters': [{
|
||||
'name': 'location',
|
||||
'type': 'string',
|
||||
'description': 'location',
|
||||
'required': True
|
||||
}],
|
||||
'reasoning_mode': 'prompt',
|
||||
'instruction': '{{#sys.query#}}',
|
||||
'memory': None,
|
||||
}
|
||||
}
|
||||
"query": ["sys", "query"],
|
||||
"parameters": [{"name": "location", "type": "string", "description": "location", "required": True}],
|
||||
"reasoning_mode": "prompt",
|
||||
"instruction": "{{#sys.query#}}",
|
||||
"memory": None,
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
result = node._extract_complete_json_response("""
|
||||
@@ -366,83 +357,77 @@ def test_extract_json_response():
|
||||
hello world.
|
||||
""")
|
||||
|
||||
assert result['location'] == 'kawaii'
|
||||
assert result["location"] == "kawaii"
|
||||
|
||||
@pytest.mark.parametrize('setup_anthropic_mock', [['none']], indirect=True)
|
||||
|
||||
@pytest.mark.parametrize("setup_anthropic_mock", [["none"]], indirect=True)
|
||||
def test_chat_parameter_extractor_with_memory(setup_anthropic_mock):
|
||||
"""
|
||||
Test chat parameter extractor with memory.
|
||||
"""
|
||||
node = ParameterExtractorNode(
|
||||
tenant_id='1',
|
||||
app_id='1',
|
||||
workflow_id='1',
|
||||
user_id='1',
|
||||
tenant_id="1",
|
||||
app_id="1",
|
||||
workflow_id="1",
|
||||
user_id="1",
|
||||
invoke_from=InvokeFrom.WEB_APP,
|
||||
user_from=UserFrom.ACCOUNT,
|
||||
config={
|
||||
'id': 'llm',
|
||||
'data': {
|
||||
'title': '123',
|
||||
'type': 'parameter-extractor',
|
||||
'model': {
|
||||
'provider': 'anthropic',
|
||||
'name': 'claude-2',
|
||||
'mode': 'chat',
|
||||
'completion_params': {}
|
||||
},
|
||||
'query': ['sys', 'query'],
|
||||
'parameters': [{
|
||||
'name': 'location',
|
||||
'type': 'string',
|
||||
'description': 'location',
|
||||
'required': True
|
||||
}],
|
||||
'reasoning_mode': 'prompt',
|
||||
'instruction': '',
|
||||
'memory': {
|
||||
'window': {
|
||||
'enabled': True,
|
||||
'size': 50
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
"id": "llm",
|
||||
"data": {
|
||||
"title": "123",
|
||||
"type": "parameter-extractor",
|
||||
"model": {"provider": "anthropic", "name": "claude-2", "mode": "chat", "completion_params": {}},
|
||||
"query": ["sys", "query"],
|
||||
"parameters": [{"name": "location", "type": "string", "description": "location", "required": True}],
|
||||
"reasoning_mode": "prompt",
|
||||
"instruction": "",
|
||||
"memory": {"window": {"enabled": True, "size": 50}},
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
node._fetch_model_config = get_mocked_fetch_model_config(
|
||||
provider='anthropic', model='claude-2', mode='chat', credentials={
|
||||
'anthropic_api_key': os.environ.get('ANTHROPIC_API_KEY')
|
||||
}
|
||||
provider="anthropic",
|
||||
model="claude-2",
|
||||
mode="chat",
|
||||
credentials={"anthropic_api_key": os.environ.get("ANTHROPIC_API_KEY")},
|
||||
)
|
||||
node._fetch_memory = get_mocked_fetch_memory('customized memory')
|
||||
node._fetch_memory = get_mocked_fetch_memory("customized memory")
|
||||
db.session.close = MagicMock()
|
||||
|
||||
# construct variable pool
|
||||
pool = VariablePool(system_variables={
|
||||
SystemVariableKey.QUERY: 'what\'s the weather in SF',
|
||||
SystemVariableKey.FILES: [],
|
||||
SystemVariableKey.CONVERSATION_ID: 'abababa',
|
||||
SystemVariableKey.USER_ID: 'aaa'
|
||||
}, user_inputs={}, environment_variables=[])
|
||||
pool = VariablePool(
|
||||
system_variables={
|
||||
SystemVariableKey.QUERY: "what's the weather in SF",
|
||||
SystemVariableKey.FILES: [],
|
||||
SystemVariableKey.CONVERSATION_ID: "abababa",
|
||||
SystemVariableKey.USER_ID: "aaa",
|
||||
},
|
||||
user_inputs={},
|
||||
environment_variables=[],
|
||||
)
|
||||
|
||||
result = node.run(pool)
|
||||
|
||||
assert result.status == WorkflowNodeExecutionStatus.SUCCEEDED
|
||||
assert result.outputs.get('location') == ''
|
||||
assert result.outputs.get('__reason') == 'Failed to extract result from function call or text response, using empty result.'
|
||||
prompts = result.process_data.get('prompts')
|
||||
assert result.outputs.get("location") == ""
|
||||
assert (
|
||||
result.outputs.get("__reason")
|
||||
== "Failed to extract result from function call or text response, using empty result."
|
||||
)
|
||||
prompts = result.process_data.get("prompts")
|
||||
|
||||
latest_role = None
|
||||
for prompt in prompts:
|
||||
if prompt.get('role') == 'user':
|
||||
if '<structure>' in prompt.get('text'):
|
||||
assert '<structure>\n{"type": "object"' in prompt.get('text')
|
||||
elif prompt.get('role') == 'system':
|
||||
assert 'customized memory' in prompt.get('text')
|
||||
if prompt.get("role") == "user":
|
||||
if "<structure>" in prompt.get("text"):
|
||||
assert '<structure>\n{"type": "object"' in prompt.get("text")
|
||||
elif prompt.get("role") == "system":
|
||||
assert "customized memory" in prompt.get("text")
|
||||
|
||||
if latest_role is not None:
|
||||
assert latest_role != prompt.get('role')
|
||||
assert latest_role != prompt.get("role")
|
||||
|
||||
if prompt.get('role') in ['user', 'assistant']:
|
||||
latest_role = prompt.get('role')
|
||||
if prompt.get("role") in ["user", "assistant"]:
|
||||
latest_role = prompt.get("role")
|
||||
|
||||
@@ -8,42 +8,39 @@ from models.workflow import WorkflowNodeExecutionStatus
|
||||
from tests.integration_tests.workflow.nodes.__mock.code_executor import setup_code_executor_mock
|
||||
|
||||
|
||||
@pytest.mark.parametrize('setup_code_executor_mock', [['none']], indirect=True)
|
||||
@pytest.mark.parametrize("setup_code_executor_mock", [["none"]], indirect=True)
|
||||
def test_execute_code(setup_code_executor_mock):
|
||||
code = '''{{args2}}'''
|
||||
code = """{{args2}}"""
|
||||
node = TemplateTransformNode(
|
||||
tenant_id='1',
|
||||
app_id='1',
|
||||
workflow_id='1',
|
||||
user_id='1',
|
||||
tenant_id="1",
|
||||
app_id="1",
|
||||
workflow_id="1",
|
||||
user_id="1",
|
||||
invoke_from=InvokeFrom.WEB_APP,
|
||||
user_from=UserFrom.END_USER,
|
||||
config={
|
||||
'id': '1',
|
||||
'data': {
|
||||
'title': '123',
|
||||
'variables': [
|
||||
"id": "1",
|
||||
"data": {
|
||||
"title": "123",
|
||||
"variables": [
|
||||
{
|
||||
'variable': 'args1',
|
||||
'value_selector': ['1', '123', 'args1'],
|
||||
"variable": "args1",
|
||||
"value_selector": ["1", "123", "args1"],
|
||||
},
|
||||
{
|
||||
'variable': 'args2',
|
||||
'value_selector': ['1', '123', 'args2']
|
||||
}
|
||||
{"variable": "args2", "value_selector": ["1", "123", "args2"]},
|
||||
],
|
||||
'template': code,
|
||||
}
|
||||
}
|
||||
"template": code,
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
# construct variable pool
|
||||
pool = VariablePool(system_variables={}, user_inputs={}, environment_variables=[])
|
||||
pool.add(['1', '123', 'args1'], 1)
|
||||
pool.add(['1', '123', 'args2'], 3)
|
||||
|
||||
pool.add(["1", "123", "args1"], 1)
|
||||
pool.add(["1", "123", "args2"], 3)
|
||||
|
||||
# execute node
|
||||
result = node.run(pool)
|
||||
|
||||
|
||||
assert result.status == WorkflowNodeExecutionStatus.SUCCEEDED
|
||||
assert result.outputs['output'] == '3'
|
||||
assert result.outputs["output"] == "3"
|
||||
|
||||
@@ -7,78 +7,79 @@ from models.workflow import WorkflowNodeExecutionStatus
|
||||
|
||||
def test_tool_variable_invoke():
|
||||
pool = VariablePool(system_variables={}, user_inputs={}, environment_variables=[])
|
||||
pool.add(['1', '123', 'args1'], '1+1')
|
||||
pool.add(["1", "123", "args1"], "1+1")
|
||||
|
||||
node = ToolNode(
|
||||
tenant_id='1',
|
||||
app_id='1',
|
||||
workflow_id='1',
|
||||
user_id='1',
|
||||
tenant_id="1",
|
||||
app_id="1",
|
||||
workflow_id="1",
|
||||
user_id="1",
|
||||
invoke_from=InvokeFrom.WEB_APP,
|
||||
user_from=UserFrom.ACCOUNT,
|
||||
config={
|
||||
'id': '1',
|
||||
'data': {
|
||||
'title': 'a',
|
||||
'desc': 'a',
|
||||
'provider_id': 'maths',
|
||||
'provider_type': 'builtin',
|
||||
'provider_name': 'maths',
|
||||
'tool_name': 'eval_expression',
|
||||
'tool_label': 'eval_expression',
|
||||
'tool_configurations': {},
|
||||
'tool_parameters': {
|
||||
'expression': {
|
||||
'type': 'variable',
|
||||
'value': ['1', '123', 'args1'],
|
||||
"id": "1",
|
||||
"data": {
|
||||
"title": "a",
|
||||
"desc": "a",
|
||||
"provider_id": "maths",
|
||||
"provider_type": "builtin",
|
||||
"provider_name": "maths",
|
||||
"tool_name": "eval_expression",
|
||||
"tool_label": "eval_expression",
|
||||
"tool_configurations": {},
|
||||
"tool_parameters": {
|
||||
"expression": {
|
||||
"type": "variable",
|
||||
"value": ["1", "123", "args1"],
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
# execute node
|
||||
result = node.run(pool)
|
||||
|
||||
|
||||
assert result.status == WorkflowNodeExecutionStatus.SUCCEEDED
|
||||
assert '2' in result.outputs['text']
|
||||
assert result.outputs['files'] == []
|
||||
assert "2" in result.outputs["text"]
|
||||
assert result.outputs["files"] == []
|
||||
|
||||
|
||||
def test_tool_mixed_invoke():
|
||||
pool = VariablePool(system_variables={}, user_inputs={}, environment_variables=[])
|
||||
pool.add(['1', 'args1'], '1+1')
|
||||
pool.add(["1", "args1"], "1+1")
|
||||
|
||||
node = ToolNode(
|
||||
tenant_id='1',
|
||||
app_id='1',
|
||||
workflow_id='1',
|
||||
user_id='1',
|
||||
tenant_id="1",
|
||||
app_id="1",
|
||||
workflow_id="1",
|
||||
user_id="1",
|
||||
invoke_from=InvokeFrom.WEB_APP,
|
||||
user_from=UserFrom.ACCOUNT,
|
||||
config={
|
||||
'id': '1',
|
||||
'data': {
|
||||
'title': 'a',
|
||||
'desc': 'a',
|
||||
'provider_id': 'maths',
|
||||
'provider_type': 'builtin',
|
||||
'provider_name': 'maths',
|
||||
'tool_name': 'eval_expression',
|
||||
'tool_label': 'eval_expression',
|
||||
'tool_configurations': {},
|
||||
'tool_parameters': {
|
||||
'expression': {
|
||||
'type': 'mixed',
|
||||
'value': '{{#1.args1#}}',
|
||||
"id": "1",
|
||||
"data": {
|
||||
"title": "a",
|
||||
"desc": "a",
|
||||
"provider_id": "maths",
|
||||
"provider_type": "builtin",
|
||||
"provider_name": "maths",
|
||||
"tool_name": "eval_expression",
|
||||
"tool_label": "eval_expression",
|
||||
"tool_configurations": {},
|
||||
"tool_parameters": {
|
||||
"expression": {
|
||||
"type": "mixed",
|
||||
"value": "{{#1.args1#}}",
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
# execute node
|
||||
result = node.run(pool)
|
||||
|
||||
|
||||
assert result.status == WorkflowNodeExecutionStatus.SUCCEEDED
|
||||
assert '2' in result.outputs['text']
|
||||
assert result.outputs['files'] == []
|
||||
assert "2" in result.outputs["text"]
|
||||
assert result.outputs["files"] == []
|
||||
|
||||
Reference in New Issue
Block a user