### Copy .env Example File Source: https://github.com/cocoindex-dev/cocoindex/blob/main/examples/gdrive_text_embedding/README.md Copy the example environment file to start configuring your credentials and folder IDs. ```bash cp .env.exmaple .env $EDITOR .env ``` -------------------------------- ### Run Frontend Application Source: https://github.com/cocoindex-dev/cocoindex/blob/main/examples/image_search/README.md Navigates to the frontend directory, installs its Node.js dependencies, and starts the development server. ```bash cd frontend ``` ```bash npm install ``` ```bash npm run dev ``` -------------------------------- ### FastAPI Application Setup and Event Handlers Source: https://context7.com/cocoindex-dev/cocoindex/llms.txt Sets up a FastAPI application, initializes CocoIndex, establishes a database connection pool, and starts a live updater for the document index flow on startup. Shuts down the live updater and closes the connection pool on shutdown. ```python app = FastAPI(title="Semantic Search API") @app.on_event("startup") def startup_event(): load_dotenv() cocoindex.init() # Initialize connection pool app.state.pool = ConnectionPool(os.getenv("COCOINDEX_DATABASE_URL")) # Start live updater to keep index fresh app.state.live_updater = cocoindex.FlowLiveUpdater(document_index_flow) app.state.live_updater.start() @app.on_event("shutdown") def shutdown_event(): app.state.live_updater.abort() app.state.live_updater.wait() app.state.pool.close() ``` -------------------------------- ### Install psycopg Library Source: https://github.com/cocoindex-dev/cocoindex/blob/main/docs/docs/getting_started/quickstart.md Install the psycopg library with binary and pool support, which is required for database interactions and querying. ```bash pip install psycopg[binary,pool] ``` -------------------------------- ### Bash: Running the CocoIndex Quickstart Script Source: https://github.com/cocoindex-dev/cocoindex/blob/main/docs/docs/getting_started/quickstart.md This command executes the Python quickstart script to interactively search the index. It will prompt the user for search queries. ```bash python quickstart.py ``` -------------------------------- ### Example .env File Configuration Source: https://github.com/cocoindex-dev/cocoindex/blob/main/examples/amazon_s3_embedding/README.md This is an example of the .env file content, showing required database and Amazon S3 configuration variables. Ensure you replace placeholder values with your actual credentials. ```dotenv # Database Configuration DATABASE_URL=postgresql://localhost:5432/cocoindex # Amazon S3 Configuration AMAZON_S3_BUCKET_NAME=your-bucket-name AMAZON_S3-SQS_QUEUE_URL=https://sqs.us-west-2.amazonaws.com/123456789/S3ChangeNotifications ``` -------------------------------- ### Install Project Dependencies Source: https://github.com/cocoindex-dev/cocoindex/blob/main/docs/README.md Installs project dependencies using Yarn. This is the first step before running any other commands. ```bash $ yarn ``` -------------------------------- ### Start Neo4j Docker Compose Source: https://github.com/cocoindex-dev/cocoindex/blob/main/docs/docs/ops/storages.md Starts a Neo4j Enterprise instance using a provided Docker Compose configuration. Note that this uses an evaluation license with a 30-day trial period. ```bash docker compose -f <(curl -L https://raw.githubusercontent.com/cocoindex-io/cocoindex/refs/heads/main/dev/neo4j.yaml) up -d ``` -------------------------------- ### Kuzu API Server Setup Source: https://github.com/cocoindex-dev/cocoindex/blob/main/docs/docs/ops/storages.md Instructions for setting up and running a local Kuzu API server using Docker. ```APIDOC ## Kuzu API Server Setup CocoIndex supports interacting with Kuzu through its API server. You can set up a local Kuzu API server using Docker with the following command: ### Command ```bash KUZU_DB_DIR=$HOME/.kuzudb KUZU_PORT=8123 docker run -d --name kuzu -p ${KUZU_PORT}:8000 -v ${KUZU_DB_DIR}:/database kuzudb/api-server:latest ``` ### Description This command starts a Kuzu API server in detached mode (`-d`), names the container `kuzu`, maps the host port `8123` to the container port `8000`, and mounts a local directory `$HOME/.kuzudb` to `/database` inside the container for persistent storage. ``` -------------------------------- ### Install Rust Source: https://github.com/cocoindex-dev/cocoindex/blob/main/docs/docs/about/contributing.md Install Rust using the official script. This is a prerequisite for building the CocoIndex project. ```bash curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh ``` -------------------------------- ### Start Local Development Server Source: https://github.com/cocoindex-dev/cocoindex/blob/main/docs/README.md Starts a local development server for the Docusaurus website. Changes are reflected live without server restarts. ```bash $ yarn start ``` -------------------------------- ### Copy and Edit .env Example Source: https://github.com/cocoindex-dev/cocoindex/blob/main/examples/amazon_s3_embedding/README.md Copy the example environment file and edit it to include your Amazon S3 bucket name and optionally a prefix. This file configures database and S3 connection details. ```bash cp .env.example .env $EDITOR .env ``` -------------------------------- ### Build and Run Docker Containers Source: https://github.com/cocoindex-dev/cocoindex/blob/main/examples/fastapi_server_docker/README.md Build the Docker images and start the services defined in the docker-compose.yml file. ```bash docker compose up --build ``` -------------------------------- ### Start CocoInsight Server Source: https://github.com/cocoindex-dev/cocoindex/blob/main/examples/docs_to_knowledge_graph/README.md Run CocoInsight to troubleshoot index generation and understand data lineage. This command starts a local server for CocoInsight. ```bash cocoindex server -ci main.py ``` -------------------------------- ### Start HTTP Server for CocoInsight Source: https://context7.com/cocoindex-dev/cocoindex/llms.txt Start the CocoIndex HTTP server, enabling integration with CocoInsight. Specify the address and enable CORS for CocoIndex. ```bash cocoindex server main.py --address 0.0.0.0:8080 --cors-cocoindex ``` -------------------------------- ### Install Maturin Source: https://github.com/cocoindex-dev/cocoindex/blob/main/docs/docs/about/contributing.md Install the maturin build tool, which is used for building and packaging Rust-based Python extensions. ```bash pip install maturin ``` -------------------------------- ### Setup Cocoindex with Main Script Source: https://github.com/cocoindex-dev/cocoindex/blob/main/examples/text_embedding/Text_Embedding.ipynb This command initializes the Cocoindex environment using the specified Python script 'main.py'. The 'yes yes |' part is used to automatically confirm any prompts during the setup process. ```bash !yes yes | cocoindex setup main.py ``` -------------------------------- ### Setup CocoIndex Index Source: https://github.com/cocoindex-dev/cocoindex/blob/main/docs/docs/getting_started/quickstart.md Use this command to set up the necessary tables in the database for the CocoIndex flow. Enter 'yes' when prompted to confirm creation. ```bash cocoindex setup quickstart.py ``` -------------------------------- ### Start Postgres Database with Docker Compose Source: https://github.com/cocoindex-dev/cocoindex/blob/main/docs/docs/getting_started/installation.md Start a PostgreSQL database with the pgvector extension for CocoIndex using Docker Compose. This command downloads the configuration and starts the service in detached mode. ```bash docker compose -f <(curl -L https://raw.githubusercontent.com/cocoindex-io/cocoindex/refs/heads/main/dev/postgres.yaml) up -d ``` -------------------------------- ### Setup CocoIndex Main Configuration Source: https://github.com/cocoindex-dev/cocoindex/blob/main/examples/docs_to_knowledge_graph/README.md Set up the main configuration file for CocoIndex. This command prepares the indexing process. ```bash cocoindex setup main.py ``` -------------------------------- ### Run Qdrant Docker Container Source: https://github.com/cocoindex-dev/cocoindex/blob/main/examples/text_embedding_qdrant/README.md This command starts a Qdrant instance in a Docker container, exposing the necessary ports for operation. ```bash docker run -d -p 6334:6334 -p 6333:6333 qdrant/qdrant ``` -------------------------------- ### Start Server with Live Updates and CORS Source: https://context7.com/cocoindex-dev/cocoindex/llms.txt Launch the CocoIndex server with live update capabilities enabled and configure CORS for a local development environment. ```bash cocoindex server main.py --live-update --cors-local 3000 ``` -------------------------------- ### Run Qdrant Vector Storage Source: https://github.com/cocoindex-dev/cocoindex/blob/main/examples/image_search/README.md Starts a Qdrant instance using Docker. Ensure Postgres is also running and the COCOINDEX_DATABASE_URL environment variable is set. ```bash docker run -d -p 6334:6334 -p 6333:6333 qdrant/qdrant ``` ```bash export COCOINDEX_DATABASE_URL="postgres://cocoindex:cocoindex@localhost/cocoindex" ``` -------------------------------- ### Drop Specific Flow Setup Source: https://context7.com/cocoindex-dev/cocoindex/llms.txt Remove the backend setup (tables and metadata) for a particular flow. Use this to clean up resources for a specific flow. ```bash cocoindex drop main.py:TextEmbedding ``` -------------------------------- ### Run Kuzu API Server with Docker Source: https://github.com/cocoindex-dev/cocoindex/blob/main/docs/docs/ops/storages.md Use this command to start a Kuzu API server locally using Docker. Ensure KUZU_DB_DIR and KUZU_PORT environment variables are set. ```bash KUZU_DB_DIR=$HOME/.kuzudb KUZU_PORT=8123 docker run -d --name kuzu -p ${KUZU_PORT}:8000 -v ${KUZU_DB_DIR}:/database kuzudb/api-server:latest ``` -------------------------------- ### Build CocoIndex Index Source: https://github.com/cocoindex-dev/cocoindex/blob/main/docs/docs/getting_started/quickstart.md Execute this command to build the index after setup. It processes data and provides statistics on the changes. ```bash cocoindex update quickstart.py ``` -------------------------------- ### Example Usage of Semantic Search Source: https://context7.com/cocoindex-dev/cocoindex/llms.txt Demonstrates how to initialize CocoIndex, set up a connection pool, and perform a semantic search using the `semantic_search` function. It then prints the results. ```python if __name__ == "__main__": from dotenv import load_dotenv load_dotenv() cocoindex.init() pool = ConnectionPool(os.getenv("COCOINDEX_DATABASE_URL")) # Search for documents results = semantic_search(pool, "How does incremental processing work?") for result in results: print(f"[{result['score']:.3f}] {result['filename']}: {result['text'][:100]}...") ``` -------------------------------- ### Enable Cache for Standalone Function Source: https://github.com/cocoindex-dev/cocoindex/blob/main/docs/docs/core/custom_function.mdx This example demonstrates how to enable caching for a standalone custom function using the `cache=True` and `behavior_version=1` arguments. ```python @cocoindex.op.function(cache=True, behavior_version=1) def compute_something(arg1: str, arg2: int | None = None) -> str: ... ``` -------------------------------- ### Export to Qdrant Vector Database Source: https://context7.com/cocoindex-dev/cocoindex/llms.txt Set up data export to the Qdrant vector database. This example demonstrates collecting data with a generated UUID as the primary key and exporting embeddings. ```python # Qdrant vector database export @cocoindex.flow_def(name="QdrantExport") def qdrant_flow(flow_builder: cocoindex.FlowBuilder, data_scope: cocoindex.DataScope): data_scope["docs"] = flow_builder.add_source(cocoindex.sources.LocalFile(path="./docs")) collector = data_scope.add_collector() with data_scope["docs"].row() as doc: doc["embedding"] = doc["content"].transform( cocoindex.functions.SentenceTransformerEmbed(model="sentence-transformers/all-MiniLM-L6-v2") ) collector.collect( id=cocoindex.GeneratedField.UUID, filename=doc["filename"], embedding=doc["embedding"] ) collector.export( "doc_embeddings", cocoindex.storages.Qdrant( collection_name="my_collection", connection=cocoindex.ref_auth_entry("QdrantConnection") # Optional: use auth registry ), primary_key_fields=["id"], ) ``` -------------------------------- ### Initialize CocoIndex with Environment Variables Source: https://github.com/cocoindex-dev/cocoindex/blob/main/docs/docs/core/settings.mdx Load settings from environment variables. Ensure that `python-dotenv` is installed if you need to load variables from a `.env` file. ```python from dotenv import load_dotenv import cocoindex load_dotenv() cocoindex.init() ``` -------------------------------- ### Run FastAPI Application Locally Source: https://github.com/cocoindex-dev/cocoindex/blob/main/examples/fastapi_server_docker/README.md Start the FastAPI application using uvicorn for local development. Use --reload for automatic restarts on code changes. ```bash uvicorn main:fastapi_app --reload --host 0.0.0.0 --port 8000 ``` -------------------------------- ### Enable Cache for Function with Executor Source: https://github.com/cocoindex-dev/cocoindex/blob/main/docs/docs/core/custom_function.mdx This example shows how to enable caching for a function defined by a spec and an executor, using `cache=True` and `behavior_version=1` on the executor class. ```python class ComputeSomething(cocoindex.op.FunctionSpec): ... @cocoindex.op.executor_class(cache=True, behavior_version=1) class ComputeSomethingExecutor: spec: ComputeSomething ... ``` -------------------------------- ### Install CocoIndex Dependencies Source: https://github.com/cocoindex-dev/cocoindex/blob/main/examples/docs_to_knowledge_graph/README.md Install the necessary Python dependencies for CocoIndex. Ensure you have Python installed. ```bash pip install -e . ``` -------------------------------- ### Install CocoIndex Package Source: https://github.com/cocoindex-dev/cocoindex/blob/main/docs/docs/getting_started/installation.md Install the CocoIndex Python package using pip. Ensure you have Python and pip installed. ```bash pip install -U cocoindex ``` -------------------------------- ### Python: Main Script Logic for CocoIndex Quickstart Source: https://github.com/cocoindex-dev/cocoindex/blob/main/docs/docs/getting_started/quickstart.md This script initializes CocoIndex and a database connection pool, then enters a loop to process user search queries. It requires the 'search' function to be defined elsewhere and handles user input for queries. ```python if __name__ == "__main__": # Initialize CocoIndex library states cocoindex.init() # Initialize the database connection pool. pool = ConnectionPool(os.getenv("COCOINDEX_DATABASE_URL")) # 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 # Run the query function with the database connection pool and the query. results = search(pool, query) print("\nSearch results:") for result in results: print(f"[{result['score']:.3f}] {result['filename']}") print(f" {result['text']}") print("---") print() except KeyboardInterrupt: break ``` -------------------------------- ### Install Cocoindex and Dependencies Source: https://github.com/cocoindex-dev/cocoindex/blob/main/examples/text_embedding/Text_Embedding.ipynb Install Cocoindex and other required packages using pip. This command ensures all necessary libraries are available for the project. ```python %pip install cocoindex python-dotenv psycopg[binary,pool] ``` -------------------------------- ### JSON Primitive Value Examples Source: https://github.com/cocoindex-dev/cocoindex/blob/main/examples/text_embedding/markdown_files/rfc8259.md These examples demonstrate valid JSON texts containing only primitive values: a string, a number, and a boolean. ```json "Hello world!" ``` ```json 42 ``` ```json true ``` -------------------------------- ### Download Demo Markdown Files Source: https://github.com/cocoindex-dev/cocoindex/blob/main/examples/text_embedding/Text_Embedding.ipynb Download sample markdown files for demonstration purposes. These files will be used as the data source for embedding. ```bash !mkdir -p markdown_files && \ wget -P markdown_files https://raw.githubusercontent.com/cocoindex-io/cocoindex/refs/heads/main/examples/text_embedding/markdown_files/1706.03762v7.md && \ wget -P markdown_files https://raw.githubusercontent.com/cocoindex-io/cocoindex/refs/heads/main/examples/text_embedding/markdown_files/1810.04805v2.md && \ wget -P markdown_files https://raw.githubusercontent.com/cocoindex-io/cocoindex/refs/heads/main/examples/text_embedding/markdown_files/rfc8259.md ``` -------------------------------- ### Install CocoIndex via Pip Source: https://context7.com/cocoindex-dev/cocoindex/llms.txt Install the CocoIndex package using pip. This is the first step before using any CocoIndex CLI commands or Python APIs. ```bash pip install cocoindex ``` -------------------------------- ### Update Rust Source: https://github.com/cocoindex-dev/cocoindex/blob/main/docs/docs/about/contributing.md Update an existing Rust installation to the latest version. Ensure your Rust toolchain is current. ```bash rustup update ``` -------------------------------- ### Drop All Persisted Flows Source: https://context7.com/cocoindex-dev/cocoindex/llms.txt Remove the backend setup for all persisted flows in the system. This is the most extensive cleanup command. ```bash cocoindex drop --all ``` -------------------------------- ### Getting App Namespace Source: https://github.com/cocoindex-dev/cocoindex/blob/main/docs/docs/core/flow_def.mdx Explains how to retrieve and utilize the application namespace for organizing backends and naming resources. ```APIDOC ## GET /api/app_namespace ### Description Retrieves the current application namespace, which can be used to organize flows and name backends across different environments. ### Method GET ### Endpoint `/api/app_namespace` ### Parameters #### Path Parameters None #### Query Parameters - **trailing_delimiter** (string) - Optional - A string to append to the app namespace if it's not empty. ### Request Example ```python collection_name=cocoindex.get_app_namespace(trailing_delimiter='__') + "doc_embeddings" ``` ### Response #### Success Response (200) - **app_namespace** (string) - The current application namespace. #### Response Example ```json { "app_namespace": "Staging" } ``` ``` -------------------------------- ### Run the Python Search Application Source: https://github.com/cocoindex-dev/cocoindex/blob/main/examples/text_embedding/Text_Embedding.ipynb This command executes the Python script 'main.py'. The script will then prompt the user for search queries. ```bash !python main.py ``` -------------------------------- ### Show All Subcommands with CLI Source: https://github.com/cocoindex-dev/cocoindex/blob/main/docs/docs/core/cli.mdx Run `cocoindex --help` to display a list of all available subcommands for the CocoIndex CLI. ```sh cocoindex --help ``` -------------------------------- ### Run CocoInsight Server with Live Updates Source: https://github.com/cocoindex-dev/cocoindex/blob/main/examples/amazon_s3_embedding/README.md Run the CocoInsight server with the `-L` flag to enable continuous index updates. This ensures the server reflects the latest changes from the source as they occur. ```bash cocoindex server -ci -L main.py ``` -------------------------------- ### Run CocoIndex Backend Source: https://github.com/cocoindex-dev/cocoindex/blob/main/examples/image_search/README.md Sets up the CocoIndex configuration and runs the FastAPI backend server. The server will reload automatically on code changes. ```bash cocoindex setup main.py ``` ```bash uvicorn main:app --reload --host 0.0.0.0 --port 8000 ``` -------------------------------- ### JSON Object Example Source: https://github.com/cocoindex-dev/cocoindex/blob/main/examples/text_embedding/markdown_files/rfc8259.md This is a standard JSON object representing image metadata. It includes nested objects and an array of numbers. ```json { "Image": { "Width": 800, "Height": 600, "Title": "View from 15th Floor", "Thumbnail": { "Url": "http://www.example.com/image/481989943", "Height": 125, "Width": 100 }, "Animated" : false, "IDs": [116, 943, 234, 38793] } } ``` -------------------------------- ### Define Search Function and Main Execution Loop Source: https://github.com/cocoindex-dev/cocoindex/blob/main/examples/text_embedding/Text_Embedding.ipynb This Python script initializes a database connection pool and enters a loop to accept user search queries. It prints formatted search results including score, filename, and text snippet. Handles KeyboardInterrupt for graceful exit. ```python %%writefile -a main.py def _main(): # Initialize the database connection pool. pool = ConnectionPool(os.getenv("COCOINDEX_DATABASE_URL")) # 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 # Run the query function with the database connection pool and the query. results = search(pool, query) print("\nSearch results:") for result in results: print(f"[{result['score']:.3f}] {result['filename']}") print(f" {result['text']}") print("---") print() except KeyboardInterrupt: break if __name__ == "__main__": load_dotenv(override=True) cocoindex.init() _main() ``` -------------------------------- ### JSON Object Example Source: https://github.com/cocoindex-dev/cocoindex/blob/main/examples/text_embedding_qdrant/markdown_files/rfc8259.md This is a standard JSON object representing image metadata. It includes nested objects and an array of numbers. ```json { "Image": { "Width": 800, "Height": 600, "Title": "View from 15th Floor", "Thumbnail": { "Url": "http://www.example.com/image/481989943", "Height": 125, "Width": 100 }, "Animated": false, "IDs": [116, 943, 234, 38793] } } ``` -------------------------------- ### Connect to Postgres Shell Source: https://github.com/cocoindex-dev/cocoindex/blob/main/examples/manuals_llm_extraction/README.md Connect to the local PostgreSQL database using the psql command-line tool. Ensure your database credentials and host are correct. ```bash psql postgres://cocoindex:cocoindex@localhost/cocoindex ``` -------------------------------- ### Handle Asynchronous Updates Source: https://context7.com/cocoindex-dev/cocoindex/llms.txt This example shows how to use CocoIndex with asynchronous operations, utilizing an async context manager for live updaters. ```python async def run_async_updates(): cocoindex.init() async with cocoindex.FlowLiveUpdater(live_index_flow) as updater: await updater.wait_async() ``` -------------------------------- ### Initialize CocoIndex with Settings Source: https://context7.com/cocoindex-dev/cocoindex/llms.txt Initializes the CocoIndex library. Settings can be loaded from environment variables or provided directly. Ensure COCOINDEX_DATABASE_URL is set if loading from environment variables. ```python import cocoindex from cocoindex import Settings, DatabaseConnectionSpec # Option 1: Load settings from environment variables # Required: COCOINDEX_DATABASE_URL # Optional: COCOINDEX_DATABASE_USER, COCOINDEX_DATABASE_PASSWORD, COCOINDEX_APP_NAMESPACE cocoindex.init() # Option 2: Provide settings directly settings = Settings( database=DatabaseConnectionSpec( url="postgresql://localhost:5432/cocoindex", user="postgres", password="secret" ), app_namespace="my_app" # Optional: prefix for flow names ) cocoindex.init(settings) ``` -------------------------------- ### Drop All Flows in a Module Source: https://context7.com/cocoindex-dev/cocoindex/llms.txt Remove the backend setup for all flows defined within a specified Python module. This is a more comprehensive cleanup operation. ```bash cocoindex drop main.py ```