@@ -11,18 +11,10 @@ const logCompletion = (uuid = randomUUID() as string) => {
1111 } ;
1212} ;
1313
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-
2214const defualtParameters = {
2315 stream : true ,
24- n_predict : 128 ,
25- temperature : 0.3 ,
16+ n_predict : 1024 ,
17+ temperature : 0.7 ,
2618 stop : [ ] ,
2719 repeat_last_n : 256 ,
2820 repeat_penalty : 1.18 ,
@@ -62,15 +54,6 @@ export async function* sendChatRequest(
6254
6355 const startTime = performance . now ( ) ;
6456
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- // });
7457 let content = "" ;
7558 let timings ;
7659 for await ( const chunk of llama ( prompt , parametersForCompletion , { url } ) ) {
@@ -80,15 +63,7 @@ export async function* sendChatRequest(
8063 content += chunk . data . content ;
8164 yield content ;
8265 }
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+
9267 // @ts -ignore
9368 if ( chunk . data . timings ) {
9469 // @ts -ignore
@@ -154,26 +129,18 @@ export async function* sendChatRequest(
154129 }
155130}
156131
157- const paramDefaults = {
158- stream : true ,
159- n_predict : 500 ,
160- temperature : 0.2 ,
161- // stop: ["</s>"],
162- } ;
163-
164132export async function * llama (
165133 prompt : string ,
166134 params = { } ,
167135 config : { controller ?: AbortController ; url ?: string } = { }
168136) {
169- let generation_settings ;
170137 let controller = config . controller ;
171138
172139 if ( ! controller ) {
173140 controller = new AbortController ( ) ;
174141 }
175142
176- const completionParams = { ...paramDefaults , ... params , prompt } ;
143+ const completionParams = { ...params , prompt } ;
177144
178145 const response = await fetch ( `${ config . url } /completion` , {
179146 method : "POST" ,
@@ -183,7 +150,7 @@ export async function* llama(
183150 "Content-Type" : "application/json" ,
184151 Accept : "text/event-stream" ,
185152 } ,
186- // signal: controller.signal,
153+ signal : controller . signal ,
187154 } ) ;
188155 // @ts -ignore
189156 const reader = response . body . getReader < any > ( ) ;
@@ -241,10 +208,6 @@ export async function* llama(
241208 // @ts -ignore
242209 if ( result . data . stop ) {
243210 // @ts -ignore
244- if ( result . data . generation_settings ) {
245- // @ts -ignore
246- generation_settings = result . data . generation_settings ;
247- }
248211 cont = false ;
249212 break ;
250213 }
@@ -284,39 +247,3 @@ export async function* llama(
284247
285248 return content ;
286249}
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