From 0493b01c9f803a76d7f11439e699189ea67e5eeb Mon Sep 17 00:00:00 2001 From: sxjeru Date: Thu, 6 Feb 2025 11:49:20 +0800 Subject: [PATCH 01/12] Update index.ts --- src/libs/agent-runtime/google/index.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/libs/agent-runtime/google/index.ts b/src/libs/agent-runtime/google/index.ts index 0e744f33f5346..4aed5ab1beb40 100644 --- a/src/libs/agent-runtime/google/index.ts +++ b/src/libs/agent-runtime/google/index.ts @@ -112,7 +112,16 @@ export class LobeGoogleAI implements LobeRuntimeAI { .generateContentStream({ contents, systemInstruction: payload.system as string, - tools: this.buildGoogleTools(payload.tools), + tools: (() => { + const tools = this.buildGoogleTools(payload.tools); + if (model.startsWith('gemini-2.0')) { + if (!tools) { + return [{ googleSearch: {} } as GoogleFunctionCallTool]; + } else { + return [...tools, { googleSearch: {} } as GoogleFunctionCallTool]; + } + } + })(), }); const googleStream = convertIterableToStream(geminiStreamResult.stream); From e2389dd558b2db74aa119f9863d6a1c7c2c5a1e7 Mon Sep 17 00:00:00 2001 From: sxjeru Date: Thu, 6 Feb 2025 12:17:02 +0800 Subject: [PATCH 02/12] Update index.ts --- src/libs/agent-runtime/google/index.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/libs/agent-runtime/google/index.ts b/src/libs/agent-runtime/google/index.ts index 4aed5ab1beb40..6bbce027c4aa6 100644 --- a/src/libs/agent-runtime/google/index.ts +++ b/src/libs/agent-runtime/google/index.ts @@ -114,7 +114,11 @@ export class LobeGoogleAI implements LobeRuntimeAI { systemInstruction: payload.system as string, tools: (() => { const tools = this.buildGoogleTools(payload.tools); - if (model.startsWith('gemini-2.0')) { + if ( + model.startsWith('gemini-2.0') && + !model.includes('thinking') && + !model.includes('lite') + ) { if (!tools) { return [{ googleSearch: {} } as GoogleFunctionCallTool]; } else { From df4fa93392f35c4f121b9687bf65b7475c2f93fd Mon Sep 17 00:00:00 2001 From: sxjeru Date: Thu, 6 Feb 2025 14:20:58 +0800 Subject: [PATCH 03/12] DynamicRetrievalMode --- src/libs/agent-runtime/google/index.ts | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/libs/agent-runtime/google/index.ts b/src/libs/agent-runtime/google/index.ts index 6bbce027c4aa6..471ec728f7c54 100644 --- a/src/libs/agent-runtime/google/index.ts +++ b/src/libs/agent-runtime/google/index.ts @@ -1,5 +1,6 @@ import { Content, + DynamicRetrievalMode, FunctionCallPart, FunctionDeclaration, Tool as GoogleFunctionCallTool, @@ -114,17 +115,27 @@ export class LobeGoogleAI implements LobeRuntimeAI { systemInstruction: payload.system as string, tools: (() => { const tools = this.buildGoogleTools(payload.tools); + const grounding = { + googleSearchRetrieval: { + dynamicRetrievalConfig: { + mode: DynamicRetrievalMode.MODE_DYNAMIC, + dynamicThreshold: 0.7, + }, + }, + } as GoogleFunctionCallTool; + if ( model.startsWith('gemini-2.0') && !model.includes('thinking') && !model.includes('lite') ) { if (!tools) { - return [{ googleSearch: {} } as GoogleFunctionCallTool]; + return [grounding]; } else { - return [...tools, { googleSearch: {} } as GoogleFunctionCallTool]; + return [...tools, grounding]; } } + return tools; })(), }); From 49a6278b63ab1414ab76e4be37760edfae27fe11 Mon Sep 17 00:00:00 2001 From: sxjeru Date: Thu, 6 Feb 2025 14:43:34 +0800 Subject: [PATCH 04/12] env --- src/libs/agent-runtime/google/index.ts | 27 +++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/src/libs/agent-runtime/google/index.ts b/src/libs/agent-runtime/google/index.ts index 471ec728f7c54..b61f787c2b1b3 100644 --- a/src/libs/agent-runtime/google/index.ts +++ b/src/libs/agent-runtime/google/index.ts @@ -115,6 +115,18 @@ export class LobeGoogleAI implements LobeRuntimeAI { systemInstruction: payload.system as string, tools: (() => { const tools = this.buildGoogleTools(payload.tools); + + if ( + process.env.GEMINI_SEARCH_ENABLED === '0' || + !( + model.startsWith('gemini-2.0') && + !model.includes('thinking') && + !model.includes('lite') + ) + ) { + return tools; + } + const grounding = { googleSearchRetrieval: { dynamicRetrievalConfig: { @@ -124,18 +136,11 @@ export class LobeGoogleAI implements LobeRuntimeAI { }, } as GoogleFunctionCallTool; - if ( - model.startsWith('gemini-2.0') && - !model.includes('thinking') && - !model.includes('lite') - ) { - if (!tools) { - return [grounding]; - } else { - return [...tools, grounding]; - } + if (!tools) { + return [grounding]; + } else { + return [...tools, grounding]; } - return tools; })(), }); From 9fb015e8dd566f028738b7df758f6733854a8189 Mon Sep 17 00:00:00 2001 From: sxjeru Date: Thu, 6 Feb 2025 14:48:21 +0800 Subject: [PATCH 05/12] fix --- src/libs/agent-runtime/google/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/agent-runtime/google/index.ts b/src/libs/agent-runtime/google/index.ts index b61f787c2b1b3..4799ebb9f2a7c 100644 --- a/src/libs/agent-runtime/google/index.ts +++ b/src/libs/agent-runtime/google/index.ts @@ -130,8 +130,8 @@ export class LobeGoogleAI implements LobeRuntimeAI { const grounding = { googleSearchRetrieval: { dynamicRetrievalConfig: { - mode: DynamicRetrievalMode.MODE_DYNAMIC, dynamicThreshold: 0.7, + mode: DynamicRetrievalMode.MODE_DYNAMIC, }, }, } as GoogleFunctionCallTool; From 28f90631d7100675dc15dd8667ce90caac015344 Mon Sep 17 00:00:00 2001 From: sxjeru Date: Thu, 6 Feb 2025 15:11:57 +0800 Subject: [PATCH 06/12] googleSearch --- src/libs/agent-runtime/google/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/agent-runtime/google/index.ts b/src/libs/agent-runtime/google/index.ts index 4799ebb9f2a7c..c4d2a3c14afd0 100644 --- a/src/libs/agent-runtime/google/index.ts +++ b/src/libs/agent-runtime/google/index.ts @@ -128,7 +128,7 @@ export class LobeGoogleAI implements LobeRuntimeAI { } const grounding = { - googleSearchRetrieval: { + googleSearch: { dynamicRetrievalConfig: { dynamicThreshold: 0.7, mode: DynamicRetrievalMode.MODE_DYNAMIC, From a94e123c74f4ea176184bdc03fa6551352715455 Mon Sep 17 00:00:00 2001 From: sxjeru Date: Thu, 6 Feb 2025 15:16:10 +0800 Subject: [PATCH 07/12] dynamicThreshold --- src/libs/agent-runtime/google/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/agent-runtime/google/index.ts b/src/libs/agent-runtime/google/index.ts index c4d2a3c14afd0..b67d1c4058aa9 100644 --- a/src/libs/agent-runtime/google/index.ts +++ b/src/libs/agent-runtime/google/index.ts @@ -130,7 +130,7 @@ export class LobeGoogleAI implements LobeRuntimeAI { const grounding = { googleSearch: { dynamicRetrievalConfig: { - dynamicThreshold: 0.7, + dynamicThreshold: 0.3, // https://ai.google.dev/gemini-api/docs/grounding?lang=node#dynamic-retrieval mode: DynamicRetrievalMode.MODE_DYNAMIC, }, }, From 45ce1ff7a588ca58f58a6aae92a89f4d6e160502 Mon Sep 17 00:00:00 2001 From: sxjeru Date: Thu, 6 Feb 2025 15:42:56 +0800 Subject: [PATCH 08/12] googleSearch --- src/libs/agent-runtime/google/index.ts | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/libs/agent-runtime/google/index.ts b/src/libs/agent-runtime/google/index.ts index b67d1c4058aa9..14cd810a2c44c 100644 --- a/src/libs/agent-runtime/google/index.ts +++ b/src/libs/agent-runtime/google/index.ts @@ -128,12 +128,7 @@ export class LobeGoogleAI implements LobeRuntimeAI { } const grounding = { - googleSearch: { - dynamicRetrievalConfig: { - dynamicThreshold: 0.3, // https://ai.google.dev/gemini-api/docs/grounding?lang=node#dynamic-retrieval - mode: DynamicRetrievalMode.MODE_DYNAMIC, - }, - }, + googleSearch: {}, } as GoogleFunctionCallTool; if (!tools) { From 4e9c435652c37486562a446dab91a7be243ae2d9 Mon Sep 17 00:00:00 2001 From: sxjeru Date: Thu, 6 Feb 2025 15:45:57 +0800 Subject: [PATCH 09/12] Update index.ts --- src/libs/agent-runtime/google/index.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/libs/agent-runtime/google/index.ts b/src/libs/agent-runtime/google/index.ts index 14cd810a2c44c..f7a83d0b0dd37 100644 --- a/src/libs/agent-runtime/google/index.ts +++ b/src/libs/agent-runtime/google/index.ts @@ -1,6 +1,5 @@ import { Content, - DynamicRetrievalMode, FunctionCallPart, FunctionDeclaration, Tool as GoogleFunctionCallTool, From a18c56637c0ffa0a2e5e602ab8c37ec3574f91cf Mon Sep 17 00:00:00 2001 From: sxjeru Date: Thu, 6 Feb 2025 16:11:37 +0800 Subject: [PATCH 10/12] Update index.ts --- src/libs/agent-runtime/google/index.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/libs/agent-runtime/google/index.ts b/src/libs/agent-runtime/google/index.ts index f7a83d0b0dd37..8d346e847aaf4 100644 --- a/src/libs/agent-runtime/google/index.ts +++ b/src/libs/agent-runtime/google/index.ts @@ -1,5 +1,6 @@ import { Content, + DynamicRetrievalMode, FunctionCallPart, FunctionDeclaration, Tool as GoogleFunctionCallTool, @@ -115,6 +116,10 @@ export class LobeGoogleAI implements LobeRuntimeAI { tools: (() => { const tools = this.buildGoogleTools(payload.tools); + if (tools) { + return tools; // 目前 Tools (例如 googleSearch) 无法与 FunctionCall 同时使用 + } + if ( process.env.GEMINI_SEARCH_ENABLED === '0' || !( From ef9839757c97f09fc413ebff3f56f3475490be53 Mon Sep 17 00:00:00 2001 From: sxjeru Date: Thu, 6 Feb 2025 16:11:53 +0800 Subject: [PATCH 11/12] Update index.ts --- src/libs/agent-runtime/google/index.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/libs/agent-runtime/google/index.ts b/src/libs/agent-runtime/google/index.ts index 8d346e847aaf4..278c1dcb070ea 100644 --- a/src/libs/agent-runtime/google/index.ts +++ b/src/libs/agent-runtime/google/index.ts @@ -1,6 +1,5 @@ import { Content, - DynamicRetrievalMode, FunctionCallPart, FunctionDeclaration, Tool as GoogleFunctionCallTool, From 9d9c45ef1bc3096b5e2cb91fdb80c97d4d6a659c Mon Sep 17 00:00:00 2001 From: sxjeru Date: Thu, 6 Feb 2025 16:25:31 +0800 Subject: [PATCH 12/12] =?UTF-8?q?=E9=BB=98=E8=AE=A4=E7=A6=81=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/libs/agent-runtime/google/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/agent-runtime/google/index.ts b/src/libs/agent-runtime/google/index.ts index 278c1dcb070ea..4250aff451751 100644 --- a/src/libs/agent-runtime/google/index.ts +++ b/src/libs/agent-runtime/google/index.ts @@ -120,7 +120,7 @@ export class LobeGoogleAI implements LobeRuntimeAI { } if ( - process.env.GEMINI_SEARCH_ENABLED === '0' || + process.env.GEMINI_SEARCH_ENABLED !== '1' || !( model.startsWith('gemini-2.0') && !model.includes('thinking') &&