From ebee617b52a96ab9b5981815bb88ef671c297054 Mon Sep 17 00:00:00 2001 From: Cristhian Zanforlin Lousa Date: Tue, 27 Jun 2023 17:24:19 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A8=20refactor(constants.tsx):=20add?= =?UTF-8?q?=20optional=20parameter=20to=20getPythonApiCode,=20getCurlCode?= =?UTF-8?q?=20and=20getPythonCode=20functions=20to=20allow=20tweaking=20of?= =?UTF-8?q?=20the=20flow=20=E2=9C=A8=20feat(ApiModal):=20pass=20the=20twea?= =?UTF-8?q?k.current=20value=20to=20the=20getPythonApiCode,=20getCurlCode?= =?UTF-8?q?=20and=20getPythonCode=20functions=20to=20allow=20tweaking=20of?= =?UTF-8?q?=20the=20flow.=20Update=20the=20tabs=20with=20the=20new=20code?= =?UTF-8?q?=20after=20a=20tweak=20is=20added.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/frontend/src/constants.tsx | 22 ++++++++++++---------- src/frontend/src/modals/ApiModal/index.tsx | 18 +++++++++++------- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/src/frontend/src/constants.tsx b/src/frontend/src/constants.tsx index f569046ea..4cebb9b54 100644 --- a/src/frontend/src/constants.tsx +++ b/src/frontend/src/constants.tsx @@ -54,7 +54,7 @@ export const TEXT_DIALOG_SUBTITLE = "Edit your text."; * @param {string} flowId - The id of the flow * @returns {string} - The python code */ -export const getPythonApiCode = (flow: FlowType): string => { +export const getPythonApiCode = (flow: FlowType, tweak?): string => { const flowId = flow.id; // create a dictionary of node ids and the values is an empty dictionary @@ -70,7 +70,9 @@ BASE_API_URL = "${window.location.protocol}//${ FLOW_ID = "${flowId}" # You can tweak the flow by adding a tweaks dictionary # e.g {"OpenAI-XXXXX": {"model_name": "gpt-4"}} -TWEAKS = ${JSON.stringify(tweaks, null, 2)} +TWEAKS = ${ + tweak ? JSON.stringify(tweak, null, 2) : JSON.stringify(tweaks, null, 2) + } def run_flow(message: str, flow_id: str, tweaks: dict = None) -> dict: """ @@ -100,7 +102,7 @@ print(run_flow("Your message", flow_id=FLOW_ID, tweaks=TWEAKS))`; * @param {string} flowId - The id of the flow * @returns {string} - The curl code */ -export const getCurlCode = (flow: FlowType): string => { +export const getCurlCode = (flow: FlowType, tweak?): string => { const flowId = flow.id; const tweaks = buildTweaks(flow); return `curl -X POST \\ @@ -108,22 +110,22 @@ export const getCurlCode = (flow: FlowType): string => { window.location.host }/api/v1/process/${flowId} \\ -H 'Content-Type: application/json' \\ - -d '{"inputs": {"input": message}, "tweaks": ${JSON.stringify( - tweaks, - null, - 2 - )}}'`; + -d '{"inputs": {"input": message}, "tweaks": ${ + tweak ? JSON.stringify(tweak, null, 2) : JSON.stringify(tweaks, null, 2) + }}'`; }; /** * Function to get the python code for the API * @param {string} flowName - The name of the flow * @returns {string} - The python code */ -export const getPythonCode = (flow: FlowType): string => { +export const getPythonCode = (flow: FlowType, tweak?): string => { const flowName = flow.name; const tweaks = buildTweaks(flow); return `from langflow import load_flow_from_json -TWEAKS = ${JSON.stringify(tweaks, null, 2)} +TWEAKS = ${ + tweak ? JSON.stringify(tweak, null, 2) : JSON.stringify(tweaks, null, 2) + } flow = load_flow_from_json("${flowName}.json", tweaks=TWEAKS) # Now you can use it like any chain flow("Hey, have you heard of LangFlow?")`; diff --git a/src/frontend/src/modals/ApiModal/index.tsx b/src/frontend/src/modals/ApiModal/index.tsx index bc4ecac87..cfef3bd37 100644 --- a/src/frontend/src/modals/ApiModal/index.tsx +++ b/src/frontend/src/modals/ApiModal/index.tsx @@ -78,13 +78,9 @@ export default function ApiModal({ flow }: { flow: FlowType }) { } } - useEffect(() => { - console.log(tweak.current); - }, [closePopUp]); - - const pythonApiCode = getPythonApiCode(flow); - const curl_code = getCurlCode(flow); - const pythonCode = getPythonCode(flow); + const pythonApiCode = getPythonApiCode(flow, tweak.current); + const curl_code = getCurlCode(flow, tweak.current); + const pythonCode = getPythonCode(flow, tweak.current); const tweaksCode = buildTweaks(flow); const tabs = [ @@ -167,6 +163,14 @@ export default function ApiModal({ flow }: { flow: FlowType }) { tweak.current.push(newTweak); } + const pythonApiCode = getPythonApiCode(flow, tweak.current); + const curl_code = getCurlCode(flow, tweak.current); + const pythonCode = getPythonCode(flow, tweak.current); + + tabs[0].code = curl_code; + tabs[1].code = pythonApiCode; + tabs[2].code = pythonCode; + console.log(tweak.current); }