Skip to content

Commit

Permalink
genai: samples for counting tokens in video (#181)
Browse files Browse the repository at this point in the history
  • Loading branch information
eliben committed Jul 18, 2024
1 parent ae10a3a commit 0fdd360
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 6 deletions.
44 changes: 41 additions & 3 deletions genai/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -531,9 +531,7 @@ func ExampleGenerativeModel_CountTokens_imageUploadFile() {
}
defer client.DeleteFile(ctx, file.Name)

fd := genai.FileData{
URI: file.URI,
}
fd := genai.FileData{URI: file.URI}
// Call `CountTokens` to get the input token count
// of the combined text and file (`total_tokens`).
// An image's display or file size does not affect its token count.
Expand All @@ -557,6 +555,46 @@ func ExampleGenerativeModel_CountTokens_imageUploadFile() {

}

func ExampleGenerativeModel_CountTokens_videoUploadFile() {
ctx := context.Background()
client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
if err != nil {
log.Fatal(err)
}
defer client.Close()

model := client.GenerativeModel("gemini-1.5-flash")
prompt := "Tell me about this video"
file, err := uploadFile(ctx, client, filepath.Join(testDataDir, "earth.mp4"))
if err != nil {
log.Fatal(err)
}
defer client.DeleteFile(ctx, file.Name)

fd := genai.FileData{URI: file.URI}
// Call `CountTokens` to get the input token count
// of the combined text and file (`total_tokens`).
// An image's display or file size does not affect its token count.
// Optionally, you can call `count_tokens` for the text and file separately.
tokResp, err := model.CountTokens(ctx, genai.Text(prompt), fd)
if err != nil {
log.Fatal(err)
}
fmt.Println("total_tokens:", tokResp.TotalTokens)
// ( total_tokens: 1481 )

resp, err := model.GenerateContent(ctx, genai.Text(prompt), fd)
if err != nil {
log.Fatal(err)
}

fmt.Println("prompt_token_count:", resp.UsageMetadata.PromptTokenCount)
fmt.Println("candidates_token_count:", resp.UsageMetadata.CandidatesTokenCount)
fmt.Println("total_token_count:", resp.UsageMetadata.TotalTokenCount)
// ( prompt_token_count: 1481, candidates_token_count: 43, total_token_count: 1524 )

}

func ExampleGenerativeModel_CountTokens_chat() {
ctx := context.Background()
client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
Expand Down
46 changes: 43 additions & 3 deletions genai/internal/samples/docs-snippets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -544,9 +544,7 @@ func ExampleGenerativeModel_CountTokens_imageUploadFile() {
}
defer client.DeleteFile(ctx, file.Name)

fd := genai.FileData{
URI: file.URI,
}
fd := genai.FileData{URI: file.URI}
// Call `CountTokens` to get the input token count
// of the combined text and file (`total_tokens`).
// An image's display or file size does not affect its token count.
Expand All @@ -570,6 +568,48 @@ func ExampleGenerativeModel_CountTokens_imageUploadFile() {
// [END tokens_multimodal_image_file_api]
}

func ExampleGenerativeModel_CountTokens_videoUploadFile() {
ctx := context.Background()
client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
if err != nil {
log.Fatal(err)
}
defer client.Close()

// [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"))
if err != nil {
log.Fatal(err)
}
defer client.DeleteFile(ctx, file.Name)

fd := genai.FileData{URI: file.URI}
// Call `CountTokens` to get the input token count
// of the combined text and file (`total_tokens`).
// An image's display or file size does not affect its token count.
// Optionally, you can call `count_tokens` for the text and file separately.
tokResp, err := model.CountTokens(ctx, genai.Text(prompt), fd)
if err != nil {
log.Fatal(err)
}
fmt.Println("total_tokens:", tokResp.TotalTokens)
// ( total_tokens: 1481 )

resp, err := model.GenerateContent(ctx, genai.Text(prompt), fd)
if err != nil {
log.Fatal(err)
}

fmt.Println("prompt_token_count:", resp.UsageMetadata.PromptTokenCount)
fmt.Println("candidates_token_count:", resp.UsageMetadata.CandidatesTokenCount)
fmt.Println("total_token_count:", resp.UsageMetadata.TotalTokenCount)
// ( prompt_token_count: 1481, candidates_token_count: 43, total_token_count: 1524 )

// [END tokens_multimodal_video_audio_file_api]
}

func ExampleGenerativeModel_CountTokens_chat() {
ctx := context.Background()
client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
Expand Down

0 comments on commit 0fdd360

Please sign in to comment.