Skip to content

Commit

Permalink
Add optional post count parameter to forum search queries
Browse files Browse the repository at this point in the history
  • Loading branch information
merefield committed Aug 28, 2023
1 parent 76a6e91 commit 281ded4
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
7 changes: 4 additions & 3 deletions config/locales/server.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,13 @@ en:
Search the local forum for information that may help you answer the question. Especially useful when the forum specialises in the subject matter of the query.
Searching the local forum is preferable to searching google or the internet and should be considered higher priority. It is quicker and cheaper.
Input should be a search query.
Input should be a search query. You can optionally also specify the number of posts you wish returned from your query.
Outputs text from the Post and a url link to it you can provide the user.
Outputs text from the Post and a url link to it you can provide the user. When presenting the url in your reply, do not embed in an anchor, just write the straight link.
parameters:
query: "search query for looking up information on the forum"
answer_summary: "The top three posts on the forum related to this query are, best match first:\n\n"
number_of_posts: "specify the number of posts you want returned from your query"
answer_summary: "The top %{number_of_posts} posts on the forum related to this query are, best match first:\n\n"
answer: "Number %{rank}: the post is at this web address: %{url}, it was written by '%{username}' on %{date} and the text is '%{raw}'.\n\n"
error: "'%{query}': my search for this on the forum failed."
google_search:
Expand Down
9 changes: 6 additions & 3 deletions lib/discourse_chatbot/functions/forum_search_function.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def description
def parameters
[
{ name: "query", type: String, description: I18n.t("chatbot.prompt.function.forum_search.parameters.query") } ,
{ name: "number_of_posts", type: Integer, description: I18n.t("chatbot.prompt.function.stock_data.parameters.number_of_posts") }
]
end

Expand All @@ -27,17 +28,19 @@ def process(args)
begin
super(args)
query = args[parameters[0][:name]]
number_of_posts = args[parameters[1][:name]].blank? ? 3 : args[parameters[1][:name]]
number_of_posts = number_of_posts > 10 ? 10 : number_of_posts

post_embedding = ::DiscourseChatbot::PostEmbeddingProcess.new
results = post_embedding.semantic_search(query)

top_results = results[0..2]
top_results = results[0..(number_of_posts - 1)]

response = I18n.t("chatbot.prompt.function.forum_search.answer_summary")
response = I18n.t("chatbot.prompt.function.forum_search.answer_summary", number_of_posts: number_of_posts)

top_results.each_with_index do |result, index|
current_post = ::Post.find(result.to_i)
url = "#{Discourse.current_hostname}/t/slug/#{current_post.topic_id}/#{current_post.post_number}"
url = "https://#{Discourse.current_hostname}/t/slug/#{current_post.topic_id}/#{current_post.post_number}"
raw = current_post.raw
username = User.find(current_post.user_id).username
date = current_post.created_at.to_date
Expand Down
2 changes: 1 addition & 1 deletion lib/discourse_chatbot/post_embedding_process.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def semantic_search(query)

begin
search_result_post_ids =
DB.query(<<~SQL, query_embedding: query_vector, limit: 8).map(
DB.query(<<~SQL, query_embedding: query_vector, limit: 10).map(
SELECT
post_id
FROM
Expand Down

0 comments on commit 281ded4

Please sign in to comment.