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

[#428] Suggestion from review #431

Merged
Merged
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: 24 additions & 26 deletions src/ai/embeddings.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::collections::HashMap;

use async_openai::{config::OpenAIConfig, types::CreateEmbeddingRequestArgs, Client};
use itertools::Itertools;

use crate::{
ai::{constants::EMBEDDING_MODEL, error::AIError},
Expand Down Expand Up @@ -57,7 +56,7 @@ WHERE LENGTH(doc.markdown) >= $4
ORDER BY doc.embedding <=> $1
LIMIT $3;";

#[derive(sqlx::FromRow, Clone, Debug)]
#[derive(sqlx::FromRow, Debug)]
pub struct RelatedDoc {
pub url: String,
pub title: String,
Expand Down Expand Up @@ -88,33 +87,32 @@ pub async fn get_related_macro_docs(
.fetch_all(pool)
.await?;

let duplicate_titles =
get_duplicate_titles(docs.clone().into_iter().map(|doc| doc.title).collect());

docs = docs
.into_iter()
.map(|doc| RelatedDoc {
title: match (duplicate_titles.contains(&doc.title), &doc.title_parent) {
(true, Some(title_parent)) => format!("{} ({})", doc.title, title_parent),
_ => doc.title,
},
..doc
})
.collect();
let duplicate_titles = get_duplicate_titles(&docs);

docs.iter_mut().for_each(|doc| {
if let (true, Some(title_parent)) =
(duplicate_titles.contains(&doc.title), &doc.title_parent)
{
doc.title = format!("{} ({})", doc.title, title_parent);
}
});

Ok(docs)
}

fn get_duplicate_titles(titles: Vec<String>) -> Vec<String> {
let mut counts: HashMap<String, u8> = HashMap::new();
for title in titles.iter() {
let count = counts.entry(title.to_string()).or_insert(0);
*count += 1;
}
counts
.into_iter()
.filter(|(_, count)| count > &1)
.map(|(title, _)| title)
fn get_duplicate_titles<'a>(docs: &'a [RelatedDoc]) -> Vec<String> {
docs
.iter()
.map(|x| &x.title)
.sorted()
.dedup_by_with_count(|a, b| a == b)
.filter_map(|(count, title)| {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where does dedup_by_with_count() come from? I don't see it in the stdlib.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But looks like we can just use duplicates() instead.

if count > 1 {
Some(title.to_string())
} else {
None
}
})
.collect()
}

Expand Down
Loading