From ac67fff00029e4011bdeaa184efe53f8b8380bca Mon Sep 17 00:00:00 2001 From: igorrCarvalho Date: Thu, 21 Sep 2023 18:31:57 -0300 Subject: [PATCH] Refactor: Make changes to api_key only show up in codetabs when authenticated --- .../langflow/services/settings/auth.py | 2 +- src/frontend/src/modals/ApiModal/index.tsx | 14 ++++++++------ src/frontend/src/utils/utils.ts | 19 ++++++++++++------- 3 files changed, 21 insertions(+), 14 deletions(-) diff --git a/src/backend/langflow/services/settings/auth.py b/src/backend/langflow/services/settings/auth.py index d1f8197f0..87a156df7 100644 --- a/src/backend/langflow/services/settings/auth.py +++ b/src/backend/langflow/services/settings/auth.py @@ -30,7 +30,7 @@ class AuthSettings(BaseSettings): # If AUTO_LOGIN = True # > The application does not request login and logs in automatically as a super user. - AUTO_LOGIN: bool = False + AUTO_LOGIN: bool = True FIRST_SUPERUSER: str = "langflow" FIRST_SUPERUSER_PASSWORD: str = "langflow" diff --git a/src/frontend/src/modals/ApiModal/index.tsx b/src/frontend/src/modals/ApiModal/index.tsx index a16fb9f3e..0d4a8647d 100644 --- a/src/frontend/src/modals/ApiModal/index.tsx +++ b/src/frontend/src/modals/ApiModal/index.tsx @@ -27,6 +27,7 @@ import { tabsArray, } from "../../utils/utils"; import BaseModal from "../baseModal"; +import { AuthContext } from "../../contexts/authContext"; const ApiModal = forwardRef( ( @@ -39,15 +40,16 @@ const ApiModal = forwardRef( }, ref ) => { + const { autoLogin } = useContext(AuthContext); const [open, setOpen] = useState(false); const [activeTab, setActiveTab] = useState("0"); const tweak = useRef([]); const tweaksList = useRef([]); const { setTweak, getTweak, tabsState } = useContext(TabsContext); - const pythonApiCode = getPythonApiCode(flow, tweak.current, tabsState); - const curl_code = getCurlCode(flow, tweak.current, tabsState); + const pythonApiCode = getPythonApiCode(flow, autoLogin, tweak.current, tabsState); + const curl_code = getCurlCode(flow, autoLogin, tweak.current, tabsState); const pythonCode = getPythonCode(flow, tweak.current, tabsState); - const widgetCode = getWidgetCode(flow, tabsState); + const widgetCode = getWidgetCode(flow, autoLogin, tabsState); const tweaksCode = buildTweaks(flow); const codesArray = [ curl_code, @@ -150,10 +152,10 @@ const ApiModal = forwardRef( tweak.current.push(newTweak); } - const pythonApiCode = getPythonApiCode(flow, tweak.current, tabsState); - const curl_code = getCurlCode(flow, tweak.current, tabsState); + const pythonApiCode = getPythonApiCode(flow, autoLogin ,tweak.current, tabsState); + const curl_code = getCurlCode(flow, autoLogin, tweak.current, tabsState); const pythonCode = getPythonCode(flow, tweak.current, tabsState); - const widgetCode = getWidgetCode(flow, tabsState); + const widgetCode = getWidgetCode(flow, autoLogin,tabsState); tabs![0].code = curl_code; tabs![1].code = pythonApiCode; diff --git a/src/frontend/src/utils/utils.ts b/src/frontend/src/utils/utils.ts index 667be3d0a..aa0825986 100644 --- a/src/frontend/src/utils/utils.ts +++ b/src/frontend/src/utils/utils.ts @@ -299,6 +299,7 @@ export function getChatInputField(flow: FlowType, tabsState?: TabsState) { */ export function getPythonApiCode( flow: FlowType, + isAuth: boolean, tweak?: any[], tabsState?: TabsState ): string { @@ -325,7 +326,7 @@ TWEAKS = ${ : JSON.stringify(tweaks, null, 2) } -def run_flow(inputs: dict, flow_id: str, tweaks: Optional[dict] = None) -> dict: +def run_flow(inputs: dict, flow_id: str, tweaks: Optional[dict] = None${!isAuth ? `, apiKey: str` : ""}) -> dict: """ Run a flow with a given message and optional tweaks. @@ -336,7 +337,7 @@ def run_flow(inputs: dict, flow_id: str, tweaks: Optional[dict] = None) -> dict: """ api_url = f"{BASE_API_URL}/{flow_id}" - payload = {"inputs": inputs} + payload = {"inputs": inputs${!isAuth ? `, "api_key": apiKey` : ""}} if tweaks: payload["tweaks"] = tweaks @@ -346,7 +347,8 @@ def run_flow(inputs: dict, flow_id: str, tweaks: Optional[dict] = None) -> dict: # Setup any tweaks you want to apply to the flow inputs = ${inputs} -print(run_flow(inputs, flow_id=FLOW_ID, tweaks=TWEAKS))`; +${!isAuth ? `api_key = "..."` : ""} +print(run_flow(inputs, flow_id=FLOW_ID, tweaks=TWEAKS${!isAuth ? `, api_key` : ""}))`; } /** @@ -356,6 +358,7 @@ print(run_flow(inputs, flow_id=FLOW_ID, tweaks=TWEAKS))`; */ export function getCurlCode( flow: FlowType, + isAuth: boolean, tweak?: any[], tabsState?: TabsState ): string { @@ -368,7 +371,7 @@ export function getCurlCode( window.location.host }/api/v1/process/${flowId} \\ -H 'Content-Type: application/json' \\ - -d '{"inputs": ${inputs},"api_key": "...", "tweaks": ${ + -d '{"inputs": ${inputs},${!isAuth ? ` "api_key": "...", ` : " "}"tweaks": ${ tweak && tweak.length > 0 ? buildTweakObject(tweak) : JSON.stringify(tweaks, null, 2) @@ -405,7 +408,7 @@ flow(inputs)`; * @param {string} flow - The current flow. * @returns {string} - The widget code */ -export function getWidgetCode(flow: FlowType, tabsState?: TabsState): string { +export function getWidgetCode(flow: FlowType, isAuth: boolean, tabsState?: TabsState): string { const flowId = flow.id; const flowName = flow.name; const inputs = buildInputs(tabsState!, flow.id); @@ -425,8 +428,10 @@ chat_input_field: Input key that you want the chat to send the user message with chat_input_field="${chat_input_field}" ` : "" - }host_url="http://localhost:7860" - api_key="..." + }host_url="http://localhost:7860"${(!isAuth ? ` + api_key="..."` + : "")} + >`; }