-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhug.js
More file actions
76 lines (70 loc) · 2.83 KB
/
hug.js
File metadata and controls
76 lines (70 loc) · 2.83 KB
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
const { randomUUID } = require("crypto")
const { env } = require("process")
const token = env.TOKEN
const hfchat = env.HFCHAT
const cookie = `token=${token}; hf-chat=${hfchat}`
function hug(msg) {
return new Promise(async (resolve) => {
if (!token) resolve("Huggingface token not provided. See the ReadMe in github")
if (!hfchat) resolve("hfchat not provided. See the ReadMe in github")
let data = process.env.CONV ? { "conversationId": process.env.CONV } : await newConversationId().then(data => data)
process.env.CONV = data.conversationId
fetch(`https://huggingface.co/chat/conversation/${data.conversationId}/__data.json?x-sveltekit-invalidated=1_1`, {
"headers": {
"cookie": cookie
},
"body": null,
"method": "GET"
})
.then(() => {
fetch(`https://huggingface.co/chat/conversation/${data.conversationId}`, {
"headers": {
"content-type": "application/json",
"cookie": cookie
},
"body": JSON.stringify({
"inputs": msg,
"parameters": {
"temperature": 0.2,
"truncate": 1000,
"max_new_tokens": 1024,
"stop": [
"</s>"
],
"top_p": 0.95,
"repetition_penalty": 1.2,
"top_k": 50,
"return_full_text": false
},
"stream": true,
"options": {
"id": randomUUID(),
"response_id": randomUUID(),
"is_retry": false,
"use_cache": false,
"web_search_id": ""
}
}),
"method": "POST"
}).then(dataa => dataa.text())
.then(dataa => {
dataa = JSON.parse(dataa.slice(dataa.lastIndexOf('data:{"token":{') + 'data:'.length)).generated_text
resolve(dataa)
})
})
})
}
function newConversationId() {
return new Promise((resolve) => {
fetch("https://huggingface.co/chat/conversation", {
"headers": {
"content-type": "application/json",
"cookie": cookie
},
"body": "{\"model\":\"OpenAssistant/oasst-sft-6-llama-30b-xor\"}",
"method": "POST"
}).then(data => data.json())
.then(data => resolve(data))
})
}
module.exports = hug