Skip to content

Commit

Permalink
genai: add basic PDF uploading example (#202)
Browse files Browse the repository at this point in the history
  • Loading branch information
eliben authored Aug 2, 2024
1 parent 361f1ce commit fb56fc2
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
26 changes: 26 additions & 0 deletions genai/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1119,6 +1119,32 @@ func ExampleClient_UploadFile_image() {

}

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

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

model := client.GenerativeModel("gemini-1.5-flash")
resp, err := model.GenerateContent(ctx,
genai.Text("Give me a summary of this pdf file."),
genai.FileData{URI: file.URI})
if err != nil {
log.Fatal(err)
}

printResponse(resp)

}

func ExampleClient_UploadFile_video() {
ctx := context.Background()
client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
Expand Down
27 changes: 27 additions & 0 deletions genai/internal/samples/docs-snippets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1150,6 +1150,33 @@ func ExampleClient_UploadFile_image() {
// [END files_create_image]
}

func ExampleClient_UploadFile_pdf() {
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 files_create_pdf]
file, err := uploadFile(ctx, client, filepath.Join(testDataDir, "test.pdf"), "")
if err != nil {
log.Fatal(err)
}
defer client.DeleteFile(ctx, file.Name)

model := client.GenerativeModel("gemini-1.5-flash")
resp, err := model.GenerateContent(ctx,
genai.Text("Give me a summary of this pdf file."),
genai.FileData{URI: file.URI})
if err != nil {
log.Fatal(err)
}

printResponse(resp)
// [END files_create_pdf]
}

func ExampleClient_UploadFile_video() {
ctx := context.Background()
client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
Expand Down
Binary file added genai/testdata/test.pdf
Binary file not shown.

0 comments on commit fb56fc2

Please sign in to comment.