|
| 1 | +package google |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "github.com/google/generative-ai-go/genai" |
| 6 | + "github.com/tmc/langchaingo/llms/googleai" |
| 7 | + "log/slog" |
| 8 | + "slices" |
| 9 | + "strings" |
| 10 | + "sync" |
| 11 | + "time" |
| 12 | +) |
| 13 | + |
| 14 | +func (g *Google) preSendingHook(ctx context.Context, model *genai.GenerativeModel, meta googleai.PreSendingHookMetadata) { |
| 15 | + key := getCacheKey(model) |
| 16 | + if key == "" { |
| 17 | + return |
| 18 | + } |
| 19 | + |
| 20 | + if g.cacheNames.Get(key) == "" { |
| 21 | + err := g.createNewToolsCache(ctx, key, meta.Options.Model, model.Tools) |
| 22 | + if err != nil { |
| 23 | + slog.Warn("Error creating cache", "error", err) |
| 24 | + return |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + model.CachedContentName = g.cacheNames.Get(key) |
| 29 | + model.Tools = nil |
| 30 | + |
| 31 | + return |
| 32 | +} |
| 33 | + |
| 34 | +func getCacheKey(model *genai.GenerativeModel) string { |
| 35 | + var funcNames []string |
| 36 | + |
| 37 | + for _, tool := range model.Tools { |
| 38 | + for _, fd := range tool.FunctionDeclarations { |
| 39 | + funcNames = append(funcNames, fd.Name) |
| 40 | + } |
| 41 | + } |
| 42 | + slices.Sort(funcNames) |
| 43 | + |
| 44 | + return strings.Join(funcNames, "") |
| 45 | +} |
| 46 | + |
| 47 | +func (g *Google) createNewToolsCache(ctx context.Context, cKey, model string, tools []*genai.Tool) error { |
| 48 | + cache, err := genaiCreateCachedContent(g, ctx, &genai.CachedContent{ |
| 49 | + Expiration: genai.ExpireTimeOrTTL{ |
| 50 | + TTL: g.cacheTTL, |
| 51 | + }, |
| 52 | + Model: model, |
| 53 | + Tools: tools, |
| 54 | + }) |
| 55 | + if err != nil { |
| 56 | + return err |
| 57 | + } |
| 58 | + |
| 59 | + g.cacheNames.Write(cKey, cache.Name) |
| 60 | + go g.startCacheRefresher(cKey, cache) |
| 61 | + |
| 62 | + return nil |
| 63 | +} |
| 64 | + |
| 65 | +func (g *Google) startCacheRefresher(cKey string, cache *genai.CachedContent) { |
| 66 | + go func() { |
| 67 | + select { |
| 68 | + case <-g.clientCtx.Done(): |
| 69 | + slog.Debug("cache refresher stopped", "cacheName", cache.Name) |
| 70 | + return |
| 71 | + case <-time.After(g.cacheRefresh): |
| 72 | + } |
| 73 | + |
| 74 | + newCache, err := genaiUpdateCachedContent(g, g.clientCtx, cache, &genai.CachedContentToUpdate{ |
| 75 | + Expiration: &genai.ExpireTimeOrTTL{ |
| 76 | + TTL: g.cacheTTL, |
| 77 | + }, |
| 78 | + }) |
| 79 | + if err != nil { |
| 80 | + slog.Warn("Error refreshing cache", "cacheName", cache.Name, "error", err) |
| 81 | + return |
| 82 | + } |
| 83 | + g.cacheNames.Write(cKey, newCache.Name) |
| 84 | + |
| 85 | + go g.startCacheRefresher(cKey, newCache) |
| 86 | + }() |
| 87 | +} |
| 88 | + |
| 89 | +func (g *Google) removeAllCaches(ctx context.Context) { |
| 90 | + wg := &sync.WaitGroup{} |
| 91 | + g.cacheNames.For(func(_, name string) { |
| 92 | + wg.Add(1) |
| 93 | + go func() { |
| 94 | + defer wg.Done() |
| 95 | + g.removeCache(ctx, name) |
| 96 | + }() |
| 97 | + }) |
| 98 | + |
| 99 | + wg.Wait() |
| 100 | +} |
| 101 | + |
| 102 | +func (g *Google) removeCache(ctx context.Context, name string) { |
| 103 | + if err := genaiDeleteCachedContent(g, ctx, name); err != nil { |
| 104 | + slog.Error("Error deleting cache", "cacheName", name, "error", err) |
| 105 | + } else { |
| 106 | + slog.Debug("Cache deleted", "cacheName", name) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +var genaiCreateCachedContent = func(g *Google, ctx context.Context, cc *genai.CachedContent) (*genai.CachedContent, error) { |
| 111 | + return g.client.GetGenaiClient().CreateCachedContent(ctx, cc) |
| 112 | +} |
| 113 | + |
| 114 | +var genaiUpdateCachedContent = func(g *Google, ctx context.Context, cc *genai.CachedContent, update *genai.CachedContentToUpdate) (*genai.CachedContent, error) { |
| 115 | + return g.client.GetGenaiClient().UpdateCachedContent(ctx, cc, update) |
| 116 | +} |
| 117 | + |
| 118 | +var genaiDeleteCachedContent = func(g *Google, ctx context.Context, name string) error { |
| 119 | + return g.client.GetGenaiClient().DeleteCachedContent(ctx, name) |
| 120 | +} |
0 commit comments