jekyll_ai_related_posts 0.1.3 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bd10032e0e9758acac7f0dc26d79aaa2a8de36e8872be41ecd62f915b4422847
4
- data.tar.gz: f999747d95b4b9dce7c1fd6fcad198959a1734c13dfc50cbdd57bdb267f0fcdc
3
+ metadata.gz: bde6fb501500ac67e8b2fbcc05707dad8acc44d3e06ea9e461a98a065e9f84e2
4
+ data.tar.gz: e0a588ca8656583ad3dcbb36bb76d8c3992107ef807e5425c3f0f0f0db021da7
5
5
  SHA512:
6
- metadata.gz: ea9b61e8f8e3b9148146eece6a453abc57e720c295db1b99d11d6ef6786c206a4efdca194c6c0d8287133699517775b242aa9b4cae5af1cc10ee40903be3c0ab
7
- data.tar.gz: 9d43069c05704e7c032840e4fc6409da6fd2ca437b5b2cf282021a84b0c64fa9b1761c03ab3694759261cf10b26cd087a49ace883f1d4c85e5f365d90d0b8bb2
6
+ metadata.gz: a5e674a295aeb7c2e0bfd1bcec208d129dad7833a3690c4beb467e130220806e06867f49c4703a677cd000944ae0fdc66305bbcdc9679e7ccb51691f1ed9ec4b
7
+ data.tar.gz: f5c95c5eab1f65ec12b56b5ed949125eb24ec41a0e0097609bff313d654ff0888152e78070648fb744b4bf04c90a2f50a5698161d77df99545037d8c26f55d26
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.1.3] - 2024-05-15
4
+
5
+ - Better (nicer to read) log messages.
6
+
3
7
  ## [0.1.2] - 2024-04-24
4
8
 
5
9
  - [Bugfix] Include up to 10 related posts (like native Jekyll does) instead of just 3.
@@ -9,9 +9,13 @@ require "json"
9
9
  module JekyllAiRelatedPosts
10
10
  class Generator < Jekyll::Generator
11
11
  def generate(site)
12
- Jekyll.logger.info "AI Related Posts:", "Generating related posts..."
12
+ Jekyll.logger.debug "AI Related Posts:", "Generating related posts..."
13
13
 
14
14
  @site = site
15
+ @stats = {
16
+ cache_hits: 0,
17
+ cache_misses: 0
18
+ }
15
19
  setup_database
16
20
 
17
21
  @indexed_posts = {}
@@ -29,13 +33,25 @@ module JekyllAiRelatedPosts
29
33
  @site.posts.docs.each do |p|
30
34
  find_related(p)
31
35
  end
36
+ Jekyll.logger.info "AI Related Posts:", "Found #{@stats[:cache_hits]} cached embeddings; fetched #{@stats[:cache_misses]}"
32
37
  else
33
38
  Jekyll.logger.info "AI Related Posts:", "Fetch disabled. Using cached related posts data."
34
39
 
35
40
  @site.posts.docs.each do |p|
36
41
  fallback_generate_related(p)
37
42
  end
43
+
44
+ case @stats[:cache_misses]
45
+ when 0
46
+ Jekyll.logger.info "AI Related Posts:", "Found #{@stats[:cache_hits]} cached embeddings; all embeddings cached"
47
+ when 1
48
+ Jekyll.logger.info "AI Related Posts:", "Found #{@stats[:cache_hits]} cached embeddings; skipped 1 fetch"
49
+ else
50
+ Jekyll.logger.info "AI Related Posts:", "Found #{@stats[:cache_hits]} cached embeddings; skipped #{@stats[:cache_misses]} fetches"
51
+ end
38
52
  end
53
+
54
+ Jekyll.logger.debug "AI Related Posts:", "Done generating related posts"
39
55
  end
40
56
 
41
57
  private
@@ -54,8 +70,14 @@ module JekyllAiRelatedPosts
54
70
  def fallback_generate_related(post)
55
71
  existing = Models::Post.find_by(relative_path: post.relative_path)
56
72
  if existing.nil?
73
+ @stats[:cache_misses] += 1
57
74
  post.data["ai_related_posts"] = post.related_posts
58
75
  else
76
+ if existing.embedding_text == embedding_text(post)
77
+ @stats[:cache_hits] += 1
78
+ else
79
+ @stats[:cache_misses] += 1
80
+ end
59
81
  find_related(post)
60
82
  end
61
83
  end
@@ -81,20 +103,25 @@ module JekyllAiRelatedPosts
81
103
  existing = nil
82
104
  end
83
105
 
84
- return unless existing.nil?
106
+ if existing.nil?
107
+ @stats[:cache_misses] += 1
85
108
 
86
- Models::Post.create!(
87
- relative_path: post.relative_path,
88
- embedding_text: embedding_text(post),
89
- embedding: embedding_for(post).to_json
90
- )
109
+ Models::Post.create!(
110
+ relative_path: post.relative_path,
111
+ embedding_text: embedding_text(post),
112
+ embedding: embedding_for(post).to_json
113
+ )
91
114
 
92
- sql = <<-SQL
93
- INSERT INTO vss_posts (rowid, post_embedding)
94
- SELECT rowid, embedding FROM posts WHERE relative_path = :relative_path;
95
- SQL
96
- ActiveRecord::Base.connection.execute(ActiveRecord::Base.sanitize_sql([ sql,
97
- { relative_path: post.relative_path } ]))
115
+ sql = <<-SQL
116
+ INSERT INTO vss_posts (rowid, post_embedding)
117
+ SELECT rowid, embedding FROM posts WHERE relative_path = :relative_path;
118
+ SQL
119
+ ActiveRecord::Base.connection.execute(
120
+ ActiveRecord::Base.sanitize_sql([ sql, { relative_path: post.relative_path } ])
121
+ )
122
+ else
123
+ @stats[:cache_hits] += 1
124
+ end
98
125
  end
99
126
 
100
127
  def find_related(post)
@@ -108,9 +135,9 @@ module JekyllAiRelatedPosts
108
135
  LIMIT 10000;
109
136
  SQL
110
137
 
111
- results = ActiveRecord::Base.connection.execute(ActiveRecord::Base.sanitize_sql([ sql, {
112
- relative_path: post.relative_path
113
- } ]))
138
+ results = ActiveRecord::Base.connection.execute(
139
+ ActiveRecord::Base.sanitize_sql([ sql, { relative_path: post.relative_path } ])
140
+ )
114
141
  # The first result is the post itself, with a distance of 0.
115
142
  rowids = results.sort_by { |r| r["distance"] }.drop(1).first(10).map { |r| r["rowid"] }
116
143
 
@@ -147,9 +174,16 @@ module JekyllAiRelatedPosts
147
174
  end
148
175
 
149
176
  def setup_database
177
+ db_path = @site.in_source_dir(".ai_related_posts_cache.sqlite3")
178
+ if File.exist?(db_path)
179
+ Jekyll.logger.debug "AI Related Posts:", "Found cache [.ai_related_posts_cache.sqlite3]"
180
+ else
181
+ Jekyll.logger.info "AI Related Posts:", "Creating cache [.ai_related_posts_cache.sqlite3]"
182
+ end
183
+
150
184
  ActiveRecord::Base.establish_connection(
151
185
  adapter: "sqlite3",
152
- database: @site.in_source_dir(".ai_related_posts_cache.sqlite3")
186
+ database: db_path
153
187
  )
154
188
  # We don't need WAL mode for this.
155
189
  ActiveRecord::Base.connection.execute("PRAGMA journal_mode=DELETE;")
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JekyllAiRelatedPosts
4
- VERSION = "0.1.3"
4
+ VERSION = "0.1.4"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll_ai_related_posts
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Kasberg
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-05-16 00:00:00.000000000 Z
11
+ date: 2024-10-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord