@@ -11,18 +11,10 @@ const logCompletion = (uuid = randomUUID() as string) => {
11
11
} ;
12
12
} ;
13
13
14
- class CustomEvent extends Event {
15
- detail : any ;
16
- constructor ( message : any , data : any ) {
17
- super ( message , data ) ;
18
- this . detail = data . detail ;
19
- }
20
- }
21
-
22
14
const defualtParameters = {
23
15
stream : true ,
24
- n_predict : 128 ,
25
- temperature : 0.3 ,
16
+ n_predict : 1024 ,
17
+ temperature : 0.7 ,
26
18
stop : [ ] ,
27
19
repeat_last_n : 256 ,
28
20
repeat_penalty : 1.18 ,
@@ -62,15 +54,6 @@ export async function* sendChatRequest(
62
54
63
55
const startTime = performance . now ( ) ;
64
56
65
- // const response = await fetch(`${url}/completion`, {
66
- // body: JSON.stringify(parametersForCompletion),
67
- // method: "POST",
68
- // headers: {
69
- // Connection: "keep-alive",
70
- // "Content-Type": "application/json",
71
- // Accept: "text/event-stream",
72
- // },
73
- // });
74
57
let content = "" ;
75
58
let timings ;
76
59
for await ( const chunk of llama ( prompt , parametersForCompletion , { url } ) ) {
@@ -80,15 +63,7 @@ export async function* sendChatRequest(
80
63
content += chunk . data . content ;
81
64
yield content ;
82
65
}
83
- // @ts -ignore
84
- // if (chunk.data.generation_settings) {
85
- // eventTarget.dispatchEvent(
86
- // new CustomEvent("generation_settings", {
87
- // // @ts -ignore
88
- // detail: chunk.data.generation_settings,
89
- // })
90
- // );
91
- // }
66
+
92
67
// @ts -ignore
93
68
if ( chunk . data . timings ) {
94
69
// @ts -ignore
@@ -154,26 +129,18 @@ export async function* sendChatRequest(
154
129
}
155
130
}
156
131
157
- const paramDefaults = {
158
- stream : true ,
159
- n_predict : 500 ,
160
- temperature : 0.2 ,
161
- // stop: ["</s>"],
162
- } ;
163
-
164
132
export async function * llama (
165
133
prompt : string ,
166
134
params = { } ,
167
135
config : { controller ?: AbortController ; url ?: string } = { }
168
136
) {
169
- let generation_settings ;
170
137
let controller = config . controller ;
171
138
172
139
if ( ! controller ) {
173
140
controller = new AbortController ( ) ;
174
141
}
175
142
176
- const completionParams = { ...paramDefaults , ... params , prompt } ;
143
+ const completionParams = { ...params , prompt } ;
177
144
178
145
const response = await fetch ( `${ config . url } /completion` , {
179
146
method : "POST" ,
@@ -183,7 +150,7 @@ export async function* llama(
183
150
"Content-Type" : "application/json" ,
184
151
Accept : "text/event-stream" ,
185
152
} ,
186
- // signal: controller.signal,
153
+ signal : controller . signal ,
187
154
} ) ;
188
155
// @ts -ignore
189
156
const reader = response . body . getReader < any > ( ) ;
@@ -241,10 +208,6 @@ export async function* llama(
241
208
// @ts -ignore
242
209
if ( result . data . stop ) {
243
210
// @ts -ignore
244
- if ( result . data . generation_settings ) {
245
- // @ts -ignore
246
- generation_settings = result . data . generation_settings ;
247
- }
248
211
cont = false ;
249
212
break ;
250
213
}
@@ -284,39 +247,3 @@ export async function* llama(
284
247
285
248
return content ;
286
249
}
287
-
288
- export const llamaEventTarget = ( prompt : string , params = { } , config = { } ) => {
289
- const eventTarget = new EventTarget ( ) ;
290
- ( async ( ) => {
291
- let content = "" ;
292
- for await ( const chunk of llama ( prompt , params , config ) ) {
293
- // @ts -ignore
294
- if ( chunk . data ) {
295
- // @ts -ignore
296
- content += chunk . data . content ;
297
- eventTarget . dispatchEvent (
298
- // @ts -ignore
299
- new CustomEvent ( "message" , { detail : chunk . data } )
300
- ) ;
301
- }
302
- // @ts -ignore
303
- if ( chunk . data . generation_settings ) {
304
- eventTarget . dispatchEvent (
305
- new CustomEvent ( "generation_settings" , {
306
- // @ts -ignore
307
- detail : chunk . data . generation_settings ,
308
- } )
309
- ) ;
310
- }
311
- // @ts -ignore
312
- if ( chunk . data . timings ) {
313
- eventTarget . dispatchEvent (
314
- // @ts -ignore
315
- new CustomEvent ( "timings" , { detail : chunk . data . timings } )
316
- ) ;
317
- }
318
- }
319
- eventTarget . dispatchEvent ( new CustomEvent ( "done" , { detail : { content } } ) ) ;
320
- } ) ( ) ;
321
- return eventTarget ;
322
- } ;
0 commit comments