forked from google/generative-ai-go
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcontent.go
101 lines (84 loc) · 2.55 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
// 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/ai/generativelanguage/apiv1/generativelanguagepb"
)
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,
}
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(),
},
}
}
// 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 }