Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

genai: generalize uploadFile in examples and add comment #185

Merged
merged 4 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 14 additions & 15 deletions genai/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,20 @@ import (
var testDataDir = filepath.Join(testhelpers.ModuleRootDir(), "genai", "testdata")

// uploadFile uploads the given file to the service, and returns a [genai.File]
// representing it.
// representing it. mime optionally specifies the MIME type of the data in the
// file; if set to "", the service will try to automatically determine the type
// from the data contents.
// To clean up the file, defer a client.DeleteFile(ctx, file.Name)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, and in the equivalent comment in docs-snippets-test.go, the word "try" suggests that the service may fail. It would be more accurate to say "it will try to determine the correct type"

Thinking about the comment a bit more, and trying to add some guidance and insight around when to specify the empty strings and when to provide a MIME type, how about something like:

mimeType optionally specifies the MIME type of the data in the file;
if omitted, the service will attempt to infer it from the file contents;
for ambiguous file types (e.g., text files), it is recommended to explicitly specify the MIME type

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

^ Feedback for future consideration

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this phrasing; will send a PR soon

// call when a file is successfully returned. file.Name will be a uniqely
// generated string to identify the file on the service.
func uploadFile(ctx context.Context, client *genai.Client, path string) (*genai.File, error) {
func uploadFile(ctx context.Context, client *genai.Client, path string, mime string) (*genai.File, error) {
eliben marked this conversation as resolved.
Show resolved Hide resolved
osf, err := os.Open(path)
if err != nil {
return nil, err
}
defer osf.Close()

opts := &genai.UploadFileOptions{}
if filepath.Ext(path) == ".txt" {
opts.MIMEType = "text/plain"
}
opts := &genai.UploadFileOptions{MIMEType: mime}
file, err := client.UploadFile(ctx, "", osf, opts)
if err != nil {
return nil, err
Expand Down Expand Up @@ -128,7 +127,7 @@ func ExampleGenerativeModel_GenerateContent_videoPrompt() {

model := client.GenerativeModel("gemini-1.5-flash")

file, err := uploadFile(ctx, client, filepath.Join(testDataDir, "earth.mp4"))
file, err := uploadFile(ctx, client, filepath.Join(testDataDir, "earth.mp4"), "")
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -362,7 +361,7 @@ func ExampleGenerativeModel_GenerateContentStream_videoPrompt() {
// START [text_gen_multimodal_video_prompt_streaming]
model := client.GenerativeModel("gemini-1.5-flash")

file, err := uploadFile(ctx, client, filepath.Join(testDataDir, "earth.mp4"))
file, err := uploadFile(ctx, client, filepath.Join(testDataDir, "earth.mp4"), "")
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -525,7 +524,7 @@ func ExampleGenerativeModel_CountTokens_imageUploadFile() {

model := client.GenerativeModel("gemini-1.5-flash")
prompt := "Tell me about this image"
file, err := uploadFile(ctx, client, filepath.Join(testDataDir, "personWorkingOnComputer.jpg"))
file, err := uploadFile(ctx, client, filepath.Join(testDataDir, "personWorkingOnComputer.jpg"), "")
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -565,7 +564,7 @@ func ExampleGenerativeModel_CountTokens_videoUploadFile() {

model := client.GenerativeModel("gemini-1.5-flash")
prompt := "Tell me about this video"
file, err := uploadFile(ctx, client, filepath.Join(testDataDir, "earth.mp4"))
file, err := uploadFile(ctx, client, filepath.Join(testDataDir, "earth.mp4"), "")
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -1034,7 +1033,7 @@ func ExampleClient_UploadFile_text() {
}
defer client.Close()

file, err := uploadFile(ctx, client, filepath.Join(testDataDir, "poem.txt"))
file, err := uploadFile(ctx, client, filepath.Join(testDataDir, "poem.txt"), "text/plain")
if err != nil {
log.Fatal(err)
}
Expand All @@ -1060,7 +1059,7 @@ func ExampleClient_UploadFile_image() {
}
defer client.Close()

file, err := uploadFile(ctx, client, filepath.Join(testDataDir, "Cajun_instruments.jpg"))
file, err := uploadFile(ctx, client, filepath.Join(testDataDir, "Cajun_instruments.jpg"), "")
if err != nil {
log.Fatal(err)
}
Expand All @@ -1086,7 +1085,7 @@ func ExampleClient_UploadFile_video() {
}
defer client.Close()

file, err := uploadFile(ctx, client, filepath.Join(testDataDir, "earth.mp4"))
file, err := uploadFile(ctx, client, filepath.Join(testDataDir, "earth.mp4"), "")
if err != nil {
log.Fatal(err)
}
Expand All @@ -1112,7 +1111,7 @@ func ExampleClient_UploadFile_audio() {
}
defer client.Close()

file, err := uploadFile(ctx, client, filepath.Join(testDataDir, "sample.mp3"))
file, err := uploadFile(ctx, client, filepath.Join(testDataDir, "sample.mp3"), "")
if err != nil {
log.Fatal(err)
}
Expand All @@ -1138,7 +1137,7 @@ func ExampleClient_GetFile() {
}
defer client.Close()

file, err := uploadFile(ctx, client, filepath.Join(testDataDir, "personWorkingOnComputer.jpg"))
file, err := uploadFile(ctx, client, filepath.Join(testDataDir, "personWorkingOnComputer.jpg"), "")
if err != nil {
log.Fatal(err)
}
Expand Down
31 changes: 16 additions & 15 deletions genai/internal/samples/docs-snippets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,20 @@ import (
var testDataDir = filepath.Join(testhelpers.ModuleRootDir(), "genai", "testdata")

// uploadFile uploads the given file to the service, and returns a [genai.File]
// representing it.
// representing it. mime optionally specifies the MIME type of the data in the
// file; if set to "", the service will try to automatically determine the type
// from the data contents.
// To clean up the file, defer a client.DeleteFile(ctx, file.Name)
// call when a file is successfully returned. file.Name will be a uniqely
// generated string to identify the file on the service.
func uploadFile(ctx context.Context, client *genai.Client, path string) (*genai.File, error) {
func uploadFile(ctx context.Context, client *genai.Client, path string, mime string) (*genai.File, error) {
osf, err := os.Open(path)
if err != nil {
return nil, err
}
defer osf.Close()

opts := &genai.UploadFileOptions{}
if filepath.Ext(path) == ".txt" {
opts.MIMEType = "text/plain"
}
opts := &genai.UploadFileOptions{MIMEType: mime}
file, err := client.UploadFile(ctx, "", osf, opts)
if err != nil {
return nil, err
Expand Down Expand Up @@ -131,7 +130,7 @@ func ExampleGenerativeModel_GenerateContent_videoPrompt() {
// [START text_gen_multimodal_video_prompt]
model := client.GenerativeModel("gemini-1.5-flash")

file, err := uploadFile(ctx, client, filepath.Join(testDataDir, "earth.mp4"))
file, err := uploadFile(ctx, client, filepath.Join(testDataDir, "earth.mp4"), "")
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -370,7 +369,7 @@ func ExampleGenerativeModel_GenerateContentStream_videoPrompt() {
// START [text_gen_multimodal_video_prompt_streaming]
model := client.GenerativeModel("gemini-1.5-flash")

file, err := uploadFile(ctx, client, filepath.Join(testDataDir, "earth.mp4"))
file, err := uploadFile(ctx, client, filepath.Join(testDataDir, "earth.mp4"), "")
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -538,7 +537,7 @@ func ExampleGenerativeModel_CountTokens_imageUploadFile() {
// [START tokens_multimodal_image_file_api]
model := client.GenerativeModel("gemini-1.5-flash")
prompt := "Tell me about this image"
file, err := uploadFile(ctx, client, filepath.Join(testDataDir, "personWorkingOnComputer.jpg"))
file, err := uploadFile(ctx, client, filepath.Join(testDataDir, "personWorkingOnComputer.jpg"), "")
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -579,7 +578,7 @@ func ExampleGenerativeModel_CountTokens_videoUploadFile() {
// [START tokens_multimodal_video_audio_file_api]
model := client.GenerativeModel("gemini-1.5-flash")
prompt := "Tell me about this video"
file, err := uploadFile(ctx, client, filepath.Join(testDataDir, "earth.mp4"))
file, err := uploadFile(ctx, client, filepath.Join(testDataDir, "earth.mp4"), "")
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -1057,7 +1056,9 @@ func ExampleClient_UploadFile_text() {
defer client.Close()

// [START files_create_text]
file, err := uploadFile(ctx, client, filepath.Join(testDataDir, "poem.txt"))
// Set MIME type explicitly for text files - the service may have difficulty
// distingushing between different MIME types of text files automatically.
file, err := uploadFile(ctx, client, filepath.Join(testDataDir, "poem.txt"), "text/plain")
if err != nil {
log.Fatal(err)
}
Expand All @@ -1084,7 +1085,7 @@ func ExampleClient_UploadFile_image() {
defer client.Close()

// [START files_create_image]
file, err := uploadFile(ctx, client, filepath.Join(testDataDir, "Cajun_instruments.jpg"))
file, err := uploadFile(ctx, client, filepath.Join(testDataDir, "Cajun_instruments.jpg"), "")
if err != nil {
log.Fatal(err)
}
Expand All @@ -1111,7 +1112,7 @@ func ExampleClient_UploadFile_video() {
defer client.Close()

// [START files_create_video]
file, err := uploadFile(ctx, client, filepath.Join(testDataDir, "earth.mp4"))
file, err := uploadFile(ctx, client, filepath.Join(testDataDir, "earth.mp4"), "")
if err != nil {
log.Fatal(err)
}
Expand All @@ -1138,7 +1139,7 @@ func ExampleClient_UploadFile_audio() {
defer client.Close()

// [START files_create_audio]
file, err := uploadFile(ctx, client, filepath.Join(testDataDir, "sample.mp3"))
file, err := uploadFile(ctx, client, filepath.Join(testDataDir, "sample.mp3"), "")
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -1166,7 +1167,7 @@ func ExampleClient_GetFile() {

// [START files_get]
// [START files_delete]
file, err := uploadFile(ctx, client, filepath.Join(testDataDir, "personWorkingOnComputer.jpg"))
file, err := uploadFile(ctx, client, filepath.Join(testDataDir, "personWorkingOnComputer.jpg"), "")
if err != nil {
log.Fatal(err)
}
Expand Down
Loading