diff --git a/src/frontend/src/modals/apiModal/utils/get-curl-code.tsx b/src/frontend/src/modals/apiModal/utils/get-curl-code.tsx index 5fa68c05952..a6d78963d28 100644 --- a/src/frontend/src/modals/apiModal/utils/get-curl-code.tsx +++ b/src/frontend/src/modals/apiModal/utils/get-curl-code.tsx @@ -12,6 +12,7 @@ export function getCurlRunCode({ isAuth, tweaksBuildedObject, endpointName, + activeTweaks, }: GetCodeType): string { let tweaksString = "{}"; const inputs = useFlowStore.getState().inputs; @@ -28,7 +29,7 @@ export function getCurlRunCode({ -H 'Content-Type: application/json'\\${ !isAuth ? `\n -H 'x-api-key: '\\` : "" } - -d '{"input_value": "message", + -d '{${!activeTweaks ? `"input_value": "message",` : ""} "output_type": ${hasChatOutput ? '"chat"' : '"text"'}, "input_type": ${hasChatInput ? '"chat"' : '"text"'}, "tweaks": ${tweaksString}}' diff --git a/src/frontend/src/modals/apiModal/utils/get-js-api-code.tsx b/src/frontend/src/modals/apiModal/utils/get-js-api-code.tsx index 950d4c6991e..70588877b9b 100644 --- a/src/frontend/src/modals/apiModal/utils/get-js-api-code.tsx +++ b/src/frontend/src/modals/apiModal/utils/get-js-api-code.tsx @@ -13,6 +13,7 @@ export default function getJsApiCode({ isAuth, tweaksBuildedObject, endpointName, + activeTweaks, }: GetCodeType): string { let tweaksString = "{}"; if (tweaksBuildedObject) @@ -23,7 +24,7 @@ export default function getJsApiCode({ this.baseURL = baseURL; this.apiKey = apiKey; } - + async post(endpoint, body, headers = {"Content-Type": "application/json"}) { if (this.apiKey) { headers["Authorization"] = \`Bearer \${this.apiKey}\`; @@ -35,7 +36,7 @@ export default function getJsApiCode({ headers: headers, body: JSON.stringify(body) }); - + const responseMessage = await response.json(); if (!response.ok) { throw new Error(\`\${response.status} \${response.statusText} - \${JSON.stringify(responseMessage)}\`); @@ -46,12 +47,12 @@ export default function getJsApiCode({ throw error; } } - + async initiateSession(flowId, inputValue, inputType = 'chat', outputType = 'chat', stream = false, tweaks = {}) { const endpoint = \`/api/v1/run/\${flowId}?stream=\${stream}\`; - return this.post(endpoint, { input_value: inputValue, input_type: inputType, output_type: outputType, tweaks: tweaks }); + return this.post(endpoint, { ${activeTweaks ? "" : "input_value: inputValue, "}input_type: inputType, output_type: outputType, tweaks: tweaks }); } - + async handleStream(streamUrl, onUpdate, onClose, onError) { try { const response = await fetch(streamUrl); @@ -66,7 +67,7 @@ export default function getJsApiCode({ } const chunk = decoder.decode(value); const lines = chunk.split(\'\\n\').filter(line => line.trim() !== ''); - + for (const line of lines) { if (line.startsWith('data: ')) { try { @@ -83,7 +84,7 @@ export default function getJsApiCode({ onError(error); } } - + async runFlow(flowIdOrName, inputValue, inputType = 'chat', outputType = 'chat', tweaks, stream = false, onUpdate, onClose, onError) { try { const initResponse = await this.initiateSession(flowIdOrName, inputValue, inputType, outputType, stream, tweaks); @@ -98,13 +99,13 @@ export default function getJsApiCode({ } } } - + async function main(inputValue, inputType = 'chat', outputType = 'chat', stream = false) { const flowIdOrName = '${endpointName || flowId}'; const langflowClient = new LangflowClient('${window.location.protocol}//${window.location.host}', ${isAuth ? "'your-api-key'" : "null"}); const tweaks = ${tweaksString}; - + try { const response = await langflowClient.runFlow( flowIdOrName, @@ -117,19 +118,19 @@ export default function getJsApiCode({ (message) => console.log("Stream Closed:", message), // onClose (error) => console.error("Stream Error:", error) // onError ); - + if (!stream && response) { const flowOutputs = response.outputs[0]; const firstComponentOutputs = flowOutputs.outputs[0]; const output = firstComponentOutputs.outputs.message; - + console.log("Final Output:", output.message.text); } } catch (error) { console.error('Main Error:', error.message); } } - + const args = process.argv.slice(2); main( args[0], // inputValue 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 f7153f9fec1..0931d1eb2b2 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 @@ -12,6 +12,7 @@ export default function getPythonApiCode({ flowId, tweaksBuildedObject, endpointName, + activeTweaks, }: GetCodeType): string { let tweaksString = "{}"; if (tweaksBuildedObject) @@ -61,7 +62,7 @@ def run_flow(message: str, api_url = f"{BASE_API_URL}/api/v1/run/{endpoint}" payload = { - "input_value": message, + ${!activeTweaks ? `"input_value": message,` : ""} "output_type": output_type, "input_type": input_type, } 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 407b7832d3e..5432d693d3d 100644 --- a/src/frontend/src/modals/apiModal/utils/get-python-code.tsx +++ b/src/frontend/src/modals/apiModal/utils/get-python-code.tsx @@ -9,6 +9,7 @@ import { GetCodeType } from "@/types/tweaks"; export default function getPythonCode({ flowName, tweaksBuildedObject, + activeTweaks, }: GetCodeType): string { let tweaksString = "{}"; if (tweaksBuildedObject) @@ -21,8 +22,7 @@ export default function getPythonCode({ TWEAKS = ${tweaksString} result = run_flow_from_json(flow="${flowName}.json", - input_value="message", - session_id="", # provide a session id if you want to use session state + ${!activeTweaks ? `input_value="message",\n ` : ""}session_id="", # provide a session id if you want to use session state fallback_to_env_vars=True, # False by default tweaks=TWEAKS)`; } diff --git a/src/frontend/src/stores/tweaksStore.ts b/src/frontend/src/stores/tweaksStore.ts index 1e5bf77a35c..8aab29b52c7 100644 --- a/src/frontend/src/stores/tweaksStore.ts +++ b/src/frontend/src/stores/tweaksStore.ts @@ -98,6 +98,7 @@ export const useTweaksStore = create((set, get) => ({ isAuth: autoLogin, tweaksBuildedObject: tweak, endpointName: flow?.endpoint_name, + activeTweaks: get().activeTweaks, }; if (getCodes) { diff --git a/src/frontend/src/types/tweaks/index.ts b/src/frontend/src/types/tweaks/index.ts index 047ba63f704..3ac7d58f5e4 100644 --- a/src/frontend/src/types/tweaks/index.ts +++ b/src/frontend/src/types/tweaks/index.ts @@ -13,4 +13,5 @@ export type GetCodeType = { isAuth: boolean; tweaksBuildedObject: {}; endpointName?: string | null; + activeTweaks: boolean; };