* Add options field to FIELD_FORMAT_ATTRIBUTES constant and import pathlib in test_initial_setup.py * Update TEXT_FILE_TYPES in utils.py and handle missing file path error in Vertex class * Fix tweak value assignment in process.py and clear session cache in test_process.py * New lock * Update repository URLs and fix file paths in code blocks * Fix data retrieval in test_pickle_graph and test_pickle_each_vertex in test_graph.py * Refactor load_starter_projects function to include type hinting in setup.py * Update name of Basic Prompting (Hello, world!) project to Basic Prompting (Hello, World) * Refactor Graph.process() method to accept start_component_id parameter * Refactor test_endpoints.py to use "Chat Output" instead of "Prompt Output" and "ChatOutput" instead of "TextOutput"
303 lines
9.3 KiB
Python
303 lines
9.3 KiB
Python
import pytest
|
|
from langflow.processing.process import process_tweaks
|
|
from langflow.services.deps import get_session_service
|
|
|
|
|
|
def test_no_tweaks():
|
|
graph_data = {
|
|
"data": {
|
|
"nodes": [
|
|
{
|
|
"id": "node1",
|
|
"data": {
|
|
"node": {
|
|
"template": {
|
|
"param1": {"value": 1},
|
|
"param2": {"value": 2},
|
|
}
|
|
}
|
|
},
|
|
},
|
|
{
|
|
"id": "node2",
|
|
"data": {
|
|
"node": {
|
|
"template": {
|
|
"param1": {"value": 3},
|
|
"param2": {"value": 4},
|
|
}
|
|
}
|
|
},
|
|
},
|
|
]
|
|
}
|
|
}
|
|
tweaks = {}
|
|
result = process_tweaks(graph_data, tweaks)
|
|
assert result == graph_data
|
|
|
|
|
|
def test_single_tweak():
|
|
graph_data = {
|
|
"data": {
|
|
"nodes": [
|
|
{
|
|
"id": "node1",
|
|
"data": {
|
|
"node": {
|
|
"template": {
|
|
"param1": {"value": 1},
|
|
"param2": {"value": 2},
|
|
}
|
|
}
|
|
},
|
|
},
|
|
{
|
|
"id": "node2",
|
|
"data": {
|
|
"node": {
|
|
"template": {
|
|
"param1": {"value": 3},
|
|
"param2": {"value": 4},
|
|
}
|
|
}
|
|
},
|
|
},
|
|
]
|
|
}
|
|
}
|
|
tweaks = {"node1": {"param1": 5}}
|
|
expected_result = {
|
|
"data": {
|
|
"nodes": [
|
|
{
|
|
"id": "node1",
|
|
"data": {
|
|
"node": {
|
|
"template": {
|
|
"param1": {"value": 5},
|
|
"param2": {"value": 2},
|
|
}
|
|
}
|
|
},
|
|
},
|
|
{
|
|
"id": "node2",
|
|
"data": {
|
|
"node": {
|
|
"template": {
|
|
"param1": {"value": 3},
|
|
"param2": {"value": 4},
|
|
}
|
|
}
|
|
},
|
|
},
|
|
]
|
|
}
|
|
}
|
|
result = process_tweaks(graph_data, tweaks)
|
|
assert result == expected_result
|
|
|
|
|
|
def test_multiple_tweaks():
|
|
graph_data = {
|
|
"data": {
|
|
"nodes": [
|
|
{
|
|
"id": "node1",
|
|
"data": {
|
|
"node": {
|
|
"template": {
|
|
"param1": {"value": 1},
|
|
"param2": {"value": 2},
|
|
}
|
|
}
|
|
},
|
|
},
|
|
{
|
|
"id": "node2",
|
|
"data": {
|
|
"node": {
|
|
"template": {
|
|
"param1": {"value": 3},
|
|
"param2": {"value": 4},
|
|
}
|
|
}
|
|
},
|
|
},
|
|
]
|
|
}
|
|
}
|
|
tweaks = {
|
|
"node1": {"param1": 5, "param2": 6},
|
|
"node2": {"param1": 7},
|
|
}
|
|
expected_result = {
|
|
"data": {
|
|
"nodes": [
|
|
{
|
|
"id": "node1",
|
|
"data": {
|
|
"node": {
|
|
"template": {
|
|
"param1": {"value": 5},
|
|
"param2": {"value": 6},
|
|
}
|
|
}
|
|
},
|
|
},
|
|
{
|
|
"id": "node2",
|
|
"data": {
|
|
"node": {
|
|
"template": {
|
|
"param1": {"value": 7},
|
|
"param2": {"value": 4},
|
|
}
|
|
}
|
|
},
|
|
},
|
|
]
|
|
}
|
|
}
|
|
result = process_tweaks(graph_data, tweaks)
|
|
assert result == expected_result
|
|
|
|
|
|
# Test twekas that just pass the param and value but no node id.
|
|
# This is a new feature that was added to the process_tweaks function
|
|
def test_tweak_no_node_id():
|
|
graph_data = {
|
|
"data": {
|
|
"nodes": [
|
|
{
|
|
"id": "node1",
|
|
"data": {
|
|
"node": {
|
|
"template": {
|
|
"param1": {"value": 1},
|
|
"param2": {"value": 2},
|
|
}
|
|
}
|
|
},
|
|
},
|
|
{
|
|
"id": "node2",
|
|
"data": {
|
|
"node": {
|
|
"template": {
|
|
"param1": {"value": 3},
|
|
"param2": {"value": 4},
|
|
}
|
|
}
|
|
},
|
|
},
|
|
]
|
|
}
|
|
}
|
|
tweaks = {"param1": 5}
|
|
expected_result = {
|
|
"data": {
|
|
"nodes": [
|
|
{
|
|
"id": "node1",
|
|
"data": {
|
|
"node": {
|
|
"template": {
|
|
"param1": {"value": 5},
|
|
"param2": {"value": 2},
|
|
}
|
|
}
|
|
},
|
|
},
|
|
{
|
|
"id": "node2",
|
|
"data": {
|
|
"node": {
|
|
"template": {
|
|
"param1": {"value": 5},
|
|
"param2": {"value": 4},
|
|
}
|
|
}
|
|
},
|
|
},
|
|
]
|
|
}
|
|
}
|
|
result = process_tweaks(graph_data, tweaks)
|
|
assert result == expected_result
|
|
|
|
|
|
def test_tweak_not_in_template():
|
|
graph_data = {
|
|
"data": {
|
|
"nodes": [
|
|
{
|
|
"id": "node1",
|
|
"data": {
|
|
"node": {
|
|
"template": {
|
|
"param1": {"value": 1},
|
|
"param2": {"value": 2},
|
|
}
|
|
}
|
|
},
|
|
},
|
|
{
|
|
"id": "node2",
|
|
"data": {
|
|
"node": {
|
|
"template": {
|
|
"param1": {"value": 3},
|
|
"param2": {"value": 4},
|
|
}
|
|
}
|
|
},
|
|
},
|
|
]
|
|
}
|
|
}
|
|
tweaks = {"node1": {"param3": 5}}
|
|
result = process_tweaks(graph_data, tweaks)
|
|
assert result == graph_data
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_load_langchain_object_with_cached_session(client, basic_graph_data):
|
|
# Provide a non-existent session_id
|
|
session_service = get_session_service()
|
|
session_id1 = "non-existent-session-id"
|
|
graph1, artifacts1 = await session_service.load_session(session_id1, basic_graph_data)
|
|
# Use the new session_id to get the langchain_object again
|
|
graph2, artifacts2 = await session_service.load_session(session_id1, basic_graph_data)
|
|
|
|
assert graph1 == graph2
|
|
assert artifacts1 == artifacts2
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_load_langchain_object_with_no_cached_session(client, basic_graph_data):
|
|
# Provide a non-existent session_id
|
|
session_service = get_session_service()
|
|
session_id1 = "non-existent-session-id"
|
|
session_id = session_service.build_key(session_id1, basic_graph_data)
|
|
graph1, artifacts1 = await session_service.load_session(session_id, data_graph=basic_graph_data, flow_id="flow_id")
|
|
# Clear the cache
|
|
await session_service.clear_session(session_id)
|
|
# Use the new session_id to get the graph again
|
|
graph2, artifacts2 = await session_service.load_session(session_id, data_graph=basic_graph_data, flow_id="flow_id")
|
|
|
|
# Since the cache was cleared, objects should be different
|
|
assert id(graph1) != id(graph2)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_load_langchain_object_without_session_id(client, basic_graph_data):
|
|
# Provide a non-existent session_id
|
|
session_service = get_session_service()
|
|
session_id1 = None
|
|
graph1, artifacts1 = await session_service.load_session(session_id1, data_graph=basic_graph_data, flow_id="flow_id")
|
|
# Use the new session_id to get the langchain_object again
|
|
graph2, artifacts2 = await session_service.load_session(session_id1, data_graph=basic_graph_data, flow_id="flow_id")
|
|
|
|
assert graph1 == graph2
|