Skip to content

Commit 84e3236

Browse files
authored
feat(vertexai/genai): add SystemInstruction (#9736)
Expose GenerateContentRequest.SystemInstruction. As usual, put it on the Model, and use the Model field to construct each request.
1 parent bb84fbd commit 84e3236

3 files changed

Lines changed: 53 additions & 9 deletions

File tree

vertexai/genai/client.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,10 @@ type GenerativeModel struct {
9393
fullName string
9494

9595
GenerationConfig
96-
SafetySettings []*SafetySetting
97-
Tools []*Tool
98-
ToolConfig *ToolConfig // configuration for tools
96+
SafetySettings []*SafetySetting
97+
Tools []*Tool
98+
ToolConfig *ToolConfig // configuration for tools
99+
SystemInstruction *Content
99100
}
100101

101102
const defaultMaxOutputTokens = 2048
@@ -142,12 +143,13 @@ func (m *GenerativeModel) generateContent(ctx context.Context, req *pb.GenerateC
142143

143144
func (m *GenerativeModel) newGenerateContentRequest(contents ...*Content) *pb.GenerateContentRequest {
144145
return &pb.GenerateContentRequest{
145-
Model: m.fullName,
146-
Contents: support.TransformSlice(contents, (*Content).toProto),
147-
SafetySettings: support.TransformSlice(m.SafetySettings, (*SafetySetting).toProto),
148-
Tools: support.TransformSlice(m.Tools, (*Tool).toProto),
149-
ToolConfig: m.ToolConfig.toProto(),
150-
GenerationConfig: m.GenerationConfig.toProto(),
146+
Model: m.fullName,
147+
Contents: support.TransformSlice(contents, (*Content).toProto),
148+
SafetySettings: support.TransformSlice(m.SafetySettings, (*SafetySetting).toProto),
149+
Tools: support.TransformSlice(m.Tools, (*Tool).toProto),
150+
ToolConfig: m.ToolConfig.toProto(),
151+
GenerationConfig: m.GenerationConfig.toProto(),
152+
SystemInstruction: m.SystemInstruction.toProto(),
151153
}
152154
}
153155

vertexai/genai/client_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,21 @@ func TestLive(t *testing.T) {
5959
got := responseString(resp)
6060
checkMatch(t, got, `15.* cm|[1-9].* inches`)
6161
})
62+
t.Run("system-instructions", func(t *testing.T) {
63+
model := client.GenerativeModel(*modelName)
64+
model.Temperature = Ptr[float32](0)
65+
model.SystemInstruction = &Content{
66+
Parts: []Part{Text("You are Yoda from Star Wars.")},
67+
}
68+
resp, err := model.GenerateContent(ctx, Text("What is the average size of a swallow?"))
69+
if err != nil {
70+
t.Fatal(err)
71+
}
72+
got := responseString(resp)
73+
checkMatch(t, got, `[1-9][0-9].* cm|[1-9].* inches`)
74+
fmt.Println(got)
75+
76+
})
6277

6378
t.Run("streaming", func(t *testing.T) {
6479
iter := model.GenerateContentStream(ctx, Text("Are you hungry?"))

vertexai/genai/example_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,33 @@ func ExampleGenerativeModel_GenerateContent() {
5151
printResponse(resp)
5252
}
5353

54+
// This example shows how to a configure a model. See [GenerationConfig]
55+
// for the complete set of configuration options.
56+
func ExampleGenerativeModel_GenerateContent_config() {
57+
ctx := context.Background()
58+
const projectID = "YOUR PROJECT ID"
59+
const location = "GCP LOCATION"
60+
client, err := genai.NewClient(ctx, projectID, location)
61+
if err != nil {
62+
log.Fatal(err)
63+
}
64+
defer client.Close()
65+
66+
model := client.GenerativeModel("gemini-1.0-pro")
67+
model.SetTemperature(0.9)
68+
model.SetTopP(0.5)
69+
model.SetTopK(20)
70+
model.SetMaxOutputTokens(100)
71+
model.SystemInstruction = &genai.Content{
72+
Parts: []genai.Part{genai.Text("You are Yoda from Star Wars.")},
73+
}
74+
resp, err := model.GenerateContent(ctx, genai.Text("What is the average size of a swallow?"))
75+
if err != nil {
76+
log.Fatal(err)
77+
}
78+
printResponse(resp)
79+
}
80+
5481
func ExampleGenerativeModel_GenerateContentStream() {
5582
ctx := context.Background()
5683
client, err := genai.NewClient(ctx, projectID, location)

0 commit comments

Comments
 (0)