Skip to content

Commit 8a06667

Browse files
authored
Merge pull request #138 from merefield/minor_embedding_refactor
REFACTOR: break out embedding api call to separate method
2 parents 473325e + 48e86fd commit 8a06667

File tree

5 files changed

+41
-50
lines changed

5 files changed

+41
-50
lines changed

lib/discourse_chatbot/embedding_process.rb

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,33 @@ def upsert(id)
2828
raise "Overwrite me!"
2929
end
3030

31-
def get_embedding_from_api(id)
31+
def get_embedding(id)
3232
raise "Overwrite me!"
3333
end
3434

35+
def get_embedding_from_api(text)
36+
begin
37+
self.setup_api
38+
39+
response = @client.embeddings(
40+
parameters: {
41+
model: @model_name,
42+
input: text
43+
}
44+
)
45+
46+
if response.dig("error")
47+
error_text = response.dig("error", "message")
48+
raise StandardError, error_text
49+
end
50+
rescue StandardError => e
51+
Rails.logger.error("Chatbot: Error occurred while attempting to retrieve Embedding for post id '#{post_id}' in topic id '#{topic.id}': #{e.message}")
52+
raise e
53+
end
54+
55+
embedding_vector = response.dig("data", 0, "embedding")
56+
end
57+
3558

3659
def semantic_search(query)
3760
raise "Overwrite me!"

lib/discourse_chatbot/post/post_embedding_process.rb

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def upsert(post_id)
99
if in_scope(post_id)
1010
if !is_valid(post_id)
1111

12-
embedding_vector = get_embedding_from_api(post_id)
12+
embedding_vector = get_embedding(post_id)
1313

1414
::DiscourseChatbot::PostEmbedding.upsert({ post_id: post_id, model: SiteSetting.chatbot_open_ai_embeddings_model, embedding: "#{embedding_vector}" }, on_duplicate: :update, unique_by: :post_id)
1515

@@ -32,32 +32,13 @@ def upsert(post_id)
3232
end
3333
end
3434

35-
def get_embedding_from_api(post_id)
36-
begin
37-
self.setup_api
38-
39-
post = ::Post.find_by(id: post_id)
40-
topic = ::Topic.find_by(id: post.topic_id)
41-
response = @client.embeddings(
42-
parameters: {
43-
model: @model_name,
44-
input: post.raw[0..SiteSetting.chatbot_open_ai_embeddings_char_limit]
45-
}
46-
)
47-
48-
if response.dig("error")
49-
error_text = response.dig("error", "message")
50-
raise StandardError, error_text
51-
end
52-
rescue StandardError => e
53-
Rails.logger.error("Chatbot: Error occurred while attempting to retrieve Embedding for post id '#{post_id}' in topic id '#{topic.id}': #{e.message}")
54-
raise e
55-
end
35+
def get_embedding(post_id)
36+
post = ::Post.find_by(id: post_id)
37+
text = post.raw[0..SiteSetting.chatbot_open_ai_embeddings_char_limit]
5638

57-
embedding_vector = response.dig("data", 0, "embedding")
39+
get_embedding_from_api(text)
5840
end
5941

60-
6142
def semantic_search(query)
6243
self.setup_api
6344

@@ -157,9 +138,11 @@ def in_scope(post_id)
157138
end
158139

159140
def is_valid(post_id)
141+
post = ::Post.find_by(id: post_id)
160142
embedding_record = ::DiscourseChatbot::PostEmbedding.find_by(post_id: post_id)
161143
return false if !embedding_record.present?
162144
return false if embedding_record.model != SiteSetting.chatbot_open_ai_embeddings_model
145+
return false if post.updated_at > embedding_record.updated_at
163146
true
164147
end
165148

lib/discourse_chatbot/topic/topic_title_embedding_process.rb

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def upsert(topic_id)
99
if in_scope(topic_id)
1010
if !is_valid(topic_id)
1111

12-
embedding_vector = get_embedding_from_api(topic_id)
12+
embedding_vector = get_embedding(topic_id)
1313

1414
::DiscourseChatbot::TopicTitleEmbedding.upsert({ topic_id: topic_id, model: SiteSetting.chatbot_open_ai_embeddings_model, embedding: "#{embedding_vector}" }, on_duplicate: :update, unique_by: :topic_id)
1515

@@ -32,28 +32,10 @@ def upsert(topic_id)
3232
end
3333
end
3434

35-
def get_embedding_from_api(topic_id)
36-
begin
37-
self.setup_api
38-
39-
topic = ::Topic.find_by(id: topic_id)
40-
response = @client.embeddings(
41-
parameters: {
42-
model: @model_name,
43-
input: topic.title
44-
}
45-
)
46-
47-
if response.dig("error")
48-
error_text = response.dig("error", "message")
49-
raise StandardError, error_text
50-
end
51-
rescue StandardError => e
52-
Rails.logger.error("Chatbot: Error occurred while attempting to retrieve Embedding for topic id '#{topic_id}': #{e.message}")
53-
raise e
54-
end
35+
def get_embedding(topic_id)
36+
topic = ::Topic.find_by(id: topic_id)
5537

56-
embedding_vector = response.dig("data", 0, "embedding")
38+
get_embedding_from_api(topic.title)
5739
end
5840

5941

@@ -138,9 +120,11 @@ def in_scope(topic_id)
138120
end
139121

140122
def is_valid(topic_id)
123+
topic = ::Topic.find_by(id: topic_id)
141124
embedding_record = ::DiscourseChatbot::TopicTitleEmbedding.find_by(topic_id: topic_id)
142125
return false if !embedding_record.present?
143126
return false if embedding_record.model != SiteSetting.chatbot_open_ai_embeddings_model
127+
return false if topic.updated_at > embedding_record.updated_at
144128
true
145129
end
146130

plugin.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
# frozen_string_literal: true
22
# name: discourse-chatbot
33
# about: a plugin that allows you to have a conversation with a configurable chatbot in Discourse Chat, Topics and Private Messages
4-
# version: 1.5.8
4+
# version: 1.5.9
55
# authors: merefield
66
# url: https://github.com/merefield/discourse-chatbot
77

8-
gem 'mime-types-data', '3.2025.0610', { require: false }
9-
gem 'mime-types', '3.7.0', { require: false }
108
gem 'multipart-post', '2.4.0', { require: false }
119
gem 'faraday-multipart', '1.0.4', { require: false }
1210
gem 'event_stream_parser', '1.0.0', { require: false }

spec/lib/post_embedding_process_spec.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,12 @@
5050
describe 'validity' do
5151
it "checks if a post embedding is valid" do
5252
SiteSetting.chatbot_open_ai_embeddings_model = "text-embedding-ada-002"
53+
freeze_time(3.days.ago)
5354
post = Fabricate(:post)
55+
freeze_time(2.days.ago)
5456
post_embedding = ::DiscourseChatbot::PostEmbedding.create!(post_id: post.id, model: "text-embedding-3-small", embedding: "[#{(1..1536).to_a.join(",")}]")
5557
expect(subject.is_valid(post.id)).to eq(false)
58+
freeze_time(1.days.ago)
5659
post_embedding = ::DiscourseChatbot::PostEmbedding.upsert({post_id: post.id, model: "text-embedding-ada-002", embedding: "[#{(1..1536).to_a.join(",")}]"}, on_duplicate: :update, unique_by: :post_id)
5760
expect(subject.is_valid(post.id)).to eq(true)
5861
end

0 commit comments

Comments
 (0)