1+ import { basename } from "https://deno.land/std@0.189.0/path/mod.ts" ;
2+ import { decodeStream , throwError } from "./util.ts" ;
13import type {
24 ChatCompletion ,
35 ChatCompletionOptions ,
6+ ChatCompletionStream ,
47 Completion ,
58 CompletionOptions ,
9+ CompletionStream ,
610 DeletedFile ,
711 DeletedFineTune ,
812 Edit ,
@@ -29,7 +33,6 @@ import type {
2933 Translation ,
3034 TranslationOptions ,
3135} from "./types.ts" ;
32- import { basename } from "https://deno.land/std@0.187.0/path/mod.ts" ;
3336
3437const defaultBaseUrl = "https://api.openai.com/v1" ;
3538
@@ -67,17 +70,7 @@ export class OpenAI {
6770 ) ;
6871 const data = await response . json ( ) ;
6972
70- if ( data . error ) {
71- let errorMessage = `${ data . error . type } ` ;
72- if ( data . error . message ) {
73- errorMessage += ": " + data . error . message ;
74- }
75- if ( data . error . code ) {
76- errorMessage += ` (${ data . error . code } )` ;
77- }
78- console . log ( data . error ) ;
79- throw new Error ( errorMessage ) ;
80- }
73+ throwError ( data ) ;
8174
8275 return data ;
8376 }
@@ -108,7 +101,6 @@ export class OpenAI {
108101 * https://platform.openai.com/docs/api-reference/completions/create
109102 */
110103 async createCompletion ( options : CompletionOptions ) : Promise < Completion > {
111- // TODO: make options.stream work
112104 return await this . #request( `/completions` , {
113105 model : options . model ,
114106 prompt : options . prompt ,
@@ -117,7 +109,6 @@ export class OpenAI {
117109 temperature : options . temperature ,
118110 top_p : options . topP ,
119111 n : options . n ,
120- stream : options . stream ,
121112 logprobs : options . logprobs ,
122113 echo : options . echo ,
123114 stop : options . stop ,
@@ -129,6 +120,46 @@ export class OpenAI {
129120 } ) ;
130121 }
131122
123+ /**
124+ * Creates a completion stream for the provided prompt and parameters
125+ *
126+ * https://platform.openai.com/docs/api-reference/completions/create
127+ */
128+ async createCompletionStream (
129+ options : Omit < CompletionOptions , "bestOf" > ,
130+ callback : ( chunk : CompletionStream ) => void ,
131+ ) : Promise < void > {
132+ const res = await fetch (
133+ `${ this . #baseUrl} /completions` ,
134+ {
135+ method : "POST" ,
136+ headers : {
137+ Authorization : `Bearer ${ this . #privateKey} ` ,
138+ "Content-Type" : "application/json" ,
139+ } ,
140+ body : JSON . stringify ( {
141+ model : options . model ,
142+ prompt : options . prompt ,
143+ suffix : options . suffix ,
144+ max_tokens : options . maxTokens ,
145+ temperature : options . temperature ,
146+ top_p : options . topP ,
147+ n : options . n ,
148+ stream : true ,
149+ logprobs : options . logprobs ,
150+ echo : options . echo ,
151+ stop : options . stop ,
152+ presence_penalty : options . presencePenalty ,
153+ frequency_penalty : options . frequencyPenalty ,
154+ logit_bias : options . logitBias ,
155+ user : options . user ,
156+ } ) ,
157+ } ,
158+ ) ;
159+
160+ await decodeStream ( res , callback ) ;
161+ }
162+
132163 /**
133164 * Creates a completion for the chat message
134165 *
@@ -143,7 +174,6 @@ export class OpenAI {
143174 temperature : options . temperature ,
144175 top_p : options . topP ,
145176 n : options . n ,
146- stream : options . stream ,
147177 stop : options . stop ,
148178 max_tokens : options . maxTokens ,
149179 presence_penalty : options . presencePenalty ,
@@ -153,6 +183,43 @@ export class OpenAI {
153183 } ) ;
154184 }
155185
186+ /**
187+ * Creates a completion stream for the chat message
188+ *
189+ * https://platform.openai.com/docs/api-reference/chat/create
190+ */
191+ async createChatCompletionStream (
192+ options : ChatCompletionOptions ,
193+ callback : ( chunk : ChatCompletionStream ) => void ,
194+ ) : Promise < void > {
195+ const res = await fetch (
196+ `${ this . #baseUrl} /chat/completions` ,
197+ {
198+ method : "POST" ,
199+ headers : {
200+ Authorization : `Bearer ${ this . #privateKey} ` ,
201+ "Content-Type" : "application/json" ,
202+ } ,
203+ body : JSON . stringify ( {
204+ model : options . model ,
205+ messages : options . messages ,
206+ temperature : options . temperature ,
207+ top_p : options . topP ,
208+ n : options . n ,
209+ stream : true ,
210+ stop : options . stop ,
211+ max_tokens : options . maxTokens ,
212+ presence_penalty : options . presencePenalty ,
213+ frequency_penalty : options . frequencyPenalty ,
214+ logit_bias : options . logitBias ,
215+ user : options . user ,
216+ } ) ,
217+ } ,
218+ ) ;
219+
220+ await decodeStream ( res , callback ) ;
221+ }
222+
156223 /**
157224 * Creates a new edit for the provided input, instruction, and parameters.
158225 *
0 commit comments