Skip to content

Commit

Permalink
genai: add FileState (#111)
Browse files Browse the repository at this point in the history
This also required updating the gensupport package.
  • Loading branch information
jba committed May 10, 2024
1 parent 47de201 commit f69f01d
Show file tree
Hide file tree
Showing 8 changed files with 6,276 additions and 9,177 deletions.
11 changes: 8 additions & 3 deletions genai/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,14 @@ types:
name: URI
MimeType:
name: MIMEType
State:
omit: true

File_State:
name: FileState
docVerb: represents
protoPrefix: File
veneerPrefix: FileState
valueNames:
File_STATE_UNSPECIFIED: FileStateUnspecified

GenerateContentResponse_UsageMetadata:
name: UsageMetadata
fields:
Expand Down
32 changes: 32 additions & 0 deletions genai/generativelanguagepb_veneer.gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,8 @@ type File struct {
Sha256Hash []byte
// Output only. The uri of the `File`.
URI string
// Output only. Processing state of the File.
State FileState
}

func (v *File) toProto() *pb.File {
Expand All @@ -374,6 +376,7 @@ func (v *File) toProto() *pb.File {
SizeBytes: v.SizeBytes,
Sha256Hash: v.Sha256Hash,
Uri: v.URI,
State: pb.File_State(v.State),
}
}

Expand All @@ -388,6 +391,7 @@ func (File) fromProto(p *pb.File) *File {
SizeBytes: p.SizeBytes,
Sha256Hash: p.Sha256Hash,
URI: p.Uri,
State: FileState(p.State),
}
}

Expand Down Expand Up @@ -419,6 +423,34 @@ func (FileData) fromProto(p *pb.FileData) *FileData {
}
}

// FileState represents states for the lifecycle of a File.
type FileState int32

const (
// FileStateUnspecified means the default value. This value is used if the state is omitted.
FileStateUnspecified FileState = 0
// FileStateProcessing means file is being processed and cannot be used for inference yet.
FileStateProcessing FileState = 1
// FileStateActive means file is processed and available for inference.
FileStateActive FileState = 2
// FileStateFailed means file failed processing.
FileStateFailed FileState = 10
)

var namesForFileState = map[FileState]string{
FileStateUnspecified: "FileStateUnspecified",
FileStateProcessing: "FileStateProcessing",
FileStateActive: "FileStateActive",
FileStateFailed: "FileStateFailed",
}

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

// FinishReason is defines the reason why the model stopped generating tokens.
type FinishReason int32

Expand Down
7,048 changes: 3,550 additions & 3,498 deletions genai/internal/generativelanguage/v1beta/generativelanguage-api.json

Large diffs are not rendered by default.

8,316 changes: 2,642 additions & 5,674 deletions genai/internal/generativelanguage/v1beta/generativelanguage-gen.go

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions genai/internal/gensupport/README
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
This directory was copied from github.com/googleapis/google-api-go-client/internal/gensupport.
It is needed for the discovery client in ../generativelanguage.

To update, first clone github.com/googleapis/google-api-go-client
into a directory we will call DIR below.
Then, from the repo root:
```
rm genai/internal/gensupport/*.go
cp $DIR/internal/gensupport/*.go genai/internal/gensupport
```
Then edit the params.go and resumable.go files to replace the reference to `internal.Version`
with the literal string from $DIR/internal/version.go, and remove the import of `internal`.
20 changes: 20 additions & 0 deletions genai/internal/gensupport/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package gensupport

import (
"net/http"
"net/url"

"google.golang.org/api/googleapi"
Expand Down Expand Up @@ -55,3 +56,22 @@ func SetOptions(u URLParams, opts ...googleapi.CallOption) {
u.Set(o.Get())
}
}

// SetHeaders sets common headers for all requests. The keyvals header pairs
// should have a corresponding value for every key provided. If there is an odd
// number of keyvals this method will panic.
func SetHeaders(userAgent, contentType string, userHeaders http.Header, keyvals ...string) http.Header {
reqHeaders := make(http.Header)
reqHeaders.Set("x-goog-api-client", "gl-go/"+GoVersion()+" gdcl/"+"0.179.0")
for i := 0; i < len(keyvals); i = i + 2 {
reqHeaders.Set(keyvals[i], keyvals[i+1])
}
reqHeaders.Set("User-Agent", userAgent)
if contentType != "" {
reqHeaders.Set("Content-Type", contentType)
}
for k, v := range userHeaders {
reqHeaders[k] = v
}
return reqHeaders
}
13 changes: 13 additions & 0 deletions genai/internal/gensupport/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package gensupport

import (
"net/http"
"testing"

"google.golang.org/api/googleapi"
Expand All @@ -18,3 +19,15 @@ func TestSetOptionsGetMulti(t *testing.T) {
t.Fatalf("URLParams.Encode() = %q, want %q", got, want)
}
}

func TestSetHeaders(t *testing.T) {
userAgent := "google-api-go-client/123"
contentType := "application/json"
userHeaders := make(http.Header)
userHeaders.Set("baz", "300")
got := SetHeaders(userAgent, contentType, userHeaders, "foo", "100", "bar", "200")

if len(got) != 6 {
t.Fatalf("SetHeaders() = %q, want len(6)", got)
}
}
3 changes: 1 addition & 2 deletions genai/internal/gensupport/resumable.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"sync"
"time"

"github.com/google/generative-ai-go/genai/internal"
"github.com/google/uuid"
)

Expand Down Expand Up @@ -83,7 +82,7 @@ func (rx *ResumableUpload) doUploadRequest(ctx context.Context, data io.Reader,

// TODO(b/274504690): Consider dropping gccl-invocation-id key since it
// duplicates the X-Goog-Gcs-Idempotency-Token header (added in v0.115.0).
baseXGoogHeader := "gl-go/" + GoVersion() + " gdcl/" + internal.Version
baseXGoogHeader := "gl-go/" + GoVersion() + " gdcl/" + "0.179.0"
invocationHeader := fmt.Sprintf("gccl-invocation-id/%s gccl-attempt-count/%d", rx.invocationID, rx.attempts)
req.Header.Set("X-Goog-Api-Client", strings.Join([]string{baseXGoogHeader, invocationHeader}, " "))

Expand Down

0 comments on commit f69f01d

Please sign in to comment.