Skip to content
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
2015fc5
feat: Qdrant storage
Anush008 Mar 20, 2025
d814974
feat: Qdrant storage support
Anush008 Mar 24, 2025
8733bc1
Merge branch 'main' into qdrant
Anush008 Mar 24, 2025
b0e4a4d
chore: review updates
Anush008 Mar 26, 2025
61aa6d0
Merge branch 'main' into qdrant
Anush008 Mar 26, 2025
678f9fd
chore: New API check_state_compatibility
Anush008 Mar 26, 2025
f857b83
refactor: Simplify payload conversion
Anush008 Mar 27, 2025
3a4791a
Merge remote-tracking branch 'upstream/main' into qdrant
Anush008 Apr 8, 2025
4a0bdf5
refactor: No ResourceSetupStatusCheck
Anush008 Apr 8, 2025
a4c6cae
refactor: Replaced SetupState with ()
Anush008 Apr 8, 2025
67f1e71
feat: Parse point ID
Anush008 Apr 8, 2025
c5f6a9d
Merge remote-tracking branch 'upstream/HEAD' into qdrant
Anush008 Apr 12, 2025
d7f419b
chore: Support all Value::Basic values
Anush008 Apr 13, 2025
8125f53
Merge remote-tracking branch 'upstream/HEAD' into qdrant
Anush008 Apr 13, 2025
379797c
chore: Handle all BasicValue types in search(), doc updates
Anush008 Apr 13, 2025
a2b58b1
doc: End-to-end example
Anush008 Apr 13, 2025
9dc9abc
feat: Support for api_key
Anush008 Apr 13, 2025
aa2acda
fix: no process-level CryptoProvider available -- call CryptoProvider…
Anush008 Apr 13, 2025
20221ae
Merge branch 'main' into qdrant
Anush008 Apr 14, 2025
67e3608
chore: Removed key_value_fields_iter()
Anush008 Apr 14, 2025
86fc14c
docs: examples/text_embedding_qdrant
Anush008 Apr 14, 2025
f749375
chore: Undo change to examples/pdf_embedding
Anush008 Apr 14, 2025
d9c93d4
feat: Optionally delete points
Anush008 Apr 14, 2025
32d94f7
chore: parse BasicValueType::Date
Anush008 Apr 14, 2025
19dee3e
refactor: Don't nest complex types
Anush008 Apr 14, 2025
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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ rustls = { version = "0.23.25" }
http-body-util = "0.1.3"
yaml-rust2 = "0.10.1"
urlencoding = "2.1.3"
qdrant-client = "1.13.0"
uuid = { version = "1.16.0", features = ["serde", "v4", "v8"] }
tokio-stream = "0.1.17"
async-stream = "0.3.6"
Expand Down
20 changes: 19 additions & 1 deletion docs/docs/ops/storages.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,28 @@ description: CocoIndex Built-in Storages

## Postgres

`Postgres` exports data to Postgres database (with pgvector extension).
Exports data to Postgres database (with pgvector extension).

The spec takes the following fields:

* `database_url` (type: `str`, optional): The URL of the Postgres database to use as the internal storage, e.g. `postgres://cocoindex:cocoindex@localhost/cocoindex`. If unspecified, will use the same database as the [internal storage](/docs/core/basics#internal-storage).

* `table_name` (type: `str`, optional): The name of the table to store to. If unspecified, will generate a new automatically. We recommend specifying a name explicitly if you want to directly query the table. It can be omitted if you want to use CocoIndex's query handlers to query the table.

## Qdrant

Exports data to a [Qdrant](https://qdrant.tech/) collection.

The spec takes the following fields:

* `collection_name` (type: `str`, required): The name of the collection to export the data to.

* `grpc_url` (type: `str`, optional): The [gRPC URL](https://qdrant.tech/documentation/interfaces/#grpc-interface) of the Qdrant instance. Defaults to `http://localhost:6334/`.

* `api_key` (type: `str`, optional). API key to authenticate requests with.

The field name for the vector embeddings must match the [vector name](https://qdrant.tech/documentation/concepts/vectors/#named-vectors) used when the collection was created.

If no primary key is set during export, a random UUID is used as the Qdrant point ID.

You can find an end-to-end example [here](https://github.com/cocoindex-io/cocoindex/tree/main/examples/text_embedding).
2 changes: 1 addition & 1 deletion examples/pdf_embedding/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def pdf_embedding_flow(flow_builder: cocoindex.FlowBuilder, data_scope: cocoinde

doc_embeddings.export(
"doc_embeddings",
cocoindex.storages.Postgres(),
cocoindex.storages.Qdrant(collection_name="cocoindex"),
primary_key_fields=["id"],
vector_index=[("embedding", cocoindex.VectorSimilarityMetric.COSINE_SIMILARITY)])

Expand Down
40 changes: 34 additions & 6 deletions examples/text_embedding/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,34 @@
Simple example for cocoindex: build embedding index based on local files.
## Description

## Prerequisite
[Install Postgres](https://cocoindex.io/docs/getting_started/installation#-install-postgres) if you don't have one.
Example to build a vector index in Qdrant based on local files.

## Pre-requisites

- [Install Postgres](https://cocoindex.io/docs/getting_started/installation#-install-postgres) if you don't have one.

- Run Qdrant.

```bash
docker run -d -p 6334:6334 -p 6333:6333 qdrant/qdrant
```

- [Create a collection](https://qdrant.tech/documentation/concepts/vectors/#named-vectors) to export the embeddings to.

```bash
curl -X PUT \
'http://localhost:6333/collections/cocoindex' \
--header 'Content-Type: application/json' \
--data-raw '{
"vectors": {
"text_embedding": {
"size": 384,
"distance": "Cosine"
}
}
}'
```

You can view the collections and data with the Qdrant dashboard at <http://localhost:6333/dashboard>.

## Run

Expand Down Expand Up @@ -29,13 +56,14 @@ Run:
python main.py
```

## CocoInsight
## CocoInsight

CocoInsight is in Early Access now (Free) 😊 You found us! A quick 3 minute video tutorial about CocoInsight: [Watch on YouTube](https://youtu.be/ZnmyoHslBSc?si=pPLXWALztkA710r9).

Run CocoInsight to understand your RAG data pipeline:

```
```bash
python main.py cocoindex server -c https://cocoindex.io
```

Then open the CocoInsight UI at [https://cocoindex.io/cocoinsight](https://cocoindex.io/cocoinsight).
Then open the CocoInsight UI at [https://cocoindex.io/cocoinsight](https://cocoindex.io/cocoinsight).
47 changes: 35 additions & 12 deletions examples/text_embedding/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,57 +2,79 @@

import cocoindex


def text_to_embedding(text: cocoindex.DataSlice) -> cocoindex.DataSlice:
"""
Embed the text using a SentenceTransformer model.
This is a shared logic between indexing and querying, so extract it as a function.
"""
return text.transform(
cocoindex.functions.SentenceTransformerEmbed(
model="sentence-transformers/all-MiniLM-L6-v2"))
model="sentence-transformers/all-MiniLM-L6-v2"
)
)


@cocoindex.flow_def(name="TextEmbedding")
def text_embedding_flow(flow_builder: cocoindex.FlowBuilder, data_scope: cocoindex.DataScope):
def text_embedding_flow(
flow_builder: cocoindex.FlowBuilder, data_scope: cocoindex.DataScope
):
"""
Define an example flow that embeds text into a vector database.
"""
data_scope["documents"] = flow_builder.add_source(
cocoindex.sources.LocalFile(path="markdown_files"))
cocoindex.sources.LocalFile(path="markdown_files")
)

doc_embeddings = data_scope.add_collector()

with data_scope["documents"].row() as doc:
doc["chunks"] = doc["content"].transform(
cocoindex.functions.SplitRecursively(),
language="markdown", chunk_size=2000, chunk_overlap=500)
language="markdown",
chunk_size=2000,
chunk_overlap=500,
)

with doc["chunks"].row() as chunk:
chunk["embedding"] = text_to_embedding(chunk["text"])
doc_embeddings.collect(filename=doc["filename"], location=chunk["location"],
text=chunk["text"], embedding=chunk["embedding"])
doc_embeddings.collect(
id=cocoindex.GeneratedField.UUID,
filename=doc["filename"],
location=chunk["location"],
text=chunk["text"],
# 'text_embedding' is the name of the vector we've created the Qdrant collection with.
text_embedding=chunk["embedding"],
)

doc_embeddings.export(
"doc_embeddings",
cocoindex.storages.Postgres(),
primary_key_fields=["filename", "location"],
vector_index=[("embedding", cocoindex.VectorSimilarityMetric.COSINE_SIMILARITY)])
cocoindex.storages.Qdrant(
collection_name="cocoindex", grpc_url="http://localhost:6334/"
),
primary_key_fields=["id"],
setup_by_user=True,
)


query_handler = cocoindex.query.SimpleSemanticsQueryHandler(
name="SemanticsSearch",
flow=text_embedding_flow,
target_name="doc_embeddings",
query_transform_flow=text_to_embedding,
default_similarity_metric=cocoindex.VectorSimilarityMetric.COSINE_SIMILARITY)
default_similarity_metric=cocoindex.VectorSimilarityMetric.COSINE_SIMILARITY,
)


@cocoindex.main_fn()
def _run():
# Run queries in a loop to demonstrate the query capabilities.
while True:
try:
query = input("Enter search query (or Enter to quit): ")
if query == '':
if query == "":
break
results, _ = query_handler.search(query, 10)
results, _ = query_handler.search(query, 10, "text_embedding")
print("\nSearch results:")
for result in results:
print(f"[{result.score:.3f}] {result.data['filename']}")
Expand All @@ -62,6 +84,7 @@ def _run():
except KeyboardInterrupt:
break


if __name__ == "__main__":
load_dotenv(override=True)
_run()
9 changes: 9 additions & 0 deletions python/cocoindex/storages.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,21 @@
from . import op
from . import index
from .auth_registry import AuthEntryReference

class Postgres(op.StorageSpec):
"""Storage powered by Postgres and pgvector."""

database_url: str | None = None
table_name: str | None = None

@dataclass
class Qdrant(op.StorageSpec):
"""Storage powered by Qdrant - https://qdrant.tech/."""

collection_name: str
grpc_url: str = "http://localhost:6334/"
api_key: str | None = None

@dataclass
class Neo4jConnectionSpec:
"""Connection spec for Neo4j."""
Expand Down
5 changes: 1 addition & 4 deletions src/base/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,7 @@ impl std::fmt::Display for KeyValue {
}

impl KeyValue {
pub fn fields_iter<'a>(
&'a self,
num_fields: usize,
) -> Result<impl Iterator<Item = &'a KeyValue>> {
pub fn fields_iter(&self, num_fields: usize) -> Result<impl Iterator<Item = &KeyValue>> {
let slice = if num_fields == 1 {
std::slice::from_ref(self)
} else {
Expand Down
1 change: 1 addition & 0 deletions src/ops/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ pub enum ExecutorFactory {
ExportTarget(Arc<dyn ExportTargetFactory + Send + Sync>),
}

#[derive(Debug)]
pub struct VectorMatchQuery {
pub vector_field_name: String,
pub vector: Vec<f32>,
Expand Down
1 change: 1 addition & 0 deletions src/ops/registration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ fn register_executor_factories(registry: &mut ExecutorFactoryRegistry) -> Result
functions::extract_by_llm::Factory.register(registry)?;

Arc::new(storages::postgres::Factory::default()).register(registry)?;
Arc::new(storages::qdrant::Factory::default()).register(registry)?;

let neo4j_pool = Arc::new(storages::neo4j::GraphPool::default());
storages::neo4j::RelationshipFactory::new(neo4j_pool).register(registry)?;
Expand Down
1 change: 1 addition & 0 deletions src/ops/storages/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod neo4j;
pub mod postgres;
pub mod qdrant;
Loading