Skip to content

Commit

Permalink
genai: add additional samples (#206)
Browse files Browse the repository at this point in the history
  • Loading branch information
eliben authored Aug 6, 2024
1 parent 7d222a0 commit 3b22eee
Show file tree
Hide file tree
Showing 2 changed files with 262 additions and 0 deletions.
129 changes: 129 additions & 0 deletions genai/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,19 @@ func ExampleGenerativeModel_GenerateContent_videoPrompt() {
}
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)
}

resp, err := model.GenerateContent(ctx,
genai.Text("Describe this video clip"),
genai.FileData{URI: file.URI})
Expand All @@ -146,6 +159,33 @@ func ExampleGenerativeModel_GenerateContent_videoPrompt() {

}

func ExampleGenerativeModel_GenerateContent_pdfPrompt() {
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")

file, err := uploadFile(ctx, client, filepath.Join(testDataDir, "test.pdf"), "")
if err != nil {
log.Fatal(err)
}
defer client.DeleteFile(ctx, file.Name)

resp, err := model.GenerateContent(ctx,
genai.Text("Give me a summary of this document:"),
genai.FileData{URI: file.URI})
if err != nil {
log.Fatal(err)
}

printResponse(resp)

}

func ExampleGenerativeModel_GenerateContent_multiImagePrompt() {
ctx := context.Background()
client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
Expand Down Expand Up @@ -378,6 +418,70 @@ func ExampleGenerativeModel_GenerateContentStream_videoPrompt() {

}

func ExampleGenerativeModel_GenerateContentStream_audioPrompt() {
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")

file, err := uploadFile(ctx, client, filepath.Join(testDataDir, "sample.mp3"), "")
if err != nil {
log.Fatal(err)
}
defer client.DeleteFile(ctx, file.Name)

iter := model.GenerateContentStream(ctx,
genai.Text("Give me a summary of this audio file."),
genai.FileData{URI: file.URI})
for {
resp, err := iter.Next()
if err == iterator.Done {
break
}
if err != nil {
log.Fatal(err)
}
printResponse(resp)
}

}

func ExampleGenerativeModel_GenerateContentStream_pdfPrompt() {
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")

file, err := uploadFile(ctx, client, filepath.Join(testDataDir, "test.pdf"), "")
if err != nil {
log.Fatal(err)
}
defer client.DeleteFile(ctx, file.Name)

iter := model.GenerateContentStream(ctx,
genai.Text("Give me a summary of this document:"),
genai.FileData{URI: file.URI})
for {
resp, err := iter.Next()
if err == iterator.Done {
break
}
if err != nil {
log.Fatal(err)
}
printResponse(resp)
}

}

func ExampleGenerativeModel_CountTokens_contextWindow() {
ctx := context.Background()
client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
Expand Down Expand Up @@ -596,6 +700,31 @@ func ExampleGenerativeModel_CountTokens_imageUploadFile() {

}

func ExampleGenerativeModel_CountTokens_pdfUploadFile() {
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 := "Give me a summary of this document."
file, err := client.UploadFileFromPath(ctx, filepath.Join(testDataDir, "test.pdf"), nil)
if err != nil {
log.Fatal(err)
}
defer client.DeleteFile(ctx, file.Name)

fd := genai.FileData{URI: file.URI}
resp, err := model.GenerateContent(ctx, genai.Text(prompt), fd)
if err != nil {
log.Fatal(err)
}
fmt.Println(resp.UsageMetadata)

}

func ExampleGenerativeModel_CountTokens_videoUploadFile() {
ctx := context.Background()
client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
Expand Down
133 changes: 133 additions & 0 deletions genai/internal/samples/docs-snippets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,19 @@ func ExampleGenerativeModel_GenerateContent_videoPrompt() {
}
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)
}

resp, err := model.GenerateContent(ctx,
genai.Text("Describe this video clip"),
genai.FileData{URI: file.URI})
Expand All @@ -149,6 +162,34 @@ func ExampleGenerativeModel_GenerateContent_videoPrompt() {
// [END text_gen_multimodal_video_prompt]
}

func ExampleGenerativeModel_GenerateContent_pdfPrompt() {
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 text_gen_multimodal_pdf]
model := client.GenerativeModel("gemini-1.5-flash")

file, err := uploadFile(ctx, client, filepath.Join(testDataDir, "test.pdf"), "")
if err != nil {
log.Fatal(err)
}
defer client.DeleteFile(ctx, file.Name)

resp, err := model.GenerateContent(ctx,
genai.Text("Give me a summary of this document:"),
genai.FileData{URI: file.URI})
if err != nil {
log.Fatal(err)
}

printResponse(resp)
// [END text_gen_multimodal_pdf_prompt]
}

func ExampleGenerativeModel_GenerateContent_multiImagePrompt() {
ctx := context.Background()
client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
Expand Down Expand Up @@ -389,6 +430,72 @@ func ExampleGenerativeModel_GenerateContentStream_videoPrompt() {
// [END text_gen_multimodal_video_prompt_streaming]
}

func ExampleGenerativeModel_GenerateContentStream_audioPrompt() {
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 text_gen_multimodal_audio_streaming]
model := client.GenerativeModel("gemini-1.5-flash")

file, err := uploadFile(ctx, client, filepath.Join(testDataDir, "sample.mp3"), "")
if err != nil {
log.Fatal(err)
}
defer client.DeleteFile(ctx, file.Name)

iter := model.GenerateContentStream(ctx,
genai.Text("Give me a summary of this audio file."),
genai.FileData{URI: file.URI})
for {
resp, err := iter.Next()
if err == iterator.Done {
break
}
if err != nil {
log.Fatal(err)
}
printResponse(resp)
}
// [END text_gen_multimodal_audio_streaming]
}

func ExampleGenerativeModel_GenerateContentStream_pdfPrompt() {
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 text_gen_multimodal_pdf_streaming]
model := client.GenerativeModel("gemini-1.5-flash")

file, err := uploadFile(ctx, client, filepath.Join(testDataDir, "test.pdf"), "")
if err != nil {
log.Fatal(err)
}
defer client.DeleteFile(ctx, file.Name)

iter := model.GenerateContentStream(ctx,
genai.Text("Give me a summary of this document:"),
genai.FileData{URI: file.URI})
for {
resp, err := iter.Next()
if err == iterator.Done {
break
}
if err != nil {
log.Fatal(err)
}
printResponse(resp)
}
// [END text_gen_multimodal_pdf_streaming]
}

func ExampleGenerativeModel_CountTokens_contextWindow() {
ctx := context.Background()
client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
Expand Down Expand Up @@ -614,6 +721,32 @@ func ExampleGenerativeModel_CountTokens_imageUploadFile() {
// [END tokens_multimodal_image_file_api]
}

func ExampleGenerativeModel_CountTokens_pdfUploadFile() {
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_pdf_file_api]
model := client.GenerativeModel("gemini-1.5-flash")
prompt := "Give me a summary of this document."
file, err := client.UploadFileFromPath(ctx, filepath.Join(testDataDir, "test.pdf"), nil)
if err != nil {
log.Fatal(err)
}
defer client.DeleteFile(ctx, file.Name)

fd := genai.FileData{URI: file.URI}
resp, err := model.GenerateContent(ctx, genai.Text(prompt), fd)
if err != nil {
log.Fatal(err)
}
fmt.Println(resp.UsageMetadata)
// [END tokens_multimodal_pdf_file_api]
}

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

0 comments on commit 3b22eee

Please sign in to comment.