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
30 changes: 16 additions & 14 deletions examples/gdrive_text_embedding/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,23 @@ def gdrive_text_embedding_flow(flow_builder: cocoindex.FlowBuilder, data_scope:

@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 == '':
# Use a `FlowLiveUpdater` to keep the flow data updated.
with cocoindex.FlowLiveUpdater(gdrive_text_embedding_flow):
# 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)
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
results, _ = query_handler.search(query, 10)
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)
Expand Down
2 changes: 1 addition & 1 deletion python/cocoindex/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from . import functions, query, sources, storages, cli
from .flow import FlowBuilder, DataScope, DataSlice, Flow, flow_def
from .flow import EvaluateAndDumpOptions, GeneratedField, SourceRefreshOptions
from .flow import update_all_flows, FlowLiveUpdaterOptions
from .flow import update_all_flows, FlowLiveUpdater, FlowLiveUpdaterOptions
from .llm import LlmSpec, LlmApiType
from .vector import VectorSimilarityMetric
from .lib import *
Expand Down
13 changes: 10 additions & 3 deletions python/cocoindex/flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ class FlowLiveUpdaterOptions:
"""
Options for live updating a flow.
"""
live_mode: bool = False
live_mode: bool = True
print_stats: bool = False

class FlowLiveUpdater:
Expand All @@ -379,9 +379,16 @@ class FlowLiveUpdater:
"""
_engine_live_updater: _engine.FlowLiveUpdater

def __init__(self, fl: Flow, options: FlowLiveUpdaterOptions):
def __init__(self, fl: Flow, options: FlowLiveUpdaterOptions | None = None):
self._engine_live_updater = _engine.FlowLiveUpdater(
fl._lazy_engine_flow(), _dump_engine_object(options))
fl._lazy_engine_flow(), _dump_engine_object(options or FlowLiveUpdaterOptions()))

def __enter__(self) -> FlowLiveUpdater:
return self

def __exit__(self, exc_type, exc_value, traceback):
self.abort()
asyncio.run(self.wait())

async def wait(self) -> None:
"""
Expand Down
10 changes: 8 additions & 2 deletions src/execution/live_updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,14 @@ impl FlowLiveUpdater {

pub async fn wait(&mut self) -> Result<()> {
while let Some(result) = self.tasks.join_next().await {
if let Err(e) = (|| anyhow::Ok(result??))() {
error!("{:?}", e.context("Error in applying changes from a source"));
match result {
Err(e) if !e.is_cancelled() => {
error!("{:?}", e);
}
Ok(Err(e)) => {
error!("{:?}", e.context("Error in applying changes from a source"));
}
_ => {}
}
}
Ok(())
Expand Down