This repository was archived by the owner on Jul 22, 2025. It is now read-only.
generated from discourse/discourse-plugin-skeleton
-
Notifications
You must be signed in to change notification settings - Fork 40
FEATURE: HyDE-powered semantic search. #136
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
...javascripts/discourse/connectors/full-page-search-below-search-header/semantic-search.hbs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| {{#if this.searchEnabled}} | ||
| <div class="semantic-search__container search-results" role="region"> | ||
| <div | ||
| class="semantic-search__results" | ||
| {{did-insert this.setup}} | ||
| {{did-insert this.debouncedSearch}} | ||
| {{will-destroy this.teardown}} | ||
| > | ||
| {{#if this.searching}} | ||
| <div class="semantic-search__searching"> | ||
| <div class="semantic-search__searching-text"> | ||
| {{i18n "discourse_ai.embeddings.semantic_search_loading"}} | ||
| </div> | ||
| <span class="semantic-search__indicator-wave"> | ||
| <span class="semantic-search__indicator-dot">.</span> | ||
| <span class="semantic-search__indicator-dot">.</span> | ||
| <span class="semantic-search__indicator-dot">.</span> | ||
| </span> | ||
| </div> | ||
| {{else}} | ||
| {{#if this.results.length}} | ||
| <div class="semantic-search__toggle-button-container"> | ||
| <DButton | ||
| @translatedTitle={{this.collapsedResultsTitle}} | ||
| @translatedLabel={{this.collapsedResultsTitle}} | ||
| @action={{fn | ||
| (mut this.collapsedResults) | ||
| (not this.collapsedResults) | ||
| }} | ||
| @class="btn-flat" | ||
| @icon={{if this.collapsedResults "chevron-right" "chevron-down"}} | ||
| /> | ||
| </div> | ||
|
|
||
| {{#unless this.collapsedResults}} | ||
| <div class="semantic-search__entries"> | ||
| <SearchResultEntries | ||
| @posts={{this.results}} | ||
| @highlightQuery={{this.highlightQuery}} | ||
| /> | ||
| </div> | ||
| {{/unless}} | ||
| {{else}} | ||
| <div class="semantic-search__searching"> | ||
| {{i18n "discourse_ai.embeddings.semantic_search_results.none"}} | ||
| </div> | ||
| {{/if}} | ||
| {{/if}} | ||
| </div> | ||
| </div> | ||
| {{/if}} |
82 changes: 82 additions & 0 deletions
82
.../javascripts/discourse/connectors/full-page-search-below-search-header/semantic-search.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| import Component from "@glimmer/component"; | ||
| import { action, computed } from "@ember/object"; | ||
| import I18n from "I18n"; | ||
| import { tracked } from "@glimmer/tracking"; | ||
| import { ajax } from "discourse/lib/ajax"; | ||
| import { translateResults } from "discourse/lib/search"; | ||
| import discourseDebounce from "discourse-common/lib/debounce"; | ||
| import { inject as service } from "@ember/service"; | ||
| import { bind } from "discourse-common/utils/decorators"; | ||
| import { SEARCH_TYPE_DEFAULT } from "discourse/controllers/full-page-search"; | ||
|
|
||
| export default class extends Component { | ||
| static shouldRender(_args, { siteSettings }) { | ||
| return siteSettings.ai_embeddings_semantic_search_enabled; | ||
| } | ||
|
|
||
| @service appEvents; | ||
|
|
||
| @tracked searching = false; | ||
| @tracked collapsedResults = true; | ||
| @tracked results = []; | ||
|
|
||
| @computed("args.outletArgs.search") | ||
| get searchTerm() { | ||
| return this.args.outletArgs.search; | ||
| } | ||
|
|
||
| @computed("args.outletArgs.type") | ||
| get searchEnabled() { | ||
| return this.args.outletArgs.type === SEARCH_TYPE_DEFAULT; | ||
| } | ||
|
|
||
| @computed("results") | ||
| get collapsedResultsTitle() { | ||
| return I18n.t("discourse_ai.embeddings.semantic_search_results.toggle", { | ||
| count: this.results.length, | ||
| }); | ||
| } | ||
|
|
||
| @action | ||
| setup() { | ||
| this.appEvents.on( | ||
| "full-page-search:trigger-search", | ||
| this, | ||
| "debouncedSearch" | ||
| ); | ||
| } | ||
|
|
||
| @action | ||
| teardown() { | ||
| this.appEvents.off( | ||
| "full-page-search:trigger-search", | ||
| this, | ||
| "debouncedSearch" | ||
| ); | ||
| } | ||
|
|
||
| @bind | ||
| performHyDESearch() { | ||
| if (!this.searchTerm || !this.searchEnabled || this.searching) { | ||
| return; | ||
| } | ||
|
|
||
| this.searching = true; | ||
| this.collapsedResults = true; | ||
| this.results = []; | ||
|
|
||
| ajax("/discourse-ai/embeddings/semantic-search", { | ||
| data: { q: this.searchTerm }, | ||
| }) | ||
| .then(async (results) => { | ||
| const model = (await translateResults(results)) || {}; | ||
| this.results = model.posts; | ||
| }) | ||
| .finally(() => (this.searching = false)); | ||
| } | ||
|
|
||
| @action | ||
| debouncedSearch() { | ||
| discourseDebounce(this, this.performHyDESearch, 500); | ||
| } | ||
| } |
63 changes: 0 additions & 63 deletions
63
assets/javascripts/initializers/semantic-full-page-search.js
This file was deleted.
Oops, something went wrong.
39 changes: 39 additions & 0 deletions
39
assets/stylesheets/modules/embeddings/common/semantic-search.scss
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| .semantic-search__container { | ||
| background: var(--primary-very-low); | ||
| margin: 1rem 0 1rem 0; | ||
|
|
||
| .semantic-search__results { | ||
| display: flex; | ||
| flex-direction: column; | ||
| align-items: baseline; | ||
|
|
||
| .semantic-search { | ||
| &__searching-text { | ||
| display: inline-block; | ||
| margin-left: 3px; | ||
| } | ||
| &__indicator-wave { | ||
| flex: 0 0 auto; | ||
| display: inline-flex; | ||
| } | ||
| &__indicator-dot { | ||
| display: inline-block; | ||
| animation: ai-summary__indicator-wave 1.8s linear infinite; | ||
| &:nth-child(2) { | ||
| animation-delay: -1.6s; | ||
| } | ||
| &:nth-child(3) { | ||
| animation-delay: -1.4s; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| .semantic-search__entries { | ||
| margin-top: 10px; | ||
| } | ||
|
|
||
| .semantic-search__searching { | ||
| margin-left: 5px; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -177,6 +177,18 @@ discourse_ai: | |
| ai_embeddings_semantic_search_enabled: | ||
| default: false | ||
| client: true | ||
| ai_embeddings_semantic_search_hyde_model: | ||
| default: "gpt-3.5-turbo" | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I remember this being neccessary for things to work, but I don't know if it's still true after all the changes. Will take a look. |
||
| type: enum | ||
| allow_any: false | ||
| choices: | ||
| - Llama2-*-chat-hf | ||
| - claude-instant-1 | ||
| - claude-2 | ||
| - gpt-3.5-turbo | ||
| - gpt-4 | ||
| - StableBeluga2 | ||
| - Upstage-Llama-2-*-instruct-v2 | ||
|
|
||
| ai_summarization_discourse_service_api_endpoint: "" | ||
| ai_summarization_discourse_service_api_key: | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module DiscourseAi | ||
| module Embeddings | ||
| module HydeGenerators | ||
| class Anthropic < DiscourseAi::Embeddings::HydeGenerators::Base | ||
| def prompt(search_term) | ||
| <<~TEXT | ||
| Given a search term given between <input> tags, generate a forum post about the search term. | ||
| Respond with the generated post between <ai> tags. | ||
|
|
||
| <input>#{search_term}</input> | ||
| TEXT | ||
| end | ||
|
|
||
| def models | ||
| %w[claude-instant-1 claude-2] | ||
| end | ||
|
|
||
| def hypothetical_post_from(query) | ||
| response = | ||
| ::DiscourseAi::Inference::AnthropicCompletions.perform!( | ||
| prompt(query), | ||
| SiteSetting.ai_embeddings_semantic_search_hyde_model, | ||
| ).dig(:completion) | ||
|
|
||
| Nokogiri::HTML5.fragment(response).at("ai").text | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.