Refactor: Make changes to api_key only show up in codetabs when authenticated

This commit is contained in:
igorrCarvalho 2023-09-21 18:31:57 -03:00 committed by anovazzi1
commit ac67fff000
3 changed files with 21 additions and 14 deletions

View file

@ -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"

View file

@ -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<tweakType>([]);
const tweaksList = useRef<string[]>([]);
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;

View file

@ -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="..."`
: "")}
></langflow-chat>`;
}