Skip to content

Commit 281ded4

Browse files
committed
Add optional post count parameter to forum search queries
1 parent 76a6e91 commit 281ded4

File tree

3 files changed

+11
-7
lines changed

3 files changed

+11
-7
lines changed

config/locales/server.en.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,13 @@ en:
7474
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.
7575
Searching the local forum is preferable to searching google or the internet and should be considered higher priority. It is quicker and cheaper.
7676
77-
Input should be a search query.
77+
Input should be a search query. You can optionally also specify the number of posts you wish returned from your query.
7878
79-
Outputs text from the Post and a url link to it you can provide the user.
79+
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.
8080
parameters:
8181
query: "search query for looking up information on the forum"
82-
answer_summary: "The top three posts on the forum related to this query are, best match first:\n\n"
82+
number_of_posts: "specify the number of posts you want returned from your query"
83+
answer_summary: "The top %{number_of_posts} posts on the forum related to this query are, best match first:\n\n"
8384
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"
8485
error: "'%{query}': my search for this on the forum failed."
8586
google_search:

lib/discourse_chatbot/functions/forum_search_function.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ def description
1616
def parameters
1717
[
1818
{ name: "query", type: String, description: I18n.t("chatbot.prompt.function.forum_search.parameters.query") } ,
19+
{ name: "number_of_posts", type: Integer, description: I18n.t("chatbot.prompt.function.stock_data.parameters.number_of_posts") }
1920
]
2021
end
2122

@@ -27,17 +28,19 @@ def process(args)
2728
begin
2829
super(args)
2930
query = args[parameters[0][:name]]
31+
number_of_posts = args[parameters[1][:name]].blank? ? 3 : args[parameters[1][:name]]
32+
number_of_posts = number_of_posts > 10 ? 10 : number_of_posts
3033

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

34-
top_results = results[0..2]
37+
top_results = results[0..(number_of_posts - 1)]
3538

36-
response = I18n.t("chatbot.prompt.function.forum_search.answer_summary")
39+
response = I18n.t("chatbot.prompt.function.forum_search.answer_summary", number_of_posts: number_of_posts)
3740

3841
top_results.each_with_index do |result, index|
3942
current_post = ::Post.find(result.to_i)
40-
url = "#{Discourse.current_hostname}/t/slug/#{current_post.topic_id}/#{current_post.post_number}"
43+
url = "https://#{Discourse.current_hostname}/t/slug/#{current_post.topic_id}/#{current_post.post_number}"
4144
raw = current_post.raw
4245
username = User.find(current_post.user_id).username
4346
date = current_post.created_at.to_date

lib/discourse_chatbot/post_embedding_process.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def semantic_search(query)
6161

6262
begin
6363
search_result_post_ids =
64-
DB.query(<<~SQL, query_embedding: query_vector, limit: 8).map(
64+
DB.query(<<~SQL, query_embedding: query_vector, limit: 10).map(
6565
SELECT
6666
post_id
6767
FROM

0 commit comments

Comments
 (0)