Merge branch 'dev' of personal:logspace-ai/langflow into dev

This commit is contained in:
anovazzi1 2023-03-09 20:18:02 -03:00
commit 43e79e2c90
4 changed files with 35 additions and 32 deletions

View file

@ -0,0 +1 @@
from langflow_backend.interface.loading import load_flow_from_json

View file

@ -12,13 +12,22 @@ def load_flow_from_json(path: str):
with open(path, "r") as f: with open(path, "r") as f:
flow_graph = json.load(f) flow_graph = json.load(f)
data_graph = flow_graph["data"] data_graph = flow_graph["data"]
edges = data_graph["edges"] extracted_json = extract_json(data_graph)
nodes = replace_zero_shot_prompt_with_prompt_template(data_graph["nodes"])
root = payload.get_root_node(data_graph)
extracted_json = payload.build_json(root, nodes, edges)
return load_langchain_type_from_config(config=extracted_json) return load_langchain_type_from_config(config=extracted_json)
def extract_json(data_graph):
nodes = data_graph["nodes"]
# Substitute ZeroShotPrompt with PromptTemplate
nodes = replace_zero_shot_prompt_with_prompt_template(nodes)
# Add input variables
nodes = payload.extract_input_variables(nodes)
# Nodes, edges and root node
edges = data_graph["edges"]
root = payload.get_root_node(nodes, edges)
return payload.build_json(root, nodes, edges)
def replace_zero_shot_prompt_with_prompt_template(nodes): def replace_zero_shot_prompt_with_prompt_template(nodes):
"""Replace ZeroShotPrompt with PromptTemplate""" """Replace ZeroShotPrompt with PromptTemplate"""
for node in nodes: for node in nodes:
@ -40,9 +49,9 @@ def load_langchain_type_from_config(config: Dict[str, Any]):
# Get type list # Get type list
type_list = get_type_list() type_list = get_type_list()
if config["_type"] in type_list["agents"]: if config["_type"] in type_list["agents"]:
return load_agent_executor_from_config(config).run return load_agent_executor_from_config(config)
elif config["_type"] in type_list["chains"]: elif config["_type"] in type_list["chains"]:
return load_chain_from_config(config).run return load_chain_from_config(config)
elif config["_type"] in type_list["llms"]: elif config["_type"] in type_list["llms"]:
return load_llm_from_config(config) return load_llm_from_config(config)
else: else:

View file

@ -2,11 +2,7 @@ import contextlib
import io import io
import re import re
from typing import Any, Dict from typing import Any, Dict
from langflow_backend.interface.loading import ( from langflow_backend.interface import loading
load_langchain_type_from_config,
replace_zero_shot_prompt_with_prompt_template,
)
from langflow_backend.utils import payload
def process_data_graph(data_graph: Dict[str, Any]): def process_data_graph(data_graph: Dict[str, Any]):
@ -14,17 +10,10 @@ def process_data_graph(data_graph: Dict[str, Any]):
Process data graph by extracting input variables and replacing ZeroShotPrompt Process data graph by extracting input variables and replacing ZeroShotPrompt
with PromptTemplate,then run the graph and return the result and thought. with PromptTemplate,then run the graph and return the result and thought.
""" """
nodes = data_graph["nodes"]
# Substitute ZeroShotPrompt with PromptTemplate
nodes = replace_zero_shot_prompt_with_prompt_template(nodes)
# Add input variables
data_graph = payload.extract_input_variables(data_graph)
# Nodes, edges and root node
message = data_graph["message"]
edges = data_graph["edges"]
root = payload.get_root_node(data_graph)
extracted_json = payload.build_json(root, nodes, edges)
extracted_json = loading.extract_json(data_graph)
message = data_graph["message"]
# Process json # Process json
result, thought = get_result_and_thought(extracted_json, message) result, thought = get_result_and_thought(extracted_json, message)
@ -42,11 +31,17 @@ def process_data_graph(data_graph: Dict[str, Any]):
def get_result_and_thought(extracted_json: Dict[str, Any], message: str): def get_result_and_thought(extracted_json: Dict[str, Any], message: str):
"""Get result and thought from extracted json""" """Get result and thought from extracted json"""
# Get type list
try: try:
loaded = load_langchain_type_from_config(config=extracted_json) loaded_langchain = loading.load_langchain_type_from_config(
config=extracted_json
)
with io.StringIO() as output_buffer, contextlib.redirect_stdout(output_buffer): with io.StringIO() as output_buffer, contextlib.redirect_stdout(output_buffer):
result = loaded(message) result = loaded_langchain(message)
result = (
result.get(loaded_langchain.output_keys[0])
if isinstance(result, dict)
else result
)
thought = output_buffer.getvalue() thought = output_buffer.getvalue()
except Exception as e: except Exception as e:
result = f"Error: {str(e)}" result = f"Error: {str(e)}"

View file

@ -2,12 +2,12 @@ import contextlib
import re import re
def extract_input_variables(data): def extract_input_variables(nodes):
""" """
Extracts input variables from the template Extracts input variables from the template
and adds them to the input_variables field. and adds them to the input_variables field.
""" """
for node in data["nodes"]: for node in nodes:
with contextlib.suppress(Exception): with contextlib.suppress(Exception):
if "input_variables" in node["data"]["node"]["template"]: if "input_variables" in node["data"]["node"]["template"]:
if node["data"]["node"]["template"]["_type"] == "prompt": if node["data"]["node"]["template"]["_type"] == "prompt":
@ -24,17 +24,15 @@ def extract_input_variables(data):
else: else:
variables = [] variables = []
node["data"]["node"]["template"]["input_variables"]["value"] = variables node["data"]["node"]["template"]["input_variables"]["value"] = variables
return data return nodes
def get_root_node(data): def get_root_node(nodes, edges):
""" """
Returns the root node of the template. Returns the root node of the template.
""" """
incoming_edges = {edge["source"] for edge in data["edges"]} incoming_edges = {edge["source"] for edge in edges}
return next( return next((node for node in nodes if node["id"] not in incoming_edges), None)
(node for node in data["nodes"] if node["id"] not in incoming_edges), None
)
def build_json(root, nodes, edges): def build_json(root, nodes, edges):