Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(vertexai/genai): add SystemInstruction #9736

Merged
merged 4 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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