# Improving quality of search filters in Discourse AI

**URL:** https://meta.discourse.org/t/improving-quality-of-search-filters-in-discourse-ai/311499
**Category:** Support
**Tags:** ai
**Created:** [June 8, 2024, 12:02am UTC](https://meta.discourse.org/t/improving-quality-of-search-filters-in-discourse-ai/311499 "2024-06-08T00:02:10Z")
**Posts on this page:** 15
**Page:** 1

<div class="post-metadata">

### Author: ![aas](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/aas/32/289331_2.png) [@aas](https://meta.discourse.org/u/aas)
#### Post date: [June 8, 2024, 12:02am UTC](https://meta.discourse.org/t/improving-quality-of-search-filters-in-discourse-ai/311499/1 "2024-06-08T00:02:10Z")

</div>

Firstly, this is truly incredible - thanks so much! 😍

I have a question about a persona I created with these Enabled Commands: Search, Categories, Read, Tags.

I have also uploaded a .txt file. **My goal with this persona is to have it always search my forum _and_ the uploads, and do RAG with both.**

After uploading the .txt file, I noticed that it stopped searching my forum. This is what I have in my Base Search Query: ` #docs #faqs #anking-wiki`  
`#docs` and `#faqs` are categories while `#anking-wiki `is a tag.

Questions:

- Do I actually need the categories and tags commands? I see these commands just list the tags and categories from my instance. It seems like maybe the search command should be enough.
- is my base search query ok?
- why did the bot stop searching my forum?
  - Is the solution simply to adjust my prompt to clarify that it should always use the search function?

Here is the prompt as seen in the debugging info:

> You are a helpful Discourse assistant for AnkiHub users.  
> You _understand_ and **generate** Discourse Markdown.  
> You live in a Discourse Forum Message.  
> You live in the forum with the URL: [https://community.ankihub.net](https://community.ankihub.net)  
> The title of your site: AnkiHub Community  
> The description is: A community forum for the AnkiHub and AnKing projects  
> The participants in this conversation are: gpt4o\_bot, andrew  
> The date now is: 2024-06-07 23:32:13 UTC, much has changed since you were  
> trained.  
> You were trained on OLD data, lean on search to get up to date information about this forum  
> When searching try to SIMPLIFY search terms  
> Discourse search joins all terms with AND. Reduce and simplify terms to find more results.  
> The following texts will give you additional guidance for your response.  
> We included them because we believe they are relevant to this conversation topic.  
> Texts:

I tried adding “Be sure to always use the search function” to my prompt. This caused it to indeed use the search function, but with poor results:  
[Missing Images After AnkiHub Version Update Issue - AI Conversation - AnkiHub Community](https://community.ankihub.net/discourse-ai/ai-bot/shared-ai-conversations/Iz4fPpAinzPSLj05Nbgn7w)

It apparently did 3 searches and wrongly reported “Found 0 results” all three times. If you click the search results links, you can see there were, in fact, results. Further, you can see that **the parameters in my “Base Search Query” weren’t used** , so it searched the whole forum instead of the specific categories.

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [June 11, 2024, 3:41am UTC](https://meta.discourse.org/t/improving-quality-of-search-filters-in-discourse-ai/311499/2 "2024-06-11T03:41:00Z")

</div>

I think part of the problem here is that our search is a bit confusing:

[https://meta.discourse.org/search?q=%23bug%20%23feature%20test](https://meta.discourse.org/search?q=%23bug%20%23feature%20test)

Searches for #Contribute > Bug AND #Contribute > Feature - test

What you want here is:

[https://meta.discourse.org/search?q=categories%3Abug%2Cfeature%20test](https://meta.discourse.org/search?q=categories%3Abug%2Cfeature%20test)

---

<div class="post-metadata">

### Author: ![aas](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/aas/32/289331_2.png) [@aas](https://meta.discourse.org/u/aas)
#### Post date: [June 11, 2024, 3:59pm UTC](https://meta.discourse.org/t/improving-quality-of-search-filters-in-discourse-ai/311499/3 "2024-06-11T15:59:54Z")

</div>

> [@sam](#):
>
> What you want here is:
> 
> [https://meta.discourse.org/search?q=categories%3Abug%2Cfeature%20test](https://meta.discourse.org/search?q=categories%3Abug%2Cfeature%20test)

I’m confused. This doesn’t work as I expect it to. I expected the search to return results only from the specified categories. However, the search returns results from any category. The first result from this search is in the #Customization > Theme component category.

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [June 11, 2024, 9:36pm UTC](https://meta.discourse.org/t/improving-quality-of-search-filters-in-discourse-ai/311499/4 "2024-06-11T21:36:13Z")

</div>

True this is looking buggy, oh no, will have a look

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [June 12, 2024, 6:07am UTC](https://meta.discourse.org/t/improving-quality-of-search-filters-in-discourse-ai/311499/7 "2024-06-12T06:07:41Z")

</div>

This is fixed per:

> <https://github.com/discourse/discourse/pull/27440>
>
> This introduces the syntax of
> 
> \`category:a,b,c\` which will search across multipl…e categories.
> 
> Previously there was no way to allow search across a wide selection of
> categories.

I agree it is intuative to support `#contribute:bug #contribute:feature something`, but this is not quite supported yet and needs a bit of discussion.

`categories:bug,feature something` will work now, and `category:bug,feature something` will as well.

---

<div class="post-metadata">

### Author: ![aas](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/aas/32/289331_2.png) [@aas](https://meta.discourse.org/u/aas)
#### Post date: [June 13, 2024, 7:39pm UTC](https://meta.discourse.org/t/improving-quality-of-search-filters-in-discourse-ai/311499/8 "2024-06-13T19:39:30Z")

</div>

Awesome! Thanks for getting that fix out so quickly. 🙇‍♂️

While the search now works properly, the Discourse AI Bot still doesn’t seem to work as expected. Here are my observations with `3.3.0.beta3-dev (a1d881f625)`:

- Doesn’t use the `base_query` option
  - it seems to ignore it entirely. is my config valid?  
 ![CleanShot 2024-06-13 at 11.10.12](https://global.discourse-cdn.com/meta/original/4X/9/d/7/9d727eae59cf21deb33f47400241806ba3b8fe2f.png)
  - afaict, there are no requirements for the values of base search query. but no matter what I put in there, it isn’t used appended to `search_query`.

- Search results aren’t added to the context for RAG
  - when the search tool _is_ called (albeit, without the `base_query`), the results don’t seem to be used in the LLM call.
  - Am I misunderstanding how this tool should work? I expected it to do a Discourse search and use the search results (i.e., the contents of the found topics) for RAG.  
See this request for example:

> **Request JSON**
>
> ```plaintext
> {"model":"gpt-4","messages":[{"role":"system","content":"You are a friendly and helpful assistant for Anki users specializing in the AnkiHub add-on and web app.\nYou _understand_ and **generate** Discourse Markdown.\nYou live in a Discourse Forum Message.\n\nYou live in the forum with the URL: https://community.ankihub.net\nYou answer questions specifically about using Anki with AnkiHub.\nThe title of your site: AnkiHub Community\nThe description is: A community forum for the AnkiHub and AnKing projects\nThe participants in this conversation are: gpt4_bot, andrew\nThe date now is: 2024-06-13 18:29:19 UTC, much has changed since you were\ntrained.\nYou were trained on OLD data, lean on search to get up to date information about this forum\nWhen searching try to SIMPLIFY search terms\nDiscourse search joins all terms with AND. Reduce and simplify terms to find more results."},{"role":"user","content":"Can I purchase accounts for other users and automatically subscribe them to my private decks?","name":"andrew"},{"role":"assistant","content":null,"tool_calls":[{"type":"function","function":{"arguments":"{\"search_query\":\"purchase accounts for other users subscribe private decks\"}","name":"search"},"id":"call_fmzwIbU4ravFRKWRsOQlqGBq"}]},{"role":"tool","tool_call_id":"call_fmzwIbU4ravFRKWRsOQlqGBq","content":"{\"column_names\":[\"title\",\"url\",\"username\",\"excerpt\",\"created\",\"category\",\"likes\",\"topic_views\",\"topic_likes\",\"topic_replies\",\"tags\"],\"rows\":[[\"👪 Organizations\",\"/t/organizations/166454/1\",\"andrew\",\"\\u003ca name=\\\"what-are-ankihub-organizations-1\\\" class=\\\"anchor\\\" href=\\\"#what-are-ankihub-organizations-1\\\"\\u003e\\u003c/a\\u003eWhat are AnkiHub Organizations\\nAnkiHub Organizations allow Organization Owners to purchase AnkiHub for other users and invite those users to the Organization. \\nAnkiHub organizations work like this and have the following features: \\n\\u003ca name=\\\"linked-decks-2\\\" class=\\\"anchor\\\" href=\\\"#linked-decks-2\\\"\\u003e\\u003c/a\\u003eLinked Decks\\nUsers in your organization can automatically get access \\u0026hellip;\",\"2024-02-08T17:44:27.842Z\",\"🎓 Docs \\u003e 🤝 Create \\u0026 Collaborate\",1,166,1,1],[\"How can I get my school to buy AnkiHub for my class?\",\"/t/how-can-i-get-my-school-to-buy-ankihub-for-my-class/228569/1\",\"andrew\",\"Some students have successfully gotten their schools to sponsor AnkiHub memberships for their students. To do so, your school must create an AnkiHub Organization: \\n\\n\\n:bulb: Here are some tips for getting your school to purchase AnkiHub: \\n\\nConnect with your school’s student government people or med e\\u0026hellip;\",\"2024-05-23T21:40:37.477Z\",\"❓ FAQs\",0,50,0,1],[\"What happens after my AnkiHub subscription ends?\",\"/t/what-happens-after-my-ankihub-subscription-ends/167190/1\",\"Ahmed7\",\"If your subscription to AnkiHub runs out and you do not wish to renew. \\nAll your decks will stay, nothing will be removed. However, you will no longer be able to sync with AnkiHub and receive new changes to your decks, nor will you be able to suggest new changes or notes to any pre-existing decks yo\\u0026hellip;\",\"2024-02-10T06:14:53.787Z\",\"❓ FAQs\",0,307,0,1],[\"What if I can't afford AnkiHub?\",\"/t/what-if-i-cant-afford-ankihub/167280/1\",\"Ahmed7\",\"AnkiHub has a scholarship program to get access at a decreased price. Apply \\u003ca href=\\\"https://www.ankihub.net/scholarships\\\"\\u003ehere \\u003c/a\\u003e. (You must \\u003ca href=\\\"https://app.ankihub.net/accounts/signup/\\\"\\u003esign up \\u003c/a\\u003efor an AnkiHub account before applying).\",\"2024-02-10T13:58:24.842Z\",\"❓ FAQs \\u003e AnKing Decks\",0,445,0,1,\"anking-wiki\"],[\"Does AnkiHub offer discounts?\",\"/t/does-ankihub-offer-discounts/228564/1\",\"andrew\",\"The are currently two ways to get a discount on AnkiHub: \\n\\nPurchase our \\u003ca href=\\\"https://app.ankihub.net/memberships/plans/\\\"\\u003eannual billing option\\u003c/a\\u003e for one free month per year\\n\\nAnkiHub is available at $4.58/user/month with an annual billing discount. We can’t afford to discount AnkiHub any further, unfortunately.\\n\\n\\nParticipate in an Ambassador program\\n\\n\\u0026hellip;\",\"2024-05-23T21:34:32.829Z\",\"❓ FAQs\",0,90,0,1]],\"args\":{\"search_query\":\"purchase accounts for other users subscribe private decks\"}}","name":"search"}],"stream":true,"stream_options":{"include_usage":true},"tools":[{"type":"function","function":{"name":"search","description":"Will search topics in the current discourse instance, when rendering always prefer to link to the topics you find","parameters":{"type":"object","properties":{"search_query":{"description":"Specific keywords to search for, space separated (correct bad spelling, remove connector words)","type":"string"},"user":{"description":"Filter search results to this username (only include if user explicitly asks to filter by user)","type":"string"},"order":{"description":"search result order","type":"string","enum":["latest","latest_topic","oldest","views","likes"]},"limit":{"description":"limit number of results returned (generally prefer to just keep to default)","type":"integer"},"max_posts":{"description":"maximum number of posts on the topics (topics where lots of people posted)","type":"integer"},"tags":{"description":"list of tags to search for. Use + to join with OR, use , to join with AND","type":"string"},"category":{"description":"category name to filter to","type":"string"},"before":{"description":"only topics created before a specific date YYYY-MM-DD","type":"string"},"after":{"description":"only topics created after a specific date YYYY-MM-DD","type":"string"},"status":{"description":"search for topics in a particular state","type":"string","enum":["open","closed","archived","noreplies","single_user"]}},"required":[]}}},{"type":"function","function":{"name":"read","description":"Will read a topic or a post on this Discourse instance","parameters":{"type":"object","properties":{"topic_id":{"description":"the id of the topic to read","type":"integer"},"post_numbers":{"description":"the post numbers to read (optional)","type":"array","items":{"type":"integer"}}},"required":["topic_id"]}}}]}
> 
> ```

You can see that the search tool was called but doesn’t contain the `base_query`.

You can also see that **`content` contains only the default system prompt** , without any of the search results.

- Doesn’t display the correct number of search results
  - it says 0 when there are many or 5 when there are 0
  - doesn’t seem to correlate with either the normal search results or semantic similarity results

- The bot will add invalid query parameters
  - e.g., it added `category:non-existent-category` to the end of the query.
  - 

# 🥺 Requests

- It would be super helpful if there was an option to enable verbose logging for `discourse-ai` so that we non-Ruby-literate folks (I’m learning! 🤓 ) can quickly get some insight into what’s happening under the hood by visiting `/logs`
- When the search tool is used, add the top results to the context for RAG
- Advice on how to ensure the `base_query` is used.

---

<div class="post-metadata">

### Author: ![aas](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/aas/32/289331_2.png) [@aas](https://meta.discourse.org/u/aas)
#### Post date: [June 13, 2024, 9:41pm UTC](https://meta.discourse.org/t/improving-quality-of-search-filters-in-discourse-ai/311499/9 "2024-06-13T21:41:09Z")

</div>

Am I right that the function call should indeed be pushing the results of the tool call into the prompt according to this?

> <https://github.com/discourse/discourse-ai/blob/f642a27f118f3b2c7b4789eb2ac67d569099c8f0/lib/ai_bot/bot.rb#L137-L138>

So perhaps it is updating the prompt, but that isn’t clear from the request that is shown from the debugging tool?

> [@aas](#):
>
> - 
> - Am I misunderstanding how this tool should work? I expected it to do a Discourse search and use the search results (i.e., the contents of the found topics) for RAG.  
> See this request for example:
> 
> Request JSON
> 
> ```plaintext
> {"model":"gpt-4","messages":[{"role":"system","content":"You are a friendly and helpful assistant for Anki users specializing in the AnkiHub add-on and web app.\nYou _understand_ and **generate** Discourse Markdown.\nYou live in a Discourse Forum Message.\n\nYou live in the forum with the URL: https://community.ankihub.net\nYou answer questions specifically about using Anki with AnkiHub.\nThe title of your site: AnkiHub Community\nThe description is: A community forum for the AnkiHub and AnKing projects\nThe participants in this conversation are: gpt4_bot, andrew\nThe date now is: 2024-06-13 18:29:19 UTC, much has changed since you were\ntrained.\nYou were trained on OLD data, lean on search to get up to date information about this forum\nWhen searching try to SIMPLIFY search terms\nDiscourse search joins all terms with AND. Reduce and simplify terms to find more results."},{"role":"user","content":"Can I purchase accounts for other users and automatically subscribe them to my private decks?","name":"andrew"},{"role":"assistant","content":null,"tool_calls":[{"type":"function","function":{"arguments":"{\"search_query\":\"purchase accounts for other users subscribe private decks\"}","name":"search"},"id":"call_fmzwIbU4ravFRKWRsOQlqGBq"}]},{"role":"tool","tool_call_id":"call_fmzwIbU4ravFRKWRsOQlqGBq","content":"{\"column_names\":[\"title\",\"url\",\"username\",\"excerpt\",\"created\",\"category\",\"likes\",\"topic_views\",\"topic_likes\",\"topic_replies\",\"tags\"],\"rows\":[[\"👪 Organizations\",\"/t/organizations/166454/1\",\"andrew\",\"\\u003ca name=\\\"what-are-ankihub-organizations-1\\\" class=\\\"anchor\\\" href=\\\"#what-are-ankihub-organizations-1\\\"\\u003e\\u003c/a\\u003eWhat are AnkiHub Organizations\\nAnkiHub Organizations allow Organization Owners to purchase AnkiHub for other users and invite those users to the Organization. \\nAnkiHub organizations work like this and have the following features: \\n\\u003ca name=\\\"linked-decks-2\\\" class=\\\"anchor\\\" href=\\\"#linked-decks-2\\\"\\u003e\\u003c/a\\u003eLinked Decks\\nUsers in your organization can automatically get access \\u0026hellip;\",\"2024-02-08T17:44:27.842Z\",\"🎓 Docs \\u003e 🤝 Create \\u0026 Collaborate\",1,166,1,1],[\"How can I get my school to buy AnkiHub for my class?\",\"/t/how-can-i-get-my-school-to-buy-ankihub-for-my-class/228569/1\",\"andrew\",\"Some students have successfully gotten their schools to sponsor AnkiHub memberships for their students. To do so, your school must create an AnkiHub Organization: \\n\\n\\n:bulb: Here are some tips for getting your school to purchase AnkiHub: \\n\\nConnect with your school’s student government people or med e\\u0026hellip;\",\"2024-05-23T21:40:37.477Z\",\"❓ FAQs\",0,50,0,1],[\"What happens after my AnkiHub subscription ends?\",\"/t/what-happens-after-my-ankihub-subscription-ends/167190/1\",\"Ahmed7\",\"If your subscription to AnkiHub runs out and you do not wish to renew. \\nAll your decks will stay, nothing will be removed. However, you will no longer be able to sync with AnkiHub and receive new changes to your decks, nor will you be able to suggest new changes or notes to any pre-existing decks yo\\u0026hellip;\",\"2024-02-10T06:14:53.787Z\",\"❓ FAQs\",0,307,0,1],[\"What if I can't afford AnkiHub?\",\"/t/what-if-i-cant-afford-ankihub/167280/1\",\"Ahmed7\",\"AnkiHub has a scholarship program to get access at a decreased price. Apply \\u003ca href=\\\"https://www.ankihub.net/scholarships\\\"\\u003ehere \\u003c/a\\u003e. (You must \\u003ca href=\\\"https://app.ankihub.net/accounts/signup/\\\"\\u003esign up \\u003c/a\\u003efor an AnkiHub account before applying).\",\"2024-02-10T13:58:24.842Z\",\"❓ FAQs \\u003e AnKing Decks\",0,445,0,1,\"anking-wiki\"],[\"Does AnkiHub offer discounts?\",\"/t/does-ankihub-offer-discounts/228564/1\",\"andrew\",\"The are currently two ways to get a discount on AnkiHub: \\n\\nPurchase our \\u003ca href=\\\"https://app.ankihub.net/memberships/plans/\\\"\\u003eannual billing option\\u003c/a\\u003e for one free month per year\\n\\nAnkiHub is available at $4.58/user/month with an annual billing discount. We can’t afford to discount AnkiHub any further, unfortunately.\\n\\n\\nParticipate in an Ambassador program\\n\\n\\u0026hellip;\",\"2024-05-23T21:34:32.829Z\",\"❓ FAQs\",0,90,0,1]],\"args\":{\"search_query\":\"purchase accounts for other users subscribe private decks\"}}","name":"search"}],"stream":true,"stream_options":{"include_usage":true},"tools":[{"type":"function","function":{"name":"search","description":"Will search topics in the current discourse instance, when rendering always prefer to link to the topics you find","parameters":{"type":"object","properties":{"search_query":{"description":"Specific keywords to search for, space separated (correct bad spelling, remove connector words)","type":"string"},"user":{"description":"Filter search results to this username (only include if user explicitly asks to filter by user)","type":"string"},"order":{"description":"search result order","type":"string","enum":["latest","latest_topic","oldest","views","likes"]},"limit":{"description":"limit number of results returned (generally prefer to just keep to default)","type":"integer"},"max_posts":{"description":"maximum number of posts on the topics (topics where lots of people posted)","type":"integer"},"tags":{"description":"list of tags to search for. Use + to join with OR, use , to join with AND","type":"string"},"category":{"description":"category name to filter to","type":"string"},"before":{"description":"only topics created before a specific date YYYY-MM-DD","type":"string"},"after":{"description":"only topics created after a specific date YYYY-MM-DD","type":"string"},"status":{"description":"search for topics in a particular state","type":"string","enum":["open","closed","archived","noreplies","single_user"]}},"required":[]}}},{"type":"function","function":{"name":"read","description":"Will read a topic or a post on this Discourse instance","parameters":{"type":"object","properties":{"topic_id":{"description":"the id of the topic to read","type":"integer"},"post_numbers":{"description":"the post numbers to read (optional)","type":"array","items":{"type":"integer"}}},"required":["topic_id"]}}}]}
> 
> ```

Should I not expect to see all of the context there? If not, that’s confusing because the entire prompt context is visible when using a persona that does RAG with uploads.

I’m still confused by the number of results that are reported.

That said, I’m getting much better results after adding the following to the system prompt!

- Always call both the search and read functions.
- Call the search function no more than 3 times.
- When calling the search function, always use this value for the category parameter: “docs,faqs”

This is going to be an absolute game-changer once I get things configured properly.

I’m not sure if you are aware of this, but you are effectively empowering us with something far more powerful than what [Intercom](https://www.intercom.com/) is trying to achieve.

I’m already trying to hijack Intercom’s messenger by directing users straight to Discourse AI 🤓 :

 ![CleanShot 2024-06-13 at 14.39.26](https://global.discourse-cdn.com/meta/original/4X/1/a/a/1aa753d156836aa0443a9d90ced4ae0ebb81c0bc.jpeg)

The AnkiHub AI link opens a PM with the AI Bot with a link like this: `/new-message?username=ankihubai_bot`

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [June 14, 2024, 5:25am UTC](https://meta.discourse.org/t/improving-quality-of-search-filters-in-discourse-ai/311499/10 "2024-06-14T05:25:31Z")

</div>

> [@aas](#):
>
> Should I not expect to see all of the context there?

Yes, debugging tool shows everything we send the LLM.

I just made a fix to number of results per:

> <https://github.com/discourse/discourse-ai/pull/668>
>
> \- Display filtered search correctly, so it is not confusing
> \- When XML stripping…, if a chunk was \`\<\` it would crash
> \- SQL Helper improved to be better aware of Data Explorer

We were not properly linking to a filtered query.

> [@aas](#):
>
> When calling the search function, always use this value for the category parameter: “docs,faqs”

Recommend against this, use base\_query, just tested it works fine: `categories:docs,faqs`

* * *

I do recommend against using GPT4 these days be sure to use GPT4o or Turbo, they are so much cheaper and faster. ’

Just double checking … you did enable

`ai_bot_debugging_allowed_groups` so you see the debug button?

---

<div class="post-metadata">

### Author: ![aas](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/aas/32/289331_2.png) [@aas](https://meta.discourse.org/u/aas)
#### Post date: [June 14, 2024, 11:45pm UTC](https://meta.discourse.org/t/improving-quality-of-search-filters-in-discourse-ai/311499/11 "2024-06-14T23:45:46Z")

</div>

> [@sam](#):
>
> Yes, debugging tool shows everything we send the LLM.

> [@sam](#):
>
> Just double checking … you did enable
> 
> `ai_bot_debugging_allowed_groups` so you see the debug button?

Yes. I’ll PM you a quick screen share to show that the results of the read function call aren’t used in the prompt.

I also noticed that the read function is never called unless I add a note in the system prompt: “always call the read function.” It would be awesome if there was a way for us to have more control over function calling. For example, [set `tool_choice: "required"`](https://platform.openai.com/docs/guides/function-calling) in the case of OpenAI.

> [@sam](#):
>
> Recommend against this, use base\_query, just tested it works fine: `categories:docs,faqs`

I just tried again with this base query and the the correct search parameters were used. This is the first time I’ve seen it work. 🤷

> [@sam](#):
>
> I do recommend against using GPT4 these days be sure to use GPT4o or Turbo, they are so much cheaper and faster. ’

I know, I’m just experimenting with different models atm 😄 Was wondering if GPT4 would decide to make different function calls. Haiku sounds very promising.

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [June 15, 2024, 2:34am UTC](https://meta.discourse.org/t/improving-quality-of-search-filters-in-discourse-ai/311499/12 "2024-06-15T02:34:09Z")

</div>

Thanks heaps

oh, that is correct, system prompt injection will only happen if the persona has uploads. If there are no uploads then we don’t inject anything

Without uploads we don’t use unconditional system prompt injection and instead rely on tool calls with tool call results, you can see them when scroll through.

A tool free mode targeted at topics is certainly interesting, it would eat a fair bit of tokens, the challenge though is fishing the right content out of a topic.

If a topic for example is 5000 tokens, which tokens would we pick? how would we pick the right tokens.

As it stands we only have N tokens embedded per topic depending on the embedding model you are using.

So we would need to start embedding more chunks per topic or come up with an algorithm to figure out what part of the topic is the most interesting.

---

<div class="post-metadata">

### Author: ![aas](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/aas/32/289331_2.png) [@aas](https://meta.discourse.org/u/aas)
#### Post date: [June 26, 2024, 4:29pm UTC](https://meta.discourse.org/t/improving-quality-of-search-filters-in-discourse-ai/311499/13 "2024-06-26T16:29:59Z")

</div>

> [@sam](#):
>
> oh, that is correct, system prompt injection will only happen if the persona has uploads. If there are no uploads then we don’t inject anything
> 
> Without uploads we don’t use unconditional system prompt injection and instead rely on tool calls with tool call results, you can see them when scroll through.

I’m confused. If the results of invoking the read tool aren’t used for RAG, what does invoking the read tool even do? I see the results of the read tool call are put into the [raw context](https://github.com/discourse/discourse-ai/blob/e39e0bdb4a9c5683d2baacf6f8863caf0015e7cc/lib/ai_bot/bot.rb#L141-L142), but it doesn’t seem like this is actually being used by the LLM in any way.

> [@sam](#):
>
> A tool free mode targeted at topics is certainly interesting, it would eat a fair bit of tokens, the challenge though is fishing the right content out of a topic.
> 
> If a topic for example is 5000 tokens, which tokens would we pick? how would we pick the right tokens.

My desired use case is simple: **enable RAG on a subset of topics as per the `base_query`.**

I think it would be best not to rely on Discourse to handle splitting my topics into multiple chunks, each with their own embedding. Rather, I want to be responsible for crafting my Docs and FAQs such that they are optimized for RAG. I.e., the entirety of the topics main post should be embeddable. I’m talking about wiki topics here, the replies to which can and should be disregarded.

ada-002 has a [max token input of 8191](https://platform.openai.com/docs/guides/embeddings/embedding-models), which is plenty for any well-written doc or faq topic. Such topics should be well under this limit. [voyage ai](https://docs.voyageai.com/docs/embeddings#model-choices) has a much larger context length. of 16000.

> [@sam](#):
>
> So we would need to start embedding more chunks per topic or come up with an algorithm to figure out what part of the topic is the most interesting.

What about something as simple as this:

- `(tokens in the system prompt - tokens in user message) = total remaining tokens available`
- inject as many topics into the prompt as possible based on `total remaining tokens available`
  - use only the first post in the found topics

I guess the near-term workaround could be the following:

- create a persona that uses the search tool
- export all the topics that could be found with the search tool
  - I won’t need to split these docs up because
    - I don’t care about topic replies
    - The main wiki topic is already small enough to embed the entire thing.

- Upload those documents to the persona for RAG

This way, the persona will actually do RAG with the search results.

I hope this makes sense! LMK if anything is unclear.

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [June 27, 2024, 1:45am UTC](https://meta.discourse.org/t/improving-quality-of-search-filters-in-discourse-ai/311499/14 "2024-06-27T01:45:30Z")

</div>

> [@aas](#):
>
> I’m confused. If the results of invoking the read tool aren’t used for RAG

When you read stuff it is injected into the conversation stream.

Eg:

- Can you read X
- Tool call read X
- Tool result “the page”
- LLM reply

RAG is a very overloaded term, but at the end reading a topic adds it to the context, the llm at the end is responsible for how to use it, various llms may make better or worse usage out of it.

Eg: [[Untitled AI bot PM] - AI Conversation - Discourse Meta](https://meta.discourse.org/discourse-ai/ai-bot/shared-ai-conversations/UrUqWYwu9Lzst5qrYwrbzA)

Search tool already inserts excerpts of topics, having it and a RAG on the same content does not make sense.

What you are saying, I guess, is that “search is giving context, but not **enough context** ”.

So one workaround is to export all the important topics into a file, then use it as a RAG. Do not use search tool.

* * *

Simplest thing for now is:

`categories:cat1,cat2,cat3 in:first`

This will restrict the search tool to first posts only.

It is very efficient, see:

> **[Twitter Authentication Setup: A Quick Configuration Guide](https://meta.discourse.org/discourse-ai/ai-bot/shared-ai-conversations/xw47vzgTQExpyYD777J5mQ)**
>
> sam: how do I configure twitter auth?

This works cause the search tool injects the following into the context:

 ![image](https://global.discourse-cdn.com/meta/original/4X/b/8/3/b831a949bbef644705d7a626f67759072ec4f93a.png)

* * *

I also want an unconditional “rag” mode where you can opt to inject even more data (eg: find top 5 documents and inject up to 5000 tokens per document, trying to find most relevant sections)

Its a complicated change, we will get there, but for now … search + read gets you quite far.

---

<div class="post-metadata">

### Author: ![Jagster](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jagster/32/192154_2.png) [@Jagster](https://meta.discourse.org/u/Jagster)
#### Post date: [June 27, 2024, 7:15am UTC](https://meta.discourse.org/t/improving-quality-of-search-filters-in-discourse-ai/311499/15 "2024-06-27T07:15:26Z")

</div>

Just saying… you guys are very close to that point where you must to do a introducing what are basic rules how to use AI. I mean you have already similar one of CSS and a couple other thing.

---

<div class="post-metadata">

### Author: ![aas](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/aas/32/289331_2.png) [@aas](https://meta.discourse.org/u/aas)
#### Post date: [June 27, 2024, 9:32pm UTC](https://meta.discourse.org/t/improving-quality-of-search-filters-in-discourse-ai/311499/16 "2024-06-27T21:32:34Z")

</div>

> [@sam](#):
>
> RAG is a very overloaded term, but at the end reading a topic adds it to the context, the llm at the end is responsible for how to use it, various llms may make better or worse usage out of it.

I see. That makes sense. From [OpenAI’s docs on function calling](https://platform.openai.com/docs/guides/function-calling):

> The basic sequence of steps for function calling is as follows:  
> 1. Call the model with the user query and a set of functions defined in the [functions parameter](https://platform.openai.com/docs/api-reference/chat/create#chat/create-functions).  
> 2. The model can choose to call one or more functions; if so, the content will be a stringified JSON object adhering to your custom schema (note: the model may hallucinate parameters).  
> 3. Parse the string into JSON in your code, and call your function with the provided arguments if they exist.  
> 4. Call the model again by appending the function response as a new message, and let the model summarize the results back to the user.

Critically, I wasn’t aware of #4. I didn’t realize that the results of invoking the tool were appended as messages. I thought it was entirely up to the client if the results were used in subsequent calls! So I was confused when I didn’t see tool call results in the prompt itself. :facepalm: This all makes a lot more sense now. Thank you!

> [@sam](#):
>
> So one workaround is to export all the important topics into a file, then use it as a RAG. Do not use search tool.

👍 🙏 Sounds like the only downside here is that bot replies won’t include those nice search results. Would you strongly recommend using doc uploads _and_ the search tool for any reason, even though it will be a bit redundant? I wonder if the benefits of getting the search results in the convo will be worth it.

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [June 28, 2024, 12:28am UTC](https://meta.discourse.org/t/improving-quality-of-search-filters-in-discourse-ai/311499/17 "2024-06-28T00:28:09Z")

</div>

> [@aas](#):
>
> . Would you strongly recommend using doc uploads _and_ the search tool for any reason, even though it will be a bit redundant?

You need to test it, it could work, it could give you surprising results and so much depends on the LLM you are using.
