-
Notifications
You must be signed in to change notification settings - Fork 40
/
client.go
401 lines (362 loc) · 11.7 KB
/
client.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
// 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.
// For the following go:generate line to work, do the following:
// Install the protoveener tool:
// git clone https://github.com/googleapis/google-cloud-go
// cd google-cloud-go
// go install ./internal/protoveneer/cmd/protoveneer
//
// Set the environment variable GOOGLE_CLOUD_GO to the path to the above repo on your machine.
// For example:
// export GOOGLE_CLOUD_GO=$HOME/repos/google-cloud-go
//go:generate protoveneer -license license.txt config.yaml $GOOGLE_CLOUD_GO/ai/generativelanguage/apiv1beta/generativelanguagepb
package genai
import (
"context"
"errors"
"fmt"
"io"
"reflect"
"strings"
gl "cloud.google.com/go/ai/generativelanguage/apiv1beta"
pb "cloud.google.com/go/ai/generativelanguage/apiv1beta/generativelanguagepb"
"github.com/google/generative-ai-go/genai/internal"
gld "github.com/google/generative-ai-go/genai/internal/generativelanguage/v1beta" // discovery client
"github.com/google/generative-ai-go/internal/support"
"google.golang.org/api/iterator"
"google.golang.org/api/option"
)
// A Client is a Google generative AI client.
type Client struct {
c *gl.GenerativeClient
mc *gl.ModelClient
fc *gl.FileClient
ds *gld.Service
}
// NewClient creates a new Google generative AI client.
//
// Clients should be reused instead of created as needed. The methods of Client
// are safe for concurrent use by multiple goroutines.
//
// You may configure the client by passing in options from the [google.golang.org/api/option]
// package.
func NewClient(ctx context.Context, opts ...option.ClientOption) (*Client, error) {
if !hasAuthOption(opts) {
return nil, errors.New(`You need an auth option to use this client.
for an API Key: Visit https://ai.google.dev to get one, put it in an environment variable like GEMINI_API_KEY,
then pass it as an option:
genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
(If you're doing that already, then maybe the environment variable is empty or unset.)
Import the option package as "google.golang.org/api/option".`)
}
c, err := gl.NewGenerativeRESTClient(ctx, opts...)
if err != nil {
return nil, err
}
mc, err := gl.NewModelRESTClient(ctx, opts...)
if err != nil {
return nil, err
}
fc, err := gl.NewFileRESTClient(ctx, opts...)
if err != nil {
return nil, err
}
ds, err := gld.NewService(ctx, opts...)
if err != nil {
return nil, err
}
c.SetGoogleClientInfo("gccl", "v"+internal.Version, "genai-go", internal.Version)
return &Client{c, mc, fc, ds}, nil
}
// hasAuthOption reports whether an authentication-related option was provided.
//
// There is no good way to make these checks, because the types of the options
// are unexported, and the struct that they populates is in an internal package.
func hasAuthOption(opts []option.ClientOption) bool {
for _, opt := range opts {
v := reflect.ValueOf(opt)
ts := v.Type().String()
switch ts {
case "option.withAPIKey":
return v.String() != ""
case "option.withHttpClient",
"option.withTokenSource",
"option.withCredentialsFile",
"option.withCredentialsJSON":
return true
}
}
return false
}
// Close closes the client.
func (c *Client) Close() error {
return errors.Join(c.c.Close(), c.mc.Close(), c.fc.Close())
}
// GenerativeModel is a model that can generate text.
// Create one with [Client.GenerativeModel], then configure
// it by setting the exported fields.
type GenerativeModel struct {
c *Client
fullName string
GenerationConfig
SafetySettings []*SafetySetting
Tools []*Tool
ToolConfig *ToolConfig // configuration for tools
// SystemInstruction (also known as "system prompt") is a more forceful prompt to the model.
// The model will adhere the instructions more strongly than if they appeared in a normal prompt.
SystemInstruction *Content
}
// GenerativeModel creates a new instance of the named generative model.
// For instance, "gemini-1.0-pro" or "models/gemini-1.0-pro".
//
// To access a tuned model named NAME, pass "tunedModels/NAME".
func (c *Client) GenerativeModel(name string) *GenerativeModel {
return &GenerativeModel{
c: c,
fullName: fullModelName(name),
}
}
func fullModelName(name string) string {
if strings.ContainsRune(name, '/') {
return name
}
return "models/" + name
}
// GenerateContent produces a single request and response.
func (m *GenerativeModel) GenerateContent(ctx context.Context, parts ...Part) (*GenerateContentResponse, error) {
content := newUserContent(parts)
req := m.newGenerateContentRequest(content)
res, err := m.c.c.GenerateContent(ctx, req)
if err != nil {
return nil, err
}
return protoToResponse(res)
}
// GenerateContentStream returns an iterator that enumerates responses.
func (m *GenerativeModel) GenerateContentStream(ctx context.Context, parts ...Part) *GenerateContentResponseIterator {
streamClient, err := m.c.c.StreamGenerateContent(ctx, m.newGenerateContentRequest(newUserContent(parts)))
return &GenerateContentResponseIterator{
sc: streamClient,
err: err,
}
}
func (m *GenerativeModel) generateContent(ctx context.Context, req *pb.GenerateContentRequest) (*GenerateContentResponse, error) {
streamClient, err := m.c.c.StreamGenerateContent(ctx, req)
iter := &GenerateContentResponseIterator{
sc: streamClient,
err: err,
}
for {
_, err := iter.Next()
if err == iterator.Done {
return iter.merged, nil
}
if err != nil {
return nil, err
}
}
}
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(),
SystemInstruction: m.SystemInstruction.toProto(),
}
}
func newUserContent(parts []Part) *Content {
return &Content{Role: roleUser, Parts: parts}
}
// GenerateContentResponseIterator is an iterator over GnerateContentResponse.
type GenerateContentResponseIterator struct {
sc pb.GenerativeService_StreamGenerateContentClient
err error
merged *GenerateContentResponse
cs *ChatSession
}
// Next returns the next response.
func (iter *GenerateContentResponseIterator) Next() (*GenerateContentResponse, error) {
if iter.err != nil {
return nil, iter.err
}
resp, err := iter.sc.Recv()
iter.err = err
if err == io.EOF {
if iter.cs != nil && iter.merged != nil {
iter.cs.addToHistory(iter.merged.Candidates)
}
return nil, iterator.Done
}
if err != nil {
return nil, err
}
gcp, err := protoToResponse(resp)
if err != nil {
iter.err = err
return nil, err
}
// Merge this response in with the ones we've already seen.
iter.merged = joinResponses(iter.merged, gcp)
// If this is part of a ChatSession, remember the response for the history.
return gcp, nil
}
func protoToResponse(resp *pb.GenerateContentResponse) (*GenerateContentResponse, error) {
gcp := (GenerateContentResponse{}).fromProto(resp)
if gcp == nil {
return nil, errors.New("empty response from model")
}
// Assume a non-nil PromptFeedback is an error.
// TODO: confirm.
if gcp.PromptFeedback != nil && gcp.PromptFeedback.BlockReason != BlockReasonUnspecified {
return nil, &BlockedError{PromptFeedback: gcp.PromptFeedback}
}
// If any candidate is blocked, error.
// TODO: is this too harsh?
for _, c := range gcp.Candidates {
if c.FinishReason == FinishReasonSafety || c.FinishReason == FinishReasonRecitation {
return nil, &BlockedError{Candidate: c}
}
}
return gcp, nil
}
// CountTokens counts the number of tokens in the content.
func (m *GenerativeModel) CountTokens(ctx context.Context, parts ...Part) (*CountTokensResponse, error) {
req := m.newCountTokensRequest(newUserContent(parts))
res, err := m.c.c.CountTokens(ctx, req)
if err != nil {
return nil, err
}
return (CountTokensResponse{}).fromProto(res), nil
}
func (m *GenerativeModel) newCountTokensRequest(contents ...*Content) *pb.CountTokensRequest {
return &pb.CountTokensRequest{
Model: m.fullName,
Contents: support.TransformSlice(contents, (*Content).toProto),
}
}
// Info returns information about the model.
func (m *GenerativeModel) Info(ctx context.Context) (*ModelInfo, error) {
return m.c.modelInfo(ctx, m.fullName)
}
func (c *Client) modelInfo(ctx context.Context, fullName string) (*ModelInfo, error) {
req := &pb.GetModelRequest{Name: fullName}
res, err := c.mc.GetModel(ctx, req)
if err != nil {
return nil, err
}
return (ModelInfo{}).fromProto(res), nil
}
// A BlockedError indicates that the model's response was blocked.
// There can be two underlying causes: the prompt or a candidate response.
type BlockedError struct {
// If non-nil, the model's response was blocked.
// Consult the FinishReason field for details.
Candidate *Candidate
// If non-nil, there was a problem with the prompt.
PromptFeedback *PromptFeedback
}
func (e *BlockedError) Error() string {
var b strings.Builder
fmt.Fprintf(&b, "blocked: ")
if e.Candidate != nil {
fmt.Fprintf(&b, "candidate: %s", e.Candidate.FinishReason)
}
if e.PromptFeedback != nil {
if e.Candidate != nil {
fmt.Fprintf(&b, ", ")
}
fmt.Fprintf(&b, "prompt: %v", e.PromptFeedback.BlockReason)
}
return b.String()
}
// joinResponses merges the two responses, which should be the result of a streaming call.
// The first argument is modified.
func joinResponses(dest, src *GenerateContentResponse) *GenerateContentResponse {
if dest == nil {
return src
}
dest.Candidates = joinCandidateLists(dest.Candidates, src.Candidates)
// Keep dest.PromptFeedback.
// TODO: Take the last UsageMetadata.
return dest
}
func joinCandidateLists(dest, src []*Candidate) []*Candidate {
indexToSrcCandidate := map[int32]*Candidate{}
for _, s := range src {
indexToSrcCandidate[s.Index] = s
}
for _, d := range dest {
s := indexToSrcCandidate[d.Index]
if s != nil {
d.Content = joinContent(d.Content, s.Content)
// Take the last of these.
d.FinishReason = s.FinishReason
// d.FinishMessage = s.FinishMessage
d.SafetyRatings = s.SafetyRatings
d.CitationMetadata = joinCitationMetadata(d.CitationMetadata, s.CitationMetadata)
}
}
return dest
}
func joinCitationMetadata(dest, src *CitationMetadata) *CitationMetadata {
if dest == nil {
return src
}
if src == nil {
return dest
}
dest.CitationSources = append(dest.CitationSources, src.CitationSources...)
return dest
}
func joinContent(dest, src *Content) *Content {
if dest == nil {
return src
}
if src == nil {
return dest
}
// Assume roles are the same.
dest.Parts = joinParts(dest.Parts, src.Parts)
return dest
}
func joinParts(dest, src []Part) []Part {
return mergeTexts(append(dest, src...))
}
func mergeTexts(in []Part) []Part {
var out []Part
i := 0
for i < len(in) {
if t, ok := in[i].(Text); ok {
texts := []string{string(t)}
var j int
for j = i + 1; j < len(in); j++ {
if t, ok := in[j].(Text); ok {
texts = append(texts, string(t))
} else {
break
}
}
// j is just after the last Text.
out = append(out, Text(strings.Join(texts, "")))
i = j
} else {
out = append(out, in[i])
i++
}
}
return out
}