Skip to content

Commit

Permalink
Fix OR search + use related entries in ai qureies
Browse files Browse the repository at this point in the history
  • Loading branch information
parterburn committed Feb 1, 2025
1 parent 499612f commit 6c00ae1
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
2 changes: 1 addition & 1 deletion app/controllers/entries_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ def export
elsif params[:search].present? && search_params[:term].present?
if search_params[:term].include?(" OR ")
filter_names = search_params[:term].split(' OR ')
sanitized_terms = filter_names.map { |term| ActiveRecord::Base.sanitize_sql_like(term) }
sanitized_terms = filter_names.map { |term| ActiveRecord::Base.sanitize_sql_like(term.downcase) }
conditions = sanitized_terms.map { |term| @entries.where("LOWER(entries.body) LIKE ?", "%#{term}%") }
@entries = conditions.reduce(:or)
elsif search_params[:term].include?('"')
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/searches_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def show
@search = Search.new(search_params)
filter_names = search_params[:term].split(' OR ')
cond_text = filter_names.map{|w| "LOWER(entries.body) like ?"}.join(" OR ")
cond_values = filter_names.map{|w| "%#{w}%"}
cond_values = filter_names.map{|w| "%#{w.downcase}%"}
@entries = current_user.entries.where(cond_text, *cond_values)
elsif search_params[:term].present? && search_params[:term].include?('"')
@search = Search.new(search_params)
Expand Down
29 changes: 25 additions & 4 deletions app/models/concerns/entry/ai_assistant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ def ai_response
entry_for_ai = entry_body
messages = [
as_life_coach,
last_5_entries,
related_entries,
last_3_entries,
entry_for_ai
].flatten.compact
response = respond_as_ai(messages)
Expand Down Expand Up @@ -138,15 +139,35 @@ def entry_body
end
end

def last_5_entries
def related_entries
return nil if @tokens_left < 20_000

entries = user.entries.where(date: 1.month.ago..).where.not(date: date).order(date: :desc).limit(3)
cond_text = hashtags.map{|w| "LOWER(entries.body) like ?"}.join(" OR ")
cond_values = hashtags.map{|w| "%##{w.downcase}%"}
entries = user.entries.where(cond_text, *cond_values).first(3)
return nil if entries.empty?

entry_bodies = entries.map { |e| { "#{e.date.to_date}": "#{e.text_bodies_for_ai.first}" } }.first(@tokens_left)
@tokens_left -= entry_bodies.length

{
role: "user",
content: "These are previous entries I've written that might be relevant to the current entry (use them as context only if relevant to the current entry): #{entry_bodies}"
}
end

def last_3_entries
return nil if @tokens_left < 20_000

entries = user.entries.where(date: 1.month.ago..).where.not(id: id).order(date: :desc).limit(3)
return nil if entries.empty?

entry_bodies = entries.map { |e| { "#{e.date.to_date}": "#{e.text_bodies_for_ai.first}" } }.first(@tokens_left)
@tokens_left -= entry_bodies.length

{
role: "user",
content: "These are the last 5 entries I've had (use them only if relevant to the current entry): #{entries.map { |e| { "#{e.date.to_date}": "#{e.text_bodies_for_ai.first}" } }}".first(@tokens_left)
content: "These are the previous entries I've written (use them as context only if relevant to the current entry): #{entry_bodies}"
}
end
end
Expand Down

0 comments on commit 6c00ae1

Please sign in to comment.