@lobehub/chat 1.96.13 → 1.96.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +25 -0
- package/changelog/v1.json +9 -0
- package/docs/self-hosting/advanced/online-search.mdx +21 -21
- package/docs/self-hosting/advanced/online-search.zh-CN.mdx +29 -29
- package/package.json +2 -3
- package/src/libs/model-runtime/google/index.test.ts +93 -36
- package/src/libs/model-runtime/google/index.ts +50 -64
- package/src/libs/model-runtime/utils/streams/google-ai.test.ts +416 -17
- package/src/libs/model-runtime/utils/streams/google-ai.ts +17 -17
- package/src/libs/model-runtime/utils/streams/vertex-ai.test.ts +129 -0
- package/src/libs/model-runtime/utils/streams/vertex-ai.ts +16 -16
- package/src/libs/model-runtime/vertexai/index.ts +9 -3
@@ -1,4 +1,4 @@
|
|
1
|
-
import {
|
1
|
+
import { GenerateContentResponse } from '@google/genai';
|
2
2
|
|
3
3
|
import { ModelTokensUsage } from '@/types/message';
|
4
4
|
import { GroundingSearch } from '@/types/search';
|
@@ -16,7 +16,7 @@ import {
|
|
16
16
|
} from './protocol';
|
17
17
|
|
18
18
|
const transformGoogleGenerativeAIStream = (
|
19
|
-
chunk:
|
19
|
+
chunk: GenerateContentResponse,
|
20
20
|
context: StreamContext,
|
21
21
|
): StreamProtocolChunk | StreamProtocolChunk[] => {
|
22
22
|
// maybe need another structure to add support for multiple choices
|
@@ -24,22 +24,22 @@ const transformGoogleGenerativeAIStream = (
|
|
24
24
|
const usage = chunk.usageMetadata;
|
25
25
|
const usageChunks: StreamProtocolChunk[] = [];
|
26
26
|
if (candidate?.finishReason && usage) {
|
27
|
-
|
28
|
-
const
|
27
|
+
// totalTokenCount = promptTokenCount + candidatesTokenCount + thoughtsTokenCount
|
28
|
+
const reasoningTokens = usage.thoughtsTokenCount;
|
29
|
+
const outputTextTokens = usage.candidatesTokenCount ?? 0;
|
30
|
+
const totalOutputTokens = outputTextTokens + (reasoningTokens ?? 0);
|
29
31
|
|
30
32
|
usageChunks.push(
|
31
33
|
{ data: candidate.finishReason, id: context?.id, type: 'stop' },
|
32
34
|
{
|
33
35
|
data: {
|
34
36
|
// TODO: Google SDK 0.24.0 don't have promptTokensDetails types
|
35
|
-
inputImageTokens:
|
36
|
-
|
37
|
-
)
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
outputReasoningTokens,
|
42
|
-
outputTextTokens: totalOutputTokens - (outputReasoningTokens ?? 0),
|
37
|
+
inputImageTokens: usage.promptTokensDetails?.find((i: any) => i.modality === 'IMAGE')
|
38
|
+
?.tokenCount,
|
39
|
+
inputTextTokens: usage.promptTokensDetails?.find((i: any) => i.modality === 'TEXT')
|
40
|
+
?.tokenCount,
|
41
|
+
outputReasoningTokens: reasoningTokens,
|
42
|
+
outputTextTokens,
|
43
43
|
totalInputTokens: usage.promptTokenCount,
|
44
44
|
totalOutputTokens,
|
45
45
|
totalTokens: usage.totalTokenCount,
|
@@ -50,7 +50,7 @@ const transformGoogleGenerativeAIStream = (
|
|
50
50
|
);
|
51
51
|
}
|
52
52
|
|
53
|
-
const functionCalls = chunk.functionCalls
|
53
|
+
const functionCalls = chunk.functionCalls;
|
54
54
|
|
55
55
|
if (functionCalls) {
|
56
56
|
return [
|
@@ -73,11 +73,11 @@ const transformGoogleGenerativeAIStream = (
|
|
73
73
|
];
|
74
74
|
}
|
75
75
|
|
76
|
-
const text = chunk.text
|
76
|
+
const text = chunk.text;
|
77
77
|
|
78
78
|
if (candidate) {
|
79
79
|
// 首先检查是否为 reasoning 内容 (thought: true)
|
80
|
-
if (Array.isArray(candidate.content
|
80
|
+
if (Array.isArray(candidate.content?.parts) && candidate.content.parts.length > 0) {
|
81
81
|
for (const part of candidate.content.parts) {
|
82
82
|
if (part && part.text && (part as any).thought === true) {
|
83
83
|
return { data: part.text, id: context.id, type: 'reasoning' };
|
@@ -122,7 +122,7 @@ const transformGoogleGenerativeAIStream = (
|
|
122
122
|
if (!!text?.trim()) return { data: text, id: context?.id, type: 'text' };
|
123
123
|
|
124
124
|
// streaming the image
|
125
|
-
if (Array.isArray(candidate.content
|
125
|
+
if (Array.isArray(candidate.content?.parts) && candidate.content.parts.length > 0) {
|
126
126
|
const part = candidate.content.parts[0];
|
127
127
|
|
128
128
|
if (part && part.inlineData && part.inlineData.data && part.inlineData.mimeType) {
|
@@ -148,7 +148,7 @@ export interface GoogleAIStreamOptions {
|
|
148
148
|
}
|
149
149
|
|
150
150
|
export const GoogleGenerativeAIStream = (
|
151
|
-
rawStream: ReadableStream<
|
151
|
+
rawStream: ReadableStream<GenerateContentResponse>,
|
152
152
|
{ callbacks, inputStartAt }: GoogleAIStreamOptions = {},
|
153
153
|
) => {
|
154
154
|
const streamStack: StreamContext = { id: 'chat_' + nanoid() };
|
@@ -330,4 +330,133 @@ describe('VertexAIStream', () => {
|
|
330
330
|
].map((i) => i + '\n'),
|
331
331
|
);
|
332
332
|
});
|
333
|
+
|
334
|
+
it('should return empty text chunk without candidates', async () => {
|
335
|
+
vi.spyOn(uuidModule, 'nanoid').mockReturnValueOnce('1');
|
336
|
+
|
337
|
+
const data = [
|
338
|
+
{
|
339
|
+
candidates: [
|
340
|
+
{
|
341
|
+
content: { parts: [{ text: '234' }], role: 'model' },
|
342
|
+
safetyRatings: [
|
343
|
+
{ category: 'HARM_CATEGORY_HATE_SPEECH', probability: 'NEGLIGIBLE' },
|
344
|
+
{ category: 'HARM_CATEGORY_DANGEROUS_CONTENT', probability: 'NEGLIGIBLE' },
|
345
|
+
{ category: 'HARM_CATEGORY_HARASSMENT', probability: 'NEGLIGIBLE' },
|
346
|
+
{ category: 'HARM_CATEGORY_SEXUALLY_EXPLICIT', probability: 'NEGLIGIBLE' },
|
347
|
+
],
|
348
|
+
},
|
349
|
+
],
|
350
|
+
usageMetadata: {
|
351
|
+
promptTokenCount: 20,
|
352
|
+
candidatesTokenCount: 3,
|
353
|
+
totalTokenCount: 23,
|
354
|
+
promptTokensDetails: [{ modality: 'TEXT', tokenCount: 20 }],
|
355
|
+
candidatesTokensDetails: [{ modality: 'TEXT', tokenCount: 3 }],
|
356
|
+
},
|
357
|
+
modelVersion: 'gemini-2.5-flash-preview-04-17',
|
358
|
+
},
|
359
|
+
{
|
360
|
+
usageMetadata: {
|
361
|
+
promptTokenCount: 20,
|
362
|
+
candidatesTokenCount: 3,
|
363
|
+
totalTokenCount: 23,
|
364
|
+
promptTokensDetails: [{ modality: 'TEXT', tokenCount: 20 }],
|
365
|
+
candidatesTokensDetails: [{ modality: 'TEXT', tokenCount: 3 }],
|
366
|
+
},
|
367
|
+
modelVersion: 'gemini-2.5-flash-preview-04-17',
|
368
|
+
},
|
369
|
+
];
|
370
|
+
|
371
|
+
const mockGoogleStream = new ReadableStream({
|
372
|
+
start(controller) {
|
373
|
+
data.forEach((item) => {
|
374
|
+
controller.enqueue(item);
|
375
|
+
});
|
376
|
+
|
377
|
+
controller.close();
|
378
|
+
},
|
379
|
+
});
|
380
|
+
|
381
|
+
const protocolStream = VertexAIStream(mockGoogleStream);
|
382
|
+
|
383
|
+
const decoder = new TextDecoder();
|
384
|
+
const chunks = [];
|
385
|
+
|
386
|
+
// @ts-ignore
|
387
|
+
for await (const chunk of protocolStream) {
|
388
|
+
chunks.push(decoder.decode(chunk, { stream: true }));
|
389
|
+
}
|
390
|
+
|
391
|
+
expect(chunks).toEqual(
|
392
|
+
['id: chat_1', 'event: text', 'data: "234"\n', 'id: chat_1', 'event: text', `data: ""\n`].map(
|
393
|
+
(i) => i + '\n',
|
394
|
+
),
|
395
|
+
);
|
396
|
+
});
|
397
|
+
|
398
|
+
it('should return stop chunk with empty content candidates', async () => {
|
399
|
+
vi.spyOn(uuidModule, 'nanoid').mockReturnValueOnce('1');
|
400
|
+
|
401
|
+
const data = [
|
402
|
+
{
|
403
|
+
candidates: [
|
404
|
+
{
|
405
|
+
content: { parts: [{ text: '234' }], role: 'model' },
|
406
|
+
safetyRatings: [
|
407
|
+
{ category: 'HARM_CATEGORY_HATE_SPEECH', probability: 'NEGLIGIBLE' },
|
408
|
+
{ category: 'HARM_CATEGORY_DANGEROUS_CONTENT', probability: 'NEGLIGIBLE' },
|
409
|
+
{ category: 'HARM_CATEGORY_HARASSMENT', probability: 'NEGLIGIBLE' },
|
410
|
+
{ category: 'HARM_CATEGORY_SEXUALLY_EXPLICIT', probability: 'NEGLIGIBLE' },
|
411
|
+
],
|
412
|
+
},
|
413
|
+
],
|
414
|
+
usageMetadata: {
|
415
|
+
promptTokenCount: 20,
|
416
|
+
candidatesTokenCount: 3,
|
417
|
+
totalTokenCount: 23,
|
418
|
+
promptTokensDetails: [{ modality: 'TEXT', tokenCount: 20 }],
|
419
|
+
candidatesTokensDetails: [{ modality: 'TEXT', tokenCount: 3 }],
|
420
|
+
},
|
421
|
+
modelVersion: 'gemini-2.5-flash-preview-04-17',
|
422
|
+
},
|
423
|
+
{
|
424
|
+
candidates: [{}],
|
425
|
+
usageMetadata: {
|
426
|
+
promptTokenCount: 20,
|
427
|
+
candidatesTokenCount: 3,
|
428
|
+
totalTokenCount: 23,
|
429
|
+
promptTokensDetails: [{ modality: 'TEXT', tokenCount: 20 }],
|
430
|
+
candidatesTokensDetails: [{ modality: 'TEXT', tokenCount: 3 }],
|
431
|
+
},
|
432
|
+
modelVersion: 'gemini-2.5-flash-preview-04-17',
|
433
|
+
},
|
434
|
+
];
|
435
|
+
|
436
|
+
const mockGoogleStream = new ReadableStream({
|
437
|
+
start(controller) {
|
438
|
+
data.forEach((item) => {
|
439
|
+
controller.enqueue(item);
|
440
|
+
});
|
441
|
+
|
442
|
+
controller.close();
|
443
|
+
},
|
444
|
+
});
|
445
|
+
|
446
|
+
const protocolStream = VertexAIStream(mockGoogleStream);
|
447
|
+
|
448
|
+
const decoder = new TextDecoder();
|
449
|
+
const chunks = [];
|
450
|
+
|
451
|
+
// @ts-ignore
|
452
|
+
for await (const chunk of protocolStream) {
|
453
|
+
chunks.push(decoder.decode(chunk, { stream: true }));
|
454
|
+
}
|
455
|
+
|
456
|
+
expect(chunks).toEqual(
|
457
|
+
['id: chat_1', 'event: text', 'data: "234"\n', 'id: chat_1', 'event: stop', `data: ""\n`].map(
|
458
|
+
(i) => i + '\n',
|
459
|
+
),
|
460
|
+
);
|
461
|
+
});
|
333
462
|
});
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import {
|
1
|
+
import { GenerateContentResponse } from '@google/genai';
|
2
2
|
|
3
3
|
import { ModelTokensUsage } from '@/types/message';
|
4
4
|
import { nanoid } from '@/utils/uuid';
|
@@ -22,8 +22,9 @@ const transformVertexAIStream = (
|
|
22
22
|
const usage = chunk.usageMetadata;
|
23
23
|
const usageChunks: StreamProtocolChunk[] = [];
|
24
24
|
if (candidate?.finishReason && usage) {
|
25
|
-
const outputReasoningTokens =
|
26
|
-
const
|
25
|
+
const outputReasoningTokens = usage.thoughtsTokenCount || undefined;
|
26
|
+
const outputTextTokens = usage.candidatesTokenCount ?? 0;
|
27
|
+
const totalOutputTokens = outputTextTokens + (outputReasoningTokens ?? 0);
|
27
28
|
|
28
29
|
usageChunks.push(
|
29
30
|
{ data: candidate.finishReason, id: context?.id, type: 'stop' },
|
@@ -37,7 +38,7 @@ const transformVertexAIStream = (
|
|
37
38
|
(i: any) => i.modality === 'TEXT',
|
38
39
|
)?.tokenCount,
|
39
40
|
outputReasoningTokens,
|
40
|
-
outputTextTokens
|
41
|
+
outputTextTokens,
|
41
42
|
totalInputTokens: usage.promptTokenCount,
|
42
43
|
totalOutputTokens,
|
43
44
|
totalTokens: usage.totalTokenCount,
|
@@ -50,7 +51,7 @@ const transformVertexAIStream = (
|
|
50
51
|
|
51
52
|
if (
|
52
53
|
candidate && // 首先检查是否为 reasoning 内容 (thought: true)
|
53
|
-
Array.isArray(candidate.content
|
54
|
+
Array.isArray(candidate.content?.parts) &&
|
54
55
|
candidate.content.parts.length > 0
|
55
56
|
) {
|
56
57
|
for (const part of candidate.content.parts) {
|
@@ -60,19 +61,18 @@ const transformVertexAIStream = (
|
|
60
61
|
}
|
61
62
|
}
|
62
63
|
|
63
|
-
|
64
|
-
if (!candidates)
|
64
|
+
if (!candidate) {
|
65
65
|
return {
|
66
66
|
data: '',
|
67
67
|
id: context?.id,
|
68
68
|
type: 'text',
|
69
69
|
};
|
70
|
+
}
|
70
71
|
|
71
|
-
|
72
|
-
|
73
|
-
const part = item.content.parts[0];
|
72
|
+
if (candidate.content) {
|
73
|
+
const part = candidate.content.parts?.[0];
|
74
74
|
|
75
|
-
if (part
|
75
|
+
if (part?.functionCall) {
|
76
76
|
const functionCall = part.functionCall;
|
77
77
|
|
78
78
|
return [
|
@@ -95,18 +95,18 @@ const transformVertexAIStream = (
|
|
95
95
|
];
|
96
96
|
}
|
97
97
|
|
98
|
-
if (
|
98
|
+
if (candidate.finishReason) {
|
99
99
|
if (chunk.usageMetadata) {
|
100
100
|
return [
|
101
|
-
!!part
|
101
|
+
!!part?.text ? { data: part.text, id: context?.id, type: 'text' } : undefined,
|
102
102
|
...usageChunks,
|
103
103
|
].filter(Boolean) as StreamProtocolChunk[];
|
104
104
|
}
|
105
|
-
return { data:
|
105
|
+
return { data: candidate.finishReason, id: context?.id, type: 'stop' };
|
106
106
|
}
|
107
107
|
|
108
108
|
return {
|
109
|
-
data: part
|
109
|
+
data: part?.text,
|
110
110
|
id: context?.id,
|
111
111
|
type: 'text',
|
112
112
|
};
|
@@ -120,7 +120,7 @@ const transformVertexAIStream = (
|
|
120
120
|
};
|
121
121
|
|
122
122
|
export const VertexAIStream = (
|
123
|
-
rawStream: ReadableStream<
|
123
|
+
rawStream: ReadableStream<GenerateContentResponse>,
|
124
124
|
{ callbacks, inputStartAt }: GoogleAIStreamOptions = {},
|
125
125
|
) => {
|
126
126
|
const streamStack: StreamContext = { id: 'chat_' + nanoid() };
|
@@ -1,13 +1,19 @@
|
|
1
|
-
import {
|
1
|
+
import { GoogleGenAI, GoogleGenAIOptions } from '@google/genai';
|
2
2
|
|
3
3
|
import { AgentRuntimeErrorType } from '../error';
|
4
4
|
import { LobeGoogleAI } from '../google';
|
5
5
|
import { AgentRuntimeError } from '../utils/createError';
|
6
6
|
|
7
|
+
const DEFAULT_VERTEXAI_LOCATION = 'global';
|
8
|
+
|
7
9
|
export class LobeVertexAI extends LobeGoogleAI {
|
8
|
-
static initFromVertexAI(params?:
|
10
|
+
static initFromVertexAI(params?: GoogleGenAIOptions) {
|
9
11
|
try {
|
10
|
-
const client = new
|
12
|
+
const client = new GoogleGenAI({
|
13
|
+
...params,
|
14
|
+
location: params?.location ?? DEFAULT_VERTEXAI_LOCATION, // @google/genai 不传 location 会报错
|
15
|
+
vertexai: true,
|
16
|
+
});
|
11
17
|
|
12
18
|
return new LobeGoogleAI({ apiKey: 'avoid-error', client, isVertexAi: true });
|
13
19
|
} catch (e) {
|