mirror of
https://github.com/discourse/discourse-ai.git
synced 2025-07-15 18:43:29 +00:00
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:
parent
175def1267
commit
4b42c09814
@ -6,10 +6,11 @@ module DiscourseAi
|
|||||||
class Anthropic < DiscourseAi::Embeddings::HydeGenerators::Base
|
class Anthropic < DiscourseAi::Embeddings::HydeGenerators::Base
|
||||||
def prompt(search_term)
|
def prompt(search_term)
|
||||||
<<~TEXT
|
<<~TEXT
|
||||||
Given a search term given between <input> tags, generate a forum post about the search term.
|
Given a search term given between <input> tags, generate a forum post about a given subject.
|
||||||
Respond with the generated post between <ai> tags.
|
#{basic_prompt_instruction}
|
||||||
|
|
||||||
<input>#{search_term}</input>
|
<input>#{search_term}</input>
|
||||||
|
|
||||||
|
Respond with the generated post between <ai> tags.
|
||||||
TEXT
|
TEXT
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -22,6 +23,7 @@ module DiscourseAi
|
|||||||
::DiscourseAi::Inference::AnthropicCompletions.perform!(
|
::DiscourseAi::Inference::AnthropicCompletions.perform!(
|
||||||
prompt(query),
|
prompt(query),
|
||||||
SiteSetting.ai_embeddings_semantic_search_hyde_model,
|
SiteSetting.ai_embeddings_semantic_search_hyde_model,
|
||||||
|
max_tokens: 400,
|
||||||
).dig(:completion)
|
).dig(:completion)
|
||||||
|
|
||||||
Nokogiri::HTML5.fragment(response).at("ai").text
|
Nokogiri::HTML5.fragment(response).at("ai").text
|
||||||
|
@ -11,6 +11,17 @@ module DiscourseAi
|
|||||||
)
|
)
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
end
|
end
|
||||||
|
@ -8,12 +8,13 @@ module DiscourseAi
|
|||||||
<<~TEXT
|
<<~TEXT
|
||||||
[INST] <<SYS>>
|
[INST] <<SYS>>
|
||||||
You are a helpful bot
|
You are a helpful bot
|
||||||
You create forum posts about a given topic
|
You create forum posts about a given subject
|
||||||
<</SYS>>
|
<</SYS>>
|
||||||
|
|
||||||
Topic: #{search_term}
|
#{basic_prompt_instruction}
|
||||||
|
#{search_term}
|
||||||
[/INST]
|
[/INST]
|
||||||
Here is a forum post about the above topic:
|
Here is a forum post about the above subject:
|
||||||
TEXT
|
TEXT
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -8,13 +8,14 @@ module DiscourseAi
|
|||||||
<<~TEXT
|
<<~TEXT
|
||||||
### System:
|
### System:
|
||||||
You are a helpful bot
|
You are a helpful bot
|
||||||
You create forum posts about a given topic
|
You create forum posts about a given subject
|
||||||
|
|
||||||
### User:
|
### User:
|
||||||
Topic: #{search_term}
|
#{basic_prompt_instruction}
|
||||||
|
#{search_term}
|
||||||
|
|
||||||
### Assistant:
|
### Assistant:
|
||||||
Here is a forum post about the above topic:
|
Here is a forum post about the above subject:
|
||||||
TEXT
|
TEXT
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -8,9 +8,9 @@ module DiscourseAi
|
|||||||
[
|
[
|
||||||
{
|
{
|
||||||
role: "system",
|
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
|
end
|
||||||
|
|
||||||
@ -22,6 +22,7 @@ module DiscourseAi
|
|||||||
::DiscourseAi::Inference::OpenAiCompletions.perform!(
|
::DiscourseAi::Inference::OpenAiCompletions.perform!(
|
||||||
prompt(query),
|
prompt(query),
|
||||||
SiteSetting.ai_embeddings_semantic_search_hyde_model,
|
SiteSetting.ai_embeddings_semantic_search_hyde_model,
|
||||||
|
max_tokens: 400,
|
||||||
).dig(:choices, 0, :message, :content)
|
).dig(:choices, 0, :message, :content)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -17,7 +17,13 @@ RSpec.describe DiscourseAi::Embeddings::SemanticSearch do
|
|||||||
SiteSetting.ai_embeddings_discourse_service_api_endpoint = "http://test.com"
|
SiteSetting.ai_embeddings_discourse_service_api_endpoint = "http://test.com"
|
||||||
|
|
||||||
prompt = DiscourseAi::Embeddings::HydeGenerators::OpenAi.new.prompt(query)
|
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]
|
hyde_embedding = [0.049382, 0.9999]
|
||||||
EmbeddingsGenerationStubs.discourse_service(
|
EmbeddingsGenerationStubs.discourse_service(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user