### Install and Run Go-GenAI Example Source: https://github.com/charmbracelet/go-genai/blob/main/examples/caches/README.md Commands to download the Go-GenAI package, navigate to the example directory, and run the specific example file using Go. Depends on Go being installed and environment variables set previously. Executes the example application with no direct inputs or outputs specified. Limitations include requiring internet access for downloading and proper Go module setup. ```bash $ go get google.golang.org/genai $ cd `go list -f '{{.Dir}}' google.golang.org/genai/examples/caches` $ go run create_get_delete.go ``` -------------------------------- ### Download, Build, and Run go-genai Example Source: https://github.com/charmbracelet/go-genai/blob/main/examples/chats/README.md Commands to fetch the go-genai library, navigate to the example chat directory, and execute the chat application. This assumes the Go environment is set up correctly. ```bash $ go get google.golang.org/genai $ cd `go list -f '{{.Dir}}' google.golang.org/genai/examples/chats` $ go run chats.go ``` -------------------------------- ### Building and Running the GenAI File Example in Go Source: https://github.com/charmbracelet/go-genai/blob/main/examples/files/README.md These shell commands fetch the Google GenAI Go package, change to the examples directory, and execute the list_download.go program. Requires Go installed and environment variables set from prior steps; inputs are API configurations, outputs include listed and downloaded files via GenAI. Limitations: Assumes network access and valid API setup. ```bash $ go get google.golang.org/genai $ cd `go list -f '{{.Dir}}' google.golang.org/genai/examples/files` $ go run list_download.go ``` -------------------------------- ### Download, Build, and Run go-genai Example (count_tokens) Source: https://github.com/charmbracelet/go-genai/blob/main/examples/models/count_tokens/README.md Provides the necessary commands to fetch the go-genai library, navigate to the count_tokens example directory, and execute it. It also shows how to optionally specify a model. ```bash $ go get google.golang.org/genai $ cd `go list -f '{{.Dir}}' google.golang.org/genai/examples/models/count_tokens` $ go run text_tokens.go // You can also specify --model arg // go run text_tokens.go --model=gemini-2.0-flash ``` -------------------------------- ### Build and run the go-genai example using Go commands Source: https://github.com/charmbracelet/go-genai/blob/main/examples/models/resource_methods/README.md the GenAI Go SDK, change to the example directory, and execute the sample program. The example can be run with the default model or a specific model by passing the --model flag. No additional code changes are required. ```bash go get google.golang.org/genai cd `go list -f '{{.Dir}}' google.golang.org/genai/examples/models/resource_methods` go run list.go # Optionally specify a model # go run list.go --model=gemini-2.0-flash ``` -------------------------------- ### Set GeminiAPI Backend Environment Variables Source: https://github.com/charmbracelet/go-genai/blob/main/examples/chats/README.md Configures the environment for using the GeminiAPI backend with go-genai. Requires an API key for authentication. This setup is used when interacting with the public Gemini API. ```bash export GOOGLE_GENAI_USE_VERTEXAI=false export GOOGLE_API_KEY={YOUR_API_KEY} ``` -------------------------------- ### Configure environment variables for go-genai backend Source: https://github.com/charmbracelet/go-genai/blob/main/examples/models/resource_methods/README.md Export the necessary environment variables to select either the VertexAI or GeminiAPI backend. Adjust the placeholders with your Google Cloud project ID, location, or API key as required. These settings control which AI service the example will use. ```bash export GOOGLE_GENAI_USE_VERTEXAI=true export GOOGLE_CLOUD_PROJECT={YOUR_PROJECT_ID} export GOOGLE_CLOUD_LOCATION={YOUR_LOCATION} ``` ```bash export GOOGLE_GENAI_USE_VERTEXAI=false export GOOGLE_API_KEY={YOUR_API_KEY} ``` -------------------------------- ### Set VertexAI Backend Environment Variables Source: https://github.com/charmbracelet/go-genai/blob/main/examples/chats/README.md Configures the environment for using the VertexAI backend with go-genai. Requires setting the project ID and location for Google Cloud. These variables are essential for the application to authenticate and connect to VertexAI services. ```bash export GOOGLE_GENAI_USE_VERTEXAI=true export GOOGLE_CLOUD_PROJECT={YOUR_PROJECT_ID} export GOOGLE_CLOUD_LOCATION={YOUR_LOCATION} ``` -------------------------------- ### genai.NewClient Source: https://context7.com/charmbracelet/go-genai/llms.txt Initializes a new GenAI client for either the Gemini Developer API or Vertex AI backend. Configuration can be provided directly or via environment variables for automatic setup. The client manages authentication and backend communication transparently. ```APIDOC ## genai.NewClient ### Description The NewClient function creates a new GenAI client instance configured for a specific backend, supporting both development (Gemini API) and production (Vertex AI) environments. ### Method go func NewClient(ctx context.Context, config *genai.ClientConfig) (*genai.Client, error) ### Parameters #### ctx - **context.Context** (context) - Required - Context for the operation. #### config - **genai.ClientConfig** (struct) - Required - Configuration struct with fields like APIKey, Project, Location, and Backend. ### Configuration Options #### APIKey - **string** - Required for Gemini API - Optional for Vertex AI - Google API key for authentication. #### Project - **string** - Required for Vertex AI - Optional for Gemini - Google Cloud project ID. #### Location - **string** - Optional - Default: "us-central1" - Region for Vertex AI. #### Backend - **genai.BackendType** - Optional - Specifies genai.BackendGeminiAPI or genai.BackendVertexAI; defaults from environment. ### Environment Variables - **GOOGLE_API_KEY** - API key for Gemini. - **GOOGLE_GENAI_USE_VERTEXAI** - Boolean to enable Vertex AI. - **GOOGLE_CLOUD_PROJECT** - Project ID for Vertex AI. - **GOOGLE_CLOUD_LOCATION** - Location for Vertex AI. ### Request Example ```go ctx := context.Background() // For Gemini API client, err := genai.NewClient(ctx, &genai.ClientConfig{ APIKey: "your-api-key", Backend: genai.BackendGeminiAPI, }) if err != nil { log.Fatalf("failed to create client: %v", err) } defer client.Close() // For Vertex AI client, err = genai.NewClient(ctx, &genai.ClientConfig{ Project: "your-project-id", Location: "us-central1", Backend: genai.BackendVertexAI, }) // Auto-config from env client, err = genai.NewClient(ctx, &genai.ClientConfig{}) ``` ### Response #### Success Response - **genai.Client** (*) - The initialized client instance. - **error** (error) - Nil if successful; otherwise, an error. #### Response Example ```go log.Printf("Connected to %s backend", client.ClientConfig().Backend) ``` ``` -------------------------------- ### Set system instructions for AI behavior in Go Source: https://context7.com/charmbracelet/go-genai/llms.txt Shows how to define persistent model behavior and personality through system instructions. Requires an API key. Outputs technical responses with code examples based on the defined assistant personality. ```go package main import ( "context" "fmt" "log" "google.golang.org/genai" ) func main() { ctx := context.Background() client, err := genai.NewClient(ctx, &genai.ClientConfig{ APIKey: "your-api-key", Backend: genai.BackendGeminiAPI, }) if err != nil { log.Fatal(err) } config := &genai.GenerateContentConfig{ SystemInstruction: &genai.Content{ Parts: []*genai.Part{{ Text: "You are a helpful AI assistant specializing in software engineering. " + "Always provide code examples when relevant and explain technical concepts clearly.", }}, }, } result, err := client.Models.GenerateContent( ctx, "gemini-2.0-flash", genai.Text("How do I implement a binary search tree in Go?"), config, ) if err != nil { log.Fatal(err) } fmt.Println(result.Candidates[0].Content.Parts[0].Text) } ``` -------------------------------- ### Initialize Chat with History in Go Source: https://context7.com/charmbracelet/go-genai/llms.txt This example shows how to initialize a chat session with pre-existing conversation history. This is useful for continuing conversations from a previous state. Requires go-genai library and API Key. ```go package main import ( "context" "fmt" "log" "google.golang.org/genai" ) func main() { ctx := context.Background() client, err := genai.NewClient(ctx, &genai.ClientConfig{ APIKey: "your-api-key", Backend: genai.BackendGeminiAPI, }) if err != nil { log.Fatal(err) } // Pre-existing conversation history history := []*genai.Content{ { Role: genai.RoleUser, Parts: []*genai.Part{{Text: "I'm planning a trip to Japan"}}, }, { Role: genai.RoleModel, Parts: []*genai.Part{{Text: "That sounds exciting! Japan has amazing culture, food, and sights. What would you like to know?"}}, }, } chat, err := client.Chats.Create(ctx, "gemini-2.0-flash", nil, history) if err != nil { log.Fatal(err) } // Continue the conversation result, err := chat.SendMessage(ctx, genai.Part{Text: "What's the best time to visit for cherry blossoms?"}) if err != nil { log.Fatal(err) } fmt.Println(result.Candidates[0].Content.Parts[0].Text) } ``` -------------------------------- ### Generate simple text with Gemini model in Go Source: https://context7.com/charmbracelet/go-genai/llms.txt Sends a text prompt to Gemini model and prints the generated response along with token usage. Includes client setup, request execution, and basic error handling. ```go package main import ( "context" "fmt" "log" "google.golang.org/genai" ) func main() { ctx := context.Background() client, err := genai.NewClient(ctx, &genai.ClientConfig{ APIKey: "your-api-key", Backend: genai.BackendGeminiAPI, }) if err != nil { log.Fatal(err) } result, err := client.Models.GenerateContent( ctx, "gemini-2.0-flash", genai.Text("Explain quantum computing in one paragraph"), nil, ) if err != nil { log.Fatal(err) } if len(result.Candidates) > 0 && len(result.Candidates[0].Content.Parts) > 0 { fmt.Println(result.Candidates[0].Content.Parts[0].Text) fmt.Printf("\nTokens used: %d\n", result.UsageMetadata.TotalTokenCount) } } ``` -------------------------------- ### List and Delete Uploaded Files - Go Source: https://context7.com/charmbracelet/go-genai/llms.txt Shows how to manage uploaded files by listing them with pagination and deleting old files. Includes error handling for deletion operations. Uses the same client configuration as file upload examples and requires appropriate permissions for file management operations. ```go package main import ( "context" "fmt" "log" "google.golang.org/genai" ) func main() { ctx := context.Background() client, err := genai.NewClient(ctx, &genai.ClientConfig{ APIKey: "your-api-key", Backend: genai.BackendGeminiAPI, }) if err != nil { log.Fatal(err) } // List all files config := &genai.ListFilesConfig{PageSize: 10} files, err := client.Files.List(ctx, config) if err != nil { log.Fatal(err) } fmt.Printf("Found %d files:\n", len(files.Files)) for _, file := range files.Files { fmt.Printf(" - %s (%s, %d bytes)\n", file.Name, file.MimeType, file.SizeBytes) // Delete old files _, err := client.Files.Delete(ctx, file.Name, nil) if err != nil { log.Printf("Failed to delete %s: %v", file.Name, err) } else { fmt.Printf(" Deleted successfully\n") } } } ``` -------------------------------- ### Create Vertex AI Client - Go Source: https://github.com/charmbracelet/go-genai/blob/main/README.md Shows how to create a client configured to use Vertex AI backend. Requires Google Cloud project ID and location, enabling integration with Vertex AI's generative AI services. ```go client, err := genai.NewClient(ctx, &genai.ClientConfig{ Project: project, Location: location, Backend: genai.BackendVertexAI, }) ``` -------------------------------- ### Create Vertex AI client in Go Source: https://context7.com/charmbracelet/go-genai/llms.txt Initializes a GenAI client for Vertex AI using a project ID and location, enabling production deployments. Includes error handling and confirms successful initialization. ```go package main import ( "context" "log" "google.golang.org/genai" ) func main() { ctx := context.Background() client, err := genai.NewClient(ctx, &genai.ClientConfig{ Project: "your-project-id", Location: "us-central1", Backend: genai.BackendVertexAI, }) if err != nil { log.Fatalf("failed to create client: %v", err) } log.Println("Vertex AI client initialized successfully") } ``` -------------------------------- ### Create Client with Environment Variables - Go Source: https://github.com/charmbracelet/go-genai/blob/main/README.md Demonstrates how to create a client using environment variables for configuration. The SDK will automatically detect and use the appropriate environment variables set for either Gemini API or Vertex AI. ```go client, err := genai.NewClient(ctx, &genai.ClientConfig{}) ``` -------------------------------- ### Configure Environment Variables - Bash Source: https://github.com/charmbracelet/go-genai/blob/main/README.md Provides bash commands for setting up environment variables required by the SDK. Shows configuration for both Gemini Developer API (API key) and Vertex AI (project and location settings). ```bash export GOOGLE_API_KEY='your-api-key' ``` ```bash export GOOGLE_GENAI_USE_VERTEXAI=true export GOOGLE_CLOUD_PROJECT='your-project-id' export GOOGLE_CLOUD_LOCATION='us-central1' ``` -------------------------------- ### Create Gemini API Client - Go Source: https://github.com/charmbracelet/go-genai/blob/main/README.md Demonstrates how to create a client configured to use the Gemini Developer API. Requires an API key and specifies the Gemini API backend for making requests to Google's generative AI models. ```go client, err := genai.NewClient(ctx, &genai.ClientConfig{ APIKey: apiKey, Backend: genai.BackendGeminiAPI, }) ``` -------------------------------- ### Implement Prompt Caching for Cost Optimization - Go Source: https://context7.com/charmbracelet/go-genai/llms.txt Demonstrates creating and managing cached content to reduce latency and costs for repeated queries. Shows cache creation with TTL, using cached content in generation, updating expiration, and cleanup. Requires Vertex AI backend configuration with project ID and location. ```go package main import ( "context" "fmt" "log" "time" "google.golang.org/genai" ) func main() { ctx := context.Background() client, err := genai.NewClient(ctx, &genai.ClientConfig{ Project: "your-project-id", Location: "us-central1", Backend: genai.BackendVertexAI, }) if err != nil { log.Fatal(err) } // Create cached content (e.g., large document) cachedContent, err := client.Caches.Create(ctx, "gemini-1.5-pro-002", &genai.CreateCachedContentConfig{ TTL: 24 * time.Hour, Contents: []*genai.Content{ { Role: genai.RoleUser, Parts: []*genai.Part{ {FileData: &genai.FileData{ MIMEType: "application/pdf", FileURI: "gs://cloud-samples-data/generative-ai/pdf/2312.11805v3.pdf", }}, }, }, }, }) if err != nil { log.Fatal(err) } fmt.Printf("Cache created: %s\n", cachedContent.Name) fmt.Printf("Expires: %s\n", cachedContent.ExpireTime) // Use cached content in generation config := &genai.GenerateContentConfig{ CachedContent: cachedContent.Name, } result, err := client.Models.GenerateContent( ctx, "gemini-1.5-pro-002", genai.Text("Summarize the key findings from this paper"), config, ) if err != nil { log.Fatal(err) } fmt.Println("\nSummary:", result.Candidates[0].Content.Parts[0].Text) // Update cache expiration _, err = client.Caches.Update(ctx, cachedContent.Name, &genai.UpdateCachedContentConfig{ ExpireTime: time.Now().Add(48 * time.Hour), }) if err != nil { log.Fatal(err) } // Clean up _, err = client.Caches.Delete(ctx, cachedContent.Name, nil) if err != nil { log.Fatal(err) } fmt.Println("Cache deleted") } ``` -------------------------------- ### Configure GenAI client via environment variables (Go & Bash) Source: https://context7.com/charmbracelet/go-genai/llms.txt Sets environment variables for both Gemini API and Vertex AI, then initializes a Go client that automatically reads those variables. This avoids hard‑coding credentials and simplifies deployment across environments. ```bash # For Gemini API export GOOGLE_API_KEY='your-api-key' # For Vertex AI export GOOGLE_GENAI_USE_VERTEXAI=true export GOOGLE_CLOUD_PROJECT='your-project-id' export GOOGLE_CLOUD_LOCATION='us-central1' ``` ```go package main import ( "context" "log" "google.golang.org/genai" ) func main() { ctx := context.Background() // Client configured from environment variables client, err := genai.NewClient(ctx, &genai.ClientConfig{}) if err != nil { log.Fatalf("client initialization failed: %v", err) } log.Printf("Auto-configured for %s", client.ClientConfig().Backend) } ``` -------------------------------- ### List Available Models in Go Source: https://context7.com/charmbracelet/go-genai/llms.txt This Go code snippet queries and lists all available GenAI models for the specified backend, including their capabilities and limits. It uses the google.golang.org/genai library and requires an API key for authentication. The output displays model names, display names, descriptions, token limits, and supported generation methods for each model. ```go package main import ( "context" "fmt" "log" "google.golang.org/genai" ) func main() { ctx := context.Background() client, err := genai.NewClient(ctx, &genai.ClientConfig{ APIKey: "your-api-key", Backend: genai.BackendGeminiAPI, }) if err != nil { log.Fatal(err) } // List all available models models, err := client.Models.List(ctx, &genai.ListModelsConfig{}) if err != nil { log.Fatal(err) } fmt.Printf("Available models: %d\n\n", len(models.Models)) for _, model := range models.Models { fmt.Printf("Model: %s\n", model.Name) fmt.Printf(" Display Name: %s\n", model.DisplayName) fmt.Printf(" Description: %s\n", model.Description) fmt.Printf(" Input token limit: %d\n", model.InputTokenLimit) fmt.Printf(" Output token limit: %d\n", model.OutputTokenLimit) fmt.Printf(" Supported generation methods: %v\n", model.SupportedGenerationMethods) fmt.Println() } } ``` -------------------------------- ### Configure generation parameters in Go Source: https://context7.com/charmbracelet/go-genai/llms.txt Illustrates advanced control over model behavior with temperature, token limits, safety settings, and JSON output format. Requires an API key. Outputs structured JSON data with programming language use cases. ```go package main import ( "context" "encoding/json" "fmt" "log" "google.golang.org/genai" ) func main() { ctx := context.Background() client, err := genai.NewClient(ctx, &genai.ClientConfig{ APIKey: "your-api-key", Backend: genai.BackendGeminiAPI, }) if err != nil { log.Fatal(err) } config := &genai.GenerateContentConfig{ Temperature: genai.Ptr[float32](0.7), TopP: genai.Ptr[float32](0.9), TopK: genai.Ptr[float32](40.0), MaxOutputTokens: 2048, ResponseMIMEType: "application/json", StopSequences: []string{"END"}, CandidateCount: 1, Seed: genai.Ptr[int32](42), PresencePenalty: genai.Ptr[float32](0.5), FrequencyPenalty: genai.Ptr[float32](0.5), } result, err := client.Models.GenerateContent( ctx, "gemini-2.0-flash", genai.Text("List 3 programming languages with their main use case"), config, ) if err != nil { log.Fatal(err) } var data interface{} jsonText := result.Candidates[0].Content.Parts[0].Text if err := json.Unmarshal([]byte(jsonText), &data); err != nil { log.Fatal(err) } formatted, _ := json.MarshalIndent(data, "", " ") fmt.Println(string(formatted)) } ``` -------------------------------- ### JavaScript WebSocket Initialization and Event Handling Source: https://github.com/charmbracelet/go-genai/blob/main/examples/live/live_streaming.html JavaScript code for initializing and managing a WebSocket connection within the go-genai project. It sets up event listeners for 'load', 'open', 'close', and 'message' events. The 'load' event prepares UI elements and initiates the WebSocket connection. The 'message' event handler parses incoming JSON data from the server, specifically checking for 'serverContent' and processing 'turnComplete' status, including handling sent audio chunks. ```javascript var audioDebug; (function () { var outputDiv; var inputDiv; var ws; var isRecording = false; var audioChunksReceived = []; var audioChunksSent = []; var chatHistory; var processor; // Audio processor. var inputTimer; // Timer for input button. var shareScreenTimer; // Timer for Share Screen button. var videoTimer = null; // Timer for Play Video button. var video; var videoCanvas = document.createElement('canvas'); var videoCtx = videoCanvas.getContext('2d'); var sampleRate = 24000; const audio = new Audio(); var audioQueue = []; var isAudioPlaying = false; function openWs() { if (ws) { return false; } ws = new WebSocket('{{.}}'); ws.onopen = function (evt) { print('OPEN'); } ws.onclose = function (evt) { print('CLOSE'); ws = null; } ws.onmessage = function (evt) { data = JSON.parse(evt.data); if (!data.serverContent) return; if (data.serverContent.turnComplete) { if (audioChunksSent.length > 0) { console.log(audioChunksSent.length); printChatAudio(encodeAudio(audioChunksSent, sampleRate ``` -------------------------------- ### Create Gemini API client in Go Source: https://context7.com/charmbracelet/go-genai/llms.txt Initializes a GenAI client using a Google API key for the Gemini Developer API. Handles errors and ensures the client is closed after use. Suitable for development environments. ```go package main import ( "context" "log" "google.golang.org/genai" ) func main() { ctx := context.Background() client, err := genai.NewClient(ctx, &genai.ClientConfig{ APIKey: "your-api-key", Backend: genai.BackendGeminiAPI, }) if err != nil { log.Fatalf("failed to create client: %v", err) } defer client.Close() // Client ready to use log.Printf("Connected to %s backend", client.ClientConfig().Backend) } ``` -------------------------------- ### WebSocket Connection and Message Handling in JavaScript Source: https://github.com/charmbracelet/go-genai/blob/main/examples/live/live_streaming.html Establishes a WebSocket connection to a server for real-time AI interaction. Handles incoming messages to process audio responses from the model, queues and plays audio chunks, and manages connection errors. Depends on WebSocket API; inputs are server messages with audio data, outputs audio playback. ```javascript function openWs() { ws = new WebSocket('ws://localhost:8080'); ws.onopen = function (evt) { print('OPEN'); } ws.onmessage = function (evt) { const data = JSON.parse(evt.data); if (data.serverContent && data.serverContent.modelTurn) { // Handle model response if (data.serverContent.modelTurn.parts && data.serverContent.modelTurn.parts[0]) { if (data.serverContent.modelTurn.parts[0].inlineData) { const inlineData = data.serverContent.modelTurn.parts[0].inlineData; print('RECEIVED: ' + typeof (inlineData) + inlineData.mimeType + inlineData.data); if (inlineData.mimeType.startsWith('audio/pcm')) { const audioData = b64ToUint8Array(inlineData.data); audioQueue.push(audioData); audioChunksReceived.push(audioData); playNextChunk(); } } } } } ws.onerror = function (evt) { print('ERROR: ' + evt.data); } } openWs(); document.getElementById('close').onclick = function (evt) { if (!ws) { return false; } ws.close(); return false; }; ``` -------------------------------- ### Analyze images with text prompts using Go Source: https://context7.com/charmbracelet/go-genai/llms.txt Demonstrates how to combine text prompts with inline image data for vision-language tasks using the Google GenAI Go client. Requires an API key and internet access to fetch image data. Outputs detailed image descriptions. ```go package main import ( "context" "fmt" "io" "log" "net/http" "google.golang.org/genai" ) func main() { ctx := context.Background() client, err := genai.NewClient(ctx, &genai.ClientConfig{ APIKey: "your-api-key", Backend: genai.BackendGeminiAPI, }) if err != nil { log.Fatal(err) } // Fetch image data resp, err := http.Get("https://storage.googleapis.com/cloud-samples-data/generative-ai/image/scones.jpg") if err != nil { log.Fatal(err) } defer resp.Body.Close() imageData, err := io.ReadAll(resp.Body) if err != nil { log.Fatal(err) } parts := []*genai.Part{ {Text: "Describe this image in detail. What items do you see?"}, {InlineData: &genai.Blob{Data: imageData, MIMEType: "image/jpeg"}}, } contents := []*genai.Content{{Parts: parts}} result, err := client.Models.GenerateContent(ctx, "gemini-2.0-flash", contents, nil) if err != nil { log.Fatal(err) } fmt.Println(result.Candidates[0].Content.Parts[0].Text) } ``` -------------------------------- ### Upload and Manage Files with Checksum Verification - Go Source: https://context7.com/charmbracelet/go-genai/llms.txt Demonstrates uploading large files to the API for multimodal prompts with persistent storage. Includes file upload, checksum verification for data integrity, and using uploaded files in content generation. Requires google.golang.org/genai package and a valid API key or Vertex AI credentials. ```go package main import ( "context" "crypto/sha256" "encoding/base64" "encoding/hex" "fmt" "io" "log" "os" "google.golang.org/genai" ) func main() { ctx := context.Background() client, err := genai.NewClient(ctx, &genai.ClientConfig{ APIKey: "your-api-key", Backend: genai.BackendGeminiAPI, }) if err != nil { log.Fatal(err) } // Upload a file file, err := client.Files.UploadFromPath(ctx, "/path/to/image.jpg", nil) if err != nil { log.Fatal(err) } fmt.Printf("File uploaded: %s\n", file.Name) fmt.Printf("URI: %s\n", file.URI) fmt.Printf("MIME Type: %s\n", file.MimeType) fmt.Printf("Size: %d bytes\n", file.SizeBytes) // Verify checksum f, _ := os.Open("/path/to/image.jpg") defer f.Close() hasher := sha256.New() io.Copy(hasher, f) localHash := hex.EncodeToString(hasher.Sum(nil)) uploadedHash, _ := base64.StdEncoding.DecodeString(file.Sha256Hash) fmt.Printf("Local hash: %s\n", localHash) fmt.Printf("Uploaded hash: %s\n", hex.EncodeToString(uploadedHash)) // Use uploaded file in generation parts := []*genai.Part{ {Text: "Describe this image"}, {FileData: &genai.FileData{FileURI: file.URI}}, } result, err := client.Models.GenerateContent( ctx, "gemini-2.0-flash", []*genai.Content{{Parts: parts}}, nil, ) if err != nil { log.Fatal(err) } fmt.Println("\nDescription:", result.Candidates[0].Content.Parts[0].Text) } ``` -------------------------------- ### Generate Multimodal Content - Go Source: https://github.com/charmbracelet/go-genai/blob/main/README.md Demonstrates how to generate content using Gemini's multimodal capabilities by combining text and image inputs. Uses the GenerateContent method with Part objects containing both text and inline image data. ```go parts := []*genai.Part{ {Text: "What's this image about?"}, {InlineData: &genai.Blob{Data: imageBytes, MIMEType: "image/jpeg"}}, } result, err := client.Models.GenerateContent(ctx, "gemini-2.0-flash", []*genai.Content{{Parts: parts}}, nil) ``` -------------------------------- ### Audio Recording and Processing in JavaScript Source: https://github.com/charmbracelet/go-genai/blob/main/examples/live/live_streaming.html Captures user audio using MediaDevices API, converts float32 to int16 PCM format, and sends base64-encoded chunks via WebSocket. Requires user media permission; inputs are microphone stream, outputs base64 audio data for transmission. Limitations include browser compatibility for AudioContext. ```javascript function recordStop() { if (processor) { processor.disconnect(); } isRecording = false; document.getElementById('record').textContent = 'Start Recording'; } function recordStart() { recordAudio(); isRecording = true; document.getElementById('record').textContent = 'Stop Recording'; } function recordAudio() { navigator.mediaDevices.getUserMedia({ audio: true }).then(stream => { const audioContext = new AudioContext({ sampleRate: sampleRate }); const source = audioContext.createMediaStreamSource(stream); processor = audioContext.createScriptProcessor(1024, 1, 1); processor.onaudioprocess = (e) => { const inputData = e.inputBuffer.getChannelData(0); const pcmData16 = convertFloat32ToInt16(inputData); if (ws && ws.readyState === WebSocket.OPEN) { audioChunksSent.push(new Uint8Array(pcmData16.buffer)); const base64Data = arrayBufferToBase64(pcmData16.buffer); ws.send(createAudioContent(base64Data)); } }; source.connect(processor); processor.connect(audioContext.destination); }); } function arrayBufferToBase64(buffer) { let binary = ''; const bytes = new Uint8Array(buffer); const len = bytes.byteLength; for (let i = 0; i < len; i++) { binary += String.fromCharCode(bytes[i]); } return btoa(binary); } function convertFloat32ToInt16(float32Array) { const int16Array = new Int16Array(float32Array.length); for (let i = 0; i < float32Array.length; i++) { int16Array[i] = Math.max(-32768, Math.min(32767, float32Array[i] * 32768)); } return int16Array; } document.getElementById('record').onclick = function (evt) { if (isRecording) { recordStop(); } else { recordStart(); } } ``` -------------------------------- ### Reference large files via URI in Go Source: https://context7.com/charmbracelet/go-genai/llms.txt Shows how to process large media files stored in Google Cloud Storage or via HTTP URLs instead of inline data. Requires a Google Cloud project ID and proper permissions. Outputs content analysis of the referenced file. ```go package main import ( "context" "fmt" "log" "google.golang.org/genai" ) func main() { ctx := context.Background() client, err := genai.NewClient(ctx, &genai.ClientConfig{ Project: "your-project-id", Location: "us-central1", Backend: genai.BackendVertexAI, }) if err != nil { log.Fatal(err) } parts := []*genai.Part{ {Text: "Summarize what happens in this video"}, {FileData: &genai.FileData{ FileURI: "gs://cloud-samples-data/video/animals.mp4", MIMEType: "video/mp4", }}, } contents := []*genai.Content{{Parts: parts}} result, err := client.Models.GenerateContent(ctx, "gemini-2.0-flash", contents, nil) if err != nil { log.Fatal(err) } fmt.Println(result.Candidates[0].Content.Parts[0].Text) } ``` -------------------------------- ### Create Stateful Chat Sessions in Go Source: https://context7.com/charmbracelet/go-genai/llms.txt This snippet demonstrates how to create and manage stateful chat sessions using go-genai. It initializes a chat session, sends messages, and accesses conversation history, maintaining context across multiple turns. Requires go-genai library and a valid API key. ```go package main import ( "context" "fmt" "log" "google.golang.org/genai" ) func main() { ctx := context.Background() client, err := genai.NewClient(ctx, &genai.ClientConfig{ APIKey: "your-api-key", Backend: genai.BackendGeminiAPI, }) if err != nil { log.Fatal(err) } // Create a new chat session chat, err := client.Chats.Create(ctx, "gemini-2.0-flash", nil, nil) if err != nil { log.Fatal(err) } // First message result, err := chat.SendMessage(ctx, genai.Part{Text: "What's the capital of France?"}) if err != nil { log.Fatal(err) } fmt.Println("Bot:", result.Candidates[0].Content.Parts[0].Text) // Follow-up message (context maintained) result, err = chat.SendMessage(ctx, genai.Part{Text: "What's the population?"}) if err != nil { log.Fatal(err) } fmt.Println("Bot:", result.Candidates[0].Content.Parts[0].Text) // Access conversation history history := chat.History(true) fmt.Printf("\nTotal conversation turns: %d\n", len(history)) } ``` -------------------------------- ### Streaming Chat Responses in Go Source: https://context7.com/charmbracelet/go-genai/llms.txt This code demonstrates streaming chat responses in real-time using go-genai. It establishes a chat session and sends messages, retrieving the response in chunks to simulate a live conversation. Requires go-genai library and API Key. ```go package main import ( "context" "fmt" "log" "google.golang.org/genai" ) func main() { ctx := context.Background() client, err := genai.NewClient(ctx, &genai.ClientConfig{ APIKey: "your-api-key", Backend: genai.BackendGeminiAPI, }) if err != nil { log.Fatal(err) } chat, err := client.Chats.Create(ctx, "gemini-2.0-flash", nil, nil) if err != nil { log.Fatal(err) } fmt.Print("User: Tell me about Mars\nBot: ") for chunk, err := range chat.SendMessageStream(ctx, genai.Part{Text: "Tell me about Mars"}) { if err != nil { log.Fatal(err) } if len(chunk.Candidates) > 0 && len(chunk.Candidates[0].Content.Parts) > 0 { fmt.Print(chunk.Candidates[0].Content.Parts[0].Text) } } fmt.Println() fmt.Print("\nUser: How far is it from Earth?\nBot: ") for chunk, err := range chat.SendMessageStream(ctx, genai.Part{Text: "How far is it from Earth?"}) { if err != nil { log.Fatal(err) } if len(chunk.Candidates) > 0 && len(chunk.Candidates[0].Content.Parts) > 0 { fmt.Print(chunk.Candidates[0].Content.Parts[0].Text) } } fmt.Println() } ``` -------------------------------- ### Import SDK - Go Source: https://github.com/charmbracelet/go-genai/blob/main/README.md Shows the import statement required to use the Google Gen AI Go SDK in your Go application. This package provides the main interface for interacting with Google's generative AI models. ```go import "google.golang.org/genai" ``` -------------------------------- ### Integrate Google Search Retrieval in Go with Gemini API Source: https://context7.com/charmbracelet/go-genai/llms.txt This Go code enables the Gemini model to perform real-time Google searches for up-to-date information by configuring search retrieval tools. It depends on the google.golang.org/genai package and an API key, accepting query prompts and outputting search-augmented text with grounding metadata. Note that it may incur search costs and requires network access for retrieval. ```go package main import ( "context" "fmt" "log" "google.golang.org/genai" ) func main() { ctx := context.Background() client, err := genai.NewClient(ctx, &genai.ClientConfig{ APIKey: "your-api-key", Backend: genai.BackendGeminiAPI, }) if err != nil { log.Fatal(err) } config := &genai.GenerateContentConfig{ Tools: []*genai.Tool{ {GoogleSearchRetrieval: &genai.GoogleSearchRetrieval{}}, }, } result, err := client.Models.GenerateContent( ctx, "gemini-2.0-flash", genai.Text("What are the latest developments in quantum computing in 2024?"), config, ) if err != nil { log.Fatal(err) } fmt.Println(result.Candidates[0].Content.Parts[0].Text) // Check for grounding metadata if result.Candidates[0].GroundingMetadata != nil { fmt.Printf("\nGrounding chunks: %d\n", len(result.Candidates[0].GroundingMetadata.GroundingChunks)) } } ``` -------------------------------- ### Video Playback and Screen Sharing in JavaScript Source: https://github.com/charmbracelet/go-genai/blob/main/examples/live/live_streaming.html Plays video files and captures frames for sending via WebSocket, or shares screen using getDisplayMedia to stream frames at 1 FPS. Depends on Canvas API for image encoding; inputs video stream or screen capture, outputs base64 JPEG images. Stopped on track end or pause; frame rate limited to reduce bandwidth. ```javascript function playVideoStart() { if (videoTimer != null) { playVideoStop(); } video.play(); videoTimer = setInterval(sendVideo, 1000); } function playVideoStop() { video.pause(); clearInterval(videoTimer); videoTimer = null; } function sendVideo() { if (video.paused || video.ended) { playVideoStop(); return; } videoCanvas.width = video.videoWidth; videoCanvas.height = video.videoHeight; videoCtx.drawImage(video, 0, 0); var encodedImage = videoCanvas.toDataURL('image/jpeg').split(';base64,')[1]; if (ws && ws.readyState === WebSocket.OPEN) { ws.send(createImageContent(encodedImage)); } } document.getElementById('playVideo').onclick = function () { if (videoTimer != null) { playVideoStop(); } else { playVideoStart(); } }; async function startScreenSharing() { try { const stream = await navigator.mediaDevices.getDisplayMedia({ video: { frameRate: 1000 }, audio: false }); const video = document.createElement('video'); video.srcObject = stream; video.play(); const canvas = document.createElement('canvas'); const ctx = canvas.getContext('2d'); shareScreenTimer = setInterval(() => { canvas.width = video.videoWidth; canvas.height = video.videoHeight; ctx.drawImage(video, 0, 0, canvas.width, canvas.height); const frameData = canvas.toDataURL('image/jpeg').split(';base64,')[1]; if (ws && ws.readyState === WebSocket.OPEN) { ws.send(createImageContent(frameData)); } }, 1000); stream.getVideoTracks()[0].onended = () => { clearInterval(shareScreenTimer); if (processor) { processor.disconnect(); } }; } catch (err) { console.error('Error getting screen stream:', err); } } document.getElementById('shareScreen').onclick = function () { if (shareScreenTimer == null) { startScreenSharing(); recordStart(); } }; ``` -------------------------------- ### Audio Playback Queue Management in JavaScript Source: https://github.com/charmbracelet/go-genai/blob/main/examples/live/live_streaming.html Queues received audio chunks from the server and plays them sequentially using HTML5 Audio element. Encodes PCM data for playback; inputs are base64 audio data, outputs played audio stream. Handles queuing to avoid overlaps, but may buffer during high latency. ```javascript function playNextChunk() { if (!isAudioPlaying && audioQueue.length > 0) { isAudioPlaying = true; const encodedAudio = encodeAudio(audioQueue, 24000, 16, 1); audioQueue = []; audio.src = URL.createObjectURL(encodedAudio); audio.onended = function () { isAudioPlaying = false; playNextChunk(); } audio.play(); } } ``` -------------------------------- ### Enable Code Execution Tool in Go with Gemini API Source: https://context7.com/charmbracelet/go-genai/llms.txt This Go code allows the Gemini model to generate and execute Python code for tasks like calculations, outputting code snippets and their results. It requires the google.golang.org/genai package, an API key, and prompts requesting computational tasks, returning executed outputs alongside generated code. Limitations include security risks with code execution and dependency on Gemini's code execution capabilities. ```go package main import ( "context" "fmt" "log" "google.golang.org/genai" ) func main() { ctx := context.Background() client, err := genai.NewClient(ctx, &genai.ClientConfig{ APIKey: "your-api-key", Backend: genai.BackendGeminiAPI, }) if err != nil { log.Fatal(err) } config := &genai.GenerateContentConfig{ Tools: []*genai.Tool{ {CodeExecution: &genai.ToolCodeExecution{}}, }, } result, err := client.Models.GenerateContent( ctx, "gemini-2.0-flash", genai.Text("Calculate the sum of the first 100 prime numbers. Generate and run code."), config, ) if err != nil { log.Fatal(err) } // Display all parts including code and execution results for i, part := range result.Candidates[0].Content.Parts { if part.ExecutableCode != nil { fmt.Printf("Code (Part %d):\n%s\n\n", i+1, part.ExecutableCode.Code) } if part.CodeExecutionResult != nil { fmt.Printf("Output (Part %d):\n%s\n\n", i+1, part.CodeExecutionResult.Output) } if part.Text != "" { fmt.Printf("Text (Part %d):\n%s\n\n", i+1, part.Text) } } } ``` -------------------------------- ### Implement Function Calling in Go with Gemini API Source: https://context7.com/charmbracelet/go-genai/llms.txt This Go code defines a function schema for controlling light brightness and color temperature, allowing the Gemini model to invoke it based on user queries. It requires the google.golang.org/genai package and an API key, taking text prompts as input and outputting function call details. Limitations include dependency on valid JSON schemas and API availability. ```go package main import ( "context" "encoding/json" "fmt" "log" "google.golang.org/genai" ) func main() { ctx := context.Background() client, err := genai.NewClient(ctx, &genai.ClientConfig{ APIKey: "your-api-key", Backend: genai.BackendGeminiAPI, }) if err != nil { log.Fatal(err) } tools := []*genai.Tool{ { FunctionDeclarations: []*genai.FunctionDeclaration{ { Name: "controlLight", Description: "Set the brightness and color temperature of a room light.", Parameters: &genai.Schema{ Type: genai.TypeObject, Properties: map[string]*genai.Schema{ "brightness": { Type: genai.TypeNumber, Description: "Light level from 0 to 100. Zero is off and 100 is full brightness.", }, "colorTemperature": { Type: genai.TypeString, Description: "Color temperature of the light fixture which can be `daylight`, `cool` or `warm`.", }, }, Required: []string{"brightness", "colorTemperature"}, }, }, }, }, } config := &genai.GenerateContentConfig{ Temperature: genai.Ptr[float32](0), Tools: tools, } result, err := client.Models.GenerateContent( ctx, "gemini-2.0-flash", genai.Text("Set the living room light to 75% brightness with cool white color"), config, ) if err != nil { log.Fatal(err) } // Extract function call from response if len(result.Candidates) > 0 && len(result.Candidates[0].Content.Parts) > 0 { part := result.Candidates[0].Content.Parts[0] if part.FunctionCall != nil { fmt.Printf("Function: %s\n", part.FunctionCall.Name) argsJSON, _ := json.MarshalIndent(part.FunctionCall.Args, "", " ") fmt.Printf("Arguments:\n%s\n", argsJSON) } } } ```