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

@google/genai SDK:chat.sendMessage配置是否会覆盖ai.chats.create配置?Vue.js场景下的对话配置疑问

Google GenAI SDK: sendMessage Config Overrides Initial chat.create Settings – Expected Behavior & Fixes

Great question! Let's break this down clearly:

Is the full config override expected behavior?

Yes, this is actually the intended design of the @google/genai SDK. When you create a chat instance with ai.chats.create(), the config you pass sets up base defaults for that chat session—but when you provide a new config object in sendMessage(), it completely replaces the initial config rather than merging with it.

The SDK takes this approach because certain config properties (like abortSignal) are tied exclusively to a single request, not the entire chat session. Implicit merging could lead to unexpected behavior (e.g., leftover abort signals from prior requests) or require overly complex logic to resolve conflicting settings. The team opted for explicit replacement to keep the API predictable.

Is manually merging the base config the correct approach?

Absolutely—this is the most reliable way to retain your persistent systemInstruction while adding request-specific settings like abortSignal. Here's how to adjust your code to make this work cleanly:

First, extract your base config into a reusable variable to avoid repetition:

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

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

// Define persistent base chat config once
const baseChatConfig = {
  model: "gemini-3-flash-preview",
  config: {
    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(baseChatConfig);

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

async function sendRequest() {
  abortController = new AbortController();
  try {
    console.log("Sending message");
    // Merge base config with request-specific abortSignal
    const response = await chat.sendMessage({
      message: inputText.value,
      config: {
        ...baseChatConfig.config,
        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>

Key Notes:

  • Using the spread operator (...baseChatConfig.config) copies all persistent settings (like systemInstruction) into the request-specific config, then adds/overrides with abortSignal.
  • This pattern works for any other request-specific config properties you might need later (e.g., adjusting temperature for a single query).
  • If you reuse this logic across components, you can wrap the config merge in a small helper function to keep your code DRY.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.28 06:40:31