Gunakan petunjuk sistem

Petunjuk sistem itu seperti pembukaan yang Anda tambahkan sebelum LLM terekspos ke instruksi lebih lanjut dari pengguna. Hal ini memungkinkan pengguna mengarahkan perilaku model berdasarkan kebutuhan dan kasus penggunaan spesifik mereka. Saat menetapkan petunjuk sistem, Anda memberi model konteks tambahan untuk memahami tugas, memberikan respons yang lebih disesuaikan, dan mematuhi panduan spesifik terkait interaksi pengguna penuh dengan model. Untuk developer, perilaku tingkat produk dapat ditentukan dalam petunjuk sistem, terpisah dari perintah yang diberikan oleh pengguna akhir. Misalnya, Anda dapat menyertakan hal-hal seperti peran atau persona, informasi kontekstual, dan petunjuk pemformatan:

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.

Model Gemini berikut mendukung petunjuk sistem:

  • gemini-1.5-flash-001
  • gemini-1.5-pro-001
  • gemini-1.0-pro-002

Jika Anda menggunakan model lain, lihat Menetapkan peran.

Anda dapat menggunakan petunjuk sistem dengan berbagai cara, termasuk:

  • Menentukan persona atau peran (misalnya untuk chatbot)
  • Menentukan format output (Markdown, YAML, dll.)
  • Menentukan gaya dan nuansa output (misalnya panjang, formalitas, dan tingkat bacaan target)
  • Menentukan sasaran atau aturan untuk tugas (misalnya, menampilkan cuplikan kode tanpa penjelasan lebih lanjut)
  • Menyediakan konteks tambahan untuk perintah (misalnya, batas pengetahuan)

Jika ditetapkan, petunjuk sistem akan berlaku untuk seluruh permintaan. Metode ini berfungsi di beberapa pengguna dan model bergantian saat disertakan dalam perintah. Meskipun petunjuk sistem terpisah dari konten perintah, petunjuk tersebut masih menjadi bagian Anda dari keseluruhan perintah, sehingga tunduk pada kebijakan penggunaan data standar.

Contoh kode

Contoh kode di tab berikut menunjukkan cara menggunakan petunjuk sistem dalam aplikasi AI generatif Anda.

Python

Untuk mempelajari cara menginstal atau mengupdate Vertex AI SDK untuk Python, lihat Menginstal Vertex AI SDK untuk Python. Untuk mengetahui informasi selengkapnya, lihat dokumentasi referensi Vertex AI SDK untuk Python API.

Respons streaming dan non-streaming

Anda dapat memilih apakah model akan menghasilkan respons streaming atau respons non-streaming. Untuk respons bertahap, Anda akan menerima setiap respons segera setelah token outputnya dibuat. Untuk respons non-streaming, Anda menerima semua respons setelah semua token output dibuat.

Untuk respons streaming, gunakan parameter stream dalam generate_content.

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

Untuk respons non-streaming, hapus parameter, atau tetapkan parameter ke False.

Kode contoh

import vertexai

from vertexai.generative_models import GenerativeModel

# TODO(developer): Update and un-comment below lines
# project_id = "PROJECT_ID"

vertexai.init(project=project_id, location="us-central1")

model = GenerativeModel(
    model_name="gemini-1.5-flash-001",
    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:
"""

contents = [prompt]

response = model.generate_content(contents)
print(response.text)

Node.js

Sebelum mencoba contoh ini, ikuti petunjuk penyiapan Node.js di Panduan memulai AI Generatif menggunakan Node.js SDK. Untuk informasi selengkapnya, lihat dokumentasi referensi Node.js SDK untuk Gemini.

Untuk mengautentikasi ke Vertex AI, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, lihat Menyiapkan autentikasi untuk lingkungan pengembangan lokal.

Respons streaming dan non-streaming

Anda dapat memilih apakah model akan menghasilkan respons streaming atau respons non-streaming. Untuk respons bertahap, Anda akan menerima setiap respons segera setelah token outputnya dibuat. Untuk respons non-streaming, Anda menerima semua respons setelah semua token output dibuat.

Untuk respons streaming, gunakan metode generateContentStream.

  const streamingResp = await generativeModel.generateContentStream(request);
  

Untuk respons non-streaming, gunakan metode generateContent.

  const streamingResp = await generativeModel.generateContent(request);
  

Kode contoh

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

Sebelum mencoba contoh ini, ikuti petunjuk penyiapan Java di panduan memulai Vertex AI. Untuk mengetahui informasi selengkapnya, lihat dokumentasi referensi Java SDK Vertex AI untuk Gemini.

Untuk mengautentikasi ke Vertex AI, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, lihat Menyiapkan autentikasi untuk lingkungan pengembangan lokal.

Respons streaming dan non-streaming

Anda dapat memilih apakah model akan menghasilkan respons streaming atau respons non-streaming. Untuk respons bertahap, Anda akan menerima setiap respons segera setelah token outputnya dibuat. Untuk respons non-streaming, Anda menerima semua respons setelah semua token output dibuat.

Untuk respons streaming, gunakan metode generateContentStream.

  public ResponseStream generateContentStream(Content content)
  

Untuk respons non-streaming, gunakan metode generateContent.

  public GenerateContentResponse generateContent(Content content)
  

Kode contoh

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

Sebelum mencoba contoh ini, ikuti petunjuk penyiapan Go di panduan memulai Vertex AI. Untuk mengetahui informasi selengkapnya, lihat dokumentasi referensi Vertex AI Go SDK untuk Gemini.

Untuk melakukan autentikasi ke Vertex AI, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, lihat Menyiapkan autentikasi untuk lingkungan pengembangan lokal.

Respons streaming dan non-streaming

Anda dapat memilih apakah model akan menghasilkan respons streaming atau respons non-streaming. Untuk respons bertahap, Anda akan menerima setiap respons segera setelah token outputnya dibuat. Untuk respons non-streaming, Anda menerima semua respons setelah semua token output dibuat.

Untuk respons streaming, gunakan metode GenerateContentStream.

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

Untuk respons non-streaming, gunakan metode GenerateContent.

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

Kode contoh

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, instruction, prompt, projectID, location, modelName string) error {
	// instruction := `
	// 		You are a helpful language translator.
	// 		Your mission is to translate text in English to French.`
	// prompt := `
	//		User input: I like bagels.
	//		Answer:`
	// 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(instruction)},
	}

	res, err := model.GenerateContent(ctx, genai.Text(prompt))
	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#

Sebelum mencoba contoh ini, ikuti petunjuk penyiapan C# di panduan memulai Vertex AI. Untuk mengetahui informasi selengkapnya, lihat dokumentasi referensi C# Vertex AI.

Untuk mengautentikasi ke Vertex AI, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, lihat Menyiapkan autentikasi untuk lingkungan pengembangan lokal.

Respons streaming dan non-streaming

Anda dapat memilih apakah model akan menghasilkan respons streaming atau respons non-streaming. Untuk respons bertahap, Anda akan menerima setiap respons segera setelah token outputnya dibuat. Untuk respons non-streaming, Anda menerima semua respons setelah semua token output dibuat.

Untuk respons streaming, gunakan metode StreamGenerateContent.

  public virtual PredictionServiceClient.StreamGenerateContentStream StreamGenerateContent(GenerateContentRequest request)
  

Untuk respons non-streaming, gunakan metode GenerateContentAsync.

  public virtual Task<GenerateContentResponse> GenerateContentAsync(GenerateContentRequest request)
  

Untuk mengetahui informasi selengkapnya tentang cara server dapat melakukan streaming respons, lihat RPC Streaming.

Kode contoh


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;
    }
}

Contoh perintah

Berikut adalah contoh dasar setelan instruksi sistem menggunakan Python SDK untuk Gemini API:

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

Berikut adalah contoh perintah sistem yang menentukan perilaku model yang diharapkan.

Pembuatan kode

Pembuatan kode
    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.
    

Pembuatan data terformat

Pembuatan data terformat
    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
    

Chatbot musik

Chatbot musik
    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.
    

Analisis keuangan

Analisis keuangan
    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.
    

Langkah selanjutnya