使用系统说明

本文档介绍如何使用系统指令。如需了解什么是系统指令以及有关使用系统指令的最佳实践,请改为参阅系统指令简介

系统指令是指模型在处理提示之前所处理的一组指令。我们建议您使用系统指令来告知模型您希望其如何运作以及如何回答提示。例如,您可以添加人设或角色、背景信息和格式设置指令等内容:

You are a friendly and helpful assistant.
Ensure your answers are complete, unless the user requests a more concise approach.
When generating code, offer explanations for code segments as necessary and maintain good coding practices.
When presented with inquiries seeking information, provide answers that reflect a deep understanding of the field, guaranteeing their correctness.
For any non-english queries, respond in the same language as the prompt unless otherwise specified by the user.
For prompts involving reasoning, provide a clear explanation of each step in the reasoning process before presenting the final answer.

如果设置了系统说明,则该说明会应用于整个请求。当提示中包含系统说明时,该说明适用于多个用户和模型轮流。虽然系统指令与提示内容是分开的,但它们仍然是整体提示的一部分,因此受标准数据使用政策的约束。

支持的模型

以下模型支持系统指令:

  • 所有 Gemini 1.5 Pro 模型版本
  • 所有 Gemini 1.5 Flash 模型版本
  • Gemini 1.0 Pro 版本 gemini-1.0-pro-002

如果您使用的是其他模型,请改为参阅分配角色

使用场景

您可以通过多种方式使用系统说明,包括:

  • 定义人设或角色(例如,针对聊天机器人)
  • 定义输出格式(Markdown、YAML 等)
  • 定义输出风格和语气(例如详细程度、正式程度和目标阅读水平)
  • 定义任务的目标或规则(例如,返回代码段而不带进一步说明)
  • 为提示提供其他上下文(例如知识临界值)
  • 指定模型应采用哪种语言进行回答(有时,即使提示采用其他语言编写,模型也可以采用您的当地语言进行回答)。如果您使用非英语语言作为提示语言,我们建议您在系统指令中添加以下内容:

    All questions should be answered comprehensively with details, unless the user requests a concise response specifically. Respond in the same language as the query.
    

代码示例

以下标签页中的代码示例演示了如何在生成式 AI 应用中使用系统指令。

Python

如需了解如何安装或更新 Python 版 Vertex AI SDK,请参阅安装 Python 版 Vertex AI SDK。如需了解详情,请参阅 Vertex AI SDK for Python API 参考文档

流式回答和非流式回答

您可以选择模型是生成流式回答还是非流式回答。 对于流式回答,您将在生成每个响应的输出词元后立即收到响应。对于非流式回答,您将在生成所有输出词元后收到所有响应。

对于流式回答,请使用 generate_content 中的 stream 参数。

  response = model.generate_content(contents=[...], stream = True)
  

对于非流式回答,请移除该参数或将参数设置为 False

示例代码

import vertexai

from vertexai.generative_models import GenerativeModel

# TODO(developer): Update and un-comment below line
# PROJECT_ID = "your-project-id"
vertexai.init(project=PROJECT_ID, location="us-central1")

model = GenerativeModel(
    model_name="gemini-1.5-flash-002",
    system_instruction=[
        "You are a helpful language translator.",
        "Your mission is to translate text in English to French.",
    ],
)

prompt = """
User input: I like bagels.
Answer:
"""
response = model.generate_content([prompt])
print(response.text)
# Example response:
# J'aime les bagels.

Node.js

在尝试此示例之前,请按照《生成式 AI 快速入门:使用 Node.js SDK》中的 Node.js 设置说明执行操作。如需了解详情,请参阅适用于 Gemini 的 Node.js SDK 参考文档

如需向 Vertex AI 进行身份验证,请设置应用默认凭据。如需了解详情,请参阅为本地开发环境设置身份验证

流式回答和非流式回答

您可以选择模型是生成流式回答还是非流式回答。 对于流式回答,您将在生成每个响应的输出词元后立即收到响应。对于非流式回答,您会在生成所有输出词元之后收到所有回答。

对于流式回答,请使用 generateContentStream 方法。

  const streamingResp = await generativeModel.generateContentStream(request);
  

对于非流式回答,请使用 generateContent 方法。

  const streamingResp = await generativeModel.generateContent(request);
  

示例代码

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

/**
 * TODO(developer): Update these variables before running the sample.
 */
async function set_system_instruction(projectId = 'PROJECT_ID') {
  const vertexAI = new VertexAI({project: projectId, location: 'us-central1'});

  const generativeModel = vertexAI.getGenerativeModel({
    model: 'gemini-1.5-flash-001',
    systemInstruction: {
      parts: [
        {text: 'You are a helpful language translator.'},
        {text: 'Your mission is to translate text in English to French.'},
      ],
    },
  });

  const textPart = {
    text: `
    User input: I like bagels.
    Answer:`,
  };

  const request = {
    contents: [{role: 'user', parts: [textPart]}],
  };

  const resp = await generativeModel.generateContent(request);
  const contentResponse = await resp.response;
  console.log(JSON.stringify(contentResponse));
}

Java

在尝试此示例之前,请按照《Vertex AI 快速入门》中的 Java 设置说明执行操作。如需了解详情,请参阅适用于 Gemini 的 Vertex AI Java SDK 参考文档

如需向 Vertex AI 进行身份验证,请设置应用默认凭据。如需了解详情,请参阅为本地开发环境设置身份验证

流式回答和非流式回答

您可以选择模型是生成流式回答还是非流式回答。 对于流式回答,您将在生成每个响应的输出词元后立即收到响应。对于非流式回答,您会在生成所有输出词元之后收到所有回答。

对于流式回答,请使用 generateContentStream 方法。

  public ResponseStream<GenerateContentResponse> generateContentStream(Content content)
  

对于非流式回答,请使用 generateContent 方法。

  public GenerateContentResponse generateContent(Content content)
  

示例代码

import com.google.cloud.vertexai.VertexAI;
import com.google.cloud.vertexai.api.GenerateContentResponse;
import com.google.cloud.vertexai.generativeai.ContentMaker;
import com.google.cloud.vertexai.generativeai.GenerativeModel;
import com.google.cloud.vertexai.generativeai.ResponseHandler;

public class WithSystemInstruction {

  public static void main(String[] args) throws Exception {
    // TODO(developer): Replace these variables before running the sample.
    String projectId = "your-google-cloud-project-id";
    String location = "us-central1";
    String modelName = "gemini-1.5-flash-001";

    String output = translateToFrench(projectId, location, modelName);
    System.out.println(output);
  }

  // Ask the model to translate from English to French with a system instruction.
  public static String translateToFrench(String projectId, String location, String modelName)
      throws Exception {
    // Initialize client that will be used to send requests.
    // This client only needs to be created once, and can be reused for multiple requests.
    try (VertexAI vertexAI = new VertexAI(projectId, location)) {
      String output;

      GenerativeModel model = new GenerativeModel(modelName, vertexAI)
          .withSystemInstruction(ContentMaker.fromString("You are a helpful assistant.\n"
            + "Your mission is to translate text in English to French."));

      GenerateContentResponse response = model.generateContent("User input: I like bagels.\n"
          + "Answer:");
      output = ResponseHandler.getText(response);
      return output;
    }
  }
}

Go

在尝试此示例之前,请按照《Vertex AI 快速入门》中的 Go 设置说明执行操作。如需了解详情,请参阅适用于 Gemini 的 Vertex AI Go SDK 参考文档

如需向 Vertex AI 进行身份验证,请设置应用默认凭据。如需了解详情,请参阅为本地开发环境设置身份验证

流式回答和非流式回答

您可以选择模型是生成流式回答还是非流式回答。 对于流式回答,您将在生成每个响应的输出词元后立即收到响应。对于非流式回答,您会在生成所有输出词元之后收到所有回答。

对于流式回答,请使用 GenerateContentStream 方法。

  iter := model.GenerateContentStream(ctx, genai.Text("Tell me a story about a lumberjack and his giant ox. Keep it very short."))
  

对于非流式回答,请使用 GenerateContent 方法。

  resp, err := model.GenerateContent(ctx, genai.Text("What is the average size of a swallow?"))
  

示例代码

import (
	"context"
	"errors"
	"fmt"
	"io"

	"cloud.google.com/go/vertexai/genai"
)

// systemInstruction shows how to provide a system instruction to the generative model.
func systemInstruction(w io.Writer, projectID, location, modelName string) error {
	// location := "us-central1"
	// modelName := "gemini-1.5-flash-001"

	ctx := context.Background()

	client, err := genai.NewClient(ctx, projectID, location)
	if err != nil {
		return fmt.Errorf("unable to create client: %w", err)
	}
	defer client.Close()

	// The System Instruction is set at model creation
	model := client.GenerativeModel(modelName)
	model.SystemInstruction = &genai.Content{
		Parts: []genai.Part{genai.Text(`
			You are a helpful language translator.
			Your mission is to translate text in English to French.
		`)},
	}

	res, err := model.GenerateContent(ctx, genai.Text(`
		User input: I like bagels.
		Answer:
	`))
	if err != nil {
		return fmt.Errorf("unable to generate contents: %w", err)
	}
	if len(res.Candidates) == 0 ||
		len(res.Candidates[0].Content.Parts) == 0 {
		return errors.New("empty response from model")
	}
	fmt.Fprintf(w, "generated response: %s\n", res.Candidates[0].Content.Parts[0])

	return nil
}

C#

在尝试此示例之前,请按照《Vertex AI 快速入门》中的 C# 设置说明执行操作。如需了解详情,请参阅 Vertex AI C# 参考文档

如需向 Vertex AI 进行身份验证,请设置应用默认凭据。如需了解详情,请参阅为本地开发环境设置身份验证

流式回答和非流式回答

您可以选择模型是生成流式回答还是非流式回答。 对于流式回答,您将在生成每个响应的输出词元后立即收到响应。对于非流式回答,您会在生成所有输出词元之后收到所有回答。

对于流式回答,请使用 StreamGenerateContent 方法。

  public virtual PredictionServiceClient.StreamGenerateContentStream StreamGenerateContent(GenerateContentRequest request)
  

对于非流式回答,请使用 GenerateContentAsync 方法。

  public virtual Task<GenerateContentResponse> GenerateContentAsync(GenerateContentRequest request)
  

如需详细了解服务器如何流式传输回答,请参阅流式传输 RPC

示例代码


using Google.Cloud.AIPlatform.V1;
using System;
using System.Threading.Tasks;

public class SystemInstruction
{
    public async Task<string> SetSystemInstruction(
        string projectId = "your-project-id",
        string location = "us-central1",
        string publisher = "google",
        string model = "gemini-1.5-flash-001")
    {

        var predictionServiceClient = new PredictionServiceClientBuilder
        {
            Endpoint = $"{location}-aiplatform.googleapis.com"
        }.Build();

        string prompt = @"User input: I like bagels.
Answer:";

        var generateContentRequest = new GenerateContentRequest
        {
            Model = $"projects/{projectId}/locations/{location}/publishers/{publisher}/models/{model}",
            Contents =
            {
                new Content
                {
                    Role = "USER",
                    Parts =
                    {
                        new Part { Text = prompt },
                    }
                }
            },
            SystemInstruction = new()
            {
                Parts =
                {
                    new Part { Text = "You are a helpful assistant." },
                    new Part { Text = "Your mission is to translate text in English to French." },
                }
            }
        };

        GenerateContentResponse response = await predictionServiceClient.GenerateContentAsync(generateContentRequest);

        string responseText = response.Candidates[0].Content.Parts[0].Text;
        Console.WriteLine(responseText);

        return responseText;
    }
}

REST

设置您的环境后,您可以使用 REST 测试文本提示。以下示例会向发布方模型端点发送请求。

在使用任何请求数据之前,请先进行以下替换:

  • GENERATE_RESPONSE_METHOD:您希望模型生成的回答类型。选择一种方法来生成您希望返回模型回答的方式:
    • streamGenerateContent:在生成回答时进行流式传输,以降低真人受众群体对于延迟的感知度。
    • generateContent:回答在完全生成后返回。
  • LOCATION:处理请求的区域。可用的选项包括:

    点击即可展开可用区域的部分列表

    • us-central1
    • us-west4
    • northamerica-northeast1
    • us-east4
    • us-west1
    • asia-northeast3
    • asia-southeast1
    • asia-northeast1
  • PROJECT_ID:您的项目 ID
  • MODEL_ID:您要使用的多模态模型 ID。一些选项包括:
    • gemini-1.0-pro-002
    • gemini-1.0-pro-vision-001
    • gemini-1.5-pro-002
    • gemini-1.5-flash
  • ROLE:与内容关联的对话中的角色。即使在单轮应用场景中,也需要指定角色。 可接受的值包括:
    • USER:指定由您发送的内容。
    • MODEL:指定模型的响应。
  • TEXT
    要包含在提示中的文本指令。例如 User input: I like bagels
  • SAFETY_CATEGORY:要为其配置阈值的安全类别。可接受的值包括:

    点击即可展开安全类别

    • HARM_CATEGORY_SEXUALLY_EXPLICIT
    • HARM_CATEGORY_HATE_SPEECH
    • HARM_CATEGORY_HARASSMENT
    • HARM_CATEGORY_DANGEROUS_CONTENT
  • THRESHOLD:基于概率阻止属于指定安全类别的回答的阈值。可接受的值包括:

    点击即可展开屏蔽阈值

    • BLOCK_NONE
    • BLOCK_ONLY_HIGH
    • BLOCK_MEDIUM_AND_ABOVE(默认)
    • BLOCK_LOW_AND_ABOVE
    BLOCK_LOW_AND_ABOVE 屏蔽得最多,而 BLOCK_ONLY_HIGH 屏蔽得最少。
  • SYSTEM_INSTRUCTION
    (可选)并非适用于所有模型。有关引导模型获得更好性能的说明。 JSON 不支持换行符。请将此字段中的所有换行符替换为 \n。例如 You are a helpful language translator.\nYour mission is to translate text in English to French.
  • TEMPERATURE:温度 (temperature) 在生成回答期间用于采样,在应用 topPtopK 时会生成回答。温度可以控制词元选择的随机性。 较低的温度有利于需要更少开放性或创造性回复的提示,而较高的温度可以带来更具多样性或创造性的结果。温度为 0 表示始终选择概率最高的词元。在这种情况下,给定提示的回复大多是确定的,但可能仍然有少量变化。

    如果模型返回的回答过于笼统、过于简短,或者模型给出后备回答,请尝试提高温度。

  • TOP_P:Top-P 可更改模型选择输出词元的方式。系统会按照概率从最高(见 top-K)到最低的顺序选择词元,直到所选词元的概率总和等于 top-P 的值。例如,如果词元 A、B 和 C 的概率分别为 0.3、0.2 和 0.1,并且 top-P 值为 0.5,则模型将选择 A 或 B 作为下一个词元(通过温度确定),并会排除 C,将其作为候选词元。

    指定较低的值可获得随机程度较低的回答,指定较高的值可获得随机程度较高的回答。

  • TOP_K:Top-K 可更改模型选择输出词元的方式。如果 top-K 设为 1,表示所选词元是模型词汇表的所有词元中概率最高的词元(也称为贪心解码)。如果 top-K 设为 3,则表示系统将从 3 个概率最高的词元(通过温度确定)中选择下一个词元。

    在每个词元选择步骤中,系统都会对概率最高的 top-K 词元进行采样。然后,系统会根据 top-P 进一步过滤词元,并使用温度采样选择最终的词元。

    指定较低的值可获得随机程度较低的回答,指定较高的值可获得随机程度较高的回答。

  • MAX_OUTPUT_TOKENS:回答中可生成的词元数量上限。词元约为 4 个字符。100 个词元对应大约 60-80 个单词。

    指定较低的值可获得较短的回答,指定较高的值可获得可能较长的回答。

  • STOP_SEQUENCES:指定一个字符串列表,告知模型在响应中遇到其中一个字符串时,停止生成文本。如果某个字符串在响应中多次出现,则响应会在首次出现的位置截断。字符串区分大小写。

    例如,未指定 stopSequences 时,如果下面的内容是返回的响应:

    public static string reverse(string myString)

    则返回的响应为以下内容,其中 stopSequences 设置为 ["Str", "reverse"]

    public static string

    指定空数组 ([]) 来停用停止序列。

如需发送请求,请选择以下方式之一:

curl

将请求正文保存在名为 request.json 的文件中。在终端中运行以下命令,在当前目录中创建或覆盖此文件:

cat > request.json << 'EOF'
{
  "contents": {
    "role": "ROLE",
    "parts": { "text": "TEXT" }
  },
  "system_instruction":
  {
    "parts": [
      {
        "text": "SYSTEM_INSTRUCTION"
      }
    ]
  },
  "safety_settings": {
    "category": "SAFETY_CATEGORY",
    "threshold": "THRESHOLD"
  },
  "generation_config": {
    "temperature": TEMPERATURE,
    "topP": TOP_P,
    "topK": TOP_K,
    "candidateCount": 1,
    "maxOutputTokens": MAX_OUTPUT_TOKENS,
    "stopSequences": STOP_SEQUENCES
  }
}
EOF

然后,执行以下命令以发送 REST 请求:

curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/MODEL_ID:GENERATE_RESPONSE_METHOD"

PowerShell

将请求正文保存在名为 request.json 的文件中。在终端中运行以下命令,在当前目录中创建或覆盖此文件:

@'
{
  "contents": {
    "role": "ROLE",
    "parts": { "text": "TEXT" }
  },
  "system_instruction":
  {
    "parts": [
      {
        "text": "SYSTEM_INSTRUCTION"
      }
    ]
  },
  "safety_settings": {
    "category": "SAFETY_CATEGORY",
    "threshold": "THRESHOLD"
  },
  "generation_config": {
    "temperature": TEMPERATURE,
    "topP": TOP_P,
    "topK": TOP_K,
    "candidateCount": 1,
    "maxOutputTokens": MAX_OUTPUT_TOKENS,
    "stopSequences": STOP_SEQUENCES
  }
}
'@  | Out-File -FilePath request.json -Encoding utf8

然后,执行以下命令以发送 REST 请求:

$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }

Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/MODEL_ID:GENERATE_RESPONSE_METHOD" | Select-Object -Expand Content

您应该收到类似以下内容的 JSON 响应。

请注意此示例网址中的以下内容:
  • 使用 generateContent 方法请求在回答完全生成后返回回答。 为了降低真人观众对于延迟的感知度,请使用 streamGenerateContent 方法在生成回答时流式传输回答。
  • 多模态模型 ID 位于网址末尾且位于方法之前(例如 gemini-1.5-flashgemini-1.0-pro-vision)。此示例可能还支持其他模型。

提示示例

以下是使用适用于 Gemini API 的 Python SDK 设置系统指令的基本示例:

model=genai.GenerativeModel(
    model_name="gemini-1.5-pro-002",
    system_instruction="You are a cat. Your name is Neko.")

以下是定义模型预期行为的系统提示示例。

代码生成

代码生成
    You are a coding expert that specializes in rendering code for front-end interfaces. When I describe a component of a website I want to build, please return the HTML and CSS needed to do so. Do not give an explanation for this code. Also offer some UI design suggestions.
    
    Create a box in the middle of the page that contains a rotating selection of images each with a caption. The image in the center of the page should have shadowing behind it to make it stand out. It should also link to another page of the site. Leave the URL blank so that I can fill it in.
    

生成已设置格式的数据

生成已设置格式的数据
    You are an assistant for home cooks. You receive a list of ingredients and respond with a list of recipes that use those ingredients. Recipes which need no extra ingredients should always be listed before those that do.

    Your response must be a JSON object containing 3 recipes. A recipe object has the following schema:

    * name: The name of the recipe
    * usedIngredients: Ingredients in the recipe that were provided in the list
    * otherIngredients: Ingredients in the recipe that were not provided in the
      list (omitted if there are no other ingredients)
    * description: A brief description of the recipe, written positively as if
      to sell it
    
    * 1 lb bag frozen broccoli
    * 1 pint heavy cream
    * 1 lb pack cheese ends and pieces
    

音乐聊天机器人

音乐聊天机器人
    You will respond as a music historian, demonstrating comprehensive knowledge across diverse musical genres and providing relevant examples. Your tone will be upbeat and enthusiastic, spreading the joy of music. If a question is not related to music, the response should be, "That is beyond my knowledge."
    
    If a person was born in the sixties, what was the most popular music genre being played when they were born? List five songs by bullet point.
    

金融分析

金融分析
    As a financial analysis expert, your role is to interpret complex financial data, offer personalized advice, and evaluate investments using statistical methods to gain insights across different financial areas.

    Accuracy is the top priority. All information, especially numbers and calculations, must be correct and reliable. Always double-check for errors before giving a response. The way you respond should change based on what the user needs. For tasks with calculations or data analysis, focus on being precise and following instructions rather than giving long explanations. If you're unsure, ask the user for more information to ensure your response meets their needs.

    For tasks that are not about numbers:

    * Use clear and simple language to avoid confusion and don't use jargon.
    * Make sure you address all parts of the user's request and provide complete information.
    * Think about the user's background knowledge and provide additional context or explanation when needed.

    Formatting and Language:

    * Follow any specific instructions the user gives about formatting or language.
    * Use proper formatting like JSON or tables to make complex data or results easier to understand.
    
    Please summarize the key insights of given numerical tables.

    CONSOLIDATED STATEMENTS OF INCOME (In millions, except per share amounts)

    |Year Ended December 31                | 2020        | 2021        | 2022        |

    |---                                                        | ---                | ---                | ---                |

    |Revenues                                        | $ 182,527| $ 257,637| $ 282,836|

    |Costs and expenses:|

    |Cost of revenues                                | 84,732        | 110,939        | 126,203|

    |Research and development        | 27,573        | 31,562        | 39,500|

    |Sales and marketing                        | 17,946        | 22,912        | 26,567|

    |General and administrative        | 11,052        | 13,510        | 15,724|

    |Total costs and expenses                | 141,303| 178,923| 207,994|

    |Income from operations                | 41,224        | 78,714        | 74,842|

    |Other income (expense), net        | 6,858        | 12,020        | (3,514)|

    |Income before income taxes        | 48,082        | 90,734        | 71,328|

    |Provision for income taxes        | 7,813        | 14,701        | 11,356|

    |Net income                                        | $40,269| $76,033        | $59,972|

    |Basic net income per share of Class A, Class B, and Class C stock        | $2.96| $5.69| $4.59|

    |Diluted net income per share of Class A, Class B, and Class C stock| $2.93| $5.61| $4.56|

    Please list important, but no more than five, highlights from 2020 to 2022 in the given table.

    Please write in a professional and business-neutral tone.

    The summary should only be based on the information presented in the table.
    

市场情感分析

市场情感分析
    You are a stock market analyst who analyzes market sentiment given a news snippet. Based on the news snippet, you extract statements that impact investor sentiment.

    Respond in JSON format and for each statement:

    * Give a score 1 - 10 to suggest if the sentiment is negative or positive (1 is most negative 10 is most positive, 5 will be neutral).
    * Reiterate the statement.
    * Give a one sentence explanation.
    
    Mobileye reported a build-up of excess inventory by top-tier customers following supply-chain constraints in
    recent years. Revenue for the first quarter is expected to be down about 50% from $458 million generated a
    year earlier, before normalizing over the remainder of 2024, Mobileye said. Mobileye forecast revenue for
    full-year 2024 at between $1.83 billion and $1.96 billion, down from the about $2.08 billion it now expects for 2023.
    

后续步骤