向 Gemini API 发送请求时指定 systemInstruction

问题描述 投票:0回答:1

使用 vertex ai 向 Gemini API 发送请求时如何指定 systemInstruction。 vertex ai Playground 展示了一种使用 python 但不使用 Node js 的方法。

指定的python方法:

def multiturn_generate_content():
  vertexai.init(project="myProjectId", location="us-central1")
  model = GenerativeModel(
    "gemini-1.0-pro-002",
    system_instruction=["""you are an ai assistant"""]
  )
  chat = model.start_chat()
  
  #rest of the code

multiturn_generate_content()

我对 Node js 的尝试:

注意它的错误:

ClientError: [VertexAI.ClientError]: No content is provided for sending chat message.


const { VertexAI } = require('@google-cloud/vertexai')

let generativeModel;

const projectId = 'myProjectId';
const location = 'us-central1';
const model = 'gemini-1.0-pro-002';

function initializeGenerativeModel() {
    try {
        const vertex_ai = new VertexAI({ project: projectId, location: location, model: model });

        generativeModel = vertex_ai.preview.getGenerativeModel({
            model: model,
            generationConfig: {
                'maxOutputTokens': 8192,
                'temperature': 1,
                'topP': 0.95,
            },
            safetySettings: [
                {
                    'category': 'HARM_CATEGORY_HATE_SPEECH',
                    'threshold': 'BLOCK_MEDIUM_AND_ABOVE'
                },
                {
                    'category': 'HARM_CATEGORY_DANGEROUS_CONTENT',
                    'threshold': 'BLOCK_MEDIUM_AND_ABOVE'
                },
                {
                    'category': 'HARM_CATEGORY_SEXUALLY_EXPLICIT',
                    'threshold': 'BLOCK_MEDIUM_AND_ABOVE'
                },
                {
                    'category': 'HARM_CATEGORY_HARASSMENT',
                    'threshold': 'BLOCK_MEDIUM_AND_ABOVE'
                }
            ],
        });
    } catch (error) {
        console.error('Error initializing VertexAI:', error);
        throw error;
    }
}

initializeGenerativeModel();

async function gemini_1_0_pro(query) {
  try {
      const chat = generativeModel.startChat();
      const systemInstruction = "You are an ai assistant";
      const result = await chat.sendMessageStream({
          role: 'system',
          content: systemInstruction
      });

      await chat.sendMessageStream({
          role: 'user',
          content: query
      });

      for await (const item of result.stream) {
          const response = item.candidates[0].content.parts[0].text;
          console.log(response)
      }
      const aggregatedResponse = await result.response;
      console.log(aggregatedResponse)
  } catch (error) {
      console.error('Error in gemini_1_0_pro:', error);
  }
}

module.exports = { gemini_1_0_pro };

node.js google-cloud-vertex-ai google-gemini
1个回答
0
投票

好的,抱歉我的误解。

看来您可以:

const generativeModel = vertexAI.getGenerativeModel({
  ...
  systemInstruction: {
    role: "system",
    parts: [{text: "You are an AI assistant"}]
  }
});

或者

const chat = generativeModel.startChat({
  systemInstruction: {
    role: "system",
    parts: [{text: "You are an AI assistant"}]
  }
});
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.