🔀 refactor(endpoints.py): rename predict_flow endpoint to process_flow and update its functionality

🔀 refactor(constants.tsx): update BASE_API_URL to reflect the changes in the predict_flow endpoint name
The predict_flow endpoint has been renamed to process_flow to better reflect its functionality. The inputs are now passed as a dictionary instead of a PredictRequest object. The tweaks parameter is now optional and is passed as a dictionary. The response model has been updated to reflect the changes in the response. The BASE_API_URL constant in constants.tsx has been updated to reflect the changes in the endpoint name.
This commit is contained in:
Gabriel Luiz Freitas Almeida 2023-06-22 18:57:17 -03:00
commit 176738987b
2 changed files with 14 additions and 14 deletions

View file

@ -1,3 +1,4 @@
from typing import Optional
from langflow.cache.utils import save_uploaded_file from langflow.cache.utils import save_uploaded_file
from langflow.database.models.flow import Flow from langflow.database.models.flow import Flow
from langflow.processing.process import process_graph_cached, process_tweaks from langflow.processing.process import process_graph_cached, process_tweaks
@ -6,8 +7,7 @@ from langflow.utils.logger import logger
from fastapi import APIRouter, Depends, HTTPException, UploadFile from fastapi import APIRouter, Depends, HTTPException, UploadFile
from langflow.api.v1.schemas import ( from langflow.api.v1.schemas import (
PredictRequest, ProcessResponse,
PredictResponse,
UploadFileResponse, UploadFileResponse,
) )
@ -24,14 +24,15 @@ def get_all():
return build_langchain_types_dict() return build_langchain_types_dict()
@router.post("/predict/{flow_id}", response_model=PredictResponse) @router.post("/process/{flow_id}", response_model=ProcessResponse)
async def predict_flow( async def process_flow(
predict_request: PredictRequest,
flow_id: str, flow_id: str,
inputs: dict,
tweaks: Optional[dict] = None,
session: Session = Depends(get_session), session: Session = Depends(get_session),
): ):
""" """
Endpoint to process a message using the flow passed in the bearer token. Endpoint to process an input with a given flow_id.
""" """
try: try:
@ -42,15 +43,14 @@ async def predict_flow(
if flow.data is None: if flow.data is None:
raise ValueError(f"Flow {flow_id} has no data") raise ValueError(f"Flow {flow_id} has no data")
graph_data = flow.data graph_data = flow.data
if predict_request.tweaks: if tweaks:
try: try:
graph_data = process_tweaks(graph_data, predict_request.tweaks) graph_data = process_tweaks(graph_data, tweaks)
except Exception as exc: except Exception as exc:
logger.error(f"Error processing tweaks: {exc}") logger.error(f"Error processing tweaks: {exc}")
response = process_graph_cached(graph_data, predict_request.message) response = process_graph_cached(graph_data, inputs)
return PredictResponse( return ProcessResponse(
result=response.get("result", ""), result=response,
intermediate_steps=response.get("thought", ""),
) )
except Exception as e: except Exception as e:
# Log stack trace # Log stack trace

View file

@ -66,7 +66,7 @@ export const getPythonApiCode = (flow: FlowType): string => {
BASE_API_URL = "${window.location.protocol}//${ BASE_API_URL = "${window.location.protocol}//${
window.location.host window.location.host
}/api/v1/predict" }/api/v1/process"
FLOW_ID = "${flowId}" FLOW_ID = "${flowId}"
# You can tweak the flow by adding a tweaks dictionary # You can tweak the flow by adding a tweaks dictionary
# e.g {"OpenAI-XXXXX": {"model_name": "gpt-4"}} # e.g {"OpenAI-XXXXX": {"model_name": "gpt-4"}}
@ -106,7 +106,7 @@ export const getCurlCode = (flow: FlowType): string => {
return `curl -X POST \\ return `curl -X POST \\
${window.location.protocol}//${ ${window.location.protocol}//${
window.location.host window.location.host
}/api/v1/predict/${flowId} \\ }/api/v1/process/${flowId} \\
-H 'Content-Type: application/json' \\ -H 'Content-Type: application/json' \\
-d '{"message": "Your message", "tweaks": ${JSON.stringify( -d '{"message": "Your message", "tweaks": ${JSON.stringify(
tweaks, tweaks,