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

refactor: Improve builder api with bon #401

Closed
wants to merge 1 commit into from
Closed
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
55 changes: 51 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ similar.opt-level = 3
[workspace.dependencies]
anyhow = { version = "1.0" }
async-trait = { version = "0.1" }
derive_builder = { version = "0.20" }
bon = { version = "2.3" }
futures-util = { version = "0.3" }
tokio = { version = "1.38", features = ["full"] }
tokio-stream = { version = "0.1" }
Expand Down Expand Up @@ -70,6 +70,8 @@ temp-dir = "0.1.13"
wiremock = "0.6.0"
test-case = "3.3.1"
insta = { version = "1.39.0", features = ["yaml"] }
pretty_assertions = { version = "1.4" }
prettyplease = { version = "0.2" }

[workspace.lints.rust]
unsafe_code = "forbid"
Expand Down
2 changes: 1 addition & 1 deletion swiftide-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ strum = { workspace = true }
strum_macros = { workspace = true }
mockall = { workspace = true, optional = true }
lazy_static = { workspace = true }
derive_builder = { workspace = true }
bon = { workspace = true }
dyn-clone = { workspace = true }

tera = { version = "1.20", default-features = false }
Expand Down
2 changes: 1 addition & 1 deletion swiftide-core/src/prelude.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub use anyhow::{Context as _, Result};
pub use async_trait::async_trait;
pub use derive_builder::Builder;
pub use bon::Builder;
pub use futures_util::{StreamExt, TryStreamExt};
pub use std::sync::Arc;
pub use tracing::Instrument;
Expand Down
14 changes: 4 additions & 10 deletions swiftide-core/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//! `states::Pending`: No documents have been retrieved
//! `states::Retrieved`: Documents have been retrieved
//! `states::Answered`: The query has been answered
use derive_builder::Builder;
use bon::Builder;

use crate::{util::debug_long_utf8, Embedding, SparseEmbedding};

Expand All @@ -19,7 +19,7 @@ type Document = String;
/// `states::Retrieved`: Documents have been retrieved
/// `states::Answered`: The query has been answered
#[derive(Clone, Default, Builder, PartialEq)]
#[builder(setter(into))]
#[builder(on(_, into))]
pub struct Query<State> {
original: String,
#[builder(default = "self.original.clone().unwrap_or_default()")]
Expand All @@ -29,10 +29,8 @@ pub struct Query<State> {
transformation_history: Vec<TransformationEvent>,

// TODO: How would this work when doing a rollup query?
#[builder(default)]
pub embedding: Option<Embedding>,

#[builder(default)]
pub sparse_embedding: Option<SparseEmbedding>,
}

Expand All @@ -49,10 +47,6 @@ impl<T: std::fmt::Debug> std::fmt::Debug for Query<T> {
}

impl<T: Clone> Query<T> {
pub fn builder() -> QueryBuilder<T> {
QueryBuilder::default().clone()
}

/// Return the query it started with
pub fn original(&self) -> &str {
&self.original
Expand Down Expand Up @@ -173,7 +167,7 @@ pub mod states {
pub struct Pending;

#[derive(Default, Clone, Builder, PartialEq)]
#[builder(setter(into))]
#[builder(on(_, into))]
/// Documents have been retrieved
pub struct Retrieved {
pub(crate) documents: Vec<Document>,
Expand All @@ -196,7 +190,7 @@ pub mod states {
}

#[derive(Default, Clone, Builder, PartialEq)]
#[builder(setter(into))]
#[builder(on(_, into))]
/// The query has been answered
pub struct Answered {
pub(crate) answer: String,
Expand Down
4 changes: 2 additions & 2 deletions swiftide-core/src/search_strategies/hybrid_search.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use derive_builder::Builder;
use bon::Builder;

use crate::{indexing::EmbeddedField, querying};

Expand All @@ -9,7 +9,7 @@ use super::{DEFAULT_TOP_K, DEFAULT_TOP_N};
///
/// Defaults to a a maximum of 10 documents and `EmbeddedField::Combined` for the field(s).
#[derive(Debug, Clone, Builder)]
#[builder(setter(into))]
#[builder(on(_, into))]
pub struct HybridSearch {
/// Maximum number of documents to return
#[builder(default)]
Expand Down
2 changes: 1 addition & 1 deletion swiftide-indexing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ swiftide-macros = { path = "../swiftide-macros", version = "0.13" }

anyhow = { workspace = true }
async-trait = { workspace = true }
derive_builder = { workspace = true }
bon = { workspace = true }
futures-util = { workspace = true }
tokio = { workspace = true, features = ["full"] }
tokio-stream = { workspace = true }
Expand Down
8 changes: 4 additions & 4 deletions swiftide-indexing/src/persist/memory_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{collections::HashMap, sync::Arc};

use anyhow::Result;
use async_trait::async_trait;
use derive_builder::Builder;
use bon::Builder;
use tokio::sync::RwLock;

use swiftide_core::{
Expand All @@ -11,18 +11,18 @@ use swiftide_core::{
};

#[derive(Debug, Default, Builder, Clone)]
#[builder(pattern = "owned")]
/// A simple in-memory storage implementation.
///
/// Great for experimentation and testing.
///
/// By default the storage will use a zero indexed, incremental counter as the key for each node if the node id
/// is not set.
#[builder(on(_, into))]
pub struct MemoryStorage {
data: Arc<RwLock<HashMap<String, Node>>>,
#[builder(default)]
batch_size: Option<usize>,
#[builder(default = "Arc::new(RwLock::new(0))")]

#[builder(skip = Arc::new(RwLock::new(0)))]
node_count: Arc<RwLock<u64>>,
}

Expand Down
70 changes: 40 additions & 30 deletions swiftide-indexing/src/transformers/chunk_markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
use std::sync::Arc;

use async_trait::async_trait;
use derive_builder::Builder;
use bon::Builder;
use swiftide_core::{indexing::IndexingStream, indexing::Node, ChunkerTransformer};
use text_splitter::{Characters, MarkdownSplitter};
use text_splitter::{Characters, ChunkConfig, ChunkSizer, MarkdownSplitter};

#[derive(Debug, Clone, Builder)]
#[builder(pattern = "owned", setter(strip_option))]
const DEFAULT_MAX_CHAR_SIZE: usize = 2056;

#[derive(Clone, Builder)]
/// A transformer that chunks markdown content into smaller pieces.
///
/// The transformer will split the markdown content into smaller pieces based on the specified
Expand All @@ -17,42 +18,52 @@ use text_splitter::{Characters, MarkdownSplitter};
///
/// Technically that might work with every splitter `text_splitter` provides.
pub struct ChunkMarkdown {
#[builder(setter(into))]
chunker: Arc<MarkdownSplitter<Characters>>,
#[builder(default)]
/// The number of concurrent chunks to process.
/// Defaults to `None`. If you use a splitter that is resource heavy, this parameter can be
/// tuned.
concurrency: Option<usize>,

/// Optional maximum number of characters per chunk.
///
/// Defaults to [`DEFAULT_MAX_CHAR_SIZE`].
#[builder(default = DEFAULT_MAX_CHAR_SIZE)]
max_characters: usize,

/// The splitter is not perfect in skipping min size nodes.
///
/// If you provide a custom chunker, you might want to set the range as well.
#[builder(default)]
range: Option<std::ops::Range<usize>>,
/// If you provide a custom chunker with a range, you might want to set the range as well.
///
/// Defaults to 0..[`max_characters`]
#[builder(default = 0..max_characters)]
range: std::ops::Range<usize>,

/// The markdown splitter from [`text_splitter`]
///
/// Defaults to a new [`MarkdownSplitter`] with the specified `max_characters`.
#[builder(into, default = Arc::new(MarkdownSplitter::new(range.clone())))]
chunker: Arc<MarkdownSplitter<Characters>>,
}

impl std::fmt::Debug for ChunkMarkdown {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ChunkMarkdown")
.field("concurrency", &self.concurrency)
.field("max_characters", &self.max_characters)
.field("range", &self.range)
.finish()
}
}

impl ChunkMarkdown {
/// Create a new transformer with a maximum number of characters per chunk.
pub fn from_max_characters(max_characters: usize) -> Self {
Self {
chunker: Arc::new(MarkdownSplitter::new(max_characters)),
concurrency: None,
range: None,
}
Self::builder().max_characters(max_characters).build()
}

/// Create a new transformer with a range of characters per chunk.
///
/// Chunks smaller than the range will be ignored.
pub fn from_chunk_range(range: std::ops::Range<usize>) -> Self {
Self {
chunker: Arc::new(MarkdownSplitter::new(range.clone())),
concurrency: None,
range: Some(range),
}
}

/// Build a custom markdown chunker.
pub fn builder() -> ChunkMarkdownBuilder {
ChunkMarkdownBuilder::default()
Self::builder().range(range).build()
}

/// Set the number of concurrent chunks to process.
Expand All @@ -63,13 +74,13 @@ impl ChunkMarkdown {
}

fn min_size(&self) -> usize {
self.range.as_ref().map_or(0, |r| r.start)
self.range.start
}
}

#[async_trait]
impl ChunkerTransformer for ChunkMarkdown {
#[tracing::instrument(skip_all, name = "transformers.chunk_markdown")]
#[tracing::instrument(skip_all)]
async fn transform_node(&self, node: Node) -> IndexingStream {
let chunks = self
.chunker
Expand Down Expand Up @@ -176,7 +187,6 @@ mod test {
.chunker(MarkdownSplitter::new(40))
.concurrency(10)
.range(10..20)
.build()
.unwrap();
.build();
}
}
Loading
Loading