jekyll_ai_related_posts 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/lib/jekyll_ai_related_posts/generator.rb +51 -17
- data/lib/jekyll_ai_related_posts/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bde6fb501500ac67e8b2fbcc05707dad8acc44d3e06ea9e461a98a065e9f84e2
|
4
|
+
data.tar.gz: e0a588ca8656583ad3dcbb36bb76d8c3992107ef807e5425c3f0f0f0db021da7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a5e674a295aeb7c2e0bfd1bcec208d129dad7833a3690c4beb467e130220806e06867f49c4703a677cd000944ae0fdc66305bbcdc9679e7ccb51691f1ed9ec4b
|
7
|
+
data.tar.gz: f5c95c5eab1f65ec12b56b5ed949125eb24ec41a0e0097609bff313d654ff0888152e78070648fb744b4bf04c90a2f50a5698161d77df99545037d8c26f55d26
|
data/CHANGELOG.md
CHANGED
@@ -9,9 +9,13 @@ require "json"
|
|
9
9
|
module JekyllAiRelatedPosts
|
10
10
|
class Generator < Jekyll::Generator
|
11
11
|
def generate(site)
|
12
|
-
Jekyll.logger.
|
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
|
-
|
106
|
+
if existing.nil?
|
107
|
+
@stats[:cache_misses] += 1
|
85
108
|
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
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
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
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(
|
112
|
-
|
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:
|
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;")
|
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.
|
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-
|
11
|
+
date: 2024-10-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|