Fix: API JS generated code (#3461)
* update js code * fix langflowid * fix code change
This commit is contained in:
parent
96ca71dab8
commit
dde0fa4566
1 changed files with 57 additions and 43 deletions
|
|
@ -21,8 +21,9 @@ export default function getJsApiCode(
|
||||||
this.baseURL = baseURL;
|
this.baseURL = baseURL;
|
||||||
this.apiKey = apiKey;
|
this.apiKey = apiKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
async post(endpoint, body, headers = {"Content-Type": "application/json"}) {
|
async post(endpoint, body, headers = {"Content-Type": "application/json"}) {
|
||||||
if (this.apiKey) {
|
if (this.apiKey) {
|
||||||
headers["Authorization"] = \`Bearer \${this.apiKey}\`;
|
headers["Authorization"] = \`Bearer \${this.apiKey}\`;
|
||||||
}
|
}
|
||||||
const url = \`\${this.baseURL}\${endpoint}\`;
|
const url = \`\${this.baseURL}\${endpoint}\`;
|
||||||
|
|
@ -32,19 +33,21 @@ export default function getJsApiCode(
|
||||||
headers: headers,
|
headers: headers,
|
||||||
body: JSON.stringify(body)
|
body: JSON.stringify(body)
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const responseMessage = await response.json();
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
throw new Error(\`HTTP error! Status: \${response.status}\`);
|
throw new Error(\`\${response.status} \${response.statusText} - \${JSON.stringify(responseMessage)}\`);
|
||||||
}
|
}
|
||||||
return await response.json();
|
return responseMessage;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Request Error:', error);
|
console.error(\`Error during POST request: \${error.message}\`);
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async initiateSession(flowId, inputValue, stream = false, tweaks = {}) {
|
async initiateSession(flowId, inputValue, inputType = 'chat', outputType = 'chat', stream = false, tweaks = {}) {
|
||||||
const endpoint = \`/api/v1/run/\${flowId}?stream=\${stream}\`;
|
const endpoint = \`/api/v1/run/\${flowId}?stream=\${stream}\`;
|
||||||
return this.post(endpoint, { input_value: inputValue, tweaks: tweaks });
|
return this.post(endpoint, { input_value: inputValue, input_type: inputType, output_type: outputType, tweaks: tweaks });
|
||||||
}
|
}
|
||||||
|
|
||||||
handleStream(streamUrl, onUpdate, onClose, onError) {
|
handleStream(streamUrl, onUpdate, onClose, onError) {
|
||||||
|
|
@ -69,47 +72,58 @@ export default function getJsApiCode(
|
||||||
return eventSource;
|
return eventSource;
|
||||||
}
|
}
|
||||||
|
|
||||||
async runFlow(flowIdOrName, inputValue, tweaks, stream = false, onUpdate, onClose, onError) {
|
async runFlow(flowIdOrName, inputValue, inputType = 'chat', outputType = 'chat', tweaks, stream = false, onUpdate, onClose, onError) {
|
||||||
try {
|
try {
|
||||||
const initResponse = await this.initiateSession(flowIdOrName, inputValue, stream, tweaks);
|
const initResponse = await this.initiateSession(flowIdOrName, inputValue, inputType, outputType, stream, tweaks);
|
||||||
console.log('Init Response:', initResponse);
|
if (stream && initResponse?.outputs?.[0]?.outputs?.[0]?.artifacts?.stream_url) {
|
||||||
if (stream && initResponse && initResponse.outputs && initResponse.outputs[0].outputs[0].artifacts.stream_url) {
|
|
||||||
const streamUrl = initResponse.outputs[0].outputs[0].artifacts.stream_url;
|
const streamUrl = initResponse.outputs[0].outputs[0].artifacts.stream_url;
|
||||||
console.log(\`Streaming from: \${streamUrl}\`);
|
console.log(\`Streaming from: \${streamUrl}\`);
|
||||||
this.handleStream(streamUrl, onUpdate, onClose, onError);
|
this.handleStream(streamUrl, onUpdate, onClose, onError);
|
||||||
}
|
}
|
||||||
return initResponse;
|
return initResponse;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error running flow:', error);
|
onError('Error initiating session');
|
||||||
onError('Error initiating session');
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function main() {
|
async function main(inputValue, inputType = 'chat', outputType = 'chat', stream = false) {
|
||||||
const flowIdOrName = '${endpointName || flowId}';
|
const flowIdOrName = '${endpointName || flowId}';
|
||||||
const inputValue = 'User message';
|
|
||||||
const stream = false;
|
|
||||||
const langflowClient = new LangflowClient('${window.location.protocol}//${window.location.host}',
|
const langflowClient = new LangflowClient('${window.location.protocol}//${window.location.host}',
|
||||||
${isAuth ? "'your-api-key'" : "null"});
|
${isAuth ? "'your-api-key'" : "null"});
|
||||||
const tweaks = ${tweaksString};
|
const tweaks = ${tweaksString};
|
||||||
response = await langflowClient.runFlow(
|
|
||||||
flowIdOrName,
|
|
||||||
inputValue,
|
|
||||||
tweaks,
|
|
||||||
stream,
|
|
||||||
(data) => console.log("Received:", data.chunk), // onUpdate
|
|
||||||
(message) => console.log("Stream Closed:", message), // onClose
|
|
||||||
(error) => console.log("Stream Error:", error) // onError
|
|
||||||
);
|
|
||||||
if (!stream) {
|
|
||||||
const flowOutputs = response.outputs[0];
|
|
||||||
const firstComponentOutputs = flowOutputs.outputs[0];
|
|
||||||
const output = firstComponentOutputs.outputs.message;
|
|
||||||
|
|
||||||
console.log("Final Output:", output.message.text);
|
try {
|
||||||
|
const response = await langflowClient.runFlow(
|
||||||
|
flowIdOrName,
|
||||||
|
inputValue,
|
||||||
|
inputType,
|
||||||
|
outputType,
|
||||||
|
tweaks,
|
||||||
|
stream,
|
||||||
|
(data) => console.log("Received:", data.chunk), // onUpdate
|
||||||
|
(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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
main();`;
|
const args = process.argv.slice(2);
|
||||||
|
main(
|
||||||
|
args[0], // inputValue
|
||||||
|
args[1], // inputType
|
||||||
|
args[2], // outputType
|
||||||
|
args[3] === 'true' // stream
|
||||||
|
);
|
||||||
|
`;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue