|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + |
| 9 | + "google.golang.org/genai" |
| 10 | +) |
| 11 | + |
| 12 | +func handleErr(err error) { |
| 13 | + if err != nil { |
| 14 | + fmt.Println("ERROR:", err) |
| 15 | + os.Exit(1) |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +func main() { |
| 20 | + ctx := context.Background() |
| 21 | + project := os.Getenv("GOOGLE_CLOUD_PROJECT") |
| 22 | + location := os.Getenv("GOOGLE_CLOUD_LOCATION") |
| 23 | + |
| 24 | + client, err := genai.NewClient(ctx, &genai.ClientConfig{ |
| 25 | + Project: project, |
| 26 | + Location: location, |
| 27 | + Backend: genai.BackendVertexAI, |
| 28 | + }) |
| 29 | + handleErr(err) |
| 30 | + |
| 31 | + for result, err := range client.Models.GenerateContentStream( |
| 32 | + ctx, |
| 33 | + "gemini-3-flash-preview", |
| 34 | + genai.Text("Why is the sky blue?"), |
| 35 | + &genai.GenerateContentConfig{ |
| 36 | + MaxOutputTokens: 512, |
| 37 | + }, |
| 38 | + ) { |
| 39 | + if err != nil { |
| 40 | + handleErr(err) |
| 41 | + } |
| 42 | + fmt.Print(result.Candidates[0].Content.Parts[0].Text) |
| 43 | + } |
| 44 | + |
| 45 | + // // Call the GenerateContent method. |
| 46 | + // result, err := client.Models.GenerateContent(ctx, |
| 47 | + // "gemini-2.5-flash", |
| 48 | + // genai.Text("Tell me about New York?"), |
| 49 | + // &genai.GenerateContentConfig{ |
| 50 | + // Temperature: genai.Ptr[float32](0.5), |
| 51 | + // TopP: genai.Ptr[float32](0.5), |
| 52 | + // TopK: genai.Ptr[float32](2.0), |
| 53 | + // ResponseMIMEType: "application/json", |
| 54 | + // StopSequences: []string{"\n"}, |
| 55 | + // CandidateCount: 2, |
| 56 | + // Seed: genai.Ptr[int32](42), |
| 57 | + // MaxOutputTokens: 128, |
| 58 | + // PresencePenalty: genai.Ptr[float32](0.5), |
| 59 | + // FrequencyPenalty: genai.Ptr[float32](0.5), |
| 60 | + // }, |
| 61 | + // ) |
| 62 | + // handleErr(err) |
| 63 | + // debugPrint(result) |
| 64 | + |
| 65 | + // ms := client.Models.All(ctx) |
| 66 | + // for m := range ms { |
| 67 | + // fmt.Println(m.Name, m.Version) |
| 68 | + // } |
| 69 | + |
| 70 | +} |
| 71 | + |
| 72 | +func debugPrint[T any](r *T) { |
| 73 | + response, err := json.MarshalIndent(*r, "", " ") |
| 74 | + handleErr(err) |
| 75 | + fmt.Println(string(response)) |
| 76 | +} |
0 commit comments