Skip to content
Merged
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
5 changes: 3 additions & 2 deletions examples/pdf_embedding/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,14 @@ def pdf_embedding_flow(flow_builder: cocoindex.FlowBuilder, data_scope: cocoinde

with doc["chunks"].row() as chunk:
chunk["embedding"] = chunk["text"].call(text_to_embedding)
doc_embeddings.collect(filename=doc["filename"], location=chunk["location"],
doc_embeddings.collect(id=cocoindex.GeneratedField.UUID,
filename=doc["filename"], location=chunk["location"],
text=chunk["text"], embedding=chunk["embedding"])

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

query_handler = cocoindex.query.SimpleSemanticsQueryHandler(
Expand Down
3 changes: 2 additions & 1 deletion python/cocoindex/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
Cocoindex is a framework for building and running indexing pipelines.
"""
from . import flow, functions, query, sources, storages, cli
from .flow import FlowBuilder, DataScope, DataSlice, Flow, flow_def, EvaluateAndDumpOptions
from .flow import FlowBuilder, DataScope, DataSlice, Flow, flow_def
from .flow import EvaluateAndDumpOptions, GeneratedField
from .llm import LlmSpec, LlmApiType
from .vector import VectorSimilarityMetric
from .lib import *
Expand Down
23 changes: 21 additions & 2 deletions python/cocoindex/flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,12 @@ def add_collector(self, name: str | None = None) -> DataCollector:
)
)

class GeneratedField(Enum):
"""
A generated field is automatically set by the engine.
"""
UUID = "Uuid"

class DataCollector:
"""A data collector is used to collect data into a collector."""
_flow_builder_state: _FlowBuilderState
Expand All @@ -248,12 +254,25 @@ def __init__(self, flow_builder_state: _FlowBuilderState,
self._flow_builder_state = flow_builder_state
self._engine_data_collector = data_collector

def collect(self, **kwargs: DataSlice):
def collect(self, **kwargs: DataSlice | GeneratedField):
"""
Collect data into the collector.
"""
regular_kwargs = []
auto_uuid_field = None
for k, v in kwargs.items():
if isinstance(v, GeneratedField):
if v == GeneratedField.UUID:
if auto_uuid_field is not None:
raise ValueError("Only one generated UUID field is allowed")
auto_uuid_field = k
else:
raise ValueError(f"Unexpected generated field: {v}")
else:
regular_kwargs.append((k, _data_slice_state(v).engine_data_slice))

self._flow_builder_state.engine_flow_builder.collect(
self._engine_data_collector, [(k, _data_slice_state(v).engine_data_slice) for k, v in kwargs.items()])
self._engine_data_collector, regular_kwargs, auto_uuid_field)

def export(self, name: str, target_spec: op.StorageSpec, /, *,
primary_key_fields: Sequence[str] | None = None,
Expand Down
6 changes: 3 additions & 3 deletions src/base/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,13 +360,13 @@ impl std::fmt::Display for CollectorSchema {
}

impl CollectorSchema {
pub fn from_fields(fields: Vec<FieldSchema>, has_auto_uuid_field: bool) -> Self {
pub fn from_fields(fields: Vec<FieldSchema>, auto_uuid_field: Option<FieldName>) -> Self {
let mut fields = fields;
let auto_uuid_field_idx = if has_auto_uuid_field {
let auto_uuid_field_idx = if let Some(auto_uuid_field) = auto_uuid_field {
fields.insert(
0,
FieldSchema::new(
"uuid".to_string(),
auto_uuid_field,
EnrichedValueType {
typ: ValueType::Basic(BasicValueType::Uuid),
nullable: false,
Expand Down
2 changes: 1 addition & 1 deletion src/builder/analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -799,7 +799,7 @@ impl AnalyzerContext<'_> {
collector_ref: add_collector(
&op.scope_name,
op.collector_name.clone(),
CollectorSchema::from_fields(fields_schema, has_auto_uuid_field),
CollectorSchema::from_fields(fields_schema, op.auto_uuid_field.clone()),
scopes,
)?,
fingerprinter,
Expand Down
4 changes: 2 additions & 2 deletions src/builder/flow_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ impl FlowBuilder {
},
scope_name: collector.scope.scope_name.clone(),
collector_name: collector.name.clone(),
auto_uuid_field,
auto_uuid_field: auto_uuid_field.clone(),
}),
};

Expand All @@ -565,7 +565,7 @@ impl FlowBuilder {
value_type: ds.data_type.schema,
})
.collect(),
has_auto_uuid_field,
auto_uuid_field,
);
{
let mut collector = collector.collector.lock().unwrap();
Expand Down