@ia-ccun/code-agent-cli 0.0.52 → 0.0.53
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/config/agent/bin/README.md +102 -0
- package/config/agent/bin/fd.exe +0 -0
- package/config/agent/bin/rg.exe +0 -0
- package/config/agent/extensions/aicode-advisor.ts +13 -0
- package/config/agent/extensions/aicode-btw.ts +66 -14
- package/config/agent/extensions/aicode-cd.ts +834 -0
- package/config/agent/extensions/aicode-grillMe.ts +198 -0
- package/config/agent/extensions/aicode-pets.ts +362 -0
- package/config/agent/extensions/aicode-safety-guard.ts +552 -0
- package/config/agent/extensions/aicode-tips.ts +70 -16
- package/config/agent/extensions/aicode-todo-progress.ts +547 -0
- package/config/agent/extensions/custom.ts +93 -95
- package/config/agent/extensions/error-monitor.ts +1 -18
- package/config/agent/extensions/ponytail.ts +441 -0
- package/config/agent/extensions/tps.ts +87 -0
- package/config/agent/extensions/working-msg.ts +165 -50
- package/config/agent/help.md +0 -7
- package/config/agent/models.json +7 -7
- package/config/agent/settings.json +2 -1
- package/config/agent/skills/chroma/LICENSE +21 -0
- package/config/agent/skills/chroma/chroma-local/SKILL.md +165 -0
- package/config/agent/skills/chroma/chroma-local/cli.md +48 -0
- package/config/agent/skills/chroma/chroma-local/data-model.md +103 -0
- package/config/agent/skills/chroma/chroma-local/error-handling/python.md +166 -0
- package/config/agent/skills/chroma/chroma-local/error-handling/typescript.md +231 -0
- package/config/agent/skills/chroma/chroma-local/local-chroma/python.md +95 -0
- package/config/agent/skills/chroma/chroma-local/local-chroma/typescript.md +101 -0
- package/config/agent/skills/chroma/chroma-local/metadata/python.md +154 -0
- package/config/agent/skills/chroma/chroma-local/metadata/typescript.md +153 -0
- package/config/agent/skills/chroma/chroma-local/querying/python.md +147 -0
- package/config/agent/skills/chroma/chroma-local/querying/typescript.md +157 -0
- package/config/agent/skills/chroma/chroma-local/regex/python.md +69 -0
- package/config/agent/skills/chroma/chroma-local/regex/typescript.md +71 -0
- package/config/agent/skills/chroma/chroma-local/understanding-a-codebase.md +80 -0
- package/config/agent/skills/chroma/chroma-local/updating-deleting/python.md +159 -0
- package/config/agent/skills/chroma/chroma-local/updating-deleting/typescript.md +228 -0
- package/config/agent/skills/milvus-skill/LICENSE +202 -0
- package/config/agent/skills/milvus-skill/README.md +37 -0
- package/config/agent/skills/milvus-skill/SKILL.md +259 -0
- package/config/agent/skills/milvus-skill/references/collection.md +86 -0
- package/config/agent/skills/milvus-skill/references/database.md +24 -0
- package/config/agent/skills/milvus-skill/references/index.md +69 -0
- package/config/agent/skills/milvus-skill/references/partition.md +29 -0
- package/config/agent/skills/milvus-skill/references/patterns.md +150 -0
- package/config/agent/skills/milvus-skill/references/user-role.md +90 -0
- package/config/agent/skills/milvus-skill/references/vector.md +247 -0
- package/config/agent/skills/ponytail/SKILL.md +100 -0
- package/config/agent/skills/ponytail-audit/SKILL.md +40 -0
- package/config/agent/skills/ponytail-debt/SKILL.md +44 -0
- package/config/agent/skills/ponytail-help/SKILL.md +68 -0
- package/config/agent/skills/ponytail-review/SKILL.md +56 -0
- package/config/agent/skills/zread/README.md +115 -0
- package/config/agent/skills/zread/SKILL.md +103 -0
- package/config/agent/skills/zread/references/stdio-protocol.md +312 -0
- package/package.json +7 -6
- package/scripts/postinstall.js +58 -1
- package/config/agent/dangerous-patterns.json +0 -106
- package/config/agent/extensions/bash-check.ts +0 -227
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Chroma Regex Filtering
|
|
3
|
+
description: Learn how to use regex filters in Chroma queries
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
## Regex Filtering in Chroma
|
|
7
|
+
|
|
8
|
+
Chroma supports regex filtering on document content. This is useful when you need exact pattern matching that semantic search can't provide.
|
|
9
|
+
|
|
10
|
+
### When to use regex vs semantic search
|
|
11
|
+
|
|
12
|
+
**Use regex when:**
|
|
13
|
+
- Matching exact patterns like email addresses, URLs, or code identifiers
|
|
14
|
+
- Finding documents containing specific formats (dates, phone numbers, IDs)
|
|
15
|
+
- Filtering by prefixes or suffixes in structured data
|
|
16
|
+
- You need deterministic, repeatable matches
|
|
17
|
+
|
|
18
|
+
**Use semantic search when:**
|
|
19
|
+
- Looking for conceptually similar content regardless of exact wording
|
|
20
|
+
- The user's query is natural language
|
|
21
|
+
- You want to find related content even if it uses different terminology
|
|
22
|
+
|
|
23
|
+
You can combine both: use regex to narrow results, then rank by semantic similarity.
|
|
24
|
+
|
|
25
|
+
### Imports and boilerplate
|
|
26
|
+
|
|
27
|
+
```typescript
|
|
28
|
+
import { DefaultEmbeddingFunction } from '@chroma-core/default-embed';
|
|
29
|
+
import { ChromaClient } from 'chromadb';
|
|
30
|
+
|
|
31
|
+
const client = new ChromaClient();
|
|
32
|
+
const embedder = new DefaultEmbeddingFunction();
|
|
33
|
+
|
|
34
|
+
const collection = await client.getOrCreateCollection({
|
|
35
|
+
name: 'example-collection',
|
|
36
|
+
embeddingFunction: embedder,
|
|
37
|
+
});
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### Basic Regex Filter
|
|
41
|
+
|
|
42
|
+
Use the `$regex` operator in `where_document` to match document content against a regular expression. The regex follows standard regex syntax.
|
|
43
|
+
|
|
44
|
+
```typescript
|
|
45
|
+
await collection.query({
|
|
46
|
+
queryTexts: ['find support tickets with email addresses'],
|
|
47
|
+
whereDocument: {
|
|
48
|
+
$regex: '^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$',
|
|
49
|
+
},
|
|
50
|
+
});
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### Combining regex with metadata filters
|
|
54
|
+
|
|
55
|
+
Regex filters can be combined with metadata filters using `$and` and `$or` operators. This is powerful for narrowing results by both content patterns and structured metadata. Note however regex can not be used on metadata string values.
|
|
56
|
+
|
|
57
|
+
```typescript
|
|
58
|
+
await collection.query({
|
|
59
|
+
queryTexts: ['query1', 'query2'],
|
|
60
|
+
whereDocument: {
|
|
61
|
+
$and: [{ $contains: 'search_string_1' }, { $regex: '[a-z]+' }],
|
|
62
|
+
},
|
|
63
|
+
});
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### Performance considerations
|
|
67
|
+
|
|
68
|
+
Regex filtering happens after the initial vector search retrieves candidates. For best performance:
|
|
69
|
+
- Keep regex patterns simple when possible
|
|
70
|
+
- Use metadata filters to reduce the candidate set before regex matching
|
|
71
|
+
- Consider whether a metadata field with pre-extracted values would be faster than runtime regex
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Integrating Chroma into an existing system
|
|
3
|
+
description: Guidance for adding Chroma search to an existing application
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
## Integrating Chroma into an existing system
|
|
7
|
+
|
|
8
|
+
Adding search to an existing application requires understanding the data flow and planning both the initial import and ongoing synchronization. This guide helps identify the key questions to answer.
|
|
9
|
+
|
|
10
|
+
### Key questions to ask the user
|
|
11
|
+
|
|
12
|
+
Before writing any code, clarify:
|
|
13
|
+
|
|
14
|
+
1. **What data should be searchable?** (documents, products, messages, etc.)
|
|
15
|
+
2. **Where does that data currently live?** (database, S3, API, files)
|
|
16
|
+
3. **How is the data structured?** (helps determine chunking strategy)
|
|
17
|
+
4. **How often does the data change?** (informs sync strategy)
|
|
18
|
+
5. **What latency is acceptable for updates to appear in search?**
|
|
19
|
+
|
|
20
|
+
## Understanding the source data
|
|
21
|
+
|
|
22
|
+
Before designing the import pipeline, ask the user if you can look at a sample of the data that will be made searchable. Seeing real records is far more useful than a description of the schema.
|
|
23
|
+
|
|
24
|
+
**If the data is in a database:** Write a short script that connects to the database and prints a few records. For example, a script that queries 3-5 rows from the relevant table and prints them to the terminal. This lets you see the actual field names, content lengths, and metadata available.
|
|
25
|
+
|
|
26
|
+
**If the data is on disk:** Read a few of the files directly to understand their structure, format, and size. For example, if indexing markdown files, read 2-3 of them to see how they're organized.
|
|
27
|
+
|
|
28
|
+
What to look for:
|
|
29
|
+
- **Which field(s) contain the searchable text** — this becomes the document content in Chroma
|
|
30
|
+
- **How long the content is** — determines whether chunking is needed
|
|
31
|
+
- **What metadata is available** — fields like category, author, date, or tenant ID that could be useful for filtering
|
|
32
|
+
- **How records are identified** — the primary key or filename that will link Chroma documents back to the source
|
|
33
|
+
|
|
34
|
+
This step prevents guesswork and leads to better chunking and metadata design decisions.
|
|
35
|
+
|
|
36
|
+
## Initial data import (offline ingest)
|
|
37
|
+
|
|
38
|
+
The first step is getting existing data into Chroma. This typically involves:
|
|
39
|
+
|
|
40
|
+
1. **Reading from the source** - database queries, S3 listing, API pagination
|
|
41
|
+
2. **Chunking** - breaking large documents into searchable pieces (see data-model.md)
|
|
42
|
+
3. **Embedding** - converting text chunks to vectors
|
|
43
|
+
4. **Writing to Chroma** - batching for efficiency
|
|
44
|
+
|
|
45
|
+
Build this as a reusable pipeline, not a one-off script. The same chunking and embedding logic will be needed for ongoing updates.
|
|
46
|
+
|
|
47
|
+
**Progress tracking:** For large imports, track which records have been processed. This allows resuming after failures and re-running for updates. A simple approach is storing the last processed ID or timestamp.
|
|
48
|
+
|
|
49
|
+
## Keeping data in sync (online writes)
|
|
50
|
+
|
|
51
|
+
After the initial import, new and updated data must flow to Chroma. There are two main patterns:
|
|
52
|
+
|
|
53
|
+
### Asynchronous (recommended)
|
|
54
|
+
|
|
55
|
+
Use a message queue (SQS, RabbitMQ, Redis streams, etc.) to decouple the primary write path from Chroma updates:
|
|
56
|
+
|
|
57
|
+
1. Application writes to primary database
|
|
58
|
+
2. Application publishes an event with the record ID
|
|
59
|
+
3. Queue consumer fetches the record, chunks, embeds, and writes to Chroma
|
|
60
|
+
|
|
61
|
+
**Benefits:** Primary writes aren't slowed by embedding latency. Retries are handled by the queue. Search updates can lag slightly without affecting the main application.
|
|
62
|
+
|
|
63
|
+
### Synchronous
|
|
64
|
+
|
|
65
|
+
If no queue infrastructure exists and slight latency is acceptable, update Chroma in the same request:
|
|
66
|
+
|
|
67
|
+
1. Application writes to primary database
|
|
68
|
+
2. Application chunks, embeds, and writes to Chroma
|
|
69
|
+
3. Request completes
|
|
70
|
+
|
|
71
|
+
**Tradeoffs:** Simpler infrastructure but adds latency to every write. Failures in Chroma can affect the primary write path unless carefully handled.
|
|
72
|
+
|
|
73
|
+
**Ask the user:** Do they have an async queue? If not, is synchronous acceptable, or should we set one up?
|
|
74
|
+
|
|
75
|
+
## Handling updates and deletes
|
|
76
|
+
|
|
77
|
+
- **Updates:** Re-chunk and re-embed the document, then use `upsert` to replace existing chunks
|
|
78
|
+
- **Deletes:** Delete all chunks for the document by ID prefix or metadata filter
|
|
79
|
+
|
|
80
|
+
Storing the source record ID in chunk metadata makes this straightforward. For example, if a blog post with ID `post-123` has 3 chunks, store `{"source_id": "post-123", "chunk_index": 0}` etc. on each chunk.
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Updating and Deleting
|
|
3
|
+
description: Update existing documents and delete data from collections
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
## Updating and Deleting
|
|
7
|
+
|
|
8
|
+
Chroma provides `update`, `upsert`, and `delete` methods for modifying data after initial insertion. Understanding when to use each is important for building reliable data sync pipelines.
|
|
9
|
+
|
|
10
|
+
### Method overview
|
|
11
|
+
|
|
12
|
+
| Method | Behavior | Use when |
|
|
13
|
+
|--------|----------|----------|
|
|
14
|
+
| `update` | Modifies existing documents, fails if ID doesn't exist | You know the document exists |
|
|
15
|
+
| `upsert` | Updates if exists, inserts if not | Syncing from external data source |
|
|
16
|
+
| `delete` | Removes documents by ID or filter | Removing stale or unwanted data |
|
|
17
|
+
|
|
18
|
+
### Imports
|
|
19
|
+
|
|
20
|
+
```python
|
|
21
|
+
import time
|
|
22
|
+
from typing import TypedDict
|
|
23
|
+
|
|
24
|
+
import chromadb
|
|
25
|
+
|
|
26
|
+
client = chromadb.HttpClient(host="localhost", port=8000)
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Update
|
|
30
|
+
|
|
31
|
+
Update modifies existing documents. If an ID doesn't exist, the operation fails silently for that ID (no error thrown, but nothing is updated).
|
|
32
|
+
|
|
33
|
+
**Important:** When you update a document's text, Chroma re-computes the embedding automatically using the collection's embedding function.
|
|
34
|
+
|
|
35
|
+
```python
|
|
36
|
+
collection = client.get_or_create_collection(name="my_collection")
|
|
37
|
+
|
|
38
|
+
collection.add(
|
|
39
|
+
ids=["doc1", "doc2"],
|
|
40
|
+
documents=["Original text for doc1", "Original text for doc2"],
|
|
41
|
+
metadatas=[{"category": "draft"}, {"category": "draft"}],
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
collection.update(
|
|
45
|
+
ids=["doc1"],
|
|
46
|
+
documents=["Updated text for doc1"],
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
collection.update(
|
|
50
|
+
ids=["doc1", "doc2"],
|
|
51
|
+
metadatas=[{"category": "published"}, {"category": "published"}],
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
collection.update(
|
|
55
|
+
ids=["doc2"],
|
|
56
|
+
documents=["Completely revised doc2 content"],
|
|
57
|
+
metadatas=[{"category": "published", "revision": 2}],
|
|
58
|
+
)
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## Upsert
|
|
62
|
+
|
|
63
|
+
Upsert is the preferred method for syncing data from an external source. It inserts new documents and updates existing ones in a single operation.
|
|
64
|
+
|
|
65
|
+
**When to use upsert vs update:**
|
|
66
|
+
- Use `upsert` when syncing from a primary database (you don't know which records are new)
|
|
67
|
+
- Use `update` when you're certain the document already exists
|
|
68
|
+
|
|
69
|
+
```python
|
|
70
|
+
collection2 = client.get_or_create_collection(name="articles")
|
|
71
|
+
|
|
72
|
+
collection2.upsert(
|
|
73
|
+
ids=["article-123", "article-456", "article-789"],
|
|
74
|
+
documents=[
|
|
75
|
+
"Content of article 123",
|
|
76
|
+
"Content of article 456",
|
|
77
|
+
"Content of article 789",
|
|
78
|
+
],
|
|
79
|
+
metadatas=[
|
|
80
|
+
{"source_id": "123", "updated_at": int(time.time())},
|
|
81
|
+
{"source_id": "456", "updated_at": int(time.time())},
|
|
82
|
+
{"source_id": "789", "updated_at": int(time.time())},
|
|
83
|
+
],
|
|
84
|
+
)
|
|
85
|
+
|
|
86
|
+
collection2.upsert(
|
|
87
|
+
ids=["article-123", "article-456"],
|
|
88
|
+
documents=[
|
|
89
|
+
"Updated content of article 123",
|
|
90
|
+
"Updated content of article 456",
|
|
91
|
+
],
|
|
92
|
+
metadatas=[
|
|
93
|
+
{"source_id": "123", "updated_at": int(time.time())},
|
|
94
|
+
{"source_id": "456", "updated_at": int(time.time())},
|
|
95
|
+
],
|
|
96
|
+
)
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
## Delete by ID
|
|
100
|
+
|
|
101
|
+
The simplest way to delete documents is by their IDs.
|
|
102
|
+
|
|
103
|
+
```python
|
|
104
|
+
awaiting_cleanup = ["article-789", "article-456"]
|
|
105
|
+
collection2.delete(ids=awaiting_cleanup)
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
## Delete by filter
|
|
109
|
+
|
|
110
|
+
Delete documents matching metadata or content filters without knowing specific IDs. Useful for bulk cleanup operations.
|
|
111
|
+
|
|
112
|
+
```python
|
|
113
|
+
collection2.delete(
|
|
114
|
+
where={"source_id": "123"},
|
|
115
|
+
)
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
## Syncing from an external data source
|
|
119
|
+
|
|
120
|
+
A common pattern is keeping Chroma in sync with a primary database. This example shows how to handle creates, updates, and deletes.
|
|
121
|
+
|
|
122
|
+
```python
|
|
123
|
+
class SourceRecord(TypedDict):
|
|
124
|
+
id: str
|
|
125
|
+
content: str
|
|
126
|
+
deleted: bool
|
|
127
|
+
updated_at: int
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
def sync_records(records: list[SourceRecord]) -> None:
|
|
131
|
+
active_records = [record for record in records if not record["deleted"]]
|
|
132
|
+
deleted_ids = [record["id"] for record in records if record["deleted"]]
|
|
133
|
+
|
|
134
|
+
if active_records:
|
|
135
|
+
collection2.upsert(
|
|
136
|
+
ids=[record["id"] for record in active_records],
|
|
137
|
+
documents=[record["content"] for record in active_records],
|
|
138
|
+
metadatas=[
|
|
139
|
+
{
|
|
140
|
+
"source_id": record["id"],
|
|
141
|
+
"updated_at": record["updated_at"],
|
|
142
|
+
}
|
|
143
|
+
for record in active_records
|
|
144
|
+
],
|
|
145
|
+
)
|
|
146
|
+
|
|
147
|
+
if deleted_ids:
|
|
148
|
+
collection2.delete(ids=deleted_ids)
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
### Sync strategy tips
|
|
152
|
+
|
|
153
|
+
**Track source IDs:** Always store the primary database ID in metadata so you can find and update documents later.
|
|
154
|
+
|
|
155
|
+
**Batch operations:** Process updates in batches of 100-500 to balance throughput and memory usage.
|
|
156
|
+
|
|
157
|
+
**Handle deletes:** When records are deleted from your primary database, delete them from Chroma too. Use metadata filters if you track `source_id`.
|
|
158
|
+
|
|
159
|
+
**Idempotent syncs:** Use `upsert` so re-running a sync doesn't create duplicates.
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Updating and Deleting
|
|
3
|
+
description: Update existing documents and delete data from collections
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
## Updating and Deleting
|
|
7
|
+
|
|
8
|
+
Chroma provides `update`, `upsert`, and `delete` methods for modifying data after initial insertion. Understanding when to use each is important for building reliable data sync pipelines.
|
|
9
|
+
|
|
10
|
+
### Method overview
|
|
11
|
+
|
|
12
|
+
| Method | Behavior | Use when |
|
|
13
|
+
|--------|----------|----------|
|
|
14
|
+
| `update` | Modifies existing documents, fails if ID doesn't exist | You know the document exists |
|
|
15
|
+
| `upsert` | Updates if exists, inserts if not | Syncing from external data source |
|
|
16
|
+
| `delete` | Removes documents by ID or filter | Removing stale or unwanted data |
|
|
17
|
+
|
|
18
|
+
### Imports
|
|
19
|
+
|
|
20
|
+
```typescript
|
|
21
|
+
import { ChromaClient } from 'chromadb';
|
|
22
|
+
import { DefaultEmbeddingFunction } from '@chroma-core/default-embed';
|
|
23
|
+
|
|
24
|
+
const client = new ChromaClient();
|
|
25
|
+
const embeddingFunction = new DefaultEmbeddingFunction();
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Update
|
|
29
|
+
|
|
30
|
+
Update modifies existing documents. If an ID doesn't exist, the operation fails silently for that ID (no error thrown, but nothing is updated).
|
|
31
|
+
|
|
32
|
+
**Important:** When you update a document's text, Chroma re-computes the embedding automatically using the collection's embedding function.
|
|
33
|
+
|
|
34
|
+
```typescript
|
|
35
|
+
const collection = await client.getOrCreateCollection({
|
|
36
|
+
name: 'my_collection',
|
|
37
|
+
embeddingFunction,
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
await collection.add({
|
|
41
|
+
ids: ['doc1', 'doc2'],
|
|
42
|
+
documents: ['Original text for doc1', 'Original text for doc2'],
|
|
43
|
+
metadatas: [{ category: 'draft' }, { category: 'draft' }],
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
await collection.update({
|
|
47
|
+
ids: ['doc1'],
|
|
48
|
+
documents: ['Updated text for doc1'],
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
await collection.update({
|
|
52
|
+
ids: ['doc1', 'doc2'],
|
|
53
|
+
metadatas: [{ category: 'published' }, { category: 'published' }],
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
await collection.update({
|
|
57
|
+
ids: ['doc2'],
|
|
58
|
+
documents: ['Completely revised doc2 content'],
|
|
59
|
+
metadatas: [{ category: 'published', revision: 2 }],
|
|
60
|
+
});
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## Upsert
|
|
64
|
+
|
|
65
|
+
Upsert is the preferred method for syncing data from an external source. It inserts new documents and updates existing ones in a single operation.
|
|
66
|
+
|
|
67
|
+
**When to use upsert vs update:**
|
|
68
|
+
- Use `upsert` when syncing from a primary database (you don't know which records are new)
|
|
69
|
+
- Use `update` when you're certain the document already exists
|
|
70
|
+
|
|
71
|
+
```typescript
|
|
72
|
+
const collection2 = await client.getOrCreateCollection({
|
|
73
|
+
name: 'articles',
|
|
74
|
+
embeddingFunction,
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
await collection2.upsert({
|
|
78
|
+
ids: ['article-123', 'article-456', 'article-789'],
|
|
79
|
+
documents: [
|
|
80
|
+
'Content of article 123',
|
|
81
|
+
'Content of article 456',
|
|
82
|
+
'Content of article 789',
|
|
83
|
+
],
|
|
84
|
+
metadatas: [
|
|
85
|
+
{ source_id: '123', updated_at: Date.now() },
|
|
86
|
+
{ source_id: '456', updated_at: Date.now() },
|
|
87
|
+
{ source_id: '789', updated_at: Date.now() },
|
|
88
|
+
],
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
await collection2.upsert({
|
|
92
|
+
ids: ['article-123', 'article-456'],
|
|
93
|
+
documents: [
|
|
94
|
+
'Updated content of article 123',
|
|
95
|
+
'Updated content of article 456',
|
|
96
|
+
],
|
|
97
|
+
metadatas: [
|
|
98
|
+
{ source_id: '123', updated_at: Date.now() },
|
|
99
|
+
{ source_id: '456', updated_at: Date.now() },
|
|
100
|
+
],
|
|
101
|
+
});
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
## Delete by ID
|
|
105
|
+
|
|
106
|
+
The simplest way to delete documents is by their IDs.
|
|
107
|
+
|
|
108
|
+
```typescript
|
|
109
|
+
const collection3 = await client.getOrCreateCollection({
|
|
110
|
+
name: 'my_collection',
|
|
111
|
+
embeddingFunction,
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
await collection3.delete({
|
|
115
|
+
ids: ['doc1', 'doc2'],
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
await collection3.delete({
|
|
119
|
+
ids: ['doc3'],
|
|
120
|
+
});
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
## Delete by filter
|
|
124
|
+
|
|
125
|
+
Delete documents matching metadata or content filters without knowing specific IDs. Useful for bulk cleanup operations.
|
|
126
|
+
|
|
127
|
+
```typescript
|
|
128
|
+
const collection4 = await client.getOrCreateCollection({
|
|
129
|
+
name: 'my_collection',
|
|
130
|
+
embeddingFunction,
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
await collection4.delete({
|
|
134
|
+
where: { status: 'archived' },
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
await collection4.delete({
|
|
138
|
+
where: { source_id: 'old-source-123' },
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
await collection4.delete({
|
|
142
|
+
whereDocument: { $contains: 'DEPRECATED' },
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
await collection4.delete({
|
|
146
|
+
ids: ['doc1', 'doc2', 'doc3', 'doc4'],
|
|
147
|
+
where: { category: 'temp' },
|
|
148
|
+
});
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
## Syncing from an external data source
|
|
152
|
+
|
|
153
|
+
A common pattern is keeping Chroma in sync with a primary database. This example shows how to handle creates, updates, and deletes.
|
|
154
|
+
|
|
155
|
+
```typescript
|
|
156
|
+
interface SourceRecord {
|
|
157
|
+
id: string;
|
|
158
|
+
content: string;
|
|
159
|
+
updated_at: number;
|
|
160
|
+
category: string;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
async function syncToChroma(
|
|
164
|
+
collectionName: string,
|
|
165
|
+
records: SourceRecord[],
|
|
166
|
+
deletedIds: string[]
|
|
167
|
+
) {
|
|
168
|
+
const collection = await client.getOrCreateCollection({
|
|
169
|
+
name: collectionName,
|
|
170
|
+
embeddingFunction,
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
if (records.length > 0) {
|
|
174
|
+
const batchSize = 100;
|
|
175
|
+
|
|
176
|
+
for (let i = 0; i < records.length; i += batchSize) {
|
|
177
|
+
const batch = records.slice(i, i + batchSize);
|
|
178
|
+
|
|
179
|
+
await collection.upsert({
|
|
180
|
+
ids: batch.map((record) => `source-${record.id}`),
|
|
181
|
+
documents: batch.map((record) => record.content),
|
|
182
|
+
metadatas: batch.map((record) => ({
|
|
183
|
+
source_id: record.id,
|
|
184
|
+
updated_at: record.updated_at,
|
|
185
|
+
category: record.category,
|
|
186
|
+
})),
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
if (deletedIds.length > 0) {
|
|
192
|
+
await collection.delete({
|
|
193
|
+
ids: deletedIds.map((id) => `source-${id}`),
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
return { synced: records.length, deleted: deletedIds.length };
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
const changedRecords: SourceRecord[] = [
|
|
201
|
+
{
|
|
202
|
+
id: '1',
|
|
203
|
+
content: 'Article about TypeScript',
|
|
204
|
+
updated_at: Date.now(),
|
|
205
|
+
category: 'tech',
|
|
206
|
+
},
|
|
207
|
+
{
|
|
208
|
+
id: '2',
|
|
209
|
+
content: 'Guide to vector databases',
|
|
210
|
+
updated_at: Date.now(),
|
|
211
|
+
category: 'tech',
|
|
212
|
+
},
|
|
213
|
+
];
|
|
214
|
+
|
|
215
|
+
const deletedRecordIds = ['old-1', 'old-2'];
|
|
216
|
+
|
|
217
|
+
await syncToChroma('articles', changedRecords, deletedRecordIds);
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
### Sync strategy tips
|
|
221
|
+
|
|
222
|
+
**Track source IDs:** Always store the primary database ID in metadata so you can find and update documents later.
|
|
223
|
+
|
|
224
|
+
**Batch operations:** Process updates in batches of 100-500 to balance throughput and memory usage.
|
|
225
|
+
|
|
226
|
+
**Handle deletes:** When records are deleted from your primary database, delete them from Chroma too. Use metadata filters if you track `source_id`.
|
|
227
|
+
|
|
228
|
+
**Idempotent syncs:** Use `upsert` so re-running a sync doesn't create duplicates.
|