1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// show feature flags in the generated documentation
// https://doc.rust-lang.org/rustdoc/unstable-features.html#extensions-to-the-doc-attribute
//! Swiftide is a Rust library for building LLM applications, enabling fast data ingestion,
//! transformation, and indexing for effective querying and prompt injection, known as Retrieval
//! Augmented Generation. It provides flexible building blocks for creating various agents, allowing
//! rapid development from concept to production with minimal code.
//!
//! Part of the [bosun.ai](https://bosun.ai) project. An upcoming platform for autonomous code improvement.
//!
//! We <3 feedback: project ideas, suggestions, and complaints are very welcome. Feel free to open
//! an issue.
//!
//! Read more about the project on the [swiftide website](https://swiftide.rs)
//!
//! ### High level features
//!
//! - Build fast, streaming indexing and querying pipelines
//! - Easily build agents, mix and match with previously built pipelines
//! - A modular and extendable API, with minimal abstractions
//! - Integrations with popular LLMs and storage providers
//! - Ready to use pipeline transformations
//!
//! # Querying
//!
//! After running an indexing pipeline, you can use the [`query`] module to query the indexed data.
//!
//! # Examples
//!
//! ## Indexing markdown
//!
//! ```no_run
//! # use swiftide::indexing::loaders::FileLoader;
//! # use swiftide::indexing::transformers::{ChunkMarkdown, Embed, MetadataQAText};
//! # use swiftide::integrations::qdrant::Qdrant;
//! # use swiftide::integrations::openai::OpenAI;
//! # use swiftide::indexing::Pipeline;
//! # use anyhow::Result;
//!
//! # #[tokio::main]
//! # async fn main() -> Result<()> {
//! # let qdrant_url = "url";
//! # let openai_client = OpenAI::builder().build()?;
//! Pipeline::from_loader(FileLoader::new(".").with_extensions(&["md"]))
//! .then_chunk(ChunkMarkdown::from_chunk_range(10..512))
//! .then(MetadataQAText::new(openai_client.clone()))
//! .then_in_batch(Embed::new(openai_client.clone()).with_batch_size(10))
//! .then_store_with(
//! Qdrant::try_from_url(qdrant_url)?
//! .batch_size(50)
//! .vector_size(1536)
//! .collection_name("swiftide-examples".to_string())
//! .build()?,
//! )
//! .run()
//! .await
//! # }
//! ```
//!
//! ## Querying
//!
//! ```no_run
//! # use anyhow::Result;
//! # use swiftide::query::{query_transformers, self, response_transformers, answers};
//! # use swiftide::integrations::openai::OpenAI;
//!
//! # #[tokio::main]
//! # async fn main() -> Result<()> {
//! # let qdrant_url = "url";
//! # let openai_client = OpenAI::builder().build()?;
//! # let qdrant = swiftide::integrations::qdrant::Qdrant::try_from_url(qdrant_url)?
//! # .batch_size(50)
//! # .vector_size(1536)
//! # .collection_name("swiftide-examples".to_string())
//! # .build()?;
//! query::Pipeline::default()
//! .then_transform_query(query_transformers::GenerateSubquestions::from_client(
//! openai_client.clone(),
//! ))
//! .then_transform_query(query_transformers::Embed::from_client(
//! openai_client.clone(),
//! ))
//! .then_retrieve(qdrant.clone())
//! .then_transform_response(response_transformers::Summary::from_client(
//! openai_client.clone(),
//! ))
//! .then_answer(answers::Simple::from_client(openai_client.clone()))
//! .query("What is swiftide?")
//! .await?;
//! # Ok(())
//! # }
//! ```
//! ## Agents with tools
//!
//!
//! ```ignore
//! #[swiftide::tool(
//! description = "Searches code",
//! param(name = "code_query", description = "The code query")
//! )]
//! async fn search_code(
//! context: &dyn AgentContext,
//! code_query: &str,
//! ) -> Result<ToolOutput, ToolError> {
//! let command_output = context
//! .executor()
//! .exec_cmd(&Command::shell(format!("rg '{code_query}'")))
//! .await?;
//!
//! Ok(command_output.into())
//! }
//!
//! agents::Agent::builder()
//! .llm(&openai)
//! .tools(vec![search_code()])
//! .build()?
//! .query("In what file can I find an example of a swiftide agent?")
//! .await?;
//! ```
//! # Feature flags
//!
//! Swiftide has little features enabled by default, as there are some dependency heavy
//! integrations. You need to cherry-pick the tools and integrations you want to use.
pub use prompt;
pub use *;
pub use swiftide_agents as agents;
/// Common traits for common behaviour, re-exported from indexing and query
/// Integrations with various platforms and external services.
/// This module serves as the main entry point for indexing in Swiftide.
///
/// The indexing system in Swiftide is designed to handle the asynchronous processing of large
/// volumes of data, including loading, transforming, and storing data chunks.
pub use *;
/// # Querying pipelines
///
/// Swiftide allows you to define sophisticated query pipelines.
///
/// Consider the following code that uses Swiftide to load some markdown text, chunk it, embed it,
/// and store it in a Qdrant index:
///
/// ```no_run
/// use swiftide::{
/// indexing::{
/// self,
/// loaders::FileLoader,
/// transformers::{ChunkMarkdown, Embed, MetadataQAText},
/// },
/// integrations::{self, qdrant::Qdrant},
/// integrations::openai::OpenAI,
/// query::{self, answers, query_transformers, response_transformers},
/// };
///
/// async fn index() -> Result<(), Box<dyn std::error::Error>> {
/// let openai_client = OpenAI::builder()
/// .default_embed_model("text-embedding-3-large")
/// .default_prompt_model("gpt-4o")
/// .build()?;
///
/// let qdrant = Qdrant::builder()
/// .batch_size(50)
/// .vector_size(3072)
/// .collection_name("swiftide-examples")
/// .build()?;
///
/// indexing::Pipeline::from_loader(FileLoader::new("README.md"))
/// .then_chunk(ChunkMarkdown::from_chunk_range(10..2048))
/// .then(MetadataQAText::new(openai_client.clone()))
/// .then_in_batch(Embed::new(openai_client.clone()).with_batch_size(10))
/// .then_store_with(qdrant.clone())
/// .run()
/// .await?;
///
/// Ok(())
/// }
/// ```
///
/// We could then define a query pipeline that uses the Qdrant index to answer questions:
///
/// ```no_run
/// # use swiftide::{
/// # indexing::{
/// # self,
/// # loaders::FileLoader,
/// # transformers::{ChunkMarkdown, Embed, MetadataQAText},
/// # },
/// # integrations::{self, qdrant::Qdrant},
/// # query::{self, answers, query_transformers, response_transformers},
/// # integrations::openai::OpenAI,
/// # };
/// # async fn query() -> Result<(), Box<dyn std::error::Error>> {
/// # let openai_client = OpenAI::builder()
/// # .default_embed_model("text-embedding-3-large")
/// # .default_prompt_model("gpt-4o")
/// # .build()?;
/// # let qdrant = Qdrant::builder()
/// # .batch_size(50)
/// # .vector_size(3072)
/// # .collection_name("swiftide-examples")
/// # .build()?;
/// // By default the search strategy is SimilaritySingleEmbedding
/// // which takes the latest query, embeds it, and does a similarity search
/// let pipeline = query::Pipeline::default()
/// .then_transform_query(query_transformers::GenerateSubquestions::from_client(
/// openai_client.clone(),
/// ))
/// .then_transform_query(query_transformers::Embed::from_client(
/// openai_client.clone(),
/// ))
/// .then_retrieve(qdrant.clone())
/// .then_transform_response(response_transformers::Summary::from_client(
/// openai_client.clone(),
/// ))
/// .then_answer(answers::Simple::from_client(openai_client.clone()));
///
/// let result = pipeline
/// .query("What is swiftide? Please provide an elaborate explanation")
/// .await?;
///
/// println!("{:?}", result.answer());
/// # Ok(())
/// # }
/// ```
///
/// By using a query pipeline to transform queries, we can improve the quality of the answers we get
/// from our index. In this example, we used an LLM to generate subquestions, embedding those and
/// then using them to search the index. Finally, we summarize the results and combine them together
/// into a single answer.
/// Re-exports for macros