Class: JekyllAiRelatedPosts::Generator

Inherits:
Jekyll::Generator
  • Object
show all
Defined in:
lib/jekyll_ai_related_posts/generator.rb

Instance Method Summary collapse

Instance Method Details

#generate(site) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/jekyll_ai_related_posts/generator.rb', line 11

def generate(site)
  Jekyll.logger.debug "AI Related Posts:", "Generating related posts..."

  @site = site
  @stats = {
    cache_hits: 0,
    cache_misses: 0
  }
  setup_database

  @indexed_posts = {}
  site.posts.docs.each do |p|
    @indexed_posts[p.relative_path] = p
  end

  if fetch_enabled?
    @embeddings_fetcher = new_fetcher

    @site.posts.docs.each do |p|
      ensure_embedding_cached(p)
    end

    @site.posts.docs.each do |p|
      find_related(p)
    end
    Jekyll.logger.info "AI Related Posts:", "Found #{@stats[:cache_hits]} cached embeddings; fetched #{@stats[:cache_misses]}"
  else
    Jekyll.logger.info "AI Related Posts:", "Fetch disabled. Using cached related posts data."

    @site.posts.docs.each do |p|
      fallback_generate_related(p)
    end

    case @stats[:cache_misses]
    when 0
      Jekyll.logger.info "AI Related Posts:", "Found #{@stats[:cache_hits]} cached embeddings; all embeddings cached"
    when 1
      Jekyll.logger.info "AI Related Posts:", "Found #{@stats[:cache_hits]} cached embeddings; skipped 1 fetch"
    else
      Jekyll.logger.info "AI Related Posts:", "Found #{@stats[:cache_hits]} cached embeddings; skipped #{@stats[:cache_misses]} fetches"
    end
  end

  Jekyll.logger.debug "AI Related Posts:", "Done generating related posts"
end