Replies: 1 comment 2 replies
-
|
To parse the import json
from pathlib import Path
from typing import List, Optional, Union
from dotenv import load_dotenv
from loguru import logger
from langflow.graph import Graph
from langflow.graph.schema import RunOutputs
from langflow.processing.process import process_tweaks, run_graph
from langflow.utils.logger import configure
from langflow.utils.util import update_settings
def run_flow_from_json(
flow: Union[Path, str, dict],
input_value: str,
tweaks: Optional[dict] = None,
input_type: str = "chat",
output_type: str = "chat",
output_component: Optional[str] = None,
log_level: Optional[str] = None,
log_file: Optional[str] = None,
env_file: Optional[str] = None,
cache: Optional[str] = None,
disable_logs: Optional[bool] = True,
fallback_to_env_vars: bool = False,
) -> List[RunOutputs]:
"""
Run a flow from a JSON file or dictionary.
Args:
flow (Union[Path, str, dict]): The path to the JSON file or the JSON dictionary representing the flow.
input_value (str): The input value to be processed by the flow.
tweaks (Optional[dict], optional): Optional tweaks to be applied to the flow. Defaults to None.
input_type (str, optional): The type of the input value. Defaults to "chat".
output_type (str, optional): The type of the output value. Defaults to "chat".
output_component (Optional[str], optional): The specific component to output. Defaults to None.
log_level (Optional[str], optional): The log level to use. Defaults to None.
log_file (Optional[str], optional): The log file to write logs to. Defaults to None.
env_file (Optional[str], optional): The environment file to load. Defaults to None.
cache (Optional[str], optional): The cache directory to use. Defaults to None.
disable_logs (Optional[bool], optional): Whether to disable logs. Defaults to True.
fallback_to_env_vars (bool, optional): Whether Global Variables should fallback to environment variables if not found. Defaults to False.
Returns:
List[RunOutputs]: A list of RunOutputs objects representing the results of running the flow.
"""
# Set all streaming to false
try:
import nest_asyncio # type: ignore
nest_asyncio.apply()
except Exception as e:
logger.warning(f"Could not apply nest_asyncio: {e}")
if tweaks is None:
tweaks = {}
tweaks["stream"] = False
graph = load_flow_from_json(
flow=flow,
tweaks=tweaks,
log_level=log_level,
log_file=log_file,
env_file=env_file,
cache=cache,
disable_logs=disable_logs,
)
# Extract and process the `message` and `job` fields from the input_value
input_json = json.loads(input_value)
message = input_json.get("message")
job = input_json.get("job")
# Process the extracted fields as needed
# For example, you can log them or pass them to the flow
logger.info(f"Message: {message}")
logger.info(f"Job: {job}")
result = run_graph(
graph=graph,
input_value=input_value,
input_type=input_type,
output_type=output_type,
output_component=output_component,
fallback_to_env_vars=fallback_to_env_vars,
)
return resultIn this example, the |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Is it possible to send a JSON object as API flow
input_valuelike so:curl -X POST \ "http://localhost/api/v1/run/my-flow" \ -H 'Content-Type: application/json'\ -d '{"input_value": { "name": "Dante" "job": "Pilot", "message": "What could I do to make my passengers more comfortable?" }, "output_type": "chat", "input_type": "chat", "tweaks": {} }'This could help change the flow based on JSON values.
Beta Was this translation helpful? Give feedback.
All reactions