You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

@google/genai SDK:chat.sendMessage配置是否覆盖chats.create?及实现问询

问题解答

1. 配置被完全覆盖是SDK的预期设计吗?

是的,这是@google/genai SDK的预期行为。该SDK的设计逻辑是:sendMessage方法传入的config参数会直接覆盖聊天会话实例(ai.chats.create创建的实例)的基础配置,而非自动合并两者的配置项。这种设计是为了让每个消息请求可以完全独立配置,避免隐式合并带来的意外行为。

2. 手动扩展基础配置是唯一正确的实现方式吗?

不是唯一方式,但是最直接、可靠的实现方式。以下是几种可行方案:

方案一:手动合并基础配置(最常用)

抽离出基础配置变量,在创建聊天实例和每次发送消息时复用并合并:

<script setup>
import { ref } from 'vue';
import { GoogleGenAI } from "@google/genai";

const ai = new GoogleGenAI({ apiKey: "BLABLABLABLABLABLA" });

// 抽离基础配置
const baseChatConfig = {
  systemInstruction: "You are an expert about tennis. Don't reply to other kinds of requests, but apologize and state that you're a tennis assistant only",
};

const chat = ai.chats.create({
  model: "gemini-3-flash-preview",
  config: baseChatConfig
});

const inputText = ref("");
const result = ref("");
let abortController;

async function sendRequest() {
  abortController = new AbortController();

  try {
    console.log("Sending message");
    const response = await chat.sendMessage({
      message: inputText.value,
      // 手动合并基础配置和当前请求的临时配置
      config: { ...baseChatConfig, abortSignal: abortController.signal }
    });
    console.log(response.text);
    result.value = response.text;
  }
  catch (e) {
    if (e && e.name === "AbortError") {
      console.log("Request was aborted");
    }
    else {
      console.log(e);
    }
  }
  finally{
    abortController = null;
  }
}

function abortRequest(){
  if(abortController){
    abortController.abort();
  }
  abortController = null;
}
</script>

<template>
  <input type="text" v-model="inputText">
  <button @click="sendRequest">Send</button>
  <button @click="abortRequest">Abort</button>
  <p>{{ result }}</p>
</template>

方案二:封装自定义发送方法

封装函数自动处理配置合并逻辑,减少重复代码:

// 封装自定义发送函数
async function sendChatMessage(message, extraConfig = {}) {
  const controller = new AbortController();
  try {
    const response = await chat.sendMessage({
      message,
      config: { ...baseChatConfig, abortSignal: controller.signal, ...extraConfig }
    });
    return { response, controller };
  } catch (e) {
    throw e;
  }
}

// 使用时
async function sendRequest() {
  try {
    const { response, controller } = await sendChatMessage(inputText.value);
    abortController = controller;
    result.value = response.text;
  } catch (e) {
    // 错误处理逻辑
  }
}

方案三:扩展聊天实例(进阶)

通过重写聊天实例的sendMessage方法,实现自动合并配置,注意需兼容SDK版本:

// 保存原始的sendMessage方法
const originalSendMessage = chat.sendMessage;
// 重写sendMessage方法
chat.sendMessage = async function(options) {
  // 合并配置
  const mergedConfig = { ...baseChatConfig, ...(options.config || {}) };
  return originalSendMessage.call(this, {
    ...options,
    config: mergedConfig
  });
};

无论哪种方案,核心都是确保基础配置(如systemInstruction)被包含在每次sendMessage的配置中,同时传入单次请求的abortSignal。

内容的提问来源于stack exchange,提问作者Curio

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.01 13:22:39