Spaces:
Paused
Paused
Create feloAI.js
Browse files- lib/feloAI.js +78 -0
lib/feloAI.js
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import axios from "axios";
|
| 2 |
+
|
| 3 |
+
async function feloAI (query, lang) {
|
| 4 |
+
const headers = {
|
| 5 |
+
Accept: "/",
|
| 6 |
+
"User-Agent": "Postify/1.0.0",
|
| 7 |
+
"Content-Encoding": "gzip, deflate, br, zstd",
|
| 8 |
+
"Content-Type": "application/json",
|
| 9 |
+
"Origin": "https://felo.ai",
|
| 10 |
+
"Referer": "https://felo.ai/"
|
| 11 |
+
};
|
| 12 |
+
|
| 13 |
+
const generateUUID = () => {
|
| 14 |
+
return (
|
| 15 |
+
Math.random().toString(16).slice(2) +
|
| 16 |
+
Math.random().toString(16).slice(2) +
|
| 17 |
+
Math.random().toString(16).slice(2) +
|
| 18 |
+
Math.random().toString(16).slice(2)
|
| 19 |
+
);
|
| 20 |
+
};
|
| 21 |
+
|
| 22 |
+
const payload = {
|
| 23 |
+
query,
|
| 24 |
+
search_uuid: generateUUID(),
|
| 25 |
+
search_options: {
|
| 26 |
+
langcode: lang
|
| 27 |
+
},
|
| 28 |
+
search_video: true,
|
| 29 |
+
};
|
| 30 |
+
|
| 31 |
+
const request = (badi) => {
|
| 32 |
+
const result = {
|
| 33 |
+
answer: "",
|
| 34 |
+
source: [],
|
| 35 |
+
};
|
| 36 |
+
badi.split("\n").forEach((line) => {
|
| 37 |
+
if (line.startsWith("data:")) {
|
| 38 |
+
try {
|
| 39 |
+
const data = JSON.parse(line.slice(5).trim());
|
| 40 |
+
if (data.data) {
|
| 41 |
+
if (data.data.text) {
|
| 42 |
+
result.answer = data.data.text
|
| 43 |
+
.replace(/\\(.?)\\/g, "$1*")
|
| 44 |
+
.replace(/^#{2,3}\s+/g, "")
|
| 45 |
+
.split("\n")
|
| 46 |
+
.map((line) => line.replace(/^#{2,3}\s+/g, "> "))
|
| 47 |
+
.join("\n");
|
| 48 |
+
}
|
| 49 |
+
if (data.data.sources) {
|
| 50 |
+
result.source = data.data.sources;
|
| 51 |
+
}
|
| 52 |
+
}
|
| 53 |
+
} catch (e) {
|
| 54 |
+
console.error(e);
|
| 55 |
+
}
|
| 56 |
+
}
|
| 57 |
+
});
|
| 58 |
+
return result;
|
| 59 |
+
};
|
| 60 |
+
|
| 61 |
+
try {
|
| 62 |
+
const response = await axios.post(
|
| 63 |
+
"https://api.felo.ai/search/threads",
|
| 64 |
+
payload,
|
| 65 |
+
{
|
| 66 |
+
headers,
|
| 67 |
+
responseType: "text",
|
| 68 |
+
},
|
| 69 |
+
);
|
| 70 |
+
|
| 71 |
+
return request(response.data);
|
| 72 |
+
} catch (error) {
|
| 73 |
+
console.error(error);
|
| 74 |
+
return null;
|
| 75 |
+
}
|
| 76 |
+
};
|
| 77 |
+
|
| 78 |
+
export { feloAI };
|