|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +// |
| 15 | +// SPDX-License-Identifier: Apache-2.0 |
| 16 | + |
| 17 | +// This sample shows how to upload a file to Gemini Files API and use it directly with Genkit. |
| 18 | +// |
| 19 | +// Usage: |
| 20 | +// 1. Set GEMINI_API_KEY environment variable |
| 21 | +// 2. Run: go run main.go |
| 22 | + |
| 23 | +package main |
| 24 | + |
| 25 | +import ( |
| 26 | + "context" |
| 27 | + "fmt" |
| 28 | + "log" |
| 29 | + "os" |
| 30 | + |
| 31 | + "github.com/firebase/genkit/go/ai" |
| 32 | + "github.com/firebase/genkit/go/genkit" |
| 33 | + "github.com/firebase/genkit/go/plugins/googlegenai" |
| 34 | + "google.golang.org/genai" |
| 35 | +) |
| 36 | + |
| 37 | +func main() { |
| 38 | + ctx := context.Background() |
| 39 | + |
| 40 | + // Initialize Genkit |
| 41 | + g, err := genkit.Init(ctx, genkit.WithPlugins(&googlegenai.GoogleAI{})) |
| 42 | + if err != nil { |
| 43 | + log.Fatal("Failed to initialize Genkit:", err) |
| 44 | + } |
| 45 | + |
| 46 | + // Create Files API client |
| 47 | + client, err := genai.NewClient(ctx, &genai.ClientConfig{ |
| 48 | + Backend: genai.BackendGeminiAPI, |
| 49 | + APIKey: os.Getenv("GEMINI_API_KEY"), |
| 50 | + }) |
| 51 | + if err != nil { |
| 52 | + log.Fatal("Failed to create client:", err) |
| 53 | + } |
| 54 | + |
| 55 | + // Upload image to Files API |
| 56 | + fmt.Println("Uploading image to Files API...") |
| 57 | + file, err := client.Files.UploadFromPath(ctx, "test.jpg", &genai.UploadFileConfig{ |
| 58 | + MIMEType: "image/jpeg", |
| 59 | + DisplayName: "Test Image", |
| 60 | + }) |
| 61 | + if err != nil { |
| 62 | + log.Fatal("Failed to upload:", err) |
| 63 | + } |
| 64 | + fmt.Printf("Uploaded! File URI: %s\n", file.URI) |
| 65 | + |
| 66 | + // Use Files API URI directly with Genkit (now supported!) |
| 67 | + fmt.Println("Analyzing image with Genkit using Files API URI...") |
| 68 | + resp, err := genkit.Generate(ctx, g, |
| 69 | + ai.WithModelName("googleai/gemini-2.0-flash"), |
| 70 | + ai.WithMessages( |
| 71 | + ai.NewUserMessage( |
| 72 | + ai.NewTextPart("What do you see in this image?"), |
| 73 | + ai.NewMediaPart("image/jpeg", file.URI), |
| 74 | + ), |
| 75 | + ), |
| 76 | + ) |
| 77 | + if err != nil { |
| 78 | + log.Fatal("Failed to analyze:", err) |
| 79 | + } |
| 80 | + |
| 81 | + fmt.Printf("Analysis: %s\n", resp.Text()) |
| 82 | + |
| 83 | + // Clean up |
| 84 | + client.Files.Delete(ctx, file.Name, nil) |
| 85 | + fmt.Println("Cleaned up uploaded file") |
| 86 | +} |
0 commit comments