mirror of
https://github.com/vxcontrol/langchaingo.git
synced 2026-08-24 21:31:28 -04:00
064f09155c
- Raised the `go` directive from 1.24.1 to 1.26.5 to satisfy pgx/v5 and ollama's minimum Go version requirements and to pick up 15 patched standard library CVEs. - Updated `pgx/v5`, `otel`/`otel/sdk`/`otlptracehttp`, `x/net`, and the AWS `eventstream` protocol package to their latest requested versions. - Remediated govulncheck-flagged CVEs in `grpc`, `x/text`, `x/crypto`, `antchfx/xpath`, AWS SDK `bedrock*`/`s3` services, `etcd/server`, and `mongo-driver` v1/v2. - Fixed ollama's `MainGPU` API type change (`int` -> `*int`) in `llms/ollama/options.go` without breaking the public `WithRunnerMainGPU` signature.
Google AI Caching Example
This example demonstrates how to use Google AI's context caching feature with langchaingo.
What is Context Caching?
Context caching allows you to cache large amounts of content (system prompts, context documents, etc.) and reuse them across multiple requests. This can significantly reduce:
- Latency: Cached content is processed faster
- Cost: You only pay for the cached content once, not on every request
- Token usage: Cached tokens don't count against your rate limits
Requirements
- Minimum cacheable content: 32,768 tokens (~24,000 words)
- Supported models: gemini-2.0-flash-exp and other Gemini 2.0+ models
- Google API key
How It Works
- Create cached content with a large system prompt or context
- Reference the cache in subsequent requests using
WithCachedContent(cacheName) - Reuse the cache across multiple different queries
- Update TTL to extend cache lifetime if needed
- Delete cache when no longer needed
Running the Example
export GOOGLE_API_KEY="your-api-key-here"
go run main.go
Expected Output
The example will:
- Create a cached content with Go programming expertise (~32k+ tokens)
- Make two different requests using the same cache
- Show cached token usage in responses
- List all cached contents
- Clean up the cache when done
Key Concepts
Minimum Size Requirement
Google AI requires at least 32,768 tokens for caching. In practice, this is approximately:
- 24,000 words (assuming ~1.4 tokens per word)
- 120,000 characters (assuming ~4 characters per token)
TTL (Time To Live)
Cached content automatically expires after the specified TTL. You can:
- Set initial TTL when creating:
1*time.Hour,24*time.Hour, etc. - Update TTL later:
helper.UpdateCachedContent(ctx, name, newTTL)
Cost Savings
Caching is particularly valuable when:
- You have a large system prompt used across many conversations
- You need to provide extensive context (documentation, knowledge base)
- You're building a chatbot with consistent personality/instructions
- You have reference materials that don't change often
API Reference
Creating a Cache
cached, err := helper.CreateCachedContent(
ctx,
"gemini-2.0-flash-exp", // Model name
messages, // Content to cache
1*time.Hour, // TTL
"my-cache-name", // Display name
)
Using a Cache
resp, err := client.GenerateContent(
ctx,
messages,
googleai.WithCachedContent(cached.Name), // Reference the cache
llms.WithMaxTokens(500),
)
Managing Caches
// Get cache details
cache, err := helper.GetCachedContent(ctx, name)
// Update TTL
updated, err := helper.UpdateCachedContent(ctx, name, 2*time.Hour)
// List all caches
for cache, err := range helper.AllCachedContents(ctx) {
// Process each cache
}
// Delete cache
err := helper.DeleteCachedContent(ctx, name)
Best Practices
- Cache Reuse: Create one cache and use it across many requests
- Appropriate TTL: Set TTL based on how often content changes
- Clean Up: Delete caches you no longer need to avoid accumulation
- Monitor Usage: Check
CachedTokensin response metadata - Size Requirements: Ensure content meets minimum 32,768 token threshold
Limitations
- Minimum size: 32,768 tokens
- Maximum cached content per project: Check current quotas
- TTL range: Minimum 5 minutes, maximum 24 hours (may vary)
- Model support: Currently only Gemini 2.0+ models