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/protoveneer): add support for ResponseMimeType #10022

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions internal/protoveneer/cmd/protoveneer/protoveneer.go
Expand Up @@ -797,6 +797,12 @@ var externalTypes = []*externalType{
convertTo: "support.MapToStructPB",
convertFrom: "support.MapFromStructPB",
},
{
qualifiedName: "any",
replaces: "*structpb.Value",
convertTo: "support.MapToValuePB",
convertFrom: "support.MapFromValuePB",
},
}

var protoTypeToExternalType = map[string]*externalType{}
Expand Down
200 changes: 157 additions & 43 deletions vertexai/genai/aiplatformpb_veneer.gen.go
Expand Up @@ -24,13 +24,14 @@ import (
"cloud.google.com/go/vertexai/internal/support"
)

// Blob contains raw media bytes.
// Blob contains content blob.
//
// Text should not be sent as raw bytes, use the 'text' field.
// It's preferred to send as [text][google.cloud.aiplatform.v1beta1.Part.text]
// directly rather than raw bytes.
type Blob struct {
// Required. The IANA standard MIME type of the source data.
MIMEType string
// Required. Raw bytes for media formats.
// Required. Raw bytes.
Data []byte
}

Expand Down Expand Up @@ -64,12 +65,19 @@ const (
BlockedReasonSafety BlockedReason = 1
// BlockedReasonOther means candidates blocked due to other reason.
BlockedReasonOther BlockedReason = 2
// BlockedReasonBlocklist means candidates blocked due to the terms which are included from the
// terminology blocklist.
BlockedReasonBlocklist BlockedReason = 3
// BlockedReasonProhibitedContent means candidates blocked due to prohibited content.
BlockedReasonProhibitedContent BlockedReason = 4
)

var namesForBlockedReason = map[BlockedReason]string{
BlockedReasonUnspecified: "BlockedReasonUnspecified",
BlockedReasonSafety: "BlockedReasonSafety",
BlockedReasonOther: "BlockedReasonOther",
BlockedReasonUnspecified: "BlockedReasonUnspecified",
BlockedReasonSafety: "BlockedReasonSafety",
BlockedReasonOther: "BlockedReasonOther",
BlockedReasonBlocklist: "BlockedReasonBlocklist",
BlockedReasonProhibitedContent: "BlockedReasonProhibitedContent",
}

func (v BlockedReason) String() string {
Expand Down Expand Up @@ -379,8 +387,8 @@ func (FunctionCall) fromProto(p *pb.FunctionCall) *FunctionCall {
type FunctionDeclaration struct {
// Required. The name of the function to call.
// Must start with a letter or an underscore.
// Must be a-z, A-Z, 0-9, or contain underscores and dashes, with a maximum
// length of 64.
// Must be a-z, A-Z, 0-9, or contain underscores, dots and dashes, with a
// maximum length of 64.
Name string
// Optional. Description and purpose of the function.
// Model uses it to decide how and whether to call the function.
Expand All @@ -389,8 +397,10 @@ type FunctionDeclaration struct {
// format. Reflects the Open API 3.03 Parameter Object. string Key: the name
// of the parameter. Parameter names are case sensitive. Schema Value: the
// Schema defining the type used for the parameter. For function with no
// parameters, this can be left unset. Example with 1 required and 1 optional
// parameter: type: OBJECT properties:
// parameters, this can be left unset. Parameter names must start with a
// letter or an underscore and must only contain chars a-z, A-Z, 0-9, or
// underscores with a maximum length of 64. Example with 1 required and 1
// optional parameter: type: OBJECT properties:
//
// param1:
// type: STRING
Expand All @@ -400,6 +410,10 @@ type FunctionDeclaration struct {
// required:
// - param1
Parameters *Schema
// Optional. Describes the output from this function in JSON Schema format.
// Reflects the Open API 3.03 Response Object. The Schema defines the type
// used for the response value of the function.
Response *Schema
}

func (v *FunctionDeclaration) toProto() *pb.FunctionDeclaration {
Expand All @@ -410,6 +424,7 @@ func (v *FunctionDeclaration) toProto() *pb.FunctionDeclaration {
Name: v.Name,
Description: v.Description,
Parameters: v.Parameters.toProto(),
Response: v.Response.toProto(),
}
}

Expand All @@ -421,6 +436,7 @@ func (FunctionDeclaration) fromProto(p *pb.FunctionDeclaration) *FunctionDeclara
Name: p.Name,
Description: p.Description,
Parameters: (Schema{}).fromProto(p.Parameters),
Response: (Schema{}).fromProto(p.Response),
}
}

Expand Down Expand Up @@ -504,19 +520,34 @@ type GenerationConfig struct {
MaxOutputTokens *int32
// Optional. Stop sequences.
StopSequences []string
// Optional. Positive penalties.
PresencePenalty *float32
// Optional. Frequency penalties.
FrequencyPenalty *float32
// Optional. Output response mimetype of the generated candidate text.
// Supported mimetype:
// - `text/plain`: (default) Text output.
// - `application/json`: JSON response in the candidates.
// The model needs to be prompted to output the appropriate response type,
// otherwise the behavior is undefined.
// This is a preview feature.
ResponseMimeType string
}

func (v *GenerationConfig) toProto() *pb.GenerationConfig {
if v == nil {
return nil
}
return &pb.GenerationConfig{
Temperature: v.Temperature,
TopP: v.TopP,
TopK: int32pToFloat32p(v.TopK),
CandidateCount: v.CandidateCount,
MaxOutputTokens: v.MaxOutputTokens,
StopSequences: v.StopSequences,
Temperature: v.Temperature,
TopP: v.TopP,
TopK: int32pToFloat32p(v.TopK),
CandidateCount: v.CandidateCount,
MaxOutputTokens: v.MaxOutputTokens,
StopSequences: v.StopSequences,
PresencePenalty: v.PresencePenalty,
FrequencyPenalty: v.FrequencyPenalty,
ResponseMimeType: v.ResponseMimeType,
}
}

Expand All @@ -525,15 +556,43 @@ func (GenerationConfig) fromProto(p *pb.GenerationConfig) *GenerationConfig {
return nil
}
return &GenerationConfig{
Temperature: p.Temperature,
TopP: p.TopP,
TopK: float32pToInt32p(p.TopK),
CandidateCount: p.CandidateCount,
MaxOutputTokens: p.MaxOutputTokens,
StopSequences: p.StopSequences,
Temperature: p.Temperature,
TopP: p.TopP,
TopK: float32pToInt32p(p.TopK),
CandidateCount: p.CandidateCount,
MaxOutputTokens: p.MaxOutputTokens,
StopSequences: p.StopSequences,
PresencePenalty: p.PresencePenalty,
FrequencyPenalty: p.FrequencyPenalty,
ResponseMimeType: p.ResponseMimeType,
}
}

// HarmBlockMethod specifies probability vs severity.
type HarmBlockMethod int32

const (
// HarmBlockMethodSafetysettingHarmBlockMethodUnspecified means the harm block method is unspecified.
HarmBlockMethodSafetysettingHarmBlockMethodUnspecified HarmBlockMethod = 0
// HarmBlockMethodSafetysettingSeverity means the harm block method uses both probability and severity scores.
HarmBlockMethodSafetysettingSeverity HarmBlockMethod = 1
// HarmBlockMethodSafetysettingProbability means the harm block method uses the probability score.
HarmBlockMethodSafetysettingProbability HarmBlockMethod = 2
)

var namesForHarmBlockMethod = map[HarmBlockMethod]string{
HarmBlockMethodSafetysettingHarmBlockMethodUnspecified: "HarmBlockMethodSafetysettingHarmBlockMethodUnspecified",
HarmBlockMethodSafetysettingSeverity: "HarmBlockMethodSafetysettingSeverity",
HarmBlockMethodSafetysettingProbability: "HarmBlockMethodSafetysettingProbability",
}

func (v HarmBlockMethod) String() string {
if n, ok := namesForHarmBlockMethod[v]; ok {
return n
}
return fmt.Sprintf("HarmBlockMethod(%d)", v)
}

// HarmBlockThreshold specifies probability based thresholds levels for blocking.
type HarmBlockThreshold int32

Expand Down Expand Up @@ -627,7 +686,7 @@ func (v HarmProbability) String() string {
return fmt.Sprintf("HarmProbability(%d)", v)
}

// HarmSeverity is harm severity levels.
// HarmSeverity specifies harm severity levels.
type HarmSeverity int32

const (
Expand Down Expand Up @@ -741,6 +800,9 @@ type SafetySetting struct {
Category HarmCategory
// Required. The harm block threshold.
Threshold HarmBlockThreshold
// Optional. Specify if the threshold is used for probability or severity
// score. If not specified, the threshold is used for probability score.
Method HarmBlockMethod
}

func (v *SafetySetting) toProto() *pb.SafetySetting {
Expand All @@ -750,6 +812,7 @@ func (v *SafetySetting) toProto() *pb.SafetySetting {
return &pb.SafetySetting{
Category: pb.HarmCategory(v.Category),
Threshold: pb.SafetySetting_HarmBlockThreshold(v.Threshold),
Method: pb.SafetySetting_HarmBlockMethod(v.Method),
}
}

Expand All @@ -760,6 +823,7 @@ func (SafetySetting) fromProto(p *pb.SafetySetting) *SafetySetting {
return &SafetySetting{
Category: HarmCategory(p.Category),
Threshold: HarmBlockThreshold(p.Threshold),
Method: HarmBlockMethod(p.Method),
}
}

Expand All @@ -773,38 +837,77 @@ type Schema struct {
// Optional. The format of the data.
// Supported formats:
//
// for NUMBER type: float, double
// for INTEGER type: int32, int64
// for NUMBER type: "float", "double"
// for INTEGER type: "int32", "int64"
// for STRING type: "email", "byte", etc
Format string
// Optional. The title of the Schema.
Title string
// Optional. The description of the data.
Description string
// Optional. Indicates if the value may be null.
Nullable bool
// Optional. Schema of the elements of Type.ARRAY.
// Optional. Default value of the data.
Default any
// Optional. SCHEMA FIELDS FOR TYPE ARRAY
// Schema of the elements of Type.ARRAY.
Items *Schema
// Optional. Minimum number of the elements for Type.ARRAY.
MinItems int64
// Optional. Maximum number of the elements for Type.ARRAY.
MaxItems int64
// Optional. Possible values of the element of Type.STRING with enum format.
// For example we can define an Enum Direction as :
// {type:STRING, format:enum, enum:["EAST", NORTH", "SOUTH", "WEST"]}
Enum []string
// Optional. Properties of Type.OBJECT.
// Optional. SCHEMA FIELDS FOR TYPE OBJECT
// Properties of Type.OBJECT.
Properties map[string]*Schema
// Optional. Required properties of Type.OBJECT.
Required []string
// Optional. Minimum number of the properties for Type.OBJECT.
MinProperties int64
// Optional. Maximum number of the properties for Type.OBJECT.
MaxProperties int64
// Optional. SCHEMA FIELDS FOR TYPE INTEGER and NUMBER
// Minimum value of the Type.INTEGER and Type.NUMBER
Minimum float64
// Optional. Maximum value of the Type.INTEGER and Type.NUMBER
Maximum float64
// Optional. SCHEMA FIELDS FOR TYPE STRING
// Minimum length of the Type.STRING
MinLength int64
// Optional. Maximum length of the Type.STRING
MaxLength int64
// Optional. Pattern of the Type.STRING to restrict a string to a regular
// expression.
Pattern string
}

func (v *Schema) toProto() *pb.Schema {
if v == nil {
return nil
}
return &pb.Schema{
Type: pb.Type(v.Type),
Format: v.Format,
Description: v.Description,
Nullable: v.Nullable,
Items: v.Items.toProto(),
Enum: v.Enum,
Properties: support.TransformMapValues(v.Properties, (*Schema).toProto),
Required: v.Required,
Type: pb.Type(v.Type),
Format: v.Format,
Title: v.Title,
Description: v.Description,
Nullable: v.Nullable,
Default: support.MapToValuePB(v.Default),
Items: v.Items.toProto(),
MinItems: v.MinItems,
MaxItems: v.MaxItems,
Enum: v.Enum,
Properties: support.TransformMapValues(v.Properties, (*Schema).toProto),
Required: v.Required,
MinProperties: v.MinProperties,
MaxProperties: v.MaxProperties,
Minimum: v.Minimum,
Maximum: v.Maximum,
MinLength: v.MinLength,
MaxLength: v.MaxLength,
Pattern: v.Pattern,
}
}

Expand All @@ -813,14 +916,25 @@ func (Schema) fromProto(p *pb.Schema) *Schema {
return nil
}
return &Schema{
Type: Type(p.Type),
Format: p.Format,
Description: p.Description,
Nullable: p.Nullable,
Items: (Schema{}).fromProto(p.Items),
Enum: p.Enum,
Properties: support.TransformMapValues(p.Properties, (Schema{}).fromProto),
Required: p.Required,
Type: Type(p.Type),
Format: p.Format,
Title: p.Title,
Description: p.Description,
Nullable: p.Nullable,
Default: support.MapFromValuePB(p.Default),
Items: (Schema{}).fromProto(p.Items),
MinItems: p.MinItems,
MaxItems: p.MaxItems,
Enum: p.Enum,
Properties: support.TransformMapValues(p.Properties, (Schema{}).fromProto),
Required: p.Required,
MinProperties: p.MinProperties,
MaxProperties: p.MaxProperties,
Minimum: p.Minimum,
Maximum: p.Maximum,
MinLength: p.MinLength,
MaxLength: p.MaxLength,
Pattern: p.Pattern,
}
}

Expand Down
7 changes: 7 additions & 0 deletions vertexai/genai/config.yaml
Expand Up @@ -30,6 +30,13 @@ types:
protoPrefix: SafetyRating_
docVerb: specifies

SafetySetting_HarmBlockMethod:
name: HarmBlockMethod
protoPrefix: SafetyRating_
docVerb: specifies
valueNames:
SafetyRating_HARM_BLOCK_METHOD: HarmBlockMethodUnspecified

Candidate_FinishReason:
name: FinishReason
protoPrefix: Candidate_
Expand Down
3 changes: 3 additions & 0 deletions vertexai/genai/content.go
Expand Up @@ -134,3 +134,6 @@ func (c *GenerationConfig) SetTopP(x float32) { c.TopP = &x }

// SetTopK sets the TopK field.
func (c *GenerationConfig) SetTopK(x int32) { c.TopK = &x }

// SetResponseMimeType sets the ResponseMimeType field.
func (c *GenerationConfig) SetResponseMimeType(x string) { c.ResponseMimeType = x }