@@ -24,11 +24,23 @@ const PROVIDER = {
24
24
}
25
25
26
26
class Generation {
27
- constructor ( { message = '' , finishReason = '' , choiceId = '' } = { } ) {
27
+ constructor ( {
28
+ message = '' ,
29
+ finishReason = '' ,
30
+ choiceId = '' ,
31
+ role,
32
+ inputTokens,
33
+ outputTokens
34
+ } = { } ) {
28
35
// stringify message as it could be a single generated message as well as a list of embeddings
29
36
this . message = typeof message === 'string' ? message : JSON . stringify ( message ) || ''
30
37
this . finishReason = finishReason || ''
31
38
this . choiceId = choiceId || undefined
39
+ this . role = role
40
+ this . usage = {
41
+ inputTokens,
42
+ outputTokens
43
+ }
32
44
}
33
45
}
34
46
@@ -202,9 +214,12 @@ function extractTextAndResponseReason (response, provider, modelName) {
202
214
if ( generations . length > 0 ) {
203
215
const generation = generations [ 0 ]
204
216
return new Generation ( {
205
- message : generation . message ,
217
+ message : generation . message . content ,
206
218
finishReason : generation . finish_reason ,
207
- choiceId : shouldSetChoiceIds ? generation . id : undefined
219
+ choiceId : shouldSetChoiceIds ? generation . id : undefined ,
220
+ role : generation . message . role ,
221
+ inputTokens : body . usage ?. prompt_tokens ,
222
+ outputTokens : body . usage ?. completion_tokens
208
223
} )
209
224
}
210
225
}
@@ -214,7 +229,9 @@ function extractTextAndResponseReason (response, provider, modelName) {
214
229
return new Generation ( {
215
230
message : completion . data ?. text ,
216
231
finishReason : completion ?. finishReason ,
217
- choiceId : shouldSetChoiceIds ? completion ?. id : undefined
232
+ choiceId : shouldSetChoiceIds ? completion ?. id : undefined ,
233
+ inputTokens : body . usage ?. prompt_tokens ,
234
+ outputTokens : body . usage ?. completion_tokens
218
235
} )
219
236
}
220
237
return new Generation ( )
@@ -226,7 +243,12 @@ function extractTextAndResponseReason (response, provider, modelName) {
226
243
const results = body . results || [ ]
227
244
if ( results . length > 0 ) {
228
245
const result = results [ 0 ]
229
- return new Generation ( { message : result . outputText , finishReason : result . completionReason } )
246
+ return new Generation ( {
247
+ message : result . outputText ,
248
+ finishReason : result . completionReason ,
249
+ inputTokens : body . inputTextTokenCount ,
250
+ outputTokens : result . tokenCount
251
+ } )
230
252
}
231
253
break
232
254
}
@@ -252,7 +274,12 @@ function extractTextAndResponseReason (response, provider, modelName) {
252
274
break
253
275
}
254
276
case PROVIDER . META : {
255
- return new Generation ( { message : body . generation , finishReason : body . stop_reason } )
277
+ return new Generation ( {
278
+ message : body . generation ,
279
+ finishReason : body . stop_reason ,
280
+ inputTokens : body . prompt_token_count ,
281
+ outputTokens : body . generation_token_count
282
+ } )
256
283
}
257
284
case PROVIDER . MISTRAL : {
258
285
const mistralGenerations = body . outputs || [ ]
0 commit comments