### Start listing vectors with Wrangler CLI Source: https://developers.cloudflare.com/vectorize/best-practices/list-vectors Initiate a new list-vectors request to get the first page of vector identifiers. Specify the index name and the desired count per page. This starts a new snapshot for pagination. ```bash wrangler vectorize list-vectors my-index --count=1000 ``` -------------------------------- ### Create Cloudflare Worker Project with npm Source: https://developers.cloudflare.com/vectorize/llms-full.txt Use npm to create a new Cloudflare Worker project. Select 'Hello World example', 'Worker only', 'TypeScript', and 'Yes' for Git, 'No' for deployment during setup. ```bash npm create cloudflare@latest -- embeddings-tutorial ``` -------------------------------- ### Prefix searching with range query on strings Source: https://developers.cloudflare.com/vectorize/reference/metadata-filtering Implement prefix searching on string metadata fields using range queries. This example matches all string values starting with 'net'. Ensure the upper bound is lexicographically the next possible prefix. ```json { "someKey": { "$gte": "net", "$lt": "neu" } } ``` -------------------------------- ### Vectorize index creation output Source: https://developers.cloudflare.com/vectorize/llms-full.txt Example output from the creation command, including the required configuration binding for wrangler.toml. ```text 🚧 Creating index: 'tutorial-index' ✅ Successfully created a new Vectorize index: 'tutorial-index' 📋 To start querying from a Worker, add the following binding configuration into 'wrangler.toml': [[vectorize]] binding = "VECTORIZE" # available in your Worker on env.VECTORIZE index_name = "tutorial-index" ``` -------------------------------- ### Create Cloudflare Worker Project with pnpm Source: https://developers.cloudflare.com/vectorize/llms-full.txt Use pnpm to create a new Cloudflare Worker project. Select 'Hello World example', 'Worker only', 'TypeScript', and 'Yes' for Git, 'No' for deployment during setup. ```bash pnpm create cloudflare@latest embeddings-tutorial ``` -------------------------------- ### Create Cloudflare Worker Project with yarn Source: https://developers.cloudflare.com/vectorize/llms-full.txt Use yarn to create a new Cloudflare Worker project. Select 'Hello World example', 'Worker only', 'TypeScript', and 'Yes' for Git, 'No' for deployment during setup. ```bash yarn create cloudflare embeddings-tutorial ``` -------------------------------- ### Example Vector Object Source: https://developers.cloudflare.com/vectorize/reference/client-api An example structure for a vector object, including its ID, values, and optional metadata. ```javascript let vectorExample = { id: "12345", values: [32.4, 6.55, 11.2, 10.3, 87.9], metadata: { key: "value", hello: "world", url: "r2://bucket/some/object.json", }, }; ``` -------------------------------- ### Create a Vectorize Index Source: https://developers.cloudflare.com/vectorize/get-started/intro Create a new Vectorize index named 'tutorial-index' with 32 dimensions and Euclidean distance metric. Ensure wrangler version 3.71.0 or later is installed. ```bash npx wrangler vectorize create tutorial-index --dimensions=32 --metric=euclidean ``` -------------------------------- ### Create a LangChain Agent with Tools Source: https://developers.cloudflare.com/vectorize/examples/langchain Initializes an agent with a custom weather tool and invokes it to process a user message. Requires installing langchain and @langchain/anthropic packages. ```typescript // First install: npm install langchain @langchain/anthropic import { z } from "zod"; import { createAgent, tool } from "langchain"; const getWeather = tool( ({ city }) => `It's always sunny in ${city}!`, { name: "get_weather", description: "Get the weather for a given city", schema: z.object({ city: z.string(), }), }, ); const agent = createAgent({ model: "anthropic:claude-sonnet-4-6", tools: [getWeather], }); console.log( await agent.invoke({ messages: [{ role: "user", content: "What's the weather in Tokyo?" }], }) ); ``` -------------------------------- ### Get Vectorize Index Info (npm) Source: https://developers.cloudflare.com/vectorize/llms-full.txt Use this command to retrieve details about a specific Vectorize index. Requires the index name as an argument. ```bash npx wrangler vectorize info [NAME] ``` -------------------------------- ### Query Index Results Source: https://developers.cloudflare.com/vectorize/get-started/intro Example JSON output from a vector similarity search, showing top matches with scores and metadata. ```json // https://vectorize-tutorial..workers.dev/ { "matches": { "count": 3, "matches": [ { "id": "4", "score": 0.46348256, "values": [ 0.17, 0.29, 0.42, 0.57, 0.64, 0.38, 0.51, 0.72, 0.22, 0.85, 0.39, 0.66, 0.74, 0.32, 0.53, 0.48, 0.21, 0.69, 0.77, 0.34, 0.8, 0.55, 0.41, 0.29, 0.7, 0.62, 0.35, 0.68, 0.53, 0.3, 0.79, 0.49 ], "metadata": { "url": "/products/sku/418313" } }, { "id": "3", "score": 0.52920616, "values": [ 0.21, 0.33, 0.55, 0.67, 0.8, 0.22, 0.47, 0.63, 0.31, 0.74, 0.35, 0.53, 0.68, 0.45, 0.55, 0.7, 0.28, 0.64, 0.71, 0.3, 0.77, 0.6, 0.43, 0.39, 0.85, 0.55, 0.31, 0.69, 0.52, 0.29, 0.72, 0.48 ], "metadata": { "url": "/products/sku/97913813" } }, { "id": "2", "score": 0.6337869, "values": [ 0.14, 0.23, 0.36, 0.51, 0.62, 0.47, 0.59, 0.74, 0.33, 0.89, 0.41, 0.53, 0.68, 0.29, 0.77, 0.45, 0.24, 0.66, 0.71, 0.34, 0.86, 0.57, 0.62, 0.48, 0.78, 0.52, 0.37, 0.61, 0.69, 0.28, 0.8, 0.53 ], "metadata": { "url": "/products/sku/10148191" } } ] } } ``` -------------------------------- ### Successful Response for Listing Vectorize Indexes Source: https://developers.cloudflare.com/vectorize/vectorize-api This is an example of a successful JSON response when listing Vectorize indexes. It includes details about the index configuration, creation, and modification timestamps. ```json { "errors": [ { "code": 1000, "message": "message", "documentation_url": "documentation_url", "source": { "pointer": "pointer" } } ], "messages": [ { "code": 1000, "message": "message", "documentation_url": "documentation_url", "source": { "pointer": "pointer" } } ], "result": [ { "config": { "dimensions": 768, "metric": "cosine" }, "created_on": "2022-11-15T18:25:44.442097Z", "description": "This is my example index.", "modified_on": "2022-11-15T18:25:44.442097Z", "name": "example-index" } ], "success": true } ``` -------------------------------- ### Get Vectorize Index Info (pnpm) Source: https://developers.cloudflare.com/vectorize/llms-full.txt Use this command to retrieve details about a specific Vectorize index. Requires the index name as an argument. ```bash pnpm wrangler vectorize info [NAME] ``` -------------------------------- ### Get Vectorize Index Info using Wrangler CLI Source: https://developers.cloudflare.com/vectorize/reference/client-api Retrieve additional details about your Vectorize index. Requires Wrangler v3.71.0 or later. ```bash wrangler vectorize info ``` -------------------------------- ### Get Vectorize Index Info (yarn) Source: https://developers.cloudflare.com/vectorize/llms-full.txt Use this command to retrieve details about a specific Vectorize index. Requires the index name as an argument. ```bash yarn vectorize info [NAME] ``` -------------------------------- ### Navigate to project directory Source: https://developers.cloudflare.com/vectorize/get-started/embeddings Change into the newly created project directory. ```bash cd embeddings-tutorial ``` -------------------------------- ### Vectorize Index Creation Output and Configuration Source: https://developers.cloudflare.com/vectorize/get-started/intro This output confirms the successful creation of the 'tutorial-index' and provides the necessary binding configuration to be added to 'wrangler.toml' for Worker access. ```bash 🚧 Creating index: 'tutorial-index' ✅ Successfully created a new Vectorize index: 'tutorial-index' 📋 To start querying from a Worker, add the following binding configuration into 'wrangler.toml': [[vectorize]] binding = "VECTORIZE" # available in your Worker on env.VECTORIZE index_name = "tutorial-index" ``` -------------------------------- ### Navigate to Worker Project Directory Source: https://developers.cloudflare.com/vectorize/get-started/intro Change the current directory to the 'vectorize-tutorial' project folder. ```bash cd vectorize-tutorial ``` -------------------------------- ### GET vectorize get Source: https://developers.cloudflare.com/vectorize/llms-full.txt Retrieves details for a specific Vectorize index by name. ```APIDOC ## GET vectorize get ### Description Get a Vectorize index by name. ### Endpoint wrangler vectorize get [NAME] ### Parameters #### Path Parameters - **NAME** (string) - Required - The name of the Vectorize index. #### Query Parameters - **--json** (boolean) - Optional - Return output as JSON - **--deprecated-v1** (boolean) - Optional - Fetch a deprecated V1 Vectorize index. ``` -------------------------------- ### Create Cloudflare Agents Starter Project Source: https://developers.cloudflare.com/vectorize/examples/agents Use this command to create a new Cloudflare Agents project. No API keys are required as it defaults to Workers AI. ```bash npx create-cloudflare@latest --template cloudflare/agents-starter ``` ```bash cd agents-starter && npm install ``` ```bash npm run dev ``` -------------------------------- ### Vectorize Get Vectors API Source: https://developers.cloudflare.com/vectorize/llms-full.txt Get vectors from a Vectorize index by providing their identifiers. ```APIDOC ## GET /vectorize/get-vectors ### Description Get vectors from a Vectorize index. ### Method GET ### Endpoint `/vectorize/get-vectors` ### Parameters #### Path Parameters - **[NAME]** (string) - Required - The name of the Vectorize index. #### Query Parameters - **--ids** (string) - Required - Vector identifiers to be fetched from the Vectorize Index. Example: `--ids a 'b' 1 '2'` ### Global Flags - **--v** (boolean) - Alias: --version - Show version number - **--cwd** (string) - Run as if Wrangler was started in the specified directory instead of the current working directory - **--config** (string) - Alias: --c - Path to Wrangler configuration file - **--env** (string) - Alias: --e - Environment to use for operations, and for selecting .env and .dev.vars files - **--env-file** (string) - Path to an .env file to load - can be specified multiple times - values from earlier files are overridden by values in later files - **--experimental-provision** (boolean) - Aliases: --x-provision - Default: true - Experimental: Enable automatic resource provisioning - **--experimental-auto-create** (boolean) - Alias: --x-auto-create - Default: true - Automatically provision draft bindings with new resources ``` -------------------------------- ### Create an index with Wrangler CLI Source: https://developers.cloudflare.com/vectorize/best-practices/create-indexes Use the wrangler CLI to initialize a new index with specified dimensions and distance metrics. ```bash npx wrangler vectorize create your-index-name --dimensions=NUM_DIMENSIONS --metric=SELECTED_METRIC ``` ```bash npx wrangler vectorize create your-index-name --dimensions=768 --metric=cosine ``` -------------------------------- ### Initialize a Cloudflare Worker project Source: https://developers.cloudflare.com/vectorize/get-started/embeddings Commands to create a new project using different package managers. ```bash npm create cloudflare@latest -- embeddings-tutorial ``` ```bash yarn create cloudflare embeddings-tutorial ``` ```bash pnpm create cloudflare@latest embeddings-tutorial ``` -------------------------------- ### Get a Vectorize Index Source: https://developers.cloudflare.com/vectorize/reference/wrangler-commands Use the `vectorize get` command to retrieve details about a specific Vectorize index by its name. ```APIDOC ## GET /vectorize/get ### Description Retrieves details of a Vectorize index by its name. ### Method GET ### Endpoint `/vectorize/get [NAME]` ### Parameters #### Path Parameters - **[NAME]** (string) - Required - The name of the Vectorize index. #### Query Parameters - **--json** (boolean) - Optional - Return output as JSON (default: false). - **--deprecated-v1** (boolean) - Optional - Fetch a deprecated V1 Vectorize index. This must be enabled if the index was created with V1 option (default: false). ### Global Flags - **--v** (boolean) - Optional - Show version number (alias: --version). - **--cwd** (string) - Optional - Run as if Wrangler was started in the specified directory instead of the current working directory. - **--config** (string) - Optional - Path to Wrangler configuration file (alias: --c). - **--env** (string) - Optional - Environment to use for operations, and for selecting .env and .dev.vars files (alias: --e). - **--env-file** (string) - Optional - Path to an .env file to load - can be specified multiple times - values from earlier files are overridden by values in later files. - **--experimental-provision** (boolean) - Optional - Experimental: Enable automatic resource provisioning (aliases: --x-provision, default: true). - **--experimental-auto-create** (boolean) - Optional - Automatically provision draft bindings with new resources (alias: --x-auto-create, default: true). ### Request Example ```bash npx wrangler vectorize get my-index ``` ### Response #### Success Response (200) - **name** (string) - The name of the index. - **id** (string) - The unique identifier of the index. - **created_at** (string) - The timestamp when the index was created. - **dimensions** (number) - The dimension size of the index. - **metric** (string) - The distance metric used by the index. - **description** (string) - The description of the index. #### Response Example ```json { "name": "my-index", "id": "a1b2c3d4-e5f6-7890-1234-567890abcdef", "created_at": "2023-10-27T10:00:00Z", "dimensions": 1536, "metric": "cosine", "description": "My embeddings index" } ``` ``` -------------------------------- ### List all Vectorize indexes using npm Source: https://developers.cloudflare.com/vectorize/llms-full.txt Execute this command to view a list of all Vectorize indexes associated with your account. No arguments are required. ```bash npx wrangler vectorize list ``` -------------------------------- ### Vector query response example Source: https://developers.cloudflare.com/vectorize/best-practices/query-vectors Example response structure for a vector query using the `cosine` distance metric. The response includes a count of matches and a list of matching vectors with their scores and IDs. ```json { "count": 5, "matches": [ { "score": 0.999909486, "id": "5" }, { "score": 0.789848214, "id": "4" }, { "score": 0.720476967, "id": "4444" }, { "score": 0.463884663, "id": "6" }, { "score": 0.378282232, "id": "1" } ] } ``` -------------------------------- ### Initialize a new Workers project Source: https://developers.cloudflare.com/vectorize/llms-full.txt Create a new project directory with the necessary boilerplate for a TypeScript-based Worker. ```bash npm create cloudflare@latest -- vectorize-tutorial ``` ```bash yarn create cloudflare vectorize-tutorial ``` ```bash pnpm create cloudflare@latest vectorize-tutorial ``` -------------------------------- ### Example NDJSON Formatted File Source: https://developers.cloudflare.com/vectorize/llms-full.txt This is an example of newline-delimited JSON (NDJSON) format for bulk uploading vector embeddings. Each vector must be on a new line and include a unique string `id`, `values`, and optional `metadata`. ```json { "id": "4444", "values": [175.1, 167.1, 129.9], "metadata": {"url": "/products/sku/918318313"}} { "id": "5555", "values": [158.8, 116.7, 311.4], "metadata": {"url": "/products/sku/183183183"}} { "id": "6666", "values": [113.2, 67.5, 11.2], "metadata": {"url": "/products/sku/717313811"}} ``` -------------------------------- ### Initialize a project non-interactively Source: https://developers.cloudflare.com/vectorize/get-started/embeddings Use the CI environment variable to skip interactive prompts during project creation. ```bash CI=true npm create cloudflare@latest embeddings-tutorial --type=simple --git --ts --deploy=false ``` -------------------------------- ### Advanced vector query response example Source: https://developers.cloudflare.com/vectorize/best-practices/query-vectors Example response for an advanced vector query including vector values and metadata. This response is based on the `cosine` distance metric and includes a single match due to `topK: 1`. ```json { "count": 1, "matches": [ { "score": 0.999909486, "id": "5", "values": [58.79999923706055, 6.699999809265137, 3.4000000953674316, ...], "metadata": { "url": "/products/sku/55519183" } } ] } ``` -------------------------------- ### GET vectorize list-vectors Source: https://developers.cloudflare.com/vectorize/llms-full.txt Lists vector identifiers within a specified Vectorize index. ```APIDOC ## GET vectorize list-vectors ### Description List vector identifiers in a Vectorize index. ### Endpoint wrangler vectorize list-vectors [NAME] ### Parameters #### Path Parameters - **NAME** (string) - Required - The name of the Vectorize index. #### Query Parameters - **--count** (number) - Optional - Maximum number of vectors to return (1-1000) - **--cursor** (string) - Optional - Cursor for pagination to get the next page of results - **--json** (boolean) - Optional - Return output as JSON ``` -------------------------------- ### Get Vectorize Index Information Source: https://developers.cloudflare.com/vectorize/reference/wrangler-commands Retrieves detailed information about a specified Vectorize index. ```APIDOC ## GET /vectorize/info ### Description Gets additional details about the specified Vectorize index. ### Method `GET` (via CLI command) ### Endpoint `wrangler vectorize info [NAME]` ### Parameters #### Path Parameters - **[NAME]** (string) - Required - The name of the Vectorize index. #### Query Parameters - **--json** (boolean) - Optional - Return output as JSON (default: false) #### Global Flags - **--v** (boolean) - alias: --version - Show version number - **--cwd** (string) - Run as if Wrangler was started in the specified directory instead of the current working directory - **--config** (string) - alias: --c - Path to Wrangler configuration file - **--env** (string) - alias: --e - Environment to use for operations, and for selecting .env and .dev.vars files - **--env-file** (string) - Path to an .env file to load - can be specified multiple times - values from earlier files are overridden by values in later files - **--experimental-provision** (boolean) - aliases: --x-provision - Experimental: Enable automatic resource provisioning (default: true) - **--experimental-auto-create** (boolean) - alias: --x-auto-create - Automatically provision draft bindings with new resources (default: true) ``` -------------------------------- ### Create another String Metadata Index Source: https://developers.cloudflare.com/vectorize/reference/metadata-filtering Create a metadata index for a string property named 'streaming_platform' on an existing Vectorize index. ```bash npx wrangler vectorize create-metadata-index tutorial-index --property-name=streaming_platform --type=string ``` -------------------------------- ### Example Vector Embedding Array Source: https://developers.cloudflare.com/vectorize/llms-full.txt A representation of a 768-dimension vector embedding for a text input. ```json [-0.019273685291409492,-0.01913292706012726,<764 dimensions here>,0.0007094172760844231,0.043409910053014755] ``` -------------------------------- ### Initialize Cloudflare Worker Non-Interactively Source: https://developers.cloudflare.com/vectorize/get-started/intro Create a basic 'Hello World' Cloudflare Worker project non-interactively using environment variables. ```bash CI=true npm create cloudflare@latest vectorize-tutorial --type=simple --git --ts --deploy=false ``` -------------------------------- ### Get Index Info (Wrangler CLI) Source: https://developers.cloudflare.com/vectorize/reference/client-api Retrieves additional details about a specified Vectorize index. ```APIDOC ## Get Index Info ### Description Get additional details about the index. ### Method `wrangler vectorize info` ### Endpoint `wrangler vectorize info ` ### Parameters #### Path Parameters - **``** (string) - Required - The name of your Vectorize index ### Request Example ```bash wrangler vectorize info my-index ``` ### Response Example (Response structure not provided in source text.) ``` -------------------------------- ### Get Vectors by ID Source: https://developers.cloudflare.com/vectorize/reference/client-api Retrieves specified vectors from the index by their IDs, including their values and metadata. ```APIDOC ## Get Vectors by ID ### Description Retrieves the specified vectors by their ID, including values and metadata. ### Method GET (conceptual, as this is a Worker API call) ### Endpoint `env.YOUR_INDEX.getByIds(ids)` ### Parameters #### Query Parameters - **ids** (Array) - Required - An array of vector IDs to retrieve. ### Request Example ```javascript let ids = ["11", "22", "33", "44"]; const vectors = await env.YOUR_INDEX.getByIds(ids); ``` ### Response #### Success Response (200) - **vectors** (Array) - An array of vector objects, each containing `id`, `values`, and `metadata`. - **id** (string) - The ID of the vector. - **values** (Array) - The values of the vector. - **metadata** (Object) - The metadata associated with the vector. #### Response Example ```json { "vectors": [ { "id": "11", "values": [1.1, 2.2, 3.3], "metadata": {"source": "doc1"} }, { "id": "22", "values": [4.4, 5.5, 6.6], "metadata": {"source": "doc2"} } ] } ``` ``` -------------------------------- ### Create a String Metadata Index Source: https://developers.cloudflare.com/vectorize/reference/metadata-filtering Create a metadata index for a string property named 'url' on an existing Vectorize index. ```bash npx wrangler vectorize create-metadata-index tutorial-index --property-name=url --type=string ``` -------------------------------- ### List all Vectorize indexes using pnpm Source: https://developers.cloudflare.com/vectorize/llms-full.txt Execute this command to view a list of all Vectorize indexes associated with your account. No arguments are required. ```bash pnpm wrangler vectorize list ``` -------------------------------- ### Get vectors from a Vectorize index Source: https://developers.cloudflare.com/vectorize/reference/wrangler-commands Use these commands to retrieve specific vectors by their identifiers from a Vectorize index. ```bash npx wrangler vectorize get-vectors [NAME] ``` ```bash pnpm wrangler vectorize get-vectors [NAME] ``` ```bash yarn wrangler vectorize get-vectors [NAME] ``` -------------------------------- ### Create Vectorize Metadata Index (npm) Source: https://developers.cloudflare.com/vectorize/llms-full.txt Enable metadata filtering on a specified property for a Vectorize index. Requires the index name and property details. ```bash npx wrangler vectorize create-metadata-index [NAME] ``` -------------------------------- ### Insert vector with metadata Source: https://developers.cloudflare.com/vectorize/best-practices/insert-vectors Example of a vector object structure containing an ID, vector values, and associated metadata. ```javascript { id: '1', values: [32.4, 74.1, 3.2, ...], metadata: { path: 'r2://bucket-name/path/to/image.png', format: 'png', category: 'profile_image' } } ``` -------------------------------- ### Workers AI Embedding Generation Source: https://developers.cloudflare.com/vectorize/best-practices/query-vectors Example of generating embeddings using Workers AI and preparing them for Vectorize queries. ```APIDOC ## Workers AI Integration ### Description Demonstrates how to generate embeddings using a Workers AI text embedding model and then use these embeddings to query a Vectorize index. ### Request Example (Embedding Generation) ```javascript interface EmbeddingResponse { shape: number[]; data: number[][]; } let userQuery = "a query from a user or service"; const queryVector: EmbeddingResponse = await env.AI.run( "@cf/baai/bge-base-en-v1.5", { text: [userQuery], }, ); // To query Vectorize, use the vector data: let matches = await env.TEXT_EMBEDDINGS.query(queryVector.data[0], { topK: 1 }); ``` ### Notes - When passing embeddings generated by Workers AI to the `query()` method, ensure you pass only the vector embedding itself from the `.data` sub-object (e.g., `queryVector.data[0]`). - Passing the entire `queryVector` object or `queryVector.data` will result in an error. ``` -------------------------------- ### Create a Vectorize index Source: https://developers.cloudflare.com/vectorize/get-started/embeddings Command to create a new index and the resulting configuration output. ```bash npx wrangler vectorize create embeddings-index --dimensions=768 --metric=cosine ``` ```toml ✅ Successfully created index 'embeddings-index' [[vectorize]] binding = "VECTORIZE" # available in your Worker on env.VECTORIZE index_name = "embeddings-index" ``` -------------------------------- ### Define Vector Object in TypeScript Source: https://developers.cloudflare.com/vectorize/llms-full.txt Example structure of a vector object including ID, embedding values, and optional metadata. ```typescript let vectorExample = { id: "12345", values: [32.4, 6.55, 11.2, 10.3, 87.9], metadata: { key: "value", hello: "world", url: "r2://bucket/some/object.json", }, }; ``` -------------------------------- ### Create Cloudflare Worker Project with npm Source: https://developers.cloudflare.com/vectorize/get-started/intro Use this command to create a new Cloudflare Worker project named 'vectorize-tutorial' using npm. ```bash npm create cloudflare@latest -- vectorize-tutorial ``` -------------------------------- ### Create metadata index Source: https://developers.cloudflare.com/vectorize/get-started/intro Enable filtering on specific metadata fields by creating a metadata index. ```bash npx wrangler vectorize create-metadata-index tutorial-index --property-name=url --type=string ``` ```text 📋 Creating metadata index... ✅ Successfully enqueued metadata index creation request. Mutation changeset identifier: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx. ``` -------------------------------- ### Create a Vectorize Index Source: https://developers.cloudflare.com/vectorize/reference/wrangler-commands Use the `vectorize create` command to provision a new Vectorize index. You can specify dimensions, metric, description, and other configuration options. ```APIDOC ## POST /vectorize/create ### Description Creates a new Vectorize index. ### Method POST ### Endpoint `/vectorize/create [NAME]` ### Parameters #### Path Parameters - **[NAME]** (string) - Required - The name of the Vectorize index to create (must be unique). #### Query Parameters - **--dimensions** (number) - Optional - The dimension size to configure this index for, based on the output dimensions of your ML model. - **--metric** (string) - Optional - The distance metric to use for searching within the index. - **--preset** (string) - Optional - The name of a preset representing an embeddings model: Vectorize will configure the dimensions and distance metric for you when provided. - **--description** (string) - Optional - An optional description for this index. - **--json** (boolean) - Optional - Return output as JSON (default: false). - **--deprecated-v1** (boolean) - Optional - Create a deprecated Vectorize V1 index. This is not recommended and indexes created with this option need all other Vectorize operations to have this option enabled (default: false). - **--use-remote** (boolean) - Optional - Use a remote binding when adding the newly created resource to your config. - **--update-config** (boolean) - Optional - Automatically update your config file with the newly added resource. - **--binding** (string) - Optional - The binding name of this resource in your Worker. ### Global Flags - **--v** (boolean) - Optional - Show version number (alias: --version). - **--cwd** (string) - Optional - Run as if Wrangler was started in the specified directory instead of the current working directory. - **--config** (string) - Optional - Path to Wrangler configuration file (alias: --c). - **--env** (string) - Optional - Environment to use for operations, and for selecting .env and .dev.vars files (alias: --e). - **--env-file** (string) - Optional - Path to an .env file to load - can be specified multiple times - values from earlier files are overridden by values in later files. - **--experimental-provision** (boolean) - Optional - Experimental: Enable automatic resource provisioning (aliases: --x-provision, default: true). - **--experimental-auto-create** (boolean) - Optional - Automatically provision draft bindings with new resources (alias: --x-auto-create, default: true). ### Request Example ```bash npx wrangler vectorize create my-index --dimensions 1536 --metric cosine --description "My embeddings index" ``` ### Response #### Success Response (200) - **name** (string) - The name of the created index. - **id** (string) - The unique identifier of the index. - **created_at** (string) - The timestamp when the index was created. - **dimensions** (number) - The dimension size of the index. - **metric** (string) - The distance metric used by the index. - **description** (string) - The description of the index. #### Response Example ```json { "name": "my-index", "id": "a1b2c3d4-e5f6-7890-1234-567890abcdef", "created_at": "2023-10-27T10:00:00Z", "dimensions": 1536, "metric": "cosine", "description": "My embeddings index" } ``` ``` -------------------------------- ### Initialize OpenAI Client and Generate Embeddings Source: https://developers.cloudflare.com/vectorize/best-practices/query-vectors Initialize the OpenAI client with your API key and generate a vector embedding for a given query. Ensure you have the `YOUR_OPENAPI_KEY` environment variable set. ```javascript const openai = new OpenAI({ apiKey: env.YOUR_OPENAPI_KEY }); let userQuery = "a query from a user or service"; let embeddingResponse = await openai.embeddings.create({ input: userQuery, model: "text-embedding-ada-002", }); ``` -------------------------------- ### Get a Vectorize index by name using yarn Source: https://developers.cloudflare.com/vectorize/llms-full.txt Use this command to retrieve details about a specific Vectorize index by its name. Ensure you have the index name available. ```bash yarn wrangler vectorize get [NAME] ``` -------------------------------- ### List Vectorize Metadata Indexes (npm) Source: https://developers.cloudflare.com/vectorize/llms-full.txt List all metadata properties on which filtering is enabled for a given Vectorize index. Requires the index name. ```bash npx wrangler vectorize list-metadata-index [NAME] ``` -------------------------------- ### Get a Vectorize index by name using pnpm Source: https://developers.cloudflare.com/vectorize/llms-full.txt Use this command to retrieve details about a specific Vectorize index by its name. Ensure you have the index name available. ```bash pnpm wrangler vectorize get [NAME] ``` -------------------------------- ### Create a Vectorize Index (Legacy V1) Source: https://developers.cloudflare.com/vectorize/reference/metadata-filtering Use this command to create a legacy Vectorize V1 index. Note that legacy index creation will be deprecated by December 2024. ```bash npx wrangler vectorize create tutorial-index --dimensions=32 --metric=cosine ``` -------------------------------- ### Create Vectorize Metadata Index (pnpm) Source: https://developers.cloudflare.com/vectorize/llms-full.txt Enable metadata filtering on a specified property for a Vectorize index. Requires the index name and property details. ```bash pnpm wrangler vectorize create-metadata-index [NAME] ``` -------------------------------- ### Get a Vectorize index by name using npm Source: https://developers.cloudflare.com/vectorize/llms-full.txt Use this command to retrieve details about a specific Vectorize index by its name. Ensure you have the index name available. ```bash npx wrangler vectorize get [NAME] ``` -------------------------------- ### Query a Vectorize index without metadata filtering Source: https://developers.cloudflare.com/vectorize/llms-full.txt Use the `query()` method with `topK`, `returnValues`, and `returnMetadata` to get similar vectors. Ensure `env.YOUR_INDEX` is configured. ```typescript let queryVector: Array = [54.8, 5.5, 3.1, ...]; let originalMatches = await env.YOUR_INDEX.query(queryVector, { topK: 3, returnValues: true, returnMetadata: 'all', }); ``` ```json { "count": 3, "matches": [ { "id": "5", "score": 0.999909486, "values": [58.79999923706055, 6.699999809265137, 3.4000000953674316], "metadata": { "url": "/products/sku/55519183", "streaming_platform": "hbo" } }, { "id": "4", "score": 0.789848214, "values": [75.0999984741211, 67.0999984741211, 29.899999618530273], "metadata": { "url": "/products/sku/418313", "streaming_platform": "netflix" } }, { "id": "2", "score": 0.611976262, "values": [15.100000381469727, 19.200000762939453, 15.800000190734863], "metadata": { "url": "/products/sku/10148191", "streaming_platform": "hbo" } } ] } ``` -------------------------------- ### List all Vectorize indexes using yarn Source: https://developers.cloudflare.com/vectorize/llms-full.txt Execute this command to view a list of all Vectorize indexes associated with your account. No arguments are required. ```bash yarn wrangler vectorize list ``` -------------------------------- ### Create a Vectorize Index using Wrangler Source: https://developers.cloudflare.com/vectorize/reference/wrangler-commands Use this command to create a new Vectorize index. Specify the index name and optionally configure dimensions, metric, preset, description, and binding name. Use the `--json` flag for JSON output and `--deprecated-v1` for V1 indexes. ```bash npx wrangler vectorize create [NAME] ``` ```bash pnpm wrangler vectorize create [NAME] ``` ```bash yarn wrangler vectorize create [NAME] ``` -------------------------------- ### Query Vectorize Index with OpenAI Embedding Source: https://developers.cloudflare.com/vectorize/best-practices/query-vectors Query a Cloudflare Vectorize index using the generated vector embedding. This example assumes you have a Vectorize index bound to `env.TEXT_EMBEDDINGS`. ```javascript let matches = await env.TEXT_EMBEDDINGS.query(embeddingResponse.embedding[0], { topK: 1, }); ``` -------------------------------- ### $in operator for metadata filtering Source: https://developers.cloudflare.com/vectorize/reference/metadata-filtering Filter vectors where a metadata field's value is present in a specified array. This example includes vectors with metadata values 'hbo' or 'netflix'. ```json { "someKey": { "$in": ["hbo", "netflix"] } } ``` -------------------------------- ### Create Cloudflare Worker Project with pnpm Source: https://developers.cloudflare.com/vectorize/get-started/intro Use this command to create a new Cloudflare Worker project named 'vectorize-tutorial' using pnpm. ```bash pnpm create cloudflare@latest vectorize-tutorial ``` -------------------------------- ### Define a Counter Agent Source: https://developers.cloudflare.com/vectorize/examples/agents Extend the `Agent` class and use the `@callable()` decorator to expose methods that can be called by clients over WebSocket. This example shows a simple counter agent. ```typescript import { Agent, callable } from "agents"; export class CounterAgent extends Agent { initialState = { count: 0 }; @callable() increment() { this.setState({ count: this.state.count + 1 }); return this.state.count; } } ``` ```typescript import { Agent, callable } from "agents"; export class CounterAgent extends Agent { initialState = { count: 0 }; @callable() increment() { this.setState({ count: this.state.count + 1 }); return this.state.count; } } ``` -------------------------------- ### Create a Vectorize index with Python Source: https://developers.cloudflare.com/vectorize/llms-full.txt Uses the requests library to send a POST request to the Cloudflare Vectorize API. Requires a valid account ID and API token. ```python import requests url = "https://api.cloudflare.com/client/v4/accounts/{}/vectorize/v2/indexes".format("your-account-id") headers = { "Authorization": "Bearer " } body = { "name": "demo-index", "description": "some index description", "config": { "dimensions": 1024, "metric": "euclidean" }, } resp = requests.post(url, headers=headers, json=body) print('Status Code:', resp.status_code) print('Response JSON:', resp.json()) ``` -------------------------------- ### Authenticate with Wrangler Source: https://developers.cloudflare.com/vectorize/get-started/intro Log in to your Cloudflare account via the CLI to enable deployment. ```bash npx wrangler login ``` -------------------------------- ### Get vectors from a Vectorize index using yarn Source: https://developers.cloudflare.com/vectorize/llms-full.txt Retrieve specific vectors from your Vectorize index by providing their IDs. This command requires the index name and a list of vector identifiers. ```bash yarn wrangler vectorize get-vectors [NAME] --ids ``` -------------------------------- ### Get vectors from a Vectorize index using pnpm Source: https://developers.cloudflare.com/vectorize/llms-full.txt Retrieve specific vectors from your Vectorize index by providing their IDs. This command requires the index name and a list of vector identifiers. ```bash pnpm wrangler vectorize get-vectors [NAME] --ids ```