fix: loaded objects return the function to be called
This commit is contained in:
parent
3081e50cb8
commit
35adfaa711
3 changed files with 59 additions and 45 deletions
|
|
@ -1,6 +1,6 @@
|
||||||
from fastapi import APIRouter, HTTPException
|
from fastapi import APIRouter, HTTPException
|
||||||
from langflow_backend.interface.types import build_langchain_types_dict, get_type_list
|
from langflow_backend.interface.types import build_langchain_types_dict
|
||||||
from langflow_backend.interface.loading import process_data_graph
|
from langflow_backend.interface.run import process_data_graph
|
||||||
from typing import Any, Dict
|
from typing import Any, Dict
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,4 @@
|
||||||
import contextlib
|
|
||||||
import json
|
import json
|
||||||
import re
|
|
||||||
import io
|
|
||||||
from typing import Any, Dict
|
from typing import Any, Dict
|
||||||
from langflow_backend.interface.types import get_type_list
|
from langflow_backend.interface.types import get_type_list
|
||||||
from langchain.agents.loading import load_agent_executor_from_config
|
from langchain.agents.loading import load_agent_executor_from_config
|
||||||
|
|
@ -38,33 +35,7 @@ def replace_zero_shot_prompt_with_prompt_template(nodes):
|
||||||
return nodes
|
return nodes
|
||||||
|
|
||||||
|
|
||||||
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"]
|
|
||||||
# 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)
|
|
||||||
|
|
||||||
# Process json
|
|
||||||
result, thought = get_result_and_thought(extracted_json, message)
|
|
||||||
|
|
||||||
# Remove unnecessary data from response
|
|
||||||
begin = thought.rfind(message)
|
|
||||||
thought = thought[(begin + len(message)) :]
|
|
||||||
|
|
||||||
return {
|
|
||||||
"result": result,
|
|
||||||
"thought": re.sub(
|
|
||||||
r"\x1b\[([0-9,A-Z]{1,2}(;[0-9,A-Z]{1,2})?)?[m|K]", "", thought
|
|
||||||
).strip(),
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def load_langchain_type_from_config(config: Dict[str, Any]):
|
def load_langchain_type_from_config(config: Dict[str, Any]):
|
||||||
|
|
@ -72,27 +43,16 @@ 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)
|
return load_agent_executor_from_config(config).run
|
||||||
elif config["_type"] in type_list["chains"]:
|
elif config["_type"] in type_list["chains"]:
|
||||||
return load_chain_from_config(config)
|
return load_chain_from_config(config).run
|
||||||
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:
|
||||||
raise ValueError("Type should be either agent, chain or llm")
|
raise ValueError("Type should be either agent, chain or llm")
|
||||||
|
|
||||||
|
|
||||||
def get_result_and_thought(extracted_json: Dict[str, Any], message: str):
|
|
||||||
"""Get result and thought from extracted json"""
|
|
||||||
# Get type list
|
|
||||||
try:
|
|
||||||
loaded = load_langchain_type_from_config(config=extracted_json)
|
|
||||||
with io.StringIO() as output_buffer, contextlib.redirect_stdout(output_buffer):
|
|
||||||
result = loaded(message)
|
|
||||||
thought = output_buffer.getvalue()
|
|
||||||
except Exception as e:
|
|
||||||
result = f"Error: {str(e)}"
|
|
||||||
thought = ""
|
|
||||||
return result, thought
|
|
||||||
|
|
||||||
|
|
||||||
def build_prompt_template(prompt, tools):
|
def build_prompt_template(prompt, tools):
|
||||||
|
|
|
||||||
54
langflow/backend/langflow_backend/interface/run.py
Normal file
54
langflow/backend/langflow_backend/interface/run.py
Normal file
|
|
@ -0,0 +1,54 @@
|
||||||
|
import contextlib
|
||||||
|
import io
|
||||||
|
import re
|
||||||
|
from typing import Any, Dict
|
||||||
|
from langflow_backend.interface.loading import (
|
||||||
|
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]):
|
||||||
|
"""
|
||||||
|
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"]
|
||||||
|
# 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)
|
||||||
|
|
||||||
|
# Process json
|
||||||
|
result, thought = get_result_and_thought(extracted_json, message)
|
||||||
|
|
||||||
|
# Remove unnecessary data from response
|
||||||
|
begin = thought.rfind(message)
|
||||||
|
thought = thought[(begin + len(message)) :]
|
||||||
|
|
||||||
|
return {
|
||||||
|
"result": result,
|
||||||
|
"thought": re.sub(
|
||||||
|
r"\x1b\[([0-9,A-Z]{1,2}(;[0-9,A-Z]{1,2})?)?[m|K]", "", thought
|
||||||
|
).strip(),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def get_result_and_thought(extracted_json: Dict[str, Any], message: str):
|
||||||
|
"""Get result and thought from extracted json"""
|
||||||
|
# Get type list
|
||||||
|
try:
|
||||||
|
loaded = load_langchain_type_from_config(config=extracted_json)
|
||||||
|
with io.StringIO() as output_buffer, contextlib.redirect_stdout(output_buffer):
|
||||||
|
result = loaded(message)
|
||||||
|
thought = output_buffer.getvalue()
|
||||||
|
except Exception as e:
|
||||||
|
result = f"Error: {str(e)}"
|
||||||
|
thought = ""
|
||||||
|
return result, thought
|
||||||
Loading…
Add table
Add a link
Reference in a new issue