| 1 |
// Copyright 2023 Google LLC |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package genai |
| 16 |
|
| 17 |
import ( |
| 18 |
"fmt" |
| 19 |
|
| 20 |
pb "cloud.google.com/go/aiplatform/apiv1beta1/aiplatformpb" |
| 21 |
) |
| 22 |
|
| 23 |
const ( |
| 24 |
roleUser = "user" |
| 25 |
roleModel = "model" |
| 26 |
) |
| 27 |
|
| 28 |
// A Part is either a Text, a Blob, or a FileData. |
| 29 |
type Part interface { |
| 30 |
toPart() *pb.Part |
| 31 |
} |
| 32 |
|
| 33 |
func partToProto(p Part) *pb.Part { |
| 34 |
if p == nil { |
| 35 |
return nil |
| 36 |
} |
| 37 |
return p.toPart() |
| 38 |
} |
| 39 |
|
| 40 |
func partFromProto(p *pb.Part) Part { |
| 41 |
switch d := p.Data.(type) { |
| 42 |
case *pb.Part_Text: |
| 43 |
return Text(d.Text) |
| 44 |
case *pb.Part_InlineData: |
| 45 |
return Blob{ |
| 46 |
MIMEType: d.InlineData.MimeType, |
| 47 |
Data: d.InlineData.Data, |
| 48 |
} |
| 49 |
case *pb.Part_FileData: |
| 50 |
return FileData{ |
| 51 |
MIMEType: d.FileData.MimeType, |
| 52 |
FileURI: d.FileData.FileUri, |
| 53 |
} |
| 54 |
case *pb.Part_FunctionCall: |
| 55 |
return *(FunctionCall{}).fromProto(d.FunctionCall) |
| 56 |
|
| 57 |
case *pb.Part_FunctionResponse: |
| 58 |
panic("FunctionResponse unimplemented") |
| 59 |
|
| 60 |
default: |
| 61 |
if p.Thought && p.ThoughtSignature != nil { |
| 62 |
return ThoughtSignature(p.ThoughtSignature) |
| 63 |
} |
| 64 |
panic(fmt.Errorf("unknown Part.Data type %T", p.Data)) |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
// A Text is a piece of text, like a question or phrase. |
| 69 |
type Text string |
| 70 |
|
| 71 |
func (t Text) toPart() *pb.Part { |
| 72 |
return &pb.Part{ |
| 73 |
Data: &pb.Part_Text{Text: string(t)}, |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
func (b Blob) toPart() *pb.Part { |
| 78 |
return &pb.Part{ |
| 79 |
Data: &pb.Part_InlineData{ |
| 80 |
InlineData: b.toProto(), |
| 81 |
}, |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
func (f FileData) toPart() *pb.Part { |
| 86 |
return &pb.Part{ |
| 87 |
Data: &pb.Part_FileData{ |
| 88 |
FileData: f.toProto(), |
| 89 |
}, |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
func (f FunctionCall) toPart() *pb.Part { |
| 94 |
return &pb.Part{ |
| 95 |
Data: &pb.Part_FunctionCall{ |
| 96 |
FunctionCall: f.toProto(), |
| 97 |
}, |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
func (f FunctionResponse) toPart() *pb.Part { |
| 102 |
return &pb.Part{ |
| 103 |
Data: &pb.Part_FunctionResponse{ |
| 104 |
FunctionResponse: f.toProto(), |
| 105 |
}, |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
// ThoughtSignature is an opaque signature for the thought |
| 110 |
// so it can be reused in subsequent requests. |
| 111 |
type ThoughtSignature []byte |
| 112 |
|
| 113 |
func (t ThoughtSignature) toPart() *pb.Part { |
| 114 |
return &pb.Part{ |
| 115 |
ThoughtSignature: t, |
| 116 |
Thought: true, |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
// ImageData is a convenience function for creating an image |
| 121 |
// Blob for input to a model. |
| 122 |
// The format should be the second part of the MIME type, after "image/". |
| 123 |
// For example, for a PNG image, pass "png". |
| 124 |
func ImageData(format string, data []byte) Blob { |
| 125 |
return Blob{ |
| 126 |
MIMEType: "image/" + format, |
| 127 |
Data: data, |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
// Ptr returns a pointer to its argument. |
| 132 |
// It can be used to initialize pointer fields: |
| 133 |
// |
| 134 |
// model.Temperature = genai.Ptr[float32](0.1) |
| 135 |
func Ptr[T any](t T) *T { return &t } |
| 136 |
|
| 137 |
// SetCandidateCount sets the CandidateCount field. |
| 138 |
func (c *GenerationConfig) SetCandidateCount(x int32) { c.CandidateCount = &x } |
| 139 |
|
| 140 |
// SetMaxOutputTokens sets the MaxOutputTokens field. |
| 141 |
func (c *GenerationConfig) SetMaxOutputTokens(x int32) { c.MaxOutputTokens = &x } |
| 142 |
|
| 143 |
// SetTemperature sets the Temperature field. |
| 144 |
func (c *GenerationConfig) SetTemperature(x float32) { c.Temperature = &x } |
| 145 |
|
| 146 |
// SetTopP sets the TopP field. |
| 147 |
func (c *GenerationConfig) SetTopP(x float32) { c.TopP = &x } |
| 148 |
|
| 149 |
// SetTopK sets the TopK field. |
| 150 |
func (c *GenerationConfig) SetTopK(x int32) { c.TopK = &x } |
| 151 |
|
| 152 |
// FunctionCalls return all the FunctionCall parts in the candidate. |
| 153 |
func (c *Candidate) FunctionCalls() []FunctionCall { |
| 154 |
if c.Content == nil { |
| 155 |
return nil |
| 156 |
} |
| 157 |
var fcs []FunctionCall |
| 158 |
for _, p := range c.Content.Parts { |
| 159 |
if fc, ok := p.(FunctionCall); ok { |
| 160 |
fcs = append(fcs, fc) |
| 161 |
} |
| 162 |
} |
| 163 |
return fcs |
| 164 |
} |
| 165 |
|
| 166 |
// NewUserContent returns a [Content] with a "user" role set and one or more |
| 167 |
// parts. |
| 168 |
func NewUserContent(parts ...Part) *Content { |
| 169 |
content := &Content{Role: roleUser, Parts: []Part{}} |
| 170 |
for _, part := range parts { |
| 171 |
content.Parts = append(content.Parts, part) |
| 172 |
} |
| 173 |
return content |
| 174 |
} |