Skip to content

Commit

Permalink
feat(vertexai/genai): add SystemInstruction (#9736)
Browse files Browse the repository at this point in the history
Expose GenerateContentRequest.SystemInstruction.

As usual, put it on the Model, and use the Model field to construct
each request.
  • Loading branch information
jba authored Apr 30, 2024
1 parent bb84fbd commit 84e3236
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 9 deletions.
20 changes: 11 additions & 9 deletions vertexai/genai/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,10 @@ type GenerativeModel struct {
fullName string

GenerationConfig
SafetySettings []*SafetySetting
Tools []*Tool
ToolConfig *ToolConfig // configuration for tools
SafetySettings []*SafetySetting
Tools []*Tool
ToolConfig *ToolConfig // configuration for tools
SystemInstruction *Content
}

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

func (m *GenerativeModel) newGenerateContentRequest(contents ...*Content) *pb.GenerateContentRequest {
return &pb.GenerateContentRequest{
Model: m.fullName,
Contents: support.TransformSlice(contents, (*Content).toProto),
SafetySettings: support.TransformSlice(m.SafetySettings, (*SafetySetting).toProto),
Tools: support.TransformSlice(m.Tools, (*Tool).toProto),
ToolConfig: m.ToolConfig.toProto(),
GenerationConfig: m.GenerationConfig.toProto(),
Model: m.fullName,
Contents: support.TransformSlice(contents, (*Content).toProto),
SafetySettings: support.TransformSlice(m.SafetySettings, (*SafetySetting).toProto),
Tools: support.TransformSlice(m.Tools, (*Tool).toProto),
ToolConfig: m.ToolConfig.toProto(),
GenerationConfig: m.GenerationConfig.toProto(),
SystemInstruction: m.SystemInstruction.toProto(),
}
}

Expand Down
15 changes: 15 additions & 0 deletions vertexai/genai/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,21 @@ func TestLive(t *testing.T) {
got := responseString(resp)
checkMatch(t, got, `15.* cm|[1-9].* inches`)
})
t.Run("system-instructions", func(t *testing.T) {
model := client.GenerativeModel(*modelName)
model.Temperature = Ptr[float32](0)
model.SystemInstruction = &Content{
Parts: []Part{Text("You are Yoda from Star Wars.")},
}
resp, err := model.GenerateContent(ctx, Text("What is the average size of a swallow?"))
if err != nil {
t.Fatal(err)
}
got := responseString(resp)
checkMatch(t, got, `[1-9][0-9].* cm|[1-9].* inches`)
fmt.Println(got)

})

t.Run("streaming", func(t *testing.T) {
iter := model.GenerateContentStream(ctx, Text("Are you hungry?"))
Expand Down
27 changes: 27 additions & 0 deletions vertexai/genai/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,33 @@ func ExampleGenerativeModel_GenerateContent() {
printResponse(resp)
}

// This example shows how to a configure a model. See [GenerationConfig]
// for the complete set of configuration options.
func ExampleGenerativeModel_GenerateContent_config() {
ctx := context.Background()
const projectID = "YOUR PROJECT ID"
const location = "GCP LOCATION"
client, err := genai.NewClient(ctx, projectID, location)
if err != nil {
log.Fatal(err)
}
defer client.Close()

model := client.GenerativeModel("gemini-1.0-pro")
model.SetTemperature(0.9)
model.SetTopP(0.5)
model.SetTopK(20)
model.SetMaxOutputTokens(100)
model.SystemInstruction = &genai.Content{
Parts: []genai.Part{genai.Text("You are Yoda from Star Wars.")},
}
resp, err := model.GenerateContent(ctx, genai.Text("What is the average size of a swallow?"))
if err != nil {
log.Fatal(err)
}
printResponse(resp)
}

func ExampleGenerativeModel_GenerateContentStream() {
ctx := context.Background()
client, err := genai.NewClient(ctx, projectID, location)
Expand Down

0 comments on commit 84e3236

Please # to comment.