@@ -12,72 +12,76 @@ const hfchat = process.env.HFCHAT
1212const cookie = `token=${ token } ; hf-chat=${ hfchat } `
1313
1414function hug ( msg ) {
15- return new Promise ( ( resolve ) => {
15+ return new Promise ( async ( resolve ) => {
1616 if ( ! token ) return "Huggingface token not provided"
1717 if ( ! hfchat ) return "hfchat not provided"
18- fetch ( "https://huggingface.co/chat/conversation" , {
18+
19+ let data = process . env . CONV ? { "conversationId" : process . env . CONV } : await newConversationId ( ) . then ( data => data )
20+
21+ process . env . CONV = data . conversationId
22+ fetch ( `https://huggingface.co/chat/conversation/${ data . conversationId } /__data.json?x-sveltekit-invalidated=1_1` , {
1923 "headers" : {
20- "content-type" : "application/json" ,
2124 "cookie" : cookie
2225 } ,
23- "body" : "{\"model\":\"OpenAssistant/oasst-sft-6-llama-30b-xor\"}" ,
24- "method" : "POST "
26+ "body" : null ,
27+ "method" : "GET "
2528 } )
26- . then ( data => data . json ( ) )
27- . then ( data => {
28- fetch ( `https://huggingface.co/chat/conversation/${ data . conversationId } /__data.json?x-sveltekit-invalidated=1_1` , {
29+ . then ( ( ) => {
30+ fetch ( `https://huggingface.co/chat/conversation/${ data . conversationId } ` , {
2931 "headers" : {
32+ "content-type" : "application/json" ,
3033 "cookie" : cookie
3134 } ,
32- "body" : null ,
33- "method" : "GET"
34- } )
35- . then ( ( ) => {
36- fetch ( `https://huggingface.co/chat/conversation/${ data . conversationId } ` , {
37- "headers" : {
38- "content-type" : "application/json" ,
39- "cookie" : cookie
40- } ,
41- "body" : JSON . stringify ( {
42- "inputs" : msg ,
43- "parameters" : {
44- "temperature" : 0.2 ,
45- "truncate" : 1000 ,
46- "max_new_tokens" : 1024 ,
47- "stop" : [
48- "</s>"
49- ] ,
50- "top_p" : 0.95 ,
51- "repetition_penalty" : 1.2 ,
52- "top_k" : 50 ,
53- "return_full_text" : false
54- } ,
55- "stream" : true ,
56- "options" : {
57- "id" : randomUUID ( ) ,
58- "response_id" : randomUUID ( ) ,
59- "is_retry" : false ,
60- "use_cache" : false ,
61- "web_search_id" : ""
62- }
63- } ) ,
64- "method" : "POST"
65- } ) . then ( dataa => dataa . text ( ) )
66- . then ( dataa => {
67- dataa = JSON . parse ( dataa . slice ( dataa . lastIndexOf ( 'data:{"token":{' ) + 'data:' . length ) ) . generated_text
68- resolve ( dataa )
69- } )
35+ "body" : JSON . stringify ( {
36+ "inputs" : msg ,
37+ "parameters" : {
38+ "temperature" : 0.2 ,
39+ "truncate" : 1000 ,
40+ "max_new_tokens" : 1024 ,
41+ "stop" : [
42+ "</s>"
43+ ] ,
44+ "top_p" : 0.95 ,
45+ "repetition_penalty" : 1.2 ,
46+ "top_k" : 50 ,
47+ "return_full_text" : false
48+ } ,
49+ "stream" : true ,
50+ "options" : {
51+ "id" : randomUUID ( ) ,
52+ "response_id" : randomUUID ( ) ,
53+ "is_retry" : false ,
54+ "use_cache" : false ,
55+ "web_search_id" : ""
56+ }
57+ } ) ,
58+ "method" : "POST"
59+ } ) . then ( dataa => dataa . text ( ) )
60+ . then ( dataa => {
61+ dataa = JSON . parse ( dataa . slice ( dataa . lastIndexOf ( 'data:{"token":{' ) + 'data:' . length ) ) . generated_text
62+ resolve ( dataa )
7063 } )
71-
72-
7364 } )
65+
66+ } )
67+ }
68+ function newConversationId ( ) {
69+ return new Promise ( ( resolve ) => {
70+ fetch ( "https://huggingface.co/chat/conversation" , {
71+ "headers" : {
72+ "content-type" : "application/json" ,
73+ "cookie" : cookie
74+ } ,
75+ "body" : "{\"model\":\"OpenAssistant/oasst-sft-6-llama-30b-xor\"}" ,
76+ "method" : "POST"
77+ } ) . then ( data => data . json ( ) )
78+ . then ( data => resolve ( data ) )
7479 } )
7580}
76-
7781function ask ( ) {
78- rl . question ( "\033[35mYou: \033[34m> \033[36m " , ( msg ) => {
82+ rl . question ( "You: > " , ( msg ) => {
7983 hug ( msg ) . then ( data => {
80- console . log ( "😊 : \033[34m " + data )
84+ console . log ( "😊 : " + data )
8185 ask ( )
8286 } )
8387 } )
0 commit comments