From 7817c64591333ac3f45b0adc823c51c5b8098c59 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Wed, 14 Jun 2023 07:21:25 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(endpoints.py):=20change=20pr?= =?UTF-8?q?edict=5Fflow=20function=20signature=20to=20include=20flow=5Fid?= =?UTF-8?q?=20and=20session=20dependencies=20=E2=9C=A8=20feat(endpoints.py?= =?UTF-8?q?):=20add=20flow=5Fid=20parameter=20to=20predict=5Fflow=20functi?= =?UTF-8?q?on=20to=20allow=20for=20running=20a=20flow=20by=20ID=20The=20pr?= =?UTF-8?q?edict=5Fflow=20function=20now=20includes=20a=20flow=5Fid=20para?= =?UTF-8?q?meter=20and=20a=20session=20dependency=20to=20allow=20for=20run?= =?UTF-8?q?ning=20a=20flow=20by=20ID.=20The=20flow=20object=20is=20retriev?= =?UTF-8?q?ed=20from=20the=20session=20using=20the=20flow=5Fid=20parameter?= =?UTF-8?q?.=20If=20the=20flow=20is=20not=20found,=20a=20ValueError=20is?= =?UTF-8?q?=20raised.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 🔨 refactor(constants.tsx): change API_URL constant to BASE_API_URL and add flow_id parameter to run_flow function The API_URL constant has been renamed to BASE_API_URL to better reflect its purpose. The run_flow function now includes a flow_id parameter to allow for running a flow by ID. The flow_id parameter is used to construct the API URL. --- src/backend/langflow/api/v1/endpoints.py | 8 ++++-- src/frontend/src/constants.tsx | 31 ++++++++++++++---------- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/src/backend/langflow/api/v1/endpoints.py b/src/backend/langflow/api/v1/endpoints.py index 35a14b822..825dd3934 100644 --- a/src/backend/langflow/api/v1/endpoints.py +++ b/src/backend/langflow/api/v1/endpoints.py @@ -38,16 +38,20 @@ def get_all(): return build_langchain_types_dict() -@router.post("/predict", response_model=PredictResponse) +@router.post("/predict/{flow_id}", response_model=PredictResponse) async def predict_flow( predict_request: PredictRequest, - flow: Flow = Depends(get_flow_from_token), + flow_id: str, + session: Session = Depends(get_session), ): """ Endpoint to process a message using the flow passed in the bearer token. """ try: + flow = session.get(Flow, flow_id) + if flow is None: + raise ValueError(f"Flow {flow_id} not found") graph_data = flow.data if predict_request.tweaks: graph_data = process_tweaks(graph_data, predict_request.tweaks) diff --git a/src/frontend/src/constants.tsx b/src/frontend/src/constants.tsx index da000a862..d5ee010a7 100644 --- a/src/frontend/src/constants.tsx +++ b/src/frontend/src/constants.tsx @@ -52,28 +52,33 @@ export const TEXT_DIALOG_SUBTITLE = "Edit you text."; export const getPythonApiCode = (flowId: string): string => { return `import requests -FLOW_ID = "${flowId}" -API_URL = f"${window.location.protocol}//${window.location.host}/predict" +BASE_API_URL = "${window.location.protocol}//${window.location.host}/predict" -def run_flow(message, tweaks=None): +def run_flow(message: str, flow_id: str, tweaks: dict = None) -> dict: + """ + Run a flow with a given message and optional tweaks. + + :param message: The message to send to the flow + :param flow_id: The ID of the flow to run + :param tweaks: Optional tweaks to customize the flow + :return: The JSON response from the flow + """ + api_url = f"{BASE_API_URL}/{flow_id}" + + payload = {"message": message} if tweaks: - payload = {'message': message, 'tweaks': tweaks} - else: - payload = {'message': message} + payload["tweaks"] = tweaks - headers = {'Authorization': - f'Bearer {FLOW_ID}', - 'Content-Type': 'application/json' - } - - response = requests.post(API_URL, json=payload) + response = requests.post(api_url, json=payload) return response.json() # Setup any tweaks you want to apply to the flow tweaks = {} # {"nodeId": {"key": "value"}, "nodeId2": {"key": "value"}} -print(run_flow("Your message", tweaks=tweaks))`; +FLOW_ID = "${flowId}" + +print(run_flow("Your message", flow_id=FLOW_ID, tweaks=tweaks))`; }; /**