docs: adding docstrings
This commit is contained in:
parent
aaccc7776b
commit
26e06bcabf
2 changed files with 16 additions and 12 deletions
|
|
@ -11,6 +11,7 @@ from langflow_backend.utils import payload
|
||||||
|
|
||||||
|
|
||||||
def load_flow_from_json(path: str):
|
def load_flow_from_json(path: str):
|
||||||
|
"""Load flow from json file"""
|
||||||
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"]
|
||||||
|
|
@ -22,6 +23,7 @@ def load_flow_from_json(path: str):
|
||||||
|
|
||||||
|
|
||||||
def replace_zero_shot_prompt_with_prompt_template(nodes):
|
def replace_zero_shot_prompt_with_prompt_template(nodes):
|
||||||
|
"""Replace ZeroShotPrompt with PromptTemplate"""
|
||||||
for node in nodes:
|
for node in nodes:
|
||||||
if node["data"]["type"] == "ZeroShotPrompt":
|
if node["data"]["type"] == "ZeroShotPrompt":
|
||||||
# Build Prompt Template
|
# Build Prompt Template
|
||||||
|
|
@ -37,6 +39,8 @@ def replace_zero_shot_prompt_with_prompt_template(nodes):
|
||||||
|
|
||||||
|
|
||||||
def process_data_graph(data_graph: Dict[str, Any]):
|
def process_data_graph(data_graph: Dict[str, Any]):
|
||||||
|
"""Process data graph by extracting input variables and replacing ZeroShotPrompt with PromptTemplate,
|
||||||
|
then run the graph and return the result and thought."""
|
||||||
nodes = data_graph["nodes"]
|
nodes = data_graph["nodes"]
|
||||||
# Substitute ZeroShotPrompt with PromptTemplate
|
# Substitute ZeroShotPrompt with PromptTemplate
|
||||||
nodes = replace_zero_shot_prompt_with_prompt_template(nodes)
|
nodes = replace_zero_shot_prompt_with_prompt_template(nodes)
|
||||||
|
|
@ -64,6 +68,7 @@ def process_data_graph(data_graph: Dict[str, Any]):
|
||||||
|
|
||||||
|
|
||||||
def load_langchain_type_from_config(config: Dict[str, Any]):
|
def load_langchain_type_from_config(config: Dict[str, Any]):
|
||||||
|
"""Load langchain type from config"""
|
||||||
# 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"]:
|
||||||
|
|
@ -77,6 +82,7 @@ def load_langchain_type_from_config(config: 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 type list
|
# Get type list
|
||||||
try:
|
try:
|
||||||
loaded = load_langchain_type_from_config(config=extracted_json)
|
loaded = load_langchain_type_from_config(config=extracted_json)
|
||||||
|
|
@ -90,6 +96,7 @@ def get_result_and_thought(extracted_json: Dict[str, Any], message: str):
|
||||||
|
|
||||||
|
|
||||||
def build_prompt_template(prompt, tools):
|
def build_prompt_template(prompt, tools):
|
||||||
|
"""Build PromptTemplate from ZeroShotPrompt"""
|
||||||
prefix = prompt["node"]["template"]["prefix"]["value"]
|
prefix = prompt["node"]["template"]["prefix"]["value"]
|
||||||
suffix = prompt["node"]["template"]["suffix"]["value"]
|
suffix = prompt["node"]["template"]["suffix"]["value"]
|
||||||
format_instructions = prompt["node"]["template"]["format_instructions"]["value"]
|
format_instructions = prompt["node"]["template"]["format_instructions"]["value"]
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
import contextlib
|
||||||
import re
|
import re
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -6,7 +7,7 @@ def extract_input_variables(data):
|
||||||
Extracts input variables from the template and adds them to the input_variables field.
|
Extracts input variables from the template and adds them to the input_variables field.
|
||||||
"""
|
"""
|
||||||
for node in data["nodes"]:
|
for node in data["nodes"]:
|
||||||
try:
|
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":
|
||||||
variables = re.findall(
|
variables = re.findall(
|
||||||
|
|
@ -22,8 +23,6 @@ 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
|
||||||
except:
|
|
||||||
pass
|
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -31,16 +30,16 @@ def get_root_node(data):
|
||||||
"""
|
"""
|
||||||
Returns the root node of the template.
|
Returns the root node of the template.
|
||||||
"""
|
"""
|
||||||
root = None
|
|
||||||
incoming_edges = {edge["source"] for edge in data["edges"]}
|
incoming_edges = {edge["source"] for edge in data["edges"]}
|
||||||
for node in data["nodes"]:
|
return next(
|
||||||
if node["id"] not in incoming_edges:
|
(node for node in data["nodes"] if node["id"] not in incoming_edges), None
|
||||||
root = node
|
)
|
||||||
break
|
|
||||||
return root
|
|
||||||
|
|
||||||
|
|
||||||
def build_json(root, nodes, edges):
|
def build_json(root, nodes, edges):
|
||||||
|
"""
|
||||||
|
Builds a json from the nodes and edges
|
||||||
|
"""
|
||||||
edge_ids = [edge["source"] for edge in edges if edge["target"] == root["id"]]
|
edge_ids = [edge["source"] for edge in edges if edge["target"] == root["id"]]
|
||||||
local_nodes = [node for node in nodes if node["id"] in edge_ids]
|
local_nodes = [node for node in nodes if node["id"] in edge_ids]
|
||||||
|
|
||||||
|
|
@ -71,9 +70,7 @@ def build_json(root, nodes, edges):
|
||||||
|
|
||||||
if value["required"] and not children:
|
if value["required"] and not children:
|
||||||
raise ValueError(f"No child with type {module_type} found")
|
raise ValueError(f"No child with type {module_type} found")
|
||||||
values = [
|
values = [build_json(child, nodes, edges) for child in children]
|
||||||
build_json(child, nodes, edges) for child in children
|
|
||||||
]
|
|
||||||
value = list(values) if value["list"] else next(iter(values), None)
|
value = list(values) if value["list"] else next(iter(values), None)
|
||||||
final_dict[key] = value
|
final_dict[key] = value
|
||||||
return final_dict
|
return final_dict
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue