Metadata-Version: 2.4
Name: cos-vectors-embed-cli
Version: 0.1.1
Summary: Standalone CLI for COS Vector operations with pluggable embeddings
Author: Tencent Cloud COS Team
License: 
                                         Apache License
                                   Version 2.0, January 2004
                                http://www.apache.org/licenses/
        
           TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
        
           1. Definitions.
        
              "License" shall mean the terms and conditions for use, reproduction,
              and distribution as defined by Sections 1 through 9 of this document.
        
              "Licensor" shall mean the copyright owner or entity authorized by
              the copyright owner that is granting the License.
        
              "Legal Entity" shall mean the union of the acting entity and all
              other entities that control, are controlled by, or are under common
              control with that entity. For the purposes of this definition,
              "control" means (i) the power, direct or indirect, to cause the
              direction or management of such entity, whether by contract or
              otherwise, or (ii) ownership of fifty percent (50%) or more of the
              outstanding shares, or (iii) beneficial ownership of such entity.
        
              "You" (or "Your") shall mean an individual or Legal Entity
              exercising permissions granted by this License.
        
              "Source" form shall mean the preferred form for making modifications,
              including but not limited to software source code, documentation
              source, and configuration files.
        
              "Object" form shall mean any form resulting from mechanical
              transformation or translation of a Source form, including but
              not limited to compiled object code, generated documentation,
              and conversions to other media types.
        
              "Work" shall mean the work of authorship, whether in Source or
              Object form, made available under the License, as indicated by a
              copyright notice that is included in or attached to the work
              (an example is provided in the Appendix below).
        
              "Derivative Works" shall mean any work, whether in Source or Object
              form, that is based on (or derived from) the Work and for which the
              editorial revisions, annotations, elaborations, or other modifications
              represent, as a whole, an original work of authorship. For the purposes
              of this License, Derivative Works shall not include works that remain
              separable from, or merely link (or bind by name) to the interfaces of,
              the Work and Derivative Works thereof.
        
              "Contribution" shall mean any work of authorship, including
              the original version of the Work and any modifications or additions
              to that Work or Derivative Works thereof, that is intentionally
              submitted to the Licensor for inclusion in the Work by the copyright owner
              or by an individual or Legal Entity authorized to submit on behalf of
              the copyright owner. For the purposes of this definition, "submitted"
              means any form of electronic, verbal, or written communication sent
              to the Licensor or its representatives, including but not limited to
              communication on electronic mailing lists, source code control systems,
              and issue tracking systems that are managed by, or on behalf of, the
              Licensor for the purpose of discussing and improving the Work, but
              excluding communication that is conspicuously marked or otherwise
              designated in writing by the copyright owner as "Not a Contribution."
        
              "Contributor" shall mean Licensor and any individual or Legal Entity
              on behalf of whom a Contribution has been received by the Licensor and
              subsequently incorporated within the Work.
        
           2. Grant of Copyright License. Subject to the terms and conditions of
              this License, each Contributor hereby grants to You a perpetual,
              worldwide, non-exclusive, no-charge, royalty-free, irrevocable
              copyright license to reproduce, prepare Derivative Works of,
              publicly display, publicly perform, sublicense, and distribute the
              Work and such Derivative Works in Source or Object form.
        
           3. Grant of Patent License. Subject to the terms and conditions of
              this License, each Contributor hereby grants to You a perpetual,
              worldwide, non-exclusive, no-charge, royalty-free, irrevocable
              (except as stated in this section) patent license to make, have made,
              use, offer to sell, sell, import, and otherwise transfer the Work,
              where such license applies only to those patent claims licensable
              by such Contributor that are necessarily infringed by their
              Contribution(s) alone or by combination of their Contribution(s)
              with the Work to which such Contribution(s) was submitted. If You
              institute patent litigation against any entity (including a
              cross-claim or counterclaim in a lawsuit) alleging that the Work
              or a Contribution incorporated within the Work constitutes direct
              or contributory patent infringement, then any patent licenses
              granted to You under this License for that Work shall terminate
              as of the date such litigation is filed.
        
           4. Redistribution. You may reproduce and distribute copies of the
              Work or Derivative Works thereof in any medium, with or without
              modifications, and in Source or Object form, provided that You
              meet the following conditions:
        
              (a) You must give any other recipients of the Work or
                  Derivative Works a copy of this License; and
        
              (b) You must cause any modified files to carry prominent notices
                  stating that You changed the files; and
        
              (c) You must retain, in the Source form of any Derivative Works
                  that You distribute, all copyright, patent, trademark, and
                  attribution notices from the Source form of the Work,
                  excluding those notices that do not pertain to any part of
                  the Derivative Works; and
        
              (d) If the Work includes a "NOTICE" text file as part of its
                  distribution, then any Derivative Works that You distribute must
                  include a readable copy of the attribution notices contained
                  within such NOTICE file, excluding any notices that do not
                  pertain to any part of the Derivative Works, in at least one
                  of the following places: within a NOTICE text file distributed
                  as part of the Derivative Works; within the Source form or
                  documentation, if provided along with the Derivative Works; or,
                  within a display generated by the Derivative Works, if and
                  wherever such third-party notices normally appear. The contents
                  of the NOTICE file are for informational purposes only and
                  do not modify the License. You may add Your own attribution
                  notices within Derivative Works that You distribute, alongside
                  or as an addendum to the NOTICE text from the Work, provided
                  that such additional attribution notices cannot be construed
                  as modifying the License.
        
              You may add Your own copyright statement to Your modifications and
              may provide additional or different license terms and conditions
              for use, reproduction, or distribution of Your modifications, or
              for any such Derivative Works as a whole, provided Your use,
              reproduction, and distribution of the Work otherwise complies with
              the conditions stated in this License.
        
           5. Submission of Contributions. Unless You explicitly state otherwise,
              any Contribution intentionally submitted for inclusion in the Work
              by You to the Licensor shall be under the terms and conditions of
              this License, without any additional terms or conditions.
              Notwithstanding the above, nothing herein shall supersede or modify
              the terms of any separate license agreement you may have executed
              with Licensor regarding such Contributions.
        
           6. Trademarks. This License does not grant permission to use the trade
              names, trademarks, service marks, or product names of the Licensor,
              except as required for reasonable and customary use in describing the
              origin of the Work and reproducing the content of the NOTICE file.
        
           7. Disclaimer of Warranty. Unless required by applicable law or
              agreed to in writing, Licensor provides the Work (and each
              Contributor provides its Contributions) on an "AS IS" BASIS,
              WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
              implied, including, without limitation, any warranties or conditions
              of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
              PARTICULAR PURPOSE. You are solely responsible for determining the
              appropriateness of using or redistributing the Work and assume any
              risks associated with Your exercise of permissions under this License.
        
           8. Limitation of Liability. In no event and under no legal theory,
              whether in tort (including negligence), contract, or otherwise,
              unless required by applicable law (such as deliberate and grossly
              negligent acts) or agreed to in writing, shall any Contributor be
              liable to You for damages, including any direct, indirect, special,
              incidental, or consequential damages of any character arising as a
              result of this License or out of the use or inability to use the
              Work (including but not limited to damages for loss of goodwill,
              work stoppage, computer failure or malfunction, or any and all
              other commercial damages or losses), even if such Contributor
              has been advised of the possibility of such damages.
        
           9. Accepting Warranty or Additional Liability. While redistributing
              the Work or Derivative Works thereof, You may choose to offer,
              and charge a fee for, acceptance of support, warranty, indemnity,
              or other liability obligations and/or rights consistent with this
              License. However, in accepting such obligations, You may act only
              on Your own behalf and on Your sole responsibility, not on behalf
              of any other Contributor, and only if You agree to indemnify,
              defend, and hold each Contributor harmless for any liability
              incurred by, or claims asserted against, such Contributor by reason
              of your accepting any such warranty or additional liability.
        
           END OF TERMS AND CONDITIONS
        
           APPENDIX: How to apply the Apache License to your work.
        
              To apply the Apache License to your work, attach the following
              boilerplate notice, with the fields enclosed by brackets "[]"
              replaced with your own identifying information. (Don't include
              the brackets!)  The text should be enclosed in the appropriate
              comment syntax for the file format.
        
              Copyright [yyyy] [name of copyright owner]
        
              Licensed under the Apache License, Version 2.0 (the "License");
              you may not use this file except in compliance with the License.
              You may obtain a copy of the License at
        
                  http://www.apache.org/licenses/LICENSE-2.0
        
              Unless required by applicable law or agreed to in writing, software
              distributed under the License is distributed on an "AS IS" BASIS,
              WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
              implied. See the License for the specific language governing
              permissions and limitations under the License.
        
Project-URL: Homepage, https://github.com/TencentCloud/cos-vectors-embed-cli
Project-URL: Repository, https://github.com/TencentCloud/cos-vectors-embed-cli
Project-URL: Issues, https://github.com/TencentCloud/cos-vectors-embed-cli/issues
Keywords: tencent,cos,vectors,embeddings,cli,machine-learning,ai
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Topic :: Utilities
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Environment :: Console
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: cos-python-sdk-v5>=1.9.40
Requires-Dist: click>=8.0.0
Requires-Dist: rich>=12.0.0
Provides-Extra: test
Requires-Dist: pytest>=7.0; extra == "test"
Requires-Dist: pytest-cov>=4.0; extra == "test"
Dynamic: license-file
Dynamic: requires-python

# cos-vectors-embed

![Python](https://img.shields.io/badge/python-%3E%3D3.9-blue)
![License](https://img.shields.io/badge/license-Apache%202.0-blue)

[中文版](README_zh.md)

CLI tool for vectorizing content and storing in Tencent COS Vector Buckets.

## Supported Commands

**cos-vectors-embed put**: Embed text, file content, or COS objects and store them as vectors in a COS vector index.
You can create and ingest vector embeddings into a COS vector index using a single put command. You specify the data input you want to create an embedding for, an embedding model ID, your COS vector bucket name, and COS vector index name. The command supports several input formats including inline text strings, local text files, COS objects via `cos://` URIs, and batch processing via local glob patterns or COS prefix wildcards. The command generates embeddings using the OpenAI-compatible `/v1/embeddings` protocol. If you are ingesting embeddings for multiple files, it automatically uses parallel batch processing with configurable workers and batch sizes to maximize throughput.

**Note**: Each file is processed as a single embedding. Document chunking is not currently supported.

**cos-vectors-embed query**: Embed a query input and search for similar vectors in a COS vector index.
You can perform similarity queries for vector embeddings in your COS vector index using a single query command. You specify your query input, an embedding model ID, the vector bucket name, and vector index name. The command accepts several types of query inputs: a text string or a local text file or a single COS text object. The command generates embeddings for your query using the specified model and then performs a similarity search to find the most relevant matches. You can control the number of results returned, apply metadata filters to narrow your search, and choose whether to include distance scores and metadata in the results.

## Installation and Configuration

### Prerequisites

- Python 3.9 or higher
- Tencent Cloud COS credentials (`COS_SECRET_ID` and `COS_SECRET_KEY`)
- Access to an OpenAI-compatible embedding API (API base URL + API key)
- A COS vector bucket and at least one vector index created in your Tencent Cloud account

### Quick Install (Recommended)

```bash
pip install cos-vectors-embed-cli
```

After installation, use the `cos-vectors-embed` command in your terminal.

### Development Install

```bash
# Clone the repository
git clone https://github.com/TencentCloud/cos-vectors-embed-cli.git
cd cos-vectors-embed-cli

# Install in development mode
pip install -e .
```

**Note**: All dependencies (`cos-python-sdk-v5`, `click`, `rich`) are automatically installed when you install the package via pip.

### Quick Start

#### 1. Configure credentials

```bash
# COS credentials
export COS_SECRET_ID="your-secret-id"
export COS_SECRET_KEY="your-secret-key"

# COS region and vectors service domain
export COS_REGION="ap-guangzhou"
# COS_DOMAIN is optional — auto-generated as vectors.{region}.coslake.com if not set
# export COS_DOMAIN="vectors.ap-guangzhou.coslake.com"

# Embedding API credentials
export EMBEDDING_API_BASE="https://api.openai.com/v1"
export EMBEDDING_API_KEY="sk-..."
```

#### 2. Embed text and store as a vector

```bash
cos-vectors-embed put \
  --vector-bucket-name mybucket-1250000000 \
  --index-name myindex \
  --model-id text-embedding-3-small \
  --text-value "Hello, world!"
```

#### 3. Query for similar vectors

```bash
cos-vectors-embed query \
  --vector-bucket-name mybucket-1250000000 \
  --index-name myindex \
  --model-id text-embedding-3-small \
  --text-value "Hello, world!" \
  --top-k 5
```

## Core Concepts

| Concept | Description |
|---------|-------------|
| **Vector Bucket** | A COS bucket enabled for vector storage. Created and managed through Tencent Cloud Console. |
| **Index** | A vector index within a vector bucket. Stores vector embeddings with associated metadata. Each index has a configured dimension size. |
| **Embedding Provider** | The service that converts text into vector embeddings. Currently supports the `openai-compatible` provider using the `/v1/embeddings` protocol. |
| **Vector Key** | A unique identifier for each vector in an index. Can be auto-generated (UUID), derived from filename (`--filename-as-key`), or explicitly set (`--key`). |
| **Metadata** | Key-value pairs associated with each vector. Includes system metadata (source location, content type) and optional user metadata. Used for filtering in queries. |
| **COS URI** | A URI format for referencing COS objects: `cos://bucket-name/object-key`. Supports single objects and prefix patterns with wildcards (`cos://bucket/prefix/*`). |

> For more details about COS Vector Storage, see the [Tencent Cloud COS Vector Storage Documentation](https://cloud.tencent.com/document/product/436/126985).

## Configuration

### Environment Variables

| Variable | Required | Description |
|----------|----------|-------------|
| `COS_SECRET_ID` | Yes | Tencent Cloud COS SecretId |
| `COS_SECRET_KEY` | Yes | Tencent Cloud COS SecretKey |
| `COS_TOKEN` | No | Temporary security token (for temporary credentials) |
| `COS_REGION` | Yes* | COS region (e.g. `ap-guangzhou`). Can also be set via `--region` CLI option. |
| `COS_DOMAIN` | No | COS Vectors service domain (e.g. `vectors.ap-guangzhou.coslake.com`). Can also be set via `--domain` CLI option. If not set, auto-generated from region as `vectors.{region}.coslake.com`. |
| `EMBEDDING_API_BASE` | Yes* | Embedding API base URL (e.g. `https://api.openai.com/v1`). Can also be set via `--embedding-api-base`. |
| `EMBEDDING_API_KEY` | Yes* | Embedding API key. Can also be set via `--embedding-api-key`. |

\* Can be provided via CLI options instead of environment variables.

### Configuration Priority

Configuration values are resolved in the following priority order (highest to lowest):

1. **Sub-command CLI options** — e.g. `cos-vectors-embed put --region ap-beijing ...` overrides the global `--region`
2. **Global CLI options** — e.g. `cos-vectors-embed --region ap-guangzhou put ...`
3. **Environment variables** — e.g. `COS_REGION=ap-guangzhou`
4. **Auto-generated defaults** — `COS_DOMAIN` is auto-generated from region as `vectors.{region}.coslake.com` if not explicitly set

### Embedding Provider

The tool uses the OpenAI-compatible `/v1/embeddings` protocol. Configure via environment variables or CLI options:

```bash
# Via environment variables
export EMBEDDING_API_BASE="https://api.openai.com/v1"
export EMBEDDING_API_KEY="sk-..."

# Or via CLI options
cos-vectors-embed put --embedding-api-base https://api.openai.com/v1 --embedding-api-key sk-... ...
```

Any API service that implements the OpenAI `/v1/embeddings` endpoint can be used (e.g. OpenAI, Azure OpenAI, local models via vLLM/Ollama, etc.).

## Usage Examples

### Put Vectors

#### 1. Embed inline text

```bash
cos-vectors-embed \
  --region ap-guangzhou \
  --domain vectors.ap-guangzhou.coslake.com \
  put \
  --vector-bucket-name mybucket-1250000000 \
  --index-name myindex \
  --model-id text-embedding-3-small \
  --text-value "Hello, world!" \
  --embedding-api-base https://api.openai.com/v1 \
  --embedding-api-key sk-xxx
```

#### 2. Embed a local text file

```bash
cos-vectors-embed put \
  --vector-bucket-name mybucket-1250000000 \
  --index-name myindex \
  --model-id text-embedding-3-small \
  --text document.txt \
  --filename-as-key
```

#### 3. Embed local text files with wildcard pattern

```bash
cos-vectors-embed put \
  --vector-bucket-name mybucket-1250000000 \
  --index-name myindex \
  --model-id text-embedding-3-small \
  --text "docs/*.txt" \
  --filename-as-key \
  --max-workers 8 \
  --batch-size 200
```

#### 4. Embed a COS object

```bash
cos-vectors-embed put \
  --vector-bucket-name mybucket-1250000000 \
  --index-name myindex \
  --model-id text-embedding-3-small \
  --text "cos://source-bucket-1250000000/documents/report.txt"
```

#### 5. Embed COS objects with prefix wildcard

```bash
cos-vectors-embed put \
  --vector-bucket-name mybucket-1250000000 \
  --index-name myindex \
  --model-id text-embedding-3-small \
  --text "cos://source-bucket-1250000000/documents/*" \
  --filename-as-key
```

#### 6. Embed with custom metadata

```bash
cos-vectors-embed put \
  --vector-bucket-name mybucket-1250000000 \
  --index-name myindex \
  --model-id text-embedding-3-small \
  --text-value "Financial report Q4" \
  --metadata '{"category": "finance", "quarter": "Q4"}'
```

#### 8. Embed with custom vector key

```bash
cos-vectors-embed put \
  --vector-bucket-name mybucket-1250000000 \
  --index-name myindex \
  --model-id text-embedding-3-small \
  --text-value "Custom key example" \
  --key "my-custom-key-1"
```

#### 9. Embed with key prefix

```bash
cos-vectors-embed put \
  --vector-bucket-name mybucket-1250000000 \
  --index-name myindex \
  --model-id text-embedding-3-small \
  --text document.txt \
  --filename-as-key \
  --key-prefix "docs/"
```

#### 9. Embed with extra inference parameters

```bash
cos-vectors-embed put \
  --vector-bucket-name mybucket-1250000000 \
  --index-name myindex \
  --model-id text-embedding-3-small \
  --text-value "Custom parameters" \
  --embedding-inference-params '{"dimensions": 512}'
```

### Query Vectors

#### 1. Direct text query

```bash
cos-vectors-embed \
  --region ap-guangzhou \
  --domain vectors.ap-guangzhou.coslake.com \
  query \
  --vector-bucket-name mybucket-1250000000 \
  --index-name myindex \
  --model-id text-embedding-3-small \
  --text-value "search query" \
  --top-k 10 \
  --embedding-api-base https://api.openai.com/v1 \
  --embedding-api-key sk-xxx
```

#### 2. Query with metadata filter

```bash
cos-vectors-embed query \
  --vector-bucket-name mybucket-1250000000 \
  --index-name myindex \
  --model-id text-embedding-3-small \
  --text-value "search query" \
  --filter '{"category": {"$eq": "finance"}}' \
  --top-k 5
```

#### 3. Query with table output format

```bash
cos-vectors-embed query \
  --vector-bucket-name mybucket-1250000000 \
  --index-name myindex \
  --model-id text-embedding-3-small \
  --text-value "search query" \
  --output table
```

#### 4. Query without distance scores

```bash
cos-vectors-embed query \
  --vector-bucket-name mybucket-1250000000 \
  --index-name myindex \
  --model-id text-embedding-3-small \
  --text-value "search query" \
  --no-return-distance
```

#### 5. Query without metadata

```bash
cos-vectors-embed query \
  --vector-bucket-name mybucket-1250000000 \
  --index-name myindex \
  --model-id text-embedding-3-small \
  --text-value "search query" \
  --no-return-metadata
```

#### 6. Query using a local text file

```bash
cos-vectors-embed query \
  --vector-bucket-name mybucket-1250000000 \
  --index-name myindex \
  --model-id text-embedding-3-small \
  --text "./query.txt" \
  --top-k 10 \
  --output table
```

#### 7. Query using a COS text object

```bash
cos-vectors-embed query \
  --vector-bucket-name mybucket-1250000000 \
  --index-name myindex \
  --model-id text-embedding-3-small \
  --text "cos://source-bucket-1250000000/queries/query.txt" \
  --top-k 10
```

### Global Options

```bash
cos-vectors-embed --help           # Show help
cos-vectors-embed --version        # Show version
cos-vectors-embed --debug put ...  # Enable debug logging
```

## Command Parameters

### Global Options

| Option | Description |
|--------|-------------|
| `--region` | COS region (e.g. `ap-guangzhou`). Falls back to `COS_REGION` env var. |
| `--domain` | COS Vectors service domain (e.g. `vectors.ap-guangzhou.coslake.com`). Falls back to `COS_DOMAIN` env var. |
| `--debug` | Enable debug output with detailed logging. |
| `--version` | Show version and exit. |

### Put Command Parameters

**Required:**

| Parameter | Description |
|-----------|-------------|
| `--vector-bucket-name` | COS vector bucket name. |
| `--index-name` | Vector index name. |
| `--model-id` | Embedding model identifier. |

**Input Options (one required):**

| Parameter | Description |
|-----------|-------------|
| `--text-value` | Direct text string to embed. |
| `--text` | Text input — supports local file path (`./doc.txt`), glob pattern (`docs/*.txt`), COS URI (`cos://bucket/key`), or COS prefix (`cos://bucket/prefix/*`). |

**Optional:**

| Parameter | Default | Description |
|-----------|---------|-------------|
| `--key` | Auto UUID | Custom vector key. Only for single-item puts. |
| `--key-prefix` | — | Prefix to prepend to all generated vector keys. |
| `--filename-as-key` | `false` | Use source filename as vector key (mutually exclusive with `--key`). |
| `--metadata` | — | Custom metadata as JSON string, e.g. `'{"category": "finance"}'`. |
| `--provider` | `openai-compatible` | Embedding provider type. |
| `--embedding-api-base` | — | Embedding API base URL. Falls back to `EMBEDDING_API_BASE` env var. |
| `--embedding-api-key` | — | Embedding API key. Falls back to `EMBEDDING_API_KEY` env var. |
| `--embedding-inference-params` | — | Extra inference parameters as JSON string. |
| `--max-workers` | `4` | Number of parallel worker threads for batch processing. |
| `--batch-size` | `100` | Number of items per storage batch. |
| `--output` | `json` | Output format: `json` or `table`. |
| `--region` | — | Override global `--region` for this command. |
| `--domain` | — | Override global `--domain` for this command. |

### Query Command Parameters

**Required:**

| Parameter | Description |
|-----------|-------------|
| `--vector-bucket-name` | COS vector bucket name. |
| `--index-name` | Vector index name. |
| `--model-id` | Embedding model identifier. |

**Query Input (one required):**

| Parameter | Description |
|-----------|-------------|
| `--text-value` | Direct text string to query with. |
| `--text` | Text file path or COS URI (`cos://bucket/key`) containing the query. |

**Optional:**

| Parameter | Default | Description |
|-----------|---------|-------------|
| `--top-k` | `5` | Number of results to return. |
| `--filter` | — | Metadata filter as JSON string. See [Metadata Filtering](#metadata-filtering) below. |
| `--return-distance` / `--no-return-distance` | `true` | Whether to include distance scores in results. |
| `--return-metadata` / `--no-return-metadata` | `true` | Whether to include metadata in results. |
| `--provider` | `openai-compatible` | Embedding provider type. |
| `--embedding-api-base` | — | Embedding API base URL. Falls back to `EMBEDDING_API_BASE` env var. |
| `--embedding-api-key` | — | Embedding API key. Falls back to `EMBEDDING_API_KEY` env var. |
| `--output` | `json` | Output format: `json` or `table`. |
| `--region` | — | Override global `--region` for this command. |
| `--domain` | — | Override global `--domain` for this command. |

## Metadata

The tool automatically attaches system metadata to every vector. You can also add custom metadata.

### System Metadata Fields

| Field | Description | Example |
|-------|-------------|---------|
| `COSVECTORS-EMBED-SRC-LOCATION` | Source file path or URI of the embedded content. Only set when the source is a file or URI (not set for `--text-value` inline text). | `document.txt`, `cos://bucket/file.txt` |
| `COSVECTORS-EMBED-SRC-TYPE` | Content type of the embedded content. | `text` |

### Custom Metadata

Use `--metadata` to attach custom key-value pairs as a JSON string:

```bash
cos-vectors-embed put \
  --vector-bucket-name mybucket-1250000000 \
  --index-name myindex \
  --model-id text-embedding-3-small \
  --text-value "Financial report" \
  --metadata '{"category": "finance", "year": "2024"}'
```

**Merge behavior**: Custom metadata is merged with system metadata. If a custom key conflicts with a system metadata key, the **custom value takes priority**.

### Metadata Filtering

When querying vectors, you can use the `--filter` option to narrow results based on metadata fields. The filter must be a **JSON object** following the COS Vectors filter syntax.

> For the full filter syntax specification, see the [COS Vectors Filter Documentation](https://cloud.tencent.com/document/product/436/127723#b6562611-7753-46e8-9d1a-14264adc4165).

#### Supported Operators

| Operator | Type | Description |
|----------|------|-------------|
| `$eq` | Comparison | Equal to |
| `$ne` | Comparison | Not equal to |
| `$gt` | Comparison | Greater than |
| `$gte` | Comparison | Greater than or equal to |
| `$lt` | Comparison | Less than |
| `$lte` | Comparison | Less than or equal to |
| `$in` | Array | Matches any value in the array |
| `$nin` | Array | Does not match any value in the array |
| `$exists` | Existence | Checks if a field exists |
| `$and` | Logical | Logical AND (all conditions must match) |
| `$or` | Logical | Logical OR (any condition matches) |

#### Filter Examples

**Exact match:**

```bash
cos-vectors-embed query \
  --vector-bucket-name mybucket-1250000000 \
  --index-name myindex \
  --model-id text-embedding-3-small \
  --text-value "search query" \
  --filter '{"category": {"$eq": "finance"}}'
```

**Numeric comparison:**

```bash
# Vectors where year >= 2020
--filter '{"year": {"$gte": 2020}}'

# Vectors where score > 0.8 and score <= 1.0
--filter '{"$and": [{"score": {"$gt": 0.8}}, {"score": {"$lte": 1.0}}]}'
```

**IN / NOT IN:**

```bash
# Vectors where category is one of the listed values
--filter '{"category": {"$in": ["finance", "tech", "health"]}}'

# Vectors where status is NOT "archived" or "deleted"
--filter '{"status": {"$nin": ["archived", "deleted"]}}'
```

**Field existence check:**

```bash
# Only vectors that have a "summary" field
--filter '{"summary": {"$exists": true}}'
```

**Logical AND / OR combinations:**

```bash
# AND: category is "finance" AND year >= 2020
--filter '{"$and": [{"category": {"$eq": "finance"}}, {"year": {"$gte": 2020}}]}'

# OR: category is "finance" OR category is "tech"
--filter '{"$or": [{"category": {"$eq": "finance"}}, {"category": {"$eq": "tech"}}]}'
```

## Batch Processing & Wildcard Patterns

### Local Glob Patterns

Use standard glob wildcards to process multiple local files:

```bash
# Process all .txt files in a directory
cos-vectors-embed put \
  --vector-bucket-name mybucket-1250000000 \
  --index-name myindex \
  --model-id text-embedding-3-small \
  --text "docs/*.txt" \
  --filename-as-key
```

### COS Prefix Patterns

Use COS URI prefix patterns to process multiple objects from a COS bucket:

```bash
# Process all supported files under a COS prefix
cos-vectors-embed put \
  --vector-bucket-name mybucket-1250000000 \
  --index-name myindex \
  --model-id text-embedding-3-small \
  --text "cos://source-bucket-1250000000/documents/*" \
  --filename-as-key
```

**Note**: COS prefix patterns filter objects by supported file extensions. Only files with recognized text extensions (`.txt`, `.md`, `.csv`, `.json`, `.xml`, `.html`, `.yaml`, `.yml`, `.log`, `.py`, `.js`, `.ts`, `.java`, `.go`, etc.) are processed.

### Batch Processing Options

| Option | Default | Description |
|--------|---------|-------------|
| `--max-workers` | `4` | Number of parallel worker threads. Increase for higher throughput on large batches. |
| `--batch-size` | `100` | Number of vectors per storage batch sent to COS. |

### Error Resilience

Batch processing is designed to be resilient: if individual files fail (e.g. unreadable or encoding errors), the batch continues processing remaining files. A summary of successes and failures is displayed at the end.

## Architecture

```
cos_vectors/
├── cli.py                     # CLI entry point (Click Group)
├── commands/
│   ├── embed_put.py           # put subcommand
│   └── embed_query.py         # query subcommand
├── core/
│   ├── embedding_provider.py  # EmbeddingProvider ABC + OpenAI implementation
│   ├── cos_vector_service.py  # COS Vectors SDK wrapper
│   ├── unified_processor.py   # Processing pipeline
│   └── streaming_batch_orchestrator.py  # Batch processing
└── utils/
    ├── config.py              # COS configuration management
    ├── models.py              # Data models and utilities
    └── multimodal_helpers.py  # File I/O and encoding helpers
```

### Data Flow

```
CLI Input (text/file/COS URI)
    │
    ▼
Content Preparation (read local file / download COS object / use inline text)
    │
    ▼
Embedding Generation (OpenAI-compatible /v1/embeddings API)
    │
    ▼
Vector Assembly (key + embedding + metadata)
    │
    ▼
COS Vector Storage (put_vectors API → vector bucket/index)
```

For batch inputs (glob patterns or COS prefix wildcards), the **StreamingBatchOrchestrator** manages the flow:
1. **Stream** — Files are discovered and chunked using `iglob` (local) or `list_objects` pagination (COS)
2. **Parallel Embed** — Each chunk is processed with a `ThreadPoolExecutor` (`--max-workers`)
3. **Batch Store** — Assembled vectors are stored in batches (`--batch-size`)

## Troubleshooting

### Common Errors

**COS credentials not found:**
```
ValueError: COS credentials are required. Set COS_SECRET_ID and COS_SECRET_KEY environment variables.
```
→ Ensure `COS_SECRET_ID` and `COS_SECRET_KEY` are set in your environment.

**Region or domain not set:**
```
ValueError: Region is required. Use --region option or set COS_REGION environment variable.
ValueError: Domain is required. Use --domain option, set COS_DOMAIN environment variable, or provide --region to auto-generate domain.
```
→ Set `COS_REGION` environment variable or use `--region` CLI option. The domain will be auto-generated from region if not set explicitly.

**Embedding API not configured:**
```
UsageError: Embedding API base URL is required.
```
→ Set `EMBEDDING_API_BASE` and `EMBEDDING_API_KEY` environment variables, or use `--embedding-api-base` and `--embedding-api-key` CLI options.

**No files matched pattern:**
```
Warning: No files matched pattern 'docs/*.txt'
```
→ Verify that the glob pattern matches existing files. Check the current working directory and file extensions.

**Invalid metadata JSON:**
```
UsageError: Invalid --metadata JSON: ...
```
→ Ensure the `--metadata` value is valid JSON. Use single quotes around the JSON string and double quotes inside.

### Debug Mode

Use `--debug` to enable detailed logging for troubleshooting:

```bash
cos-vectors-embed --debug put \
  --vector-bucket-name mybucket-1250000000 \
  --index-name myindex \
  --model-id text-embedding-3-small \
  --text-value "debug example"
```

Debug mode shows:
- COS client creation status
- Auto-detected index dimensions
- Embedding generation progress
- Detailed error stack traces

## FAQ

**Q: Which embedding providers are supported?**

A: Currently, the tool supports the `openai-compatible` provider, which uses the standard OpenAI `/v1/embeddings` API protocol. Any service that implements this protocol can be used, including OpenAI, Azure OpenAI, and local model servers like vLLM or Ollama.

**Q: What file types are supported for batch processing?**

A: Text files: `.txt`, `.md`, `.csv`, `.json`, `.xml`, `.html`, `.htm`, `.yaml`, `.yml`, `.log`, `.cfg`, `.ini`, `.conf`, `.py`, `.js`, `.ts`, `.java`, `.c`, `.cpp`, `.h`, `.go`, `.rs`, `.rb`, `.php`, `.sh`, `.bash`.

**Q: How does the COS URI format work?**

A: COS URIs follow the format `cos://bucket-name/object-key`. For single objects, use the full key (e.g. `cos://mybucket/docs/file.txt`). For batch processing, use a prefix with a wildcard (e.g. `cos://mybucket/docs/*`).

**Q: How are vector keys generated?**

A: By default, a UUID is auto-generated for each vector. Use `--key` to set an explicit key (single-item only), `--filename-as-key` to use the source filename, or `--key-prefix` to add a prefix to any generated key.

**Q: Can I use a temporary security token?**

A: Yes. Set the `COS_TOKEN` environment variable with your temporary security token. This is used alongside `COS_SECRET_ID` and `COS_SECRET_KEY` for temporary credential authentication.

## License

This project is licensed under the [Apache License 2.0](LICENSE).
