-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhug-1.js
90 lines (83 loc) · 3.14 KB
/
hug-1.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
const { randomUUID } = require("crypto")
const process = require("process")
const readLine = require("readline")
const rl = readLine.createInterface({
input: process.stdin,
output: process.stdout
})
const token = process.env.TOKEN
const hfchat = process.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 github readme")
if (!hfchat) resolve("hfchat not provided. See the github readme")
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))
})
}
function ask() {
rl.question("\033[35mYou: \033[34m> \033[36m", (msg) => {
hug(msg).then(data => {
console.log("😊 : \033[34m" + data)
ask()
})
})
}
ask()