11import type { InferenceTask , Options , RequestArgs } from "../../types" ;
2- import { makeRequestOptions } from "../../lib/makeRequestOptions" ;
3- import type { EventSourceMessage } from "../../vendor/fetch-event-source/parse" ;
4- import { getLines , getMessages } from "../../vendor/fetch-event-source/parse" ;
5-
2+ import { innerStreamingRequest } from "../../utils/request" ;
63/**
74 * Primitive to make custom inference calls that expect server-sent events, and returns the response through a generator
5+ * @deprecated Use specific task functions instead. This function will be removed in a future version.
86 */
97export async function * streamingRequest < T > (
108 args : RequestArgs ,
@@ -15,86 +13,5 @@ export async function* streamingRequest<T>(
1513 chatCompletion ?: boolean ;
1614 }
1715) : AsyncGenerator < T > {
18- const { url, info } = await makeRequestOptions ( { ...args , stream : true } , options ) ;
19- const response = await ( options ?. fetch ?? fetch ) ( url , info ) ;
20-
21- if ( options ?. retry_on_error !== false && response . status === 503 ) {
22- return yield * streamingRequest ( args , options ) ;
23- }
24- if ( ! response . ok ) {
25- if ( response . headers . get ( "Content-Type" ) ?. startsWith ( "application/json" ) ) {
26- const output = await response . json ( ) ;
27- if ( [ 400 , 422 , 404 , 500 ] . includes ( response . status ) && options ?. chatCompletion ) {
28- throw new Error ( `Server ${ args . model } does not seem to support chat completion. Error: ${ output . error } ` ) ;
29- }
30- if ( typeof output . error === "string" ) {
31- throw new Error ( output . error ) ;
32- }
33- if ( output . error && "message" in output . error && typeof output . error . message === "string" ) {
34- /// OpenAI errors
35- throw new Error ( output . error . message ) ;
36- }
37- }
38-
39- throw new Error ( `Server response contains error: ${ response . status } ` ) ;
40- }
41- if ( ! response . headers . get ( "content-type" ) ?. startsWith ( "text/event-stream" ) ) {
42- throw new Error (
43- `Server does not support event stream content type, it returned ` + response . headers . get ( "content-type" )
44- ) ;
45- }
46-
47- if ( ! response . body ) {
48- return ;
49- }
50-
51- const reader = response . body . getReader ( ) ;
52- let events : EventSourceMessage [ ] = [ ] ;
53-
54- const onEvent = ( event : EventSourceMessage ) => {
55- // accumulate events in array
56- events . push ( event ) ;
57- } ;
58-
59- const onChunk = getLines (
60- getMessages (
61- ( ) => { } ,
62- ( ) => { } ,
63- onEvent
64- )
65- ) ;
66-
67- try {
68- while ( true ) {
69- const { done, value } = await reader . read ( ) ;
70- if ( done ) {
71- return ;
72- }
73- onChunk ( value ) ;
74- for ( const event of events ) {
75- if ( event . data . length > 0 ) {
76- if ( event . data === "[DONE]" ) {
77- return ;
78- }
79- const data = JSON . parse ( event . data ) ;
80- if ( typeof data === "object" && data !== null && "error" in data ) {
81- const errorStr =
82- typeof data . error === "string"
83- ? data . error
84- : typeof data . error === "object" &&
85- data . error &&
86- "message" in data . error &&
87- typeof data . error . message === "string"
88- ? data . error . message
89- : JSON . stringify ( data . error ) ;
90- throw new Error ( `Error forwarded from backend: ` + errorStr ) ;
91- }
92- yield data as T ;
93- }
94- }
95- events = [ ] ;
96- }
97- } finally {
98- reader . releaseLock ( ) ;
99- }
16+ yield * innerStreamingRequest ( args , options ) ;
10017}
0 commit comments