fix: Refactor API key header and remove streaming (#4427)
refactor: Update API key header in get-js-api-code.tsx and remove streaming
This commit is contained in:
parent
73df2182de
commit
0c049deee3
1 changed files with 10 additions and 51 deletions
|
|
@ -27,7 +27,7 @@ export default function getJsApiCode({
|
||||||
|
|
||||||
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["x-api-key"] = \`\${this.apiKey}\`;
|
||||||
}
|
}
|
||||||
const url = \`\${this.baseURL}\${endpoint}\`;
|
const url = \`\${this.baseURL}\${endpoint}\`;
|
||||||
try {
|
try {
|
||||||
|
|
@ -48,51 +48,14 @@ export default function getJsApiCode({
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async initiateSession(flowId, inputValue, inputType = 'chat', outputType = 'chat', stream = false, tweaks = {}) {
|
async initiateSession(flowId, inputValue, inputType = 'chat', outputType = 'chat', tweaks = {}) {
|
||||||
const endpoint = \`/api/v1/run/\${flowId}?stream=\${stream}\`;
|
const endpoint = \`/api/v1/run/\${flowId}\`;
|
||||||
return this.post(endpoint, { ${activeTweaks ? "" : "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) {
|
async runFlow(flowIdOrName, inputValue, inputType = 'chat', outputType = 'chat', tweaks, onError) {
|
||||||
try {
|
try {
|
||||||
const response = await fetch(streamUrl);
|
const initResponse = await this.initiateSession(flowIdOrName, inputValue, inputType, outputType, tweaks);
|
||||||
const reader = response.body.getReader();
|
|
||||||
const decoder = new TextDecoder();
|
|
||||||
|
|
||||||
while (true) {
|
|
||||||
const { done, value } = await reader.read();
|
|
||||||
if (done) {
|
|
||||||
onClose('Stream closed');
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
const chunk = decoder.decode(value);
|
|
||||||
const lines = chunk.split(\'\\n\').filter(line => line.trim() !== '');
|
|
||||||
|
|
||||||
for (const line of lines) {
|
|
||||||
if (line.startsWith('data: ')) {
|
|
||||||
try {
|
|
||||||
const data = JSON.parse(line.slice(6));
|
|
||||||
onUpdate(data);
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Error parsing JSON:', error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Stream Error:', error);
|
|
||||||
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);
|
|
||||||
if (stream && initResponse?.outputs?.[0]?.outputs?.[0]?.artifacts?.stream_url) {
|
|
||||||
const streamUrl = this.baseURL + initResponse.outputs[0].outputs[0].artifacts.stream_url;
|
|
||||||
console.log(\`Streaming from: \${streamUrl}\`);
|
|
||||||
this.handleStream(streamUrl, onUpdate, onClose, onError);
|
|
||||||
}
|
|
||||||
return initResponse;
|
return initResponse;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
onError('Error initiating session');
|
onError('Error initiating session');
|
||||||
|
|
@ -100,7 +63,7 @@ export default function getJsApiCode({
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function main(inputValue, inputType = 'chat', outputType = 'chat', stream = false) {
|
async function main(inputValue, inputType = 'chat', outputType = 'chat') {
|
||||||
const flowIdOrName = '${endpointName || flowId}';
|
const flowIdOrName = '${endpointName || flowId}';
|
||||||
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"});
|
||||||
|
|
@ -113,13 +76,10 @@ export default function getJsApiCode({
|
||||||
inputType,
|
inputType,
|
||||||
outputType,
|
outputType,
|
||||||
tweaks,
|
tweaks,
|
||||||
stream,
|
(error) => console.error("Error:", error) // onError
|
||||||
(data) => console.log("Received:", data.chunk), // onUpdate
|
|
||||||
(message) => console.log("Stream Closed:", message), // onClose
|
|
||||||
(error) => console.error("Stream Error:", error) // onError
|
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!stream && response) {
|
if (response) {
|
||||||
const flowOutputs = response.outputs[0];
|
const flowOutputs = response.outputs[0];
|
||||||
const firstComponentOutputs = flowOutputs.outputs[0];
|
const firstComponentOutputs = flowOutputs.outputs[0];
|
||||||
const output = firstComponentOutputs.outputs.message;
|
const output = firstComponentOutputs.outputs.message;
|
||||||
|
|
@ -136,7 +96,6 @@ export default function getJsApiCode({
|
||||||
args[0], // inputValue
|
args[0], // inputValue
|
||||||
args[1], // inputType
|
args[1], // inputType
|
||||||
args[2], // outputType
|
args[2], // outputType
|
||||||
args[3] === 'true' // streaming
|
|
||||||
);
|
);
|
||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue