1
1
const { NativeEmbedder } = require ( "../../EmbeddingEngines/native" ) ;
2
- const {
3
- handleDefaultStreamResponseV2,
4
- } = require ( "../../helpers/chat/responses" ) ;
5
-
6
2
const { v4 : uuidv4 } = require ( "uuid" ) ;
7
3
const {
8
4
writeResponseChunk,
@@ -98,6 +94,24 @@ class ApiPieLLM {
98
94
) ;
99
95
}
100
96
97
+ chatModels ( ) {
98
+ const allModels = this . models ( ) ;
99
+ return Object . entries ( allModels ) . reduce (
100
+ ( chatModels , [ modelId , modelInfo ] ) => {
101
+ // Filter for chat models
102
+ if (
103
+ modelInfo . subtype &&
104
+ ( modelInfo . subtype . includes ( "chat" ) ||
105
+ modelInfo . subtype . includes ( "chatx" ) )
106
+ ) {
107
+ chatModels [ modelId ] = modelInfo ;
108
+ }
109
+ return chatModels ;
110
+ } ,
111
+ { }
112
+ ) ;
113
+ }
114
+
101
115
streamingEnabled ( ) {
102
116
return "streamGetChatCompletion" in this ;
103
117
}
@@ -114,13 +128,13 @@ class ApiPieLLM {
114
128
}
115
129
116
130
promptWindowLimit ( ) {
117
- const availableModels = this . models ( ) ;
131
+ const availableModels = this . chatModels ( ) ;
118
132
return availableModels [ this . model ] ?. maxLength || 4096 ;
119
133
}
120
134
121
135
async isValidChatCompletionModel ( model = "" ) {
122
136
await this . #syncModels( ) ;
123
- const availableModels = this . models ( ) ;
137
+ const availableModels = this . chatModels ( ) ;
124
138
return availableModels . hasOwnProperty ( model ) ;
125
139
}
126
140
@@ -189,22 +203,20 @@ class ApiPieLLM {
189
203
return result . choices [ 0 ] . message . content ;
190
204
}
191
205
192
- // APIPie says it supports streaming, but it does not work across all models and providers.
193
- // Notably, it is not working for OpenRouter models at all.
194
- // async streamGetChatCompletion(messages = null, { temperature = 0.7 }) {
195
- // if (!(await this.isValidChatCompletionModel(this.model)))
196
- // throw new Error(
197
- // `ApiPie chat: ${this.model} is not valid for chat completion!`
198
- // );
199
-
200
- // const streamRequest = await this.openai.chat.completions.create({
201
- // model: this.model,
202
- // stream: true,
203
- // messages,
204
- // temperature,
205
- // });
206
- // return streamRequest;
207
- // }
206
+ async streamGetChatCompletion ( messages = null , { temperature = 0.7 } ) {
207
+ if ( ! ( await this . isValidChatCompletionModel ( this . model ) ) )
208
+ throw new Error (
209
+ `ApiPie chat: ${ this . model } is not valid for chat completion!`
210
+ ) ;
211
+
212
+ const streamRequest = await this . openai . chat . completions . create ( {
213
+ model : this . model ,
214
+ stream : true ,
215
+ messages,
216
+ temperature,
217
+ } ) ;
218
+ return streamRequest ;
219
+ }
208
220
209
221
handleStream ( response , stream , responseProps ) {
210
222
const { uuid = uuidv4 ( ) , sources = [ ] } = responseProps ;
@@ -264,10 +276,6 @@ class ApiPieLLM {
264
276
} ) ;
265
277
}
266
278
267
- // handleStream(response, stream, responseProps) {
268
- // return handleDefaultStreamResponseV2(response, stream, responseProps);
269
- // }
270
-
271
279
// Simple wrapper for dynamic embedder & normalize interface for all LLM implementations
272
280
async embedTextInput ( textInput ) {
273
281
return await this . embedder . embedTextInput ( textInput ) ;
@@ -300,6 +308,7 @@ async function fetchApiPieModels(providedApiKey = null) {
300
308
id : `${ model . provider } /${ model . model } ` ,
301
309
name : `${ model . provider } /${ model . model } ` ,
302
310
organization : model . provider ,
311
+ subtype : model . subtype ,
303
312
maxLength : model . max_tokens ,
304
313
} ;
305
314
} ) ;
0 commit comments