From f8bf0088781226936b4080eeed9b0e6801b8cdd4 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Wed, 14 Jun 2023 19:59:42 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(constants.tsx):=20change=20n?= =?UTF-8?q?ode=20type=20id=20format=20to=20include=20the=20type=20name=20i?= =?UTF-8?q?n=20uppercase=20=E2=9C=A8=20feat(constants.tsx):=20add=20suppor?= =?UTF-8?q?t=20for=20tweaks=20in=20getPythonApiCode=20and=20getCurlCode=20?= =?UTF-8?q?functions=20=F0=9F=8E=A8=20style(ApiModal/index.tsx):=20remove?= =?UTF-8?q?=20unused=20imports=20and=20components=20=F0=9F=8E=A8=20style(P?= =?UTF-8?q?ageComponent/index.tsx):=20change=20node=20type=20id=20format?= =?UTF-8?q?=20to=20include=20the=20type=20name=20in=20uppercase=20?= =?UTF-8?q?=E2=9C=A8=20feat(utils.ts):=20add=20buildTweaks=20function=20to?= =?UTF-8?q?=20create=20an=20empty=20tweaks=20object=20for=20each=20node=20?= =?UTF-8?q?The=20node=20type=20id=20format=20is=20now=20changed=20to=20inc?= =?UTF-8?q?lude=20the=20type=20name=20in=20uppercase,=20which=20improves?= =?UTF-8?q?=20consistency=20with=20the=20naming=20conventions.=20Support?= =?UTF-8?q?=20for=20tweaks=20is=20added=20to=20the=20getPythonApiCode=20an?= =?UTF-8?q?d=20getCurlCode=20functions,=20which=20allows=20the=20API=20to?= =?UTF-8?q?=20apply=20tweaks=20to=20the=20flow.=20Unused=20imports=20and?= =?UTF-8?q?=20components=20are=20removed=20from=20the=20ApiModal=20compone?= =?UTF-8?q?nt.=20The=20buildTweaks=20function=20is=20added=20to=20create?= =?UTF-8?q?=20an=20empty=20tweaks=20object=20for=20each=20node.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/frontend/src/constants.tsx | 38 +++++++++++++++++----- src/frontend/src/modals/ApiModal/index.tsx | 18 ++++------ src/frontend/src/utils.ts | 7 ++++ 3 files changed, 42 insertions(+), 21 deletions(-) diff --git a/src/frontend/src/constants.tsx b/src/frontend/src/constants.tsx index 838df3f15..55a4123ce 100644 --- a/src/frontend/src/constants.tsx +++ b/src/frontend/src/constants.tsx @@ -1,5 +1,8 @@ // src/constants.tsx +import { FlowType } from "./types/flow"; +import { buildTweaks } from "./utils"; + /** * The base text for subtitle of Export Dialog (Toolbar) * @constant @@ -51,11 +54,21 @@ export const TEXT_DIALOG_SUBTITLE = "Edit your text."; * @param {string} flowId - The id of the flow * @returns {string} - The python code */ -export const getPythonApiCode = (flowId: string): string => { +export const getPythonApiCode = (flow: FlowType): string => { + const flowId = flow.id; + + // create a dictionary of node ids and the values is an empty dictionary + // flow.data.nodes.forEach((node) => { + // node.data.id + // } + const tweaks = buildTweaks(flow); return `import requests -BASE_API_URL = "${window.location.protocol}//${window.location.host}/ap1/v1/predict" +BASE_API_URL = "${window.location.protocol}//${ + window.location.host + }/ap1/v1/predict" FLOW_ID = "${flowId}" +TWEAKS = ${JSON.stringify(tweaks, null, 2)} def run_flow(message: str, flow_id: str, tweaks: dict = None) -> dict: """ @@ -77,28 +90,35 @@ def run_flow(message: str, flow_id: str, tweaks: dict = None) -> dict: 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", flow_id=FLOW_ID, tweaks=tweaks))`; +print(run_flow("Your message", flow_id=FLOW_ID, tweaks=TWEAKS))`; }; /** * Function to get the curl code for the API * @param {string} flowId - The id of the flow * @returns {string} - The curl code */ -export const getCurlCode = (flowId: string): string => { +export const getCurlCode = (flow: FlowType): string => { + const flowId = flow.id; + const tweaks = buildTweaks(flow); return `curl -X POST \\ - ${window.location.protocol}//${window.location.host}/api/v1/predict/${flowId} \\ + ${window.location.protocol}//${ + window.location.host + }/api/v1/predict/${flowId} \\ -H 'Content-Type: application/json' \\ - -d '{"message": "Your message"}'`; + -d '{"message": "Your message", "tweaks": ${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 = (flowName: string): string => { +export const getPythonCode = (flow: FlowType): string => { + const flowName = flow.name; return `from langflow import load_flow_from_json flow = load_flow_from_json("${flowName}.json") diff --git a/src/frontend/src/modals/ApiModal/index.tsx b/src/frontend/src/modals/ApiModal/index.tsx index d2f376918..4f992dfa3 100644 --- a/src/frontend/src/modals/ApiModal/index.tsx +++ b/src/frontend/src/modals/ApiModal/index.tsx @@ -1,10 +1,6 @@ -import { IconCheck, IconClipboard, IconDownload } from "@tabler/icons-react"; -import { - XMarkIcon, - CommandLineIcon, - CodeBracketSquareIcon, -} from "@heroicons/react/24/outline"; -import { Fragment, useContext, useRef, useState } from "react"; +import { IconCheck, IconClipboard } from "@tabler/icons-react"; +import { CodeBracketSquareIcon } from "@heroicons/react/24/outline"; +import { useContext, useState } from "react"; import { PopUpContext } from "../../contexts/popUpContext"; import "ace-builds/src-noconflict/mode-python"; import "ace-builds/src-noconflict/theme-github"; @@ -18,12 +14,10 @@ import { Dialog, DialogContent, DialogDescription, - DialogFooter, DialogHeader, DialogTitle, DialogTrigger, } from "../../components/ui/dialog"; -import { Button } from "../../components/ui/button"; import { FlowType } from "../../types/flow/index"; import { getCurlCode, getPythonApiCode, getPythonCode } from "../../constants"; import { EXPORT_CODE_DIALOG } from "../../constants"; @@ -55,10 +49,10 @@ export default function ApiModal({ flow }: { flow: FlowType }) { } } - const pythonApiCode = getPythonApiCode(flow.id); + const pythonApiCode = getPythonApiCode(flow); - const curl_code = getCurlCode(flow.id); - const pythonCode = getPythonCode(flow.name); + const curl_code = getCurlCode(flow); + const pythonCode = getPythonCode(flow); const tabs = [ { diff --git a/src/frontend/src/utils.ts b/src/frontend/src/utils.ts index 4298a3003..a95da9e34 100644 --- a/src/frontend/src/utils.ts +++ b/src/frontend/src/utils.ts @@ -711,3 +711,10 @@ export function groupByFamily(data, baseClasses) { return groupedObj; } + +export function buildTweaks(flow) { + return flow.data.nodes.reduce((acc, node) => { + acc[node.data.id] = {}; + return acc; + }, {}); +}