@datastax/langflow-client
Version:
A JavaScript client for the Langflow API
91 lines • 3.14 kB
JavaScript
// Copyright DataStax, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import { FormData, Headers } from "undici";
import { FlowResponse } from "./flow_response.js";
import { UploadResponse } from "./upload_response.js";
export class Flow {
client;
id;
tweaks;
constructor(client, flowId, tweaks = {}) {
this.client = client;
this.id = flowId;
this.tweaks = tweaks;
}
tweak(key, tweak) {
const newTweaks = structuredClone(this.tweaks);
newTweaks[key] = tweak;
return new Flow(this.client, this.id, newTweaks);
}
async run(input_value, options = {}) {
const { input_type = "chat", output_type = "chat", session_id, signal, } = options;
const tweaks = { ...this.tweaks, ...options.tweaks };
const body = JSON.stringify({
input_type,
output_type,
input_value,
tweaks,
session_id,
});
const headers = new Headers();
headers.set("Content-Type", "application/json");
headers.set("Accept", "application/json");
const result = (await this.client.request({
path: `/v1/run/${this.id}`,
method: "POST",
body,
headers,
signal,
}));
return new FlowResponse(result);
}
async stream(input_value, options = {}) {
const { input_type = "chat", output_type = "chat", session_id, signal, } = options;
const tweaks = { ...this.tweaks, ...options.tweaks };
const body = JSON.stringify({
input_type,
output_type,
input_value,
tweaks,
session_id,
});
const headers = new Headers();
headers.set("Content-Type", "application/json");
headers.set("Accept", "application/json");
const streamingResult = await this.client.stream({
path: `/v1/run/${this.id}`,
method: "POST",
body,
headers,
signal,
});
return streamingResult;
}
async uploadFile(file, options = {}) {
const { signal } = options;
const form = new FormData();
form.append("file", file);
const headers = new Headers();
headers.set("Accept", "application/json");
const response = (await this.client.request({
path: `/v1/files/upload/${this.id}`,
method: "POST",
body: form,
headers,
signal,
}));
return new UploadResponse(response);
}
}
//# sourceMappingURL=flow.js.map