Skip to main content

Running a CocoIndex Flow

After a flow is defined as discussed in Flow Definition, you can start to transform data with it.

It can be achieved in two ways:

  • Use CocoIndex CLI.

  • Use APIs provided by the library. You have a cocoindex.Flow object after defining the flow in your code, and you can interact with it later.

The following sections assume you have a flow demo_flow:

main.py
@cocoindex.flow_def(name="DemoFlow")
def demo_flow(flow_builder: cocoindex.FlowBuilder, data_scope: cocoindex.DataScope):
...

It creates a demo_flow object in cocoindex.Flow type. To enable CLI, you also need to make sure you have a main function decorated with @cocoindex.main_fn():

main.py
@cocoindex.main_fn()
def main():
...

if __name__ == "__main__":
main()

Build / update target data

The major goal of a flow is to perform the transformations on source data and build / update data in the target storage (the index). This action has two modes:

  • One time update. It builds/update the target data based on source data up to the current moment. After the target data is at least as fresh as the source data when update starts, it's done. It fits into situations that you need to access the fresh target data at certain time points.

  • Live update. It continuously captures changes from the source data and updates the target data accordingly. It's long-running and only stops when being aborted explicitly. It fits into situations that you need to access the fresh target data continuously in most of the time.

info

For both modes, CocoIndex is performing incremental processing, i.e. we only perform computations and storage mutations on source data that are changed, or the flow has changed. This is to achieve best efficiency.

One time update

The cocoindex update subcommand creates/updates data in the target storage.

Once it's done, the target data is fresh up to the moment when the function is called.

python main.py cocoindex update

Live update

A data source may enable one or multiple change capture mechanisms:

  • Configured with a refresh interval, which is generally applicable to all data sources.

  • Specific data sources also provide their specific change capture mechanisms. For example, GoogleDrive source allows polling recent modified files. See documentations for specific data sources.

Change capture mechanisms enable CocoIndex to continuously capture changes from the source data and update the target data accordingly, under live update mode.

To perform live update, run the cocoindex update subcommand with -L option:

python main.py cocoindex update -L

If there's at least one data source with change capture mechanism enabled, it will keep running until the aborted (e.g. by Ctrl-C). Otherwise, it falls back to the same behavior as one time update, and will finish after a one-time update is done.

Evaluate the flow

CocoIndex allows you to run the transformations defined by the flow without updating the target storage.

The cocoindex evaluate subcommand runs the transformation and dumps flow outputs. It takes the following options:

  • --output-dir (optional): The directory to dump the result to. If not provided, it will use eval_{flow_name}_{timestamp}.
  • --no-cache (optional): By default, we use already-cached intermediate data if available. This flag will turn it off. Note that we only read existing cached data without updating the cache, even if it's turned on.

Example:

python main.py cocoindex evaluate --output-dir ./eval_output