Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ai-help): switch to markdown context #410

Merged
merged 3 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 48 additions & 2 deletions src/ai/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ pub struct AIHelpConfig {
pub make_context: fn(Vec<RelatedDoc>) -> String,
}

fn join_with_tags(related_docs: Vec<RelatedDoc>) -> String {
related_docs
.into_iter()
.flat_map(|d| ["<article>".to_string(), d.content, "</article>".to_string()])
.join("\n")
}

pub const AI_HELP_GPT3_5_FULL_DOC_NEW_PROMPT: AIHelpConfig = AIHelpConfig {
name: "20230901-full_doc-new_prompt",
model: "gpt-3.5-turbo-1106",
Expand All @@ -25,7 +32,7 @@ pub const AI_HELP_GPT3_5_FULL_DOC_NEW_PROMPT: AIHelpConfig = AIHelpConfig {
token_limit: 16_384,
context_limit: 12_000,
max_completion_tokens: 2_048,
make_context: |related_docs| related_docs.into_iter().map(|d| d.content).join("\n"),
make_context: join_with_tags,
};

pub const AI_HELP_GPT4_FULL_DOC_NEW_PROMPT: AIHelpConfig = AIHelpConfig {
Expand All @@ -37,7 +44,7 @@ pub const AI_HELP_GPT4_FULL_DOC_NEW_PROMPT: AIHelpConfig = AIHelpConfig {
token_limit: 32_768,
context_limit: 20_000,
max_completion_tokens: 4_096,
make_context: |related_docs| related_docs.into_iter().map(|d| d.content).join("\n"),
make_context: join_with_tags,
};

pub const MODEL: &str = "gpt-3.5-turbo";
Expand Down Expand Up @@ -69,3 +76,42 @@ outputted in markdown format.\
";

pub const AI_EXPLAIN_VERSION: i64 = 1;

#[cfg(test)]
mod test {
use super::*;

#[test]
fn test_join() {
let related_docs = vec![
RelatedDoc {
url: "".into(),
title: "".into(),
content: "content1".into(),
similarity: 0f64,
},
RelatedDoc {
url: "".into(),
title: "".into(),
content: "content2".into(),
similarity: 0f64,
},
RelatedDoc {
url: "".into(),
title: "".into(),
content: "content3".into(),
similarity: 0f64,
},
];
let expected = r#"<article>
content1
</article>
<article>
content2
</article>
<article>
content3
</article>"#;
assert_eq!(join_with_tags(related_docs), expected)
}
}
4 changes: 2 additions & 2 deletions src/ai/embeddings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ const MACRO_EMB_DOC_LIMIT: i64 = 5;
const MACRO_DOCS_QUERY: &str = "select
mdn_doc_macro.mdn_url as url,
mdn_doc_macro.title,
mdn_doc_macro.html as content,
mdn_doc_macro.markdown as content,
mdn_doc_macro.embedding <=> $1 as similarity
from mdn_doc_macro
where length(mdn_doc_macro.html) >= $4
where length(mdn_doc_macro.markdown) >= $4
and (mdn_doc_macro.embedding <=> $1) < $2
and mdn_doc_macro.mdn_url not like '/en-US/docs/MDN%'
order by mdn_doc_macro.embedding <=> $1
Expand Down
Loading