-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtibetan-ml-kit.ts
199 lines (177 loc) · 5.42 KB
/
tibetan-ml-kit.ts
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import axios from "axios";
import { DataParam } from "./common/data-param";
import {
fetchCsrfToken,
getTranslateSyncResponse,
} from "./common/tb-conversion";
import { Server, WebSocketServer } from "ws";
import { defaultPromtMessage, genAI } from "./constant";
import { ModelConfig } from "./common/model-config";
import { GoogleGenerativeAI } from "@google/generative-ai";
// You can use a class if you prefer
class TibetanMlKit {
wss: WebSocketServer;
/**
*
* @param server
* @param modelConfig
*/
constructor(server: any, modelConfig: ModelConfig) {
this.wss = new WebSocketServer({ server });
/**
* Initaite the websocket server
*/
this.wss.on("connection", (ws) => {
this.handleConnection(ws, modelConfig);
});
}
handleConnection(ws: import("ws"), modelConfig: ModelConfig) {
const genAI = new GoogleGenerativeAI(modelConfig.apiKey);
const model = genAI.getGenerativeModel({
model: modelConfig.modelName,
});
const chat = model.startChat({
history: [
{
role: "user",
parts: [
{
text: modelConfig.promptMessage
? modelConfig.promptMessage
: defaultPromtMessage,
},
],
},
{
role: "model",
parts: [{ text: "Sure things. What would you like to know?" }],
},
{
role: "user",
parts: [{ text: "Tashi Delek" }],
},
],
generationConfig: { maxOutputTokens: 100 },
});
ws.on("message", async (message) => {
const msg = message.toString();
displayChatsTokenCount(model, chat, msg);
const result = await chat.sendMessage(msg);
const response = result.response;
if (response && response.candidates) {
let en = response.candidates[0].content.parts[0].text;
console.log("en===>", en);
const { csrfToken, cookie } = await fetchCsrfToken();
let tb: any = await getTranslateSyncResponse({
input: en ?? "",
direction: "bo",
cookie: cookie ?? "",
csrfToken: csrfToken,
});
ws.send(
JSON.stringify({
en: en,
tb: tb["generated_text"],
})
);
}
});
}
static async translateWithAsync({
input,
direction = "en",
}: DataParam): Promise<any> {
const formData = new FormData();
formData.append("input", input);
formData.append("direction", direction);
try {
// Create form data
const formData = new FormData();
formData.append("input", input); // 'input' is the query
formData.append("direction", direction ?? "en"); // 'direction' is set to 'en'
// Make a POST request to the stream API with custom headers
const response = await axios({
method: "post",
url: "https://monlam-file-api-latest.onrender.com/mt/playground/stream",
data: formData,
headers: {
Accept: "*/*",
"Accept-Encoding": "gzip, deflate, br, zstd",
"Accept-Language": "en-US,en;q=0.9",
Origin: "https://monlam.ai",
Referer: "https://monlam.ai/",
"Sec-Ch-Ua":
'"Brave";v="125", "Chromium";v="125", "Not.A/Brand";v="24"',
"Sec-Ch-Ua-Mobile": "?1",
"Sec-Ch-Ua-Platform": '"Android"',
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "cross-site",
"Sec-Gpc": "1",
"User-Agent":
"Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Mobile Safari/537.36",
},
responseType: "stream",
});
let finalData: any = null;
// Collect stream data
response.data.on("data", (chunk: any) => {
console.log("Response:" + chunk);
const lines = chunk.toString().split("\n");
lines.forEach((line: any) => {
if (line.trim().startsWith("data:")) {
const json = JSON.parse(line.replace("data:", "").trim());
if (json.token && json.token.special) {
finalData = json;
}
}
});
});
// Send the final data when the stream ends
response.data.on("end", () => {
return finalData;
});
response.data.on("error", (err: any) => {
console.error("Stream error:", err);
return finalData;
});
} catch (error) {
console.error("Error calling stream API:", error);
throw error;
}
}
/**
*
* @param DataParam
* @returns translateWithSync
*/
static async translateWithSync({
input,
direction = "en",
csrfToken,
cookie,
}: DataParam) {
return await getTranslateSyncResponse({
input,
direction,
csrfToken,
cookie,
});
}
/**
*
* @param DataParam
* @returns
*/
static async chatSync({ input, direction = "en" }: DataParam): Promise<any> {}
}
async function displayTokensCount(model: any, request: any) {
const { totalTokens } = await model.countTokens(request);
console.log("Token count: ", totalTokens);
}
async function displayChatsTokenCount(model: any, chat: any, msg: string) {
const history = await chat.getHistory();
const msgContent = { role: "user", parts: [{ text: msg }] };
await displayTokensCount(model, { contents: [...history, msgContent] });
}
export { TibetanMlKit, DataParam as PostDataParams };