-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
content.go
139 lines (116 loc) · 3.33 KB
/
content.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package genai
import (
"fmt"
pb "cloud.google.com/go/aiplatform/apiv1beta1/aiplatformpb"
)
const (
roleUser = "user"
roleModel = "model"
)
// A Part is either a Text, a Blob, or a FileData.
type Part interface {
toPart() *pb.Part
}
func partToProto(p Part) *pb.Part {
if p == nil {
return nil
}
return p.toPart()
}
func partFromProto(p *pb.Part) Part {
switch d := p.Data.(type) {
case *pb.Part_Text:
return Text(d.Text)
case *pb.Part_InlineData:
return Blob{
MIMEType: d.InlineData.MimeType,
Data: d.InlineData.Data,
}
case *pb.Part_FileData:
return FileData{
MIMEType: d.FileData.MimeType,
FileURI: d.FileData.FileUri,
}
case *pb.Part_FunctionCall:
return *(FunctionCall{}).fromProto(d.FunctionCall)
case *pb.Part_FunctionResponse:
panic("FunctionResponse unimplemented")
default:
panic(fmt.Errorf("unknown Part.Data type %T", p.Data))
}
}
// A Text is a piece of text, like a question or phrase.
type Text string
func (t Text) toPart() *pb.Part {
return &pb.Part{
Data: &pb.Part_Text{Text: string(t)},
}
}
func (b Blob) toPart() *pb.Part {
return &pb.Part{
Data: &pb.Part_InlineData{
InlineData: b.toProto(),
},
}
}
func (f FileData) toPart() *pb.Part {
return &pb.Part{
Data: &pb.Part_FileData{
FileData: f.toProto(),
},
}
}
func (f FunctionCall) toPart() *pb.Part {
return &pb.Part{
Data: &pb.Part_FunctionCall{
FunctionCall: f.toProto(),
},
}
}
func (f FunctionResponse) toPart() *pb.Part {
return &pb.Part{
Data: &pb.Part_FunctionResponse{
FunctionResponse: f.toProto(),
},
}
}
// ImageData is a convenience function for creating an image
// Blob for input to a model.
// The format should be the second part of the MIME type, after "image/".
// For example, for a PNG image, pass "png".
func ImageData(format string, data []byte) Blob {
return Blob{
MIMEType: "image/" + format,
Data: data,
}
}
// Ptr returns a pointer to its argument.
// It can be used to initialize pointer fields:
//
// model.Temperature = genai.Ptr[float32](0.1)
func Ptr[T any](t T) *T { return &t }
// SetCandidateCount sets the CandidateCount field.
func (c *GenerationConfig) SetCandidateCount(x int32) { c.CandidateCount = &x }
// SetMaxOutputTokens sets the MaxOutputTokens field.
func (c *GenerationConfig) SetMaxOutputTokens(x int32) { c.MaxOutputTokens = &x }
// SetTemperature sets the Temperature field.
func (c *GenerationConfig) SetTemperature(x float32) { c.Temperature = &x }
// SetTopP sets the TopP field.
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 }