FEATURE: Tweak HyDE prompts for better grounding in forum subject and limit response size (#200)

* FEATURE: Tweak HyDE prompts for better grounding in forum subject and limit response size

* fix test

* lint
This commit is contained in:
Rafael dos Santos Silva 2023-09-05 16:11:07 -03:00 committed by GitHub
parent 175def1267
commit 4b42c09814
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 34 additions and 12 deletions

View File

@ -6,10 +6,11 @@ module DiscourseAi
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.
Given a search term given between <input> tags, generate a forum post about a given subject.
#{basic_prompt_instruction}
<input>#{search_term}</input>
Respond with the generated post between <ai> tags.
TEXT
end
@ -22,6 +23,7 @@ module DiscourseAi
::DiscourseAi::Inference::AnthropicCompletions.perform!(
prompt(query),
SiteSetting.ai_embeddings_semantic_search_hyde_model,
max_tokens: 400,
).dig(:completion)
Nokogiri::HTML5.fragment(response).at("ai").text

View File

@ -11,6 +11,17 @@ module DiscourseAi
)
end
end
def basic_prompt_instruction
<<~TEXT
Act as a content writer for a forum.
The forum description is as follows:
#{SiteSetting.title}
#{SiteSetting.site_description}
Given the forum description write a forum post about the following subject:
TEXT
end
end
end
end

View File

@ -8,12 +8,13 @@ module DiscourseAi
<<~TEXT
[INST] <<SYS>>
You are a helpful bot
You create forum posts about a given topic
You create forum posts about a given subject
<</SYS>>
Topic: #{search_term}
#{basic_prompt_instruction}
#{search_term}
[/INST]
Here is a forum post about the above topic:
Here is a forum post about the above subject:
TEXT
end

View File

@ -8,13 +8,14 @@ module DiscourseAi
<<~TEXT
### System:
You are a helpful bot
You create forum posts about a given topic
You create forum posts about a given subject
### User:
Topic: #{search_term}
#{basic_prompt_instruction}
#{search_term}
### Assistant:
Here is a forum post about the above topic:
Here is a forum post about the above subject:
TEXT
end

View File

@ -8,9 +8,9 @@ module DiscourseAi
[
{
role: "system",
content: "You are a helpful bot. You create forum posts about a given topic.",
content: "You are a helpful bot. You create forum posts about a given subject.",
},
{ role: "user", content: "Create a forum post about the topic: #{search_term}" },
{ role: "user", content: "#{basic_prompt_instruction}\n#{search_term}" },
]
end
@ -22,6 +22,7 @@ module DiscourseAi
::DiscourseAi::Inference::OpenAiCompletions.perform!(
prompt(query),
SiteSetting.ai_embeddings_semantic_search_hyde_model,
max_tokens: 400,
).dig(:choices, 0, :message, :content)
end
end

View File

@ -17,7 +17,13 @@ RSpec.describe DiscourseAi::Embeddings::SemanticSearch do
SiteSetting.ai_embeddings_discourse_service_api_endpoint = "http://test.com"
prompt = DiscourseAi::Embeddings::HydeGenerators::OpenAi.new.prompt(query)
OpenAiCompletionsInferenceStubs.stub_response(prompt, hypothetical_post)
OpenAiCompletionsInferenceStubs.stub_response(
prompt,
hypothetical_post,
req_opts: {
max_tokens: 400,
},
)
hyde_embedding = [0.049382, 0.9999]
EmbeddingsGenerationStubs.discourse_service(