From 5dce2cddef9d2bef9385e72882b58be35faebd6b Mon Sep 17 00:00:00 2001 From: ogabrielluiz Date: Tue, 28 May 2024 15:45:16 -0300 Subject: [PATCH] chore: Update Python API code to use optional typing and fix formatting --- .../apiModal/utils/get-python-api-code.tsx | 74 +++++++++---------- .../modals/apiModal/utils/get-python-code.tsx | 12 +-- 2 files changed, 43 insertions(+), 43 deletions(-) diff --git a/src/frontend/src/modals/apiModal/utils/get-python-api-code.tsx b/src/frontend/src/modals/apiModal/utils/get-python-api-code.tsx index b77ff3c5e..a15c3ea94 100644 --- a/src/frontend/src/modals/apiModal/utils/get-python-api-code.tsx +++ b/src/frontend/src/modals/apiModal/utils/get-python-api-code.tsx @@ -8,51 +8,51 @@ export default function getPythonApiCode( flowId: string, isAuth: boolean, - tweaksBuildedObject, + tweaksBuildedObject ): string { const tweaksObject = tweaksBuildedObject[0]; return `import requests - from typing import Optional +from typing import Optional - BASE_API_URL = "${window.location.protocol}//${window.location.host}/api/v1/run" - FLOW_ID = "${flowId}" - # You can tweak the flow by adding a tweaks dictionary - # e.g {"OpenAI-XXXXX": {"model_name": "gpt-4"}} - TWEAKS = ${JSON.stringify(tweaksObject, null, 2)} +BASE_API_URL = "${window.location.protocol}//${window.location.host}/api/v1/run" +FLOW_ID = "${flowId}" +# You can tweak the flow by adding a tweaks dictionary +# e.g {"OpenAI-XXXXX": {"model_name": "gpt-4"}} +TWEAKS = ${JSON.stringify(tweaksObject, null, 2)} - def run_flow(message: str, - flow_id: str, - output_type: str = "chat", - input_type: str = "chat", - tweaks: Optional[dict] = None, - api_key: Optional[str] = None) -> dict: - """ - Run a flow with a given message and optional tweaks. +def run_flow(message: str, + flow_id: str, + output_type: str = "chat", + input_type: str = "chat", + tweaks: Optional[dict] = None, + api_key: Optional[str] = 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}" + :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 = { - "input_value": message, - "output_type": output_type, - "input_type": input_type, - } - headers = None - if tweaks: - payload["tweaks"] = tweaks - if api_key: - headers = {"x-api-key": api_key} - response = requests.post(api_url, json=payload, headers=headers) - return response.json() + payload = { + "input_value": message, + "output_type": output_type, + "input_type": input_type, + } + headers = None + if tweaks: + payload["tweaks"] = tweaks + if api_key: + headers = {"x-api-key": api_key} + response = requests.post(api_url, json=payload, headers=headers) + return response.json() - # Setup any tweaks you want to apply to the flow - message = "message" - ${!isAuth ? `api_key = ""` : ""} - print(run_flow(message=message, flow_id=FLOW_ID, tweaks=TWEAKS${ +# Setup any tweaks you want to apply to the flow +message = "message" +${!isAuth ? `api_key = ""` : ""} +print(run_flow(message=message, flow_id=FLOW_ID, tweaks=TWEAKS${ !isAuth ? `, api_key=api_key` : "" }))`; } diff --git a/src/frontend/src/modals/apiModal/utils/get-python-code.tsx b/src/frontend/src/modals/apiModal/utils/get-python-code.tsx index 9e1ad8052..9fd8048f2 100644 --- a/src/frontend/src/modals/apiModal/utils/get-python-code.tsx +++ b/src/frontend/src/modals/apiModal/utils/get-python-code.tsx @@ -6,15 +6,15 @@ */ export default function getPythonCode( flowName: string, - tweaksBuildedObject, + tweaksBuildedObject ): string { const tweaksObject = tweaksBuildedObject[0]; return `from langflow.load import run_flow_from_json - TWEAKS = ${JSON.stringify(tweaksObject, null, 2)} +TWEAKS = ${JSON.stringify(tweaksObject, null, 2)} - result = run_flow_from_json(flow="${flowName}.json", - input_value="message", - fallback_to_env_vars=True, # False by default - tweaks=TWEAKS)`; +result = run_flow_from_json(flow="${flowName}.json", + input_value="message", + fallback_to_env_vars=True, # False by default + tweaks=TWEAKS)`; }