From 823be8392920a37aff7119554632c5702a948067 Mon Sep 17 00:00:00 2001 From: Eli Bendersky Date: Tue, 30 Jul 2024 06:14:53 -0700 Subject: [PATCH] genai: update samples with explicit file upload for video, and comments --- genai/example_test.go | 35 ++++++++++++++++++-- genai/internal/samples/docs-snippets_test.go | 35 ++++++++++++++++++-- 2 files changed, 64 insertions(+), 6 deletions(-) diff --git a/genai/example_test.go b/genai/example_test.go index 0a3f8ff..aa8f5a1 100644 --- a/genai/example_test.go +++ b/genai/example_test.go @@ -411,6 +411,7 @@ func ExampleGenerativeModel_CountTokens_textOnly() { model := client.GenerativeModel("gemini-1.5-flash") prompt := "The quick brown fox jumps over the lazy dog" + // Call CountTokens to get the input token count (`total tokens`). tokResp, err := model.CountTokens(ctx, genai.Text(prompt)) if err != nil { log.Fatal(err) @@ -424,6 +425,10 @@ func ExampleGenerativeModel_CountTokens_textOnly() { log.Fatal(err) } + // On the response for GenerateContent, use UsageMetadata to get + // separate input and output token counts (PromptTokenCount and + // CandidatesTokenCount, respectively), as well as the combined + // token count (TotalTokenCount). fmt.Println("prompt_token_count:", resp.UsageMetadata.PromptTokenCount) fmt.Println("candidates_token_count:", resp.UsageMetadata.CandidatesTokenCount) fmt.Println("total_token_count:", resp.UsageMetadata.TotalTokenCount) @@ -459,6 +464,10 @@ func ExampleGenerativeModel_CountTokens_tools() { }}} model.Tools = tools + + // The total token count includes everything sent to the GenerateContent + // request. When you use tools (like function calling), the total + // token count increases. tokResp, err = model.CountTokens(ctx, genai.Text(prompt)) if err != nil { log.Fatal(err) @@ -682,7 +691,6 @@ func ExampleGenerativeModel_CountTokens_systemInstruction() { model := client.GenerativeModel("gemini-1.5-flash") prompt := "The quick brown fox jumps over the lazy dog" - // Without system instruction respNoInstruction, err := model.CountTokens(ctx, genai.Text(prompt)) if err != nil { log.Fatal(err) @@ -690,7 +698,9 @@ func ExampleGenerativeModel_CountTokens_systemInstruction() { fmt.Println("total_tokens:", respNoInstruction.TotalTokens) // ( total_tokens: 10 ) - // Same prompt, this time with system instruction + // The total token count includes everything sent to the GenerateContent + // request. When you use system instructions, the total token + // count increases. model.SystemInstruction = genai.NewUserContent(genai.Text("You are a cat. Your name is Neko.")) respWithInstruction, err := model.CountTokens(ctx, genai.Text(prompt)) if err != nil { @@ -1117,12 +1127,31 @@ func ExampleClient_UploadFile_video() { } defer client.Close() - file, err := uploadFile(ctx, client, filepath.Join(testDataDir, "earth.mp4"), "") + osf, err := os.Open(filepath.Join(testDataDir, "earth.mp4")) + if err != nil { + log.Fatal(err) + } + defer osf.Close() + + file, err := client.UploadFile(ctx, "", osf, nil) if err != nil { log.Fatal(err) } defer client.DeleteFile(ctx, file.Name) + // Videos need to be processed before you can use them. + for file.State == genai.FileStateProcessing { + log.Printf("processing %s", file.Name) + time.Sleep(5 * time.Second) + var err error + if file, err = client.GetFile(ctx, file.Name); err != nil { + log.Fatal(err) + } + } + if file.State != genai.FileStateActive { + log.Fatalf("uploaded file has state %s, not active", file.State) + } + model := client.GenerativeModel("gemini-1.5-flash") resp, err := model.GenerateContent(ctx, genai.FileData{URI: file.URI}, diff --git a/genai/internal/samples/docs-snippets_test.go b/genai/internal/samples/docs-snippets_test.go index 23fff17..45b062f 100644 --- a/genai/internal/samples/docs-snippets_test.go +++ b/genai/internal/samples/docs-snippets_test.go @@ -424,6 +424,7 @@ func ExampleGenerativeModel_CountTokens_textOnly() { model := client.GenerativeModel("gemini-1.5-flash") prompt := "The quick brown fox jumps over the lazy dog" + // Call CountTokens to get the input token count (`total tokens`). tokResp, err := model.CountTokens(ctx, genai.Text(prompt)) if err != nil { log.Fatal(err) @@ -437,6 +438,10 @@ func ExampleGenerativeModel_CountTokens_textOnly() { log.Fatal(err) } + // On the response for GenerateContent, use UsageMetadata to get + // separate input and output token counts (PromptTokenCount and + // CandidatesTokenCount, respectively), as well as the combined + // token count (TotalTokenCount). fmt.Println("prompt_token_count:", resp.UsageMetadata.PromptTokenCount) fmt.Println("candidates_token_count:", resp.UsageMetadata.CandidatesTokenCount) fmt.Println("total_token_count:", resp.UsageMetadata.TotalTokenCount) @@ -473,6 +478,10 @@ func ExampleGenerativeModel_CountTokens_tools() { }}} model.Tools = tools + + // The total token count includes everything sent to the GenerateContent + // request. When you use tools (like function calling), the total + // token count increases. tokResp, err = model.CountTokens(ctx, genai.Text(prompt)) if err != nil { log.Fatal(err) @@ -704,7 +713,6 @@ func ExampleGenerativeModel_CountTokens_systemInstruction() { model := client.GenerativeModel("gemini-1.5-flash") prompt := "The quick brown fox jumps over the lazy dog" - // Without system instruction respNoInstruction, err := model.CountTokens(ctx, genai.Text(prompt)) if err != nil { log.Fatal(err) @@ -712,7 +720,9 @@ func ExampleGenerativeModel_CountTokens_systemInstruction() { fmt.Println("total_tokens:", respNoInstruction.TotalTokens) // ( total_tokens: 10 ) - // Same prompt, this time with system instruction + // The total token count includes everything sent to the GenerateContent + // request. When you use system instructions, the total token + // count increases. model.SystemInstruction = genai.NewUserContent(genai.Text("You are a cat. Your name is Neko.")) respWithInstruction, err := model.CountTokens(ctx, genai.Text(prompt)) if err != nil { @@ -1149,12 +1159,31 @@ func ExampleClient_UploadFile_video() { defer client.Close() // [START files_create_video] - file, err := uploadFile(ctx, client, filepath.Join(testDataDir, "earth.mp4"), "") + osf, err := os.Open(filepath.Join(testDataDir, "earth.mp4")) + if err != nil { + log.Fatal(err) + } + defer osf.Close() + + file, err := client.UploadFile(ctx, "", osf, nil) if err != nil { log.Fatal(err) } defer client.DeleteFile(ctx, file.Name) + // Videos need to be processed before you can use them. + for file.State == genai.FileStateProcessing { + log.Printf("processing %s", file.Name) + time.Sleep(5 * time.Second) + var err error + if file, err = client.GetFile(ctx, file.Name); err != nil { + log.Fatal(err) + } + } + if file.State != genai.FileStateActive { + log.Fatalf("uploaded file has state %s, not active", file.State) + } + model := client.GenerativeModel("gemini-1.5-flash") resp, err := model.GenerateContent(ctx, genai.FileData{URI: file.URI},