-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrendTracker.js
133 lines (119 loc) · 4.84 KB
/
trendTracker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Note: Replace **<YOUR_APPLICATION_TOKEN>** with your actual Application token
require("dotenv").config();
const fs = require("fs");
class LangflowClient {
constructor(baseURL, applicationToken) {
this.baseURL = baseURL;
this.applicationToken = applicationToken;
}
async post(endpoint, body, headers = {"Content-Type": "application/json"}) {
headers["Authorization"] = `Bearer ${this.applicationToken}`;
headers["Content-Type"] = "application/json";
const url = `${this.baseURL}${endpoint}`;
try {
const response = await fetch(url, {
method: 'POST',
headers: headers,
body: JSON.stringify(body)
});
const responseMessage = await response.json();
if (!response.ok) {
throw new Error(`${response.status} ${response.statusText} - ${JSON.stringify(responseMessage)}`);
}
return responseMessage;
} catch (error) {
console.error('Request Error:', error.message);
throw error;
}
}
async initiateSession(flowId, langflowId, inputValue, inputType = 'chat', outputType = 'chat', stream = false, tweaks = {}) {
const endpoint = `/lf/${langflowId}/api/v1/run/${flowId}?stream=${stream}`;
return this.post(endpoint, { input_value: inputValue, input_type: inputType, output_type: outputType, tweaks: tweaks });
}
handleStream(streamUrl, onUpdate, onClose, onError) {
const eventSource = new EventSource(streamUrl);
eventSource.onmessage = event => {
const data = JSON.parse(event.data);
onUpdate(data);
};
eventSource.onerror = event => {
console.error('Stream Error:', event);
onError(event);
eventSource.close();
};
eventSource.addEventListener("close", () => {
onClose('Stream closed');
eventSource.close();
});
return eventSource;
}
async runFlow(flowIdOrName, langflowId, inputValue, inputType = 'chat', outputType = 'chat', tweaks = {}, stream = false, onUpdate, onClose, onError) {
try {
const initResponse = await this.initiateSession(flowIdOrName, langflowId, inputValue, inputType, outputType, stream, tweaks);
console.log('Init Response:', initResponse);
if (stream && initResponse && initResponse.outputs && initResponse.outputs[0].outputs[0].artifacts.stream_url) {
const streamUrl = initResponse.outputs[0].outputs[0].artifacts.stream_url;
console.log(`Streaming from: ${streamUrl}`);
this.handleStream(streamUrl, onUpdate, onClose, onError);
}
return initResponse;
} catch (error) {
console.error('Error running flow:', error);
onError('Error initiating session');
}
}
}
async function main(inputValue, inputType = 'chat', outputType = 'chat', stream = false) {
const flowIdOrName = 'da1a232a-c8bc-428f-9545-6c7816e73abe';
const langflowId = 'd0968e29-4ecc-447d-8780-343e720f1cde';
const applicationToken = process.env.YOUR_APPLICATION_TOKEN;
const langflowClient = new LangflowClient('https://api.langflow.astra.datastax.com',
applicationToken);
try {
const tweaks = {
"ChatInput-XPaM0": {},
"ChatOutput-7pejO": {},
"Prompt-K3ADF": {},
"AstraDB-u07rO": {},
"GroqModel-4BsNY": {},
"ParseData-aRS5h": {},
"GroqModel-PlJiS": {},
"Prompt-2tEnI": {},
"File-ZKfzo": {},
"SplitText-8e5H4": {},
"AstraDB-AC7xt": {},
"HuggingFaceInferenceAPIEmbeddings-5txe1": {},
"HuggingFaceInferenceAPIEmbeddings-hfaHZ": {}
};
response = await langflowClient.runFlow(
flowIdOrName,
langflowId,
inputValue,
inputType,
outputType,
tweaks,
stream,
(data) => fs.appendFileSync("output.md", `Received: ${data.chunk}\n`), // onUpdate
(message) => fs.appendFileSync("output.md", `Stream Closed: ${message}\n`), // onClose
(error) => fs.appendFileSync("output.md", `Stream Error: ${error}\n`) // onError
);
if (!stream && response && response.outputs) {
const flowOutputs = response.outputs[0];
const firstComponentOutputs = flowOutputs.outputs[0];
const output = firstComponentOutputs.outputs.message;
fs.writeFileSync("output.md", `Final Output: ${output.message.text}\n`);
}
} catch (error) {
fs.writeFileSync("output.md", `Main Error: ${error.message}\n`);
}
}
const args = process.argv.slice(2);
if (args.length < 1) {
console.error('Please run the file with the message as an argument: node <YOUR_FILE_NAME>.js "user_message"');
}
main(
args[0], // inputValue
args[1], // inputType
args[2], // outputType
args[3] === 'true' // stream
);