|
| 1 | +--- |
| 2 | +title: "Integrate TiDB Vector Search with Cohere Embeddings API" |
| 3 | +description: "Learn how to integrate TiDB Vector Search with Cohere Embeddings API to store embeddings and perform semantic search." |
| 4 | +keywords: "TiDB, Cohere, Vector search, text embeddings, multilingual embeddings" |
| 5 | +--- |
| 6 | + |
| 7 | +# Integrate TiDB Vector Search with Cohere Embeddings API |
| 8 | + |
| 9 | +This tutorial demonstrates how to use [Cohere](https://cohere.com/embed) to generate text embeddings, store them in TiDB vector storage, and perform semantic search. |
| 10 | + |
| 11 | +!!! info |
| 12 | + |
| 13 | + Currently, only the following product and regions support native SQL functions for integrating the Cohere Embeddings API: |
| 14 | + |
| 15 | + - [TiDB Cloud Starter](https://tidbcloud.com/?utm_source=github&utm_medium=referral&utm_campaign=pytidb_readme) on AWS: `Frankfurt (eu-central-1)` and `Singapore (ap-southeast-1)` |
| 16 | + |
| 17 | +## Cohere Embeddings |
| 18 | + |
| 19 | +Cohere offers multilingual embedding models for search, RAG, and classification. The latest `embed-v4.0` model supports text, images, and mixed content. You can use the Cohere Embeddings API with TiDB through the AI SDK or native SQL functions for automatic embedding generation. |
| 20 | + |
| 21 | +### Supported Models |
| 22 | + |
| 23 | +| Model Name | Dimensions | Max Input Tokens | Description | |
| 24 | +|----------------------------------|------------|------------------|-------------| |
| 25 | +| `cohere/embed-v4.0` | 256, 512, 1024, 1536 (default) | 128k | Latest multimodal model supporting text, images, and mixed content (PDFs) | |
| 26 | +| `cohere/embed-english-v3.0` | 1024 | 512 | High-performance English embedding model optimized for search and classification | |
| 27 | +| `cohere/embed-multilingual-v3.0`| 1024 | 512 | Multilingual model supporting 100+ languages | |
| 28 | +| `cohere/embed-english-light-v3.0` | 384 | 512 | Lightweight English model for faster processing with similar performance | |
| 29 | +| `cohere/embed-multilingual-light-v3.0` | 384 | 512 | Lightweight multilingual model for faster processing with similar performance | |
| 30 | + |
| 31 | +For a complete list of supported models and detailed specifications, see the [Cohere Embeddings Documentation](https://docs.cohere.com/docs/cohere-embed). |
| 32 | + |
| 33 | +## Usage example |
| 34 | + |
| 35 | +This example demonstrates creating a vector table, inserting documents, and performing similarity search using Cohere embedding models. |
| 36 | + |
| 37 | +### Step 1: Connect to the database |
| 38 | + |
| 39 | +=== "Python" |
| 40 | + |
| 41 | + ```python |
| 42 | + from pytidb import TiDBClient |
| 43 | + |
| 44 | + tidb_client = TiDBClient.connect( |
| 45 | + host="{gateway-region}.prod.aws.tidbcloud.com", |
| 46 | + port=4000, |
| 47 | + username="{prefix}.root", |
| 48 | + password="{password}", |
| 49 | + database="{database}", |
| 50 | + ensure_db=True, |
| 51 | + ) |
| 52 | + ``` |
| 53 | + |
| 54 | +=== "SQL" |
| 55 | + |
| 56 | + ```bash |
| 57 | + mysql -h {gateway-region}.prod.aws.tidbcloud.com \ |
| 58 | + -P 4000 \ |
| 59 | + -u {prefix}.root \ |
| 60 | + -p{password} \ |
| 61 | + -D {database} |
| 62 | + ``` |
| 63 | + |
| 64 | +### Step 2: Configure the API key |
| 65 | + |
| 66 | +Create your API key from the [Cohere Dashboard](https://dashboard.cohere.com/api-keys) and bring your own key (BYOK) to use the embedding service. |
| 67 | + |
| 68 | +=== "Python" |
| 69 | + |
| 70 | + Configure the API key for the Cohere embedding provider using the TiDB Client: |
| 71 | + |
| 72 | + ```python |
| 73 | + tidb_client.configure_embedding_provider( |
| 74 | + provider="cohere", |
| 75 | + api_key="{your-cohere-api-key}", |
| 76 | + ) |
| 77 | + ``` |
| 78 | + |
| 79 | +=== "SQL" |
| 80 | + |
| 81 | + Set the API key for the Cohere embedding provider using SQL: |
| 82 | + |
| 83 | + ```sql |
| 84 | + SET @@GLOBAL.TIDB_EXP_EMBED_COHERE_API_KEY = "{your-cohere-api-key}"; |
| 85 | + ``` |
| 86 | + |
| 87 | +### Step 3: Create a vector table |
| 88 | + |
| 89 | +Create a table with a vector field that uses the `cohere/embed-v4.0` model to generate 1536-dimensional vectors (default dimension): |
| 90 | + |
| 91 | +=== "Python" |
| 92 | + |
| 93 | + ```python |
| 94 | + from pytidb.schema import TableModel, Field |
| 95 | + from pytidb.embeddings import EmbeddingFunction |
| 96 | + from pytidb.datatype import TEXT |
| 97 | + |
| 98 | + class Document(TableModel): |
| 99 | + __tablename__ = "sample_documents" |
| 100 | + id: int = Field(primary_key=True) |
| 101 | + content: str = Field(sa_type=TEXT) |
| 102 | + embedding: list[float] = EmbeddingFunction( |
| 103 | + model_name="cohere/embed-v4.0" |
| 104 | + ).VectorField(source_field="content") |
| 105 | + |
| 106 | + table = tidb_client.create_table(schema=Document, if_exists="overwrite") |
| 107 | + ``` |
| 108 | + |
| 109 | +=== "SQL" |
| 110 | + |
| 111 | + ```sql |
| 112 | + CREATE TABLE sample_documents ( |
| 113 | + `id` INT PRIMARY KEY, |
| 114 | + `content` TEXT, |
| 115 | + `embedding` VECTOR(1536) GENERATED ALWAYS AS (EMBED_TEXT( |
| 116 | + "cohere/embed-v4.0", |
| 117 | + `content` |
| 118 | + )) STORED |
| 119 | + ); |
| 120 | + ``` |
| 121 | + |
| 122 | +### Step 4: Insert data into the table |
| 123 | + |
| 124 | +=== "Python" |
| 125 | + |
| 126 | + Use the `table.insert()` or `table.bulk_insert()` API to add data: |
| 127 | + |
| 128 | + ```python |
| 129 | + documents = [ |
| 130 | + Document(id=1, content="Python: High-level programming language for data science and web development."), |
| 131 | + Document(id=2, content="Python snake: Non-venomous constrictor found in tropical regions."), |
| 132 | + Document(id=3, content="Python framework: Django and Flask are popular web frameworks."), |
| 133 | + Document(id=4, content="Python libraries: NumPy and Pandas for data analysis."), |
| 134 | + Document(id=5, content="Python ecosystem: Rich collection of packages and tools."), |
| 135 | + ] |
| 136 | + table.bulk_insert(documents) |
| 137 | + ``` |
| 138 | + |
| 139 | +=== "SQL" |
| 140 | + |
| 141 | + Insert data using the `INSERT INTO` statement: |
| 142 | + |
| 143 | + ```sql |
| 144 | + INSERT INTO sample_documents (id, content) |
| 145 | + VALUES |
| 146 | + (1, "Python: High-level programming language for data science and web development."), |
| 147 | + (2, "Python snake: Non-venomous constrictor found in tropical regions."), |
| 148 | + (3, "Python framework: Django and Flask are popular web frameworks."), |
| 149 | + (4, "Python libraries: NumPy and Pandas for data analysis."), |
| 150 | + (5, "Python ecosystem: Rich collection of packages and tools."); |
| 151 | + ``` |
| 152 | + |
| 153 | +### Step 5: Search for similar documents |
| 154 | + |
| 155 | +=== "Python" |
| 156 | + |
| 157 | + Use the `table.search()` API to perform vector search: |
| 158 | + |
| 159 | + ```python |
| 160 | + results = table.search("How to learn Python programming?") \ |
| 161 | + .limit(2) \ |
| 162 | + .to_list() |
| 163 | + print(results) |
| 164 | + ``` |
| 165 | + |
| 166 | +=== "SQL" |
| 167 | + |
| 168 | + Use the `VEC_EMBED_COSINE_DISTANCE` function to perform vector search based on cosine distance metric: |
| 169 | + |
| 170 | + ```sql |
| 171 | + SELECT |
| 172 | + `id`, |
| 173 | + `content`, |
| 174 | + VEC_EMBED_COSINE_DISTANCE(embedding, "How to learn Python programming?") AS _distance |
| 175 | + FROM sample_documents |
| 176 | + ORDER BY _distance ASC |
| 177 | + LIMIT 2; |
| 178 | + ``` |
0 commit comments