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: update samples with explicit file upload for video, and comments #200

Merged
merged 1 commit into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
35 changes: 32 additions & 3 deletions genai/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -682,15 +691,16 @@ 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)
}
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 {
Expand Down Expand Up @@ -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},
Expand Down
35 changes: 32 additions & 3 deletions genai/internal/samples/docs-snippets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -704,15 +713,16 @@ 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)
}
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 {
Expand Down Expand Up @@ -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},
Expand Down
Loading