Expand description
§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:
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:
// 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());
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.
Modules§
- __
mock_ Mock Answer - __
mock_ Mock Answer_ Answer - __
mock_ Mock Answer_ Clone - __
mock_ Mock Evaluate Query - __
mock_ Mock Evaluate Query_ Clone - __
mock_ Mock Evaluate Query_ Evaluate Query - __
mock_ Mock Transform Query - __
mock_ Mock Transform Query_ Clone - __
mock_ Mock Transform Query_ Transform Query - __
mock_ Mock Transform Response - __
mock_ Mock Transform Response_ Clone - __
mock_ Mock Transform Response_ Transform Response - answers
- Given a query, generate an answer
- evaluators
- This module contains evaluators for evaluating the quality of a pipeline.
- query_
transformers - Transform queries that are yet to be made
- response_
transformers - Transform retrieved queries
- search_
strategies - states
- States of a query
Structs§
- Document
- A document represents a single unit of retrieved text
- Document
Builder - Builder for
Document
. - Mock
Answer - Mock
Evaluate Query - Mock
Transform Query - Mock
Transform Response - Pipeline
- The starting point of a query pipeline
- Query
- A query is the main object going through a query pipeline
- Query
Builder - Builder for
Query
. - Query
Stream - Internally used by a query pipeline
Enums§
- Document
Builder Error - Error type for DocumentBuilder
- Query
Builder Error - Error type for QueryBuilder
- Query
Evaluation - Wraps a query for evaluation. Used by the
crate::query_traits::EvaluateQuery
trait. - Transformation
Event - Records changes to a query
Traits§
- Answer
- Can answer the original query
- CanRetrieve
- Marker trait for query states that can still retrieve
- Evaluate
Query - Evaluates a query
- Query
State - Marker trait for query states
- Retrieve
- Can retrieve documents given a SearchStrategy
- Search
Strategy - A search strategy for the query pipeline
- Stream
Ext - An extension trait for
Stream
s that provides a variety of convenient combinator functions. - Transform
Query - Can transform queries before retrieval
- Transform
Response - Can transform a response after retrieval
- TryStream
Ext - Adapters specific to
Result
-returning streams