kaiiddo commited on
Commit
baa9725
·
verified ·
1 Parent(s): 2b4dda3

Create services/huggingfaceService.js

Browse files
Files changed (1) hide show
  1. src/services/huggingfaceService.js +126 -0
src/services/huggingfaceService.js ADDED
@@ -0,0 +1,126 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Hugging Face Inference API service
2
+ export class HuggingFaceService {
3
+ constructor(token) {
4
+ this.token = token;
5
+ }
6
+
7
+ async streamChatCompletion(messages, modelConfig, onChunk, onComplete, onError) {
8
+ try {
9
+ const response = await fetch(
10
+ `https://api-inference.huggingface.co/models/${modelConfig.endpoint}`,
11
+ {
12
+ method: 'POST',
13
+ headers: {
14
+ 'Authorization': `Bearer ${this.token}`,
15
+ 'Content-Type': 'application/json',
16
+ },
17
+ body: JSON.stringify({
18
+ inputs: messages[messages.length - 1].content,
19
+ parameters: {
20
+ max_new_tokens: 1024,
21
+ temperature: 0.7,
22
+ top_p: 0.9,
23
+ return_full_text: false
24
+ },
25
+ stream: true
26
+ })
27
+ }
28
+ );
29
+
30
+ if (!response.ok) {
31
+ throw new Error(`API error: ${response.status}`);
32
+ }
33
+
34
+ const reader = response.body.getReader();
35
+ const decoder = new TextDecoder();
36
+ let buffer = '';
37
+
38
+ while (true) {
39
+ const { done, value } = await reader.read();
40
+ if (done) break;
41
+
42
+ buffer += decoder.decode(value, { stream: true });
43
+ const lines = buffer.split('\n');
44
+
45
+ buffer = lines.pop() || '';
46
+
47
+ for (const line of lines) {
48
+ if (line.startsWith('data: ') && line !== 'data: [DONE]') {
49
+ try {
50
+ const data = JSON.parse(line.slice(6));
51
+ if (data.token && data.token.text) {
52
+ onChunk(data.token.text);
53
+ }
54
+ } catch (e) {
55
+ // Skip invalid JSON
56
+ }
57
+ }
58
+ }
59
+ }
60
+
61
+ onComplete();
62
+ } catch (error) {
63
+ onError(error.message);
64
+ }
65
+ }
66
+
67
+ // Alternative method using chat completion format
68
+ async streamChatCompletionAlt(messages, modelConfig, onChunk, onComplete, onError) {
69
+ try {
70
+ // Using the provider-based endpoint
71
+ const response = await fetch(
72
+ 'https://api-inference.huggingface.co/chat/completions',
73
+ {
74
+ method: 'POST',
75
+ headers: {
76
+ 'Authorization': `Bearer ${this.token}`,
77
+ 'Content-Type': 'application/json',
78
+ },
79
+ body: JSON.stringify({
80
+ model: modelConfig.endpoint,
81
+ messages: messages,
82
+ stream: true,
83
+ max_tokens: 1024,
84
+ temperature: 0.7
85
+ })
86
+ }
87
+ );
88
+
89
+ if (!response.ok) {
90
+ const errorData = await response.text();
91
+ throw new Error(`API error: ${response.status} - ${errorData}`);
92
+ }
93
+
94
+ const reader = response.body.getReader();
95
+ const decoder = new TextDecoder();
96
+ let buffer = '';
97
+
98
+ while (true) {
99
+ const { done, value } = await reader.read();
100
+ if (done) break;
101
+
102
+ buffer += decoder.decode(value, { stream: true });
103
+ const lines = buffer.split('\n');
104
+
105
+ buffer = lines.pop() || '';
106
+
107
+ for (const line of lines) {
108
+ if (line.startsWith('data: ') && line !== 'data: [DONE]') {
109
+ try {
110
+ const data = JSON.parse(line.slice(6));
111
+ if (data.choices && data.choices[0] && data.choices[0].delta && data.choices[0].delta.content) {
112
+ onChunk(data.choices[0].delta.content);
113
+ }
114
+ } catch (e) {
115
+ // Skip invalid JSON
116
+ }
117
+ }
118
+ }
119
+ }
120
+
121
+ onComplete();
122
+ } catch (error) {
123
+ onError(error.message);
124
+ }
125
+ }
126
+ }