Skip to content
Merged
Show file tree
Hide file tree
Changes from 24 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
33 changes: 32 additions & 1 deletion docs/docs/ops/storages.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,41 @@ 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.

Before exporting, you must create a collection with a [vector name](https://qdrant.tech/documentation/concepts/vectors/#named-vectors) that matches the vector field name in CocoIndex, and set `setup_by_user=True` during export.

Example:

```python
doc_embeddings.export(
"doc_embeddings",
cocoindex.storages.Qdrant(
collection_name="cocoindex",
grpc_url="http://xyz-example.cloud-region.cloud-provider.cloud.qdrant.io:6334/",
api_key="<your-api-key-here>",
),
primary_key_fields=["id_field"],
setup_by_user=True,
)
```

You can find an end-to-end example [here](https://github.com/cocoindex-io/cocoindex/tree/main/examples/text_embedding_qdrant).
2 changes: 2 additions & 0 deletions examples/text_embedding_qdrant/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Postgres database address for cocoindex
COCOINDEX_DATABASE_URL=postgres://cocoindex:cocoindex@localhost/cocoindex
69 changes: 69 additions & 0 deletions examples/text_embedding_qdrant/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
## Description

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

Install dependencies:

```bash
pip install -e .
```

Setup:

```bash
python main.py cocoindex setup
```

Update index:

```bash
python main.py cocoindex update
```

Run:

```bash
python main.py
```

## 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).
90 changes: 90 additions & 0 deletions examples/text_embedding_qdrant/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
from dotenv import load_dotenv

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"
)
)


@cocoindex.flow_def(name="TextEmbedding")
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")
)

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,
)

with doc["chunks"].row() as chunk:
chunk["embedding"] = text_to_embedding(chunk["text"])
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.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,
)


@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 == "":
break
results, _ = query_handler.search(query, 10, "text_embedding")
print("\nSearch results:")
for result in results:
print(f"[{result.score:.3f}] {result.data['filename']}")
print(f" {result.data['text']}")
print("---")
print()
except KeyboardInterrupt:
break


if __name__ == "__main__":
load_dotenv(override=True)
_run()
Loading