@@ -33,12 +33,12 @@ export class InceptionApi extends OpenAIApi {
3333 } ) ;
3434 }
3535
36- // Add custom edit completions method
36+ // Add custom edit completions method.
3737 async * editCompletionStream (
3838 body : ChatCompletionCreateParamsStreaming ,
3939 signal : AbortSignal ,
4040 ) : AsyncGenerator < ChatCompletionChunk , any , unknown > {
41- const endpoint = new URL ( "chat /completions" , this . apiBase ) ;
41+ const endpoint = new URL ( "edit /completions" , this . apiBase ) ;
4242 const resp = await customFetch ( this . config . requestOptions ) ( endpoint , {
4343 method : "POST" ,
4444 body : JSON . stringify ( {
@@ -71,16 +71,42 @@ export class InceptionApi extends OpenAIApi {
7171 }
7272 }
7373
74- // Override the regular chat method to route to edit endpoint for certain models
74+ // Add custom edit completions method (non-streaming).
75+ async editCompletionNonStream (
76+ body : ChatCompletionCreateParamsNonStreaming ,
77+ signal : AbortSignal ,
78+ ) : Promise < ChatCompletion > {
79+ const endpoint = new URL ( "edit/completions" , this . apiBase ) ;
80+ const resp = await customFetch ( this . config . requestOptions ) ( endpoint , {
81+ method : "POST" ,
82+ body : JSON . stringify ( {
83+ model : body . model ,
84+ messages : body . messages ,
85+ max_tokens : body . max_tokens ,
86+ temperature : body . temperature ,
87+ top_p : body . top_p ,
88+ frequency_penalty : body . frequency_penalty ,
89+ presence_penalty : body . presence_penalty ,
90+ stop : body . stop ,
91+ stream : false , // Set to false for non-streaming
92+ } ) ,
93+ headers : {
94+ "Content-Type" : "application/json" ,
95+ Accept : "application/json" ,
96+ Authorization : `Bearer ${ this . config . apiKey } ` ,
97+ } ,
98+ signal,
99+ } ) ;
100+
101+ const data = await resp . json ( ) ;
102+ return data as ChatCompletion ;
103+ }
104+
105+ // Override the regular chat stream method to route to edit endpoint for next edit requests.
75106 async * chatCompletionStream (
76107 body : ChatCompletionCreateParamsStreaming ,
77108 signal : AbortSignal ,
78109 ) : AsyncGenerator < ChatCompletionChunk , any , unknown > {
79- // Cast to access the custom property
80- // const bodyWithNextEdit =
81- // body as InceptionChatCompletionCreateParamsStreaming;
82- // const { nextEdit, ...cleanBody } = bodyWithNextEdit;
83-
84110 if ( this . isNextEdit ( body . messages ) ) {
85111 body . messages = this . removeNextEditToken ( body . messages ) ;
86112 yield * this . editCompletionStream ( body , signal ) ;
@@ -89,16 +115,14 @@ export class InceptionApi extends OpenAIApi {
89115 }
90116 }
91117
118+ // Override the regular chat non stream method to route to edit endpoint for next edit requests.
92119 async chatCompletionNonStream (
93120 body : ChatCompletionCreateParamsNonStreaming ,
94121 signal : AbortSignal ,
95122 ) : Promise < ChatCompletion > {
96- // const bodyWithNextEdit =
97- // body as InceptionChatCompletionCreateParamsNonStreaming;
98- // const { nextEdit, ...cleanBody } = bodyWithNextEdit;
99-
100123 if ( this . isNextEdit ( body . messages ) ) {
101- throw new Error ( "Non-streaming edit completions not yet implemented" ) ;
124+ body . messages = this . removeNextEditToken ( body . messages ) ;
125+ return this . editCompletionNonStream ( body , signal ) ;
102126 } else {
103127 return super . chatCompletionNonStream ( body , signal ) ;
104128 }
@@ -146,15 +170,16 @@ export class InceptionApi extends OpenAIApi {
146170 throw new Error ( "Method not implemented." ) ;
147171 }
148172
173+ // Check if any message contains the unique next edit token.
149174 private isNextEdit ( messages : ChatCompletionMessageParam [ ] ) : boolean {
150- // Check if any message contains the unique next edit token.
151175 return messages . some (
152176 ( message ) =>
153177 typeof message . content === "string" &&
154178 message . content . endsWith ( UNIQUE_TOKEN ) ,
155179 ) ;
156180 }
157181
182+ // Remove the unique token from messages.
158183 private removeNextEditToken (
159184 messages : ChatCompletionMessageParam [ ] ,
160185 ) : ChatCompletionMessageParam [ ] {
0 commit comments