chub-dev 0.1.0 → 0.1.2-beta.0
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/README.md +55 -0
- package/bin/chub-mcp +2 -0
- package/dist/airtable/docs/database/javascript/DOC.md +1437 -0
- package/dist/airtable/docs/database/python/DOC.md +1735 -0
- package/dist/amplitude/docs/analytics/javascript/DOC.md +1282 -0
- package/dist/amplitude/docs/analytics/python/DOC.md +1199 -0
- package/dist/anthropic/docs/claude-api/javascript/DOC.md +503 -0
- package/dist/anthropic/docs/claude-api/python/DOC.md +389 -0
- package/dist/asana/docs/tasks/DOC.md +1396 -0
- package/dist/assemblyai/docs/transcription/DOC.md +1043 -0
- package/dist/atlassian/docs/confluence/javascript/DOC.md +1347 -0
- package/dist/atlassian/docs/confluence/python/DOC.md +1604 -0
- package/dist/auth0/docs/identity/javascript/DOC.md +968 -0
- package/dist/auth0/docs/identity/python/DOC.md +1199 -0
- package/dist/aws/docs/s3/javascript/DOC.md +1773 -0
- package/dist/aws/docs/s3/python/DOC.md +1807 -0
- package/dist/binance/docs/trading/javascript/DOC.md +1315 -0
- package/dist/binance/docs/trading/python/DOC.md +1454 -0
- package/dist/braintree/docs/gateway/javascript/DOC.md +1278 -0
- package/dist/braintree/docs/gateway/python/DOC.md +1179 -0
- package/dist/chromadb/docs/embeddings-db/javascript/DOC.md +1263 -0
- package/dist/chromadb/docs/embeddings-db/python/DOC.md +1707 -0
- package/dist/clerk/docs/auth/javascript/DOC.md +1220 -0
- package/dist/clerk/docs/auth/python/DOC.md +274 -0
- package/dist/cloudflare/docs/workers/javascript/DOC.md +918 -0
- package/dist/cloudflare/docs/workers/python/DOC.md +994 -0
- package/dist/cockroachdb/docs/distributed-db/DOC.md +1500 -0
- package/dist/cohere/docs/llm/DOC.md +1335 -0
- package/dist/datadog/docs/monitoring/javascript/DOC.md +1740 -0
- package/dist/datadog/docs/monitoring/python/DOC.md +1815 -0
- package/dist/deepgram/docs/speech/javascript/DOC.md +885 -0
- package/dist/deepgram/docs/speech/python/DOC.md +685 -0
- package/dist/deepl/docs/translation/javascript/DOC.md +887 -0
- package/dist/deepl/docs/translation/python/DOC.md +944 -0
- package/dist/deepseek/docs/llm/DOC.md +1220 -0
- package/dist/directus/docs/headless-cms/javascript/DOC.md +1128 -0
- package/dist/directus/docs/headless-cms/python/DOC.md +1276 -0
- package/dist/discord/docs/bot/javascript/DOC.md +1090 -0
- package/dist/discord/docs/bot/python/DOC.md +1130 -0
- package/dist/elasticsearch/docs/search/DOC.md +1634 -0
- package/dist/elevenlabs/docs/text-to-speech/javascript/DOC.md +336 -0
- package/dist/elevenlabs/docs/text-to-speech/python/DOC.md +552 -0
- package/dist/firebase/docs/auth/DOC.md +1015 -0
- package/dist/gemini/docs/genai/javascript/DOC.md +691 -0
- package/dist/gemini/docs/genai/python/DOC.md +555 -0
- package/dist/github/docs/octokit/DOC.md +1560 -0
- package/dist/google/docs/bigquery/javascript/DOC.md +1688 -0
- package/dist/google/docs/bigquery/python/DOC.md +1503 -0
- package/dist/hubspot/docs/crm/javascript/DOC.md +1805 -0
- package/dist/hubspot/docs/crm/python/DOC.md +2033 -0
- package/dist/huggingface/docs/transformers/DOC.md +948 -0
- package/dist/intercom/docs/messaging/javascript/DOC.md +1844 -0
- package/dist/intercom/docs/messaging/python/DOC.md +1797 -0
- package/dist/jira/docs/issues/javascript/DOC.md +1420 -0
- package/dist/jira/docs/issues/python/DOC.md +1492 -0
- package/dist/kafka/docs/streaming/javascript/DOC.md +1671 -0
- package/dist/kafka/docs/streaming/python/DOC.md +1464 -0
- package/dist/landingai-ade/docs/api/DOC.md +620 -0
- package/dist/landingai-ade/docs/sdk/python/DOC.md +489 -0
- package/dist/landingai-ade/docs/sdk/typescript/DOC.md +542 -0
- package/dist/landingai-ade/skills/SKILL.md +489 -0
- package/dist/launchdarkly/docs/feature-flags/javascript/DOC.md +1191 -0
- package/dist/launchdarkly/docs/feature-flags/python/DOC.md +1671 -0
- package/dist/linear/docs/tracker/DOC.md +1554 -0
- package/dist/livekit/docs/realtime/javascript/DOC.md +303 -0
- package/dist/livekit/docs/realtime/python/DOC.md +163 -0
- package/dist/mailchimp/docs/marketing/DOC.md +1420 -0
- package/dist/meilisearch/docs/search/DOC.md +1241 -0
- package/dist/microsoft/docs/onedrive/javascript/DOC.md +1421 -0
- package/dist/microsoft/docs/onedrive/python/DOC.md +1549 -0
- package/dist/mongodb/docs/atlas/DOC.md +2041 -0
- package/dist/notion/docs/workspace-api/javascript/DOC.md +1435 -0
- package/dist/notion/docs/workspace-api/python/DOC.md +1400 -0
- package/dist/okta/docs/identity/javascript/DOC.md +1171 -0
- package/dist/okta/docs/identity/python/DOC.md +1401 -0
- package/dist/openai/docs/chat/javascript/DOC.md +407 -0
- package/dist/openai/docs/chat/python/DOC.md +568 -0
- package/dist/paypal/docs/checkout/DOC.md +278 -0
- package/dist/pinecone/docs/sdk/javascript/DOC.md +984 -0
- package/dist/pinecone/docs/sdk/python/DOC.md +1395 -0
- package/dist/plaid/docs/banking/javascript/DOC.md +1163 -0
- package/dist/plaid/docs/banking/python/DOC.md +1203 -0
- package/dist/playwright-community/skills/login-flows/SKILL.md +108 -0
- package/dist/postmark/docs/transactional-email/DOC.md +1168 -0
- package/dist/prisma/docs/orm/javascript/DOC.md +1419 -0
- package/dist/prisma/docs/orm/python/DOC.md +1317 -0
- package/dist/qdrant/docs/vector-search/javascript/DOC.md +1221 -0
- package/dist/qdrant/docs/vector-search/python/DOC.md +1653 -0
- package/dist/rabbitmq/docs/message-queue/javascript/DOC.md +1193 -0
- package/dist/rabbitmq/docs/message-queue/python/DOC.md +1243 -0
- package/dist/razorpay/docs/payments/javascript/DOC.md +1219 -0
- package/dist/razorpay/docs/payments/python/DOC.md +1330 -0
- package/dist/redis/docs/key-value/javascript/DOC.md +1851 -0
- package/dist/redis/docs/key-value/python/DOC.md +2054 -0
- package/dist/registry.json +2817 -0
- package/dist/replicate/docs/model-hosting/DOC.md +1318 -0
- package/dist/resend/docs/email/DOC.md +1271 -0
- package/dist/salesforce/docs/crm/javascript/DOC.md +1241 -0
- package/dist/salesforce/docs/crm/python/DOC.md +1183 -0
- package/dist/search-index.json +1 -0
- package/dist/sendgrid/docs/email-api/javascript/DOC.md +371 -0
- package/dist/sendgrid/docs/email-api/python/DOC.md +656 -0
- package/dist/sentry/docs/error-tracking/javascript/DOC.md +1073 -0
- package/dist/sentry/docs/error-tracking/python/DOC.md +1309 -0
- package/dist/shopify/docs/storefront/DOC.md +457 -0
- package/dist/slack/docs/workspace/javascript/DOC.md +933 -0
- package/dist/slack/docs/workspace/python/DOC.md +271 -0
- package/dist/square/docs/payments/javascript/DOC.md +1855 -0
- package/dist/square/docs/payments/python/DOC.md +1728 -0
- package/dist/stripe/docs/api/DOC.md +1727 -0
- package/dist/stripe/docs/payments/DOC.md +1726 -0
- package/dist/stytch/docs/auth/javascript/DOC.md +1813 -0
- package/dist/stytch/docs/auth/python/DOC.md +1962 -0
- package/dist/supabase/docs/client/DOC.md +1606 -0
- package/dist/twilio/docs/messaging/python/DOC.md +469 -0
- package/dist/twilio/docs/messaging/typescript/DOC.md +946 -0
- package/dist/vercel/docs/platform/DOC.md +1940 -0
- package/dist/weaviate/docs/vector-db/javascript/DOC.md +1268 -0
- package/dist/weaviate/docs/vector-db/python/DOC.md +1388 -0
- package/dist/zendesk/docs/support/javascript/DOC.md +2150 -0
- package/dist/zendesk/docs/support/python/DOC.md +2297 -0
- package/package.json +22 -6
- package/skills/get-api-docs/SKILL.md +84 -0
- package/src/commands/annotate.js +83 -0
- package/src/commands/build.js +12 -1
- package/src/commands/feedback.js +150 -0
- package/src/commands/get.js +83 -42
- package/src/commands/search.js +7 -0
- package/src/index.js +43 -17
- package/src/lib/analytics.js +90 -0
- package/src/lib/annotations.js +57 -0
- package/src/lib/bm25.js +170 -0
- package/src/lib/cache.js +69 -6
- package/src/lib/config.js +8 -3
- package/src/lib/identity.js +99 -0
- package/src/lib/registry.js +103 -20
- package/src/lib/telemetry.js +86 -0
- package/src/mcp/server.js +177 -0
- package/src/mcp/tools.js +251 -0
|
@@ -0,0 +1,1388 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: vector-db
|
|
3
|
+
description: "Weaviate Python SDK (v4) — use the official weaviate-client package for vector database operations"
|
|
4
|
+
metadata:
|
|
5
|
+
languages: "python"
|
|
6
|
+
versions: "4.17.0"
|
|
7
|
+
updated-on: "2026-03-02"
|
|
8
|
+
source: maintainer
|
|
9
|
+
tags: "weaviate,vector-db,search,embeddings,ai"
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
# Weaviate Python SDK (v4)
|
|
13
|
+
|
|
14
|
+
## Golden Rule
|
|
15
|
+
|
|
16
|
+
**ALWAYS use the official `weaviate-client` package (v4.x).**
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
pip install weaviate-client
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
**NEVER use:**
|
|
23
|
+
- `weaviate-python-client` (deprecated)
|
|
24
|
+
- The v3 client (deprecated as of 2024)
|
|
25
|
+
- Unofficial or outdated packages
|
|
26
|
+
|
|
27
|
+
The v4 client uses gRPC for 60-80% faster performance, typed classes with autocomplete, and is only compatible with Weaviate v1.23.6+.
|
|
28
|
+
|
|
29
|
+
---
|
|
30
|
+
|
|
31
|
+
## Installation
|
|
32
|
+
|
|
33
|
+
### Install the Client
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
pip install weaviate-client
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### Environment Variables
|
|
40
|
+
|
|
41
|
+
Create a `.env` file:
|
|
42
|
+
|
|
43
|
+
```env
|
|
44
|
+
WEAVIATE_URL=https://your-instance.weaviate.network
|
|
45
|
+
WEAVIATE_API_KEY=your-api-key-here
|
|
46
|
+
OPENAI_API_KEY=your-openai-key-here
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### Load Environment Variables
|
|
50
|
+
|
|
51
|
+
```python
|
|
52
|
+
import os
|
|
53
|
+
from dotenv import load_dotenv
|
|
54
|
+
|
|
55
|
+
load_dotenv()
|
|
56
|
+
|
|
57
|
+
WEAVIATE_URL = os.getenv("WEAVIATE_URL")
|
|
58
|
+
WEAVIATE_API_KEY = os.getenv("WEAVIATE_API_KEY")
|
|
59
|
+
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
---
|
|
63
|
+
|
|
64
|
+
## Initialization
|
|
65
|
+
|
|
66
|
+
### Connect to Weaviate Cloud
|
|
67
|
+
|
|
68
|
+
```python
|
|
69
|
+
import weaviate
|
|
70
|
+
from weaviate.classes.init import Auth
|
|
71
|
+
|
|
72
|
+
client = weaviate.connect_to_weaviate_cloud(
|
|
73
|
+
cluster_url=WEAVIATE_URL,
|
|
74
|
+
auth_credentials=Auth.api_key(WEAVIATE_API_KEY),
|
|
75
|
+
headers={
|
|
76
|
+
"X-OpenAI-Api-Key": OPENAI_API_KEY
|
|
77
|
+
}
|
|
78
|
+
)
|
|
79
|
+
|
|
80
|
+
print("Connected to Weaviate")
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
### Connect to Local Instance
|
|
84
|
+
|
|
85
|
+
```python
|
|
86
|
+
import weaviate
|
|
87
|
+
|
|
88
|
+
client = weaviate.connect_to_local(
|
|
89
|
+
host="localhost",
|
|
90
|
+
port=8080,
|
|
91
|
+
grpc_port=50051,
|
|
92
|
+
headers={
|
|
93
|
+
"X-OpenAI-Api-Key": OPENAI_API_KEY
|
|
94
|
+
}
|
|
95
|
+
)
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
### Custom Connection
|
|
99
|
+
|
|
100
|
+
```python
|
|
101
|
+
import weaviate
|
|
102
|
+
from weaviate.classes.init import Auth, AdditionalConfig, Timeout
|
|
103
|
+
|
|
104
|
+
client = weaviate.connect_to_custom(
|
|
105
|
+
http_host="weaviate.example.com",
|
|
106
|
+
http_port=443,
|
|
107
|
+
http_secure=True,
|
|
108
|
+
grpc_host="grpc.weaviate.example.com",
|
|
109
|
+
grpc_port=443,
|
|
110
|
+
grpc_secure=True,
|
|
111
|
+
auth_credentials=Auth.api_key(WEAVIATE_API_KEY),
|
|
112
|
+
headers={
|
|
113
|
+
"X-OpenAI-Api-Key": OPENAI_API_KEY
|
|
114
|
+
},
|
|
115
|
+
additional_config=AdditionalConfig(
|
|
116
|
+
timeout=Timeout(init=30, query=60, insert=120)
|
|
117
|
+
)
|
|
118
|
+
)
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
### Context Manager Pattern
|
|
122
|
+
|
|
123
|
+
```python
|
|
124
|
+
import weaviate
|
|
125
|
+
from weaviate.classes.init import Auth
|
|
126
|
+
|
|
127
|
+
with weaviate.connect_to_weaviate_cloud(
|
|
128
|
+
cluster_url=WEAVIATE_URL,
|
|
129
|
+
auth_credentials=Auth.api_key(WEAVIATE_API_KEY)
|
|
130
|
+
) as client:
|
|
131
|
+
# Your code here
|
|
132
|
+
collections = client.collections.list_all()
|
|
133
|
+
print(collections)
|
|
134
|
+
# Connection automatically closed
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
### Close Connection
|
|
138
|
+
|
|
139
|
+
```python
|
|
140
|
+
client.close()
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
---
|
|
144
|
+
|
|
145
|
+
## Collections
|
|
146
|
+
|
|
147
|
+
### Create a Collection
|
|
148
|
+
|
|
149
|
+
#### Basic Collection
|
|
150
|
+
|
|
151
|
+
```python
|
|
152
|
+
from weaviate.classes.config import Property, DataType
|
|
153
|
+
|
|
154
|
+
client.collections.create(
|
|
155
|
+
name="Article",
|
|
156
|
+
properties=[
|
|
157
|
+
Property(name="title", data_type=DataType.TEXT),
|
|
158
|
+
Property(name="body", data_type=DataType.TEXT),
|
|
159
|
+
Property(name="author", data_type=DataType.TEXT),
|
|
160
|
+
Property(name="publish_date", data_type=DataType.DATE),
|
|
161
|
+
Property(name="view_count", data_type=DataType.INT),
|
|
162
|
+
],
|
|
163
|
+
)
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
#### Collection with Vectorizer
|
|
167
|
+
|
|
168
|
+
```python
|
|
169
|
+
from weaviate.classes.config import Configure, Property, DataType
|
|
170
|
+
|
|
171
|
+
client.collections.create(
|
|
172
|
+
name="Article",
|
|
173
|
+
vectorizer_config=Configure.Vectorizer.text2vec_openai(
|
|
174
|
+
model="text-embedding-3-small"
|
|
175
|
+
),
|
|
176
|
+
properties=[
|
|
177
|
+
Property(name="title", data_type=DataType.TEXT),
|
|
178
|
+
Property(name="body", data_type=DataType.TEXT),
|
|
179
|
+
Property(name="author", data_type=DataType.TEXT),
|
|
180
|
+
],
|
|
181
|
+
)
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
#### Collection with Multiple Named Vectors
|
|
185
|
+
|
|
186
|
+
```python
|
|
187
|
+
from weaviate.classes.config import Configure, Property, DataType
|
|
188
|
+
|
|
189
|
+
client.collections.create(
|
|
190
|
+
name="MultiModalArticle",
|
|
191
|
+
properties=[
|
|
192
|
+
Property(name="title", data_type=DataType.TEXT),
|
|
193
|
+
Property(name="body", data_type=DataType.TEXT),
|
|
194
|
+
Property(name="image", data_type=DataType.BLOB),
|
|
195
|
+
],
|
|
196
|
+
vectorizer_config=[
|
|
197
|
+
Configure.NamedVectors.text2vec_openai(
|
|
198
|
+
name="text_vector",
|
|
199
|
+
source_properties=["title", "body"],
|
|
200
|
+
),
|
|
201
|
+
Configure.NamedVectors.multi2vec_clip(
|
|
202
|
+
name="image_vector",
|
|
203
|
+
image_fields=["image"],
|
|
204
|
+
),
|
|
205
|
+
],
|
|
206
|
+
)
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
#### Collection with Generative Module
|
|
210
|
+
|
|
211
|
+
```python
|
|
212
|
+
from weaviate.classes.config import Configure, Property, DataType
|
|
213
|
+
|
|
214
|
+
client.collections.create(
|
|
215
|
+
name="Article",
|
|
216
|
+
vectorizer_config=Configure.Vectorizer.text2vec_openai(),
|
|
217
|
+
generative_config=Configure.Generative.openai(
|
|
218
|
+
model="gpt-4"
|
|
219
|
+
),
|
|
220
|
+
properties=[
|
|
221
|
+
Property(name="title", data_type=DataType.TEXT),
|
|
222
|
+
Property(name="body", data_type=DataType.TEXT),
|
|
223
|
+
],
|
|
224
|
+
)
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
### List All Collections
|
|
228
|
+
|
|
229
|
+
```python
|
|
230
|
+
collections = client.collections.list_all()
|
|
231
|
+
for collection_name in collections:
|
|
232
|
+
print(collection_name)
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
### Get a Collection Reference
|
|
236
|
+
|
|
237
|
+
```python
|
|
238
|
+
articles = client.collections.get("Article")
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
### Delete a Collection
|
|
242
|
+
|
|
243
|
+
```python
|
|
244
|
+
client.collections.delete("Article")
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
### Check if Collection Exists
|
|
248
|
+
|
|
249
|
+
```python
|
|
250
|
+
exists = client.collections.exists("Article")
|
|
251
|
+
print(f"Collection exists: {exists}")
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
---
|
|
255
|
+
|
|
256
|
+
## Data Types
|
|
257
|
+
|
|
258
|
+
### Available Data Types
|
|
259
|
+
|
|
260
|
+
```python
|
|
261
|
+
from weaviate.classes.config import DataType
|
|
262
|
+
|
|
263
|
+
# Text types
|
|
264
|
+
DataType.TEXT # Single text value
|
|
265
|
+
DataType.TEXT_ARRAY # List of text values
|
|
266
|
+
|
|
267
|
+
# Numeric types
|
|
268
|
+
DataType.NUMBER # Float/double
|
|
269
|
+
DataType.INT # Integer
|
|
270
|
+
DataType.INT_ARRAY # List of integers
|
|
271
|
+
DataType.NUMBER_ARRAY # List of numbers
|
|
272
|
+
|
|
273
|
+
# Boolean
|
|
274
|
+
DataType.BOOL # True/false
|
|
275
|
+
DataType.BOOL_ARRAY # List of booleans
|
|
276
|
+
|
|
277
|
+
# Date and UUID
|
|
278
|
+
DataType.DATE # ISO 8601 date-time
|
|
279
|
+
DataType.DATE_ARRAY # List of dates
|
|
280
|
+
DataType.UUID # UUID
|
|
281
|
+
DataType.UUID_ARRAY # List of UUIDs
|
|
282
|
+
|
|
283
|
+
# Binary data
|
|
284
|
+
DataType.BLOB # Base64 encoded binary data
|
|
285
|
+
DataType.BLOB_ARRAY # List of blobs
|
|
286
|
+
|
|
287
|
+
# Geolocation
|
|
288
|
+
DataType.GEO_COORDINATES # {"latitude": float, "longitude": float}
|
|
289
|
+
|
|
290
|
+
# Object reference
|
|
291
|
+
DataType.OBJECT # Nested object
|
|
292
|
+
DataType.OBJECT_ARRAY # List of nested objects
|
|
293
|
+
```
|
|
294
|
+
|
|
295
|
+
---
|
|
296
|
+
|
|
297
|
+
## Insert Data
|
|
298
|
+
|
|
299
|
+
### Insert Single Object
|
|
300
|
+
|
|
301
|
+
```python
|
|
302
|
+
from datetime import datetime
|
|
303
|
+
from weaviate.util import generate_uuid5
|
|
304
|
+
|
|
305
|
+
articles = client.collections.get("Article")
|
|
306
|
+
|
|
307
|
+
uuid = articles.data.insert(
|
|
308
|
+
properties={
|
|
309
|
+
"title": "Weaviate is Amazing",
|
|
310
|
+
"body": "A comprehensive guide to vector databases",
|
|
311
|
+
"author": "John Doe",
|
|
312
|
+
"publish_date": datetime(2024, 1, 15).isoformat(),
|
|
313
|
+
"view_count": 1250,
|
|
314
|
+
}
|
|
315
|
+
)
|
|
316
|
+
|
|
317
|
+
print(f"Inserted object with UUID: {uuid}")
|
|
318
|
+
```
|
|
319
|
+
|
|
320
|
+
### Insert with Custom UUID
|
|
321
|
+
|
|
322
|
+
```python
|
|
323
|
+
from weaviate.util import generate_uuid5
|
|
324
|
+
|
|
325
|
+
my_uuid = generate_uuid5("my-unique-id")
|
|
326
|
+
|
|
327
|
+
articles.data.insert(
|
|
328
|
+
properties={
|
|
329
|
+
"title": "Custom UUID Article",
|
|
330
|
+
"body": "Article with custom UUID",
|
|
331
|
+
},
|
|
332
|
+
uuid=my_uuid
|
|
333
|
+
)
|
|
334
|
+
```
|
|
335
|
+
|
|
336
|
+
### Insert with Vector
|
|
337
|
+
|
|
338
|
+
```python
|
|
339
|
+
uuid = articles.data.insert(
|
|
340
|
+
properties={
|
|
341
|
+
"title": "Manual Vector Article",
|
|
342
|
+
"body": "Article with manually provided vector",
|
|
343
|
+
},
|
|
344
|
+
vector=[0.1, 0.2, 0.3, ...] # Your embedding vector
|
|
345
|
+
)
|
|
346
|
+
```
|
|
347
|
+
|
|
348
|
+
### Batch Insert (Dynamic Batching)
|
|
349
|
+
|
|
350
|
+
```python
|
|
351
|
+
articles = client.collections.get("Article")
|
|
352
|
+
|
|
353
|
+
data_objects = [
|
|
354
|
+
{
|
|
355
|
+
"title": "First Article",
|
|
356
|
+
"body": "Content of first article",
|
|
357
|
+
"author": "Jane Smith",
|
|
358
|
+
},
|
|
359
|
+
{
|
|
360
|
+
"title": "Second Article",
|
|
361
|
+
"body": "Content of second article",
|
|
362
|
+
"author": "Bob Johnson",
|
|
363
|
+
},
|
|
364
|
+
{
|
|
365
|
+
"title": "Third Article",
|
|
366
|
+
"body": "Content of third article",
|
|
367
|
+
"author": "Alice Williams",
|
|
368
|
+
},
|
|
369
|
+
]
|
|
370
|
+
|
|
371
|
+
with articles.batch.dynamic() as batch:
|
|
372
|
+
for data_obj in data_objects:
|
|
373
|
+
batch.add_object(properties=data_obj)
|
|
374
|
+
|
|
375
|
+
print(f"Batch insert completed")
|
|
376
|
+
```
|
|
377
|
+
|
|
378
|
+
### Batch Insert with Error Handling
|
|
379
|
+
|
|
380
|
+
```python
|
|
381
|
+
articles = client.collections.get("Article")
|
|
382
|
+
|
|
383
|
+
with articles.batch.dynamic() as batch:
|
|
384
|
+
for data_obj in data_objects:
|
|
385
|
+
batch.add_object(properties=data_obj)
|
|
386
|
+
|
|
387
|
+
# Check for errors within the context
|
|
388
|
+
if batch.number_errors > 0:
|
|
389
|
+
print(f"Errors occurred: {batch.number_errors}")
|
|
390
|
+
for failed in batch.failed_objects:
|
|
391
|
+
print(f"Failed: {failed}")
|
|
392
|
+
```
|
|
393
|
+
|
|
394
|
+
### Batch Insert with Vectors
|
|
395
|
+
|
|
396
|
+
```python
|
|
397
|
+
data_with_vectors = [
|
|
398
|
+
{"title": "Article 1", "body": "Content 1"},
|
|
399
|
+
{"title": "Article 2", "body": "Content 2"},
|
|
400
|
+
]
|
|
401
|
+
|
|
402
|
+
vectors = [
|
|
403
|
+
[0.1, 0.2, 0.3, ...],
|
|
404
|
+
[0.4, 0.5, 0.6, ...],
|
|
405
|
+
]
|
|
406
|
+
|
|
407
|
+
with articles.batch.dynamic() as batch:
|
|
408
|
+
for i, data_obj in enumerate(data_with_vectors):
|
|
409
|
+
batch.add_object(
|
|
410
|
+
properties=data_obj,
|
|
411
|
+
vector=vectors[i]
|
|
412
|
+
)
|
|
413
|
+
```
|
|
414
|
+
|
|
415
|
+
### Fixed Size Batching
|
|
416
|
+
|
|
417
|
+
```python
|
|
418
|
+
with articles.batch.fixed_size(batch_size=100) as batch:
|
|
419
|
+
for data_obj in data_objects:
|
|
420
|
+
batch.add_object(properties=data_obj)
|
|
421
|
+
```
|
|
422
|
+
|
|
423
|
+
### Rate Limited Batching
|
|
424
|
+
|
|
425
|
+
```python
|
|
426
|
+
with articles.batch.rate_limit(requests_per_minute=600) as batch:
|
|
427
|
+
for data_obj in data_objects:
|
|
428
|
+
batch.add_object(properties=data_obj)
|
|
429
|
+
```
|
|
430
|
+
|
|
431
|
+
---
|
|
432
|
+
|
|
433
|
+
## Query Data
|
|
434
|
+
|
|
435
|
+
### Fetch Objects (No Search)
|
|
436
|
+
|
|
437
|
+
```python
|
|
438
|
+
articles = client.collections.get("Article")
|
|
439
|
+
|
|
440
|
+
response = articles.query.fetch_objects(
|
|
441
|
+
limit=10,
|
|
442
|
+
offset=0
|
|
443
|
+
)
|
|
444
|
+
|
|
445
|
+
for obj in response.objects:
|
|
446
|
+
print(obj.properties)
|
|
447
|
+
print(f"UUID: {obj.uuid}")
|
|
448
|
+
```
|
|
449
|
+
|
|
450
|
+
### Fetch by ID
|
|
451
|
+
|
|
452
|
+
```python
|
|
453
|
+
obj = articles.query.fetch_object_by_id(uuid)
|
|
454
|
+
if obj:
|
|
455
|
+
print(obj.properties)
|
|
456
|
+
```
|
|
457
|
+
|
|
458
|
+
### Fetch with Filters
|
|
459
|
+
|
|
460
|
+
```python
|
|
461
|
+
from weaviate.classes.query import Filter
|
|
462
|
+
|
|
463
|
+
response = articles.query.fetch_objects(
|
|
464
|
+
filters=Filter.by_property("view_count").greater_than(1000),
|
|
465
|
+
limit=5
|
|
466
|
+
)
|
|
467
|
+
|
|
468
|
+
for obj in response.objects:
|
|
469
|
+
print(obj.properties)
|
|
470
|
+
```
|
|
471
|
+
|
|
472
|
+
---
|
|
473
|
+
|
|
474
|
+
## Vector Search
|
|
475
|
+
|
|
476
|
+
### near_text Search
|
|
477
|
+
|
|
478
|
+
```python
|
|
479
|
+
from weaviate.classes.query import MetadataQuery
|
|
480
|
+
|
|
481
|
+
articles = client.collections.get("Article")
|
|
482
|
+
|
|
483
|
+
response = articles.query.near_text(
|
|
484
|
+
query="vector databases",
|
|
485
|
+
limit=5,
|
|
486
|
+
return_metadata=MetadataQuery(distance=True, score=True)
|
|
487
|
+
)
|
|
488
|
+
|
|
489
|
+
for obj in response.objects:
|
|
490
|
+
print(obj.properties)
|
|
491
|
+
print(f"Distance: {obj.metadata.distance}")
|
|
492
|
+
print(f"Score: {obj.metadata.score}")
|
|
493
|
+
```
|
|
494
|
+
|
|
495
|
+
### near_text with Filters
|
|
496
|
+
|
|
497
|
+
```python
|
|
498
|
+
from weaviate.classes.query import Filter, MetadataQuery
|
|
499
|
+
|
|
500
|
+
response = articles.query.near_text(
|
|
501
|
+
query="machine learning",
|
|
502
|
+
limit=3,
|
|
503
|
+
filters=Filter.by_property("author").equal("John Doe"),
|
|
504
|
+
return_metadata=MetadataQuery(distance=True)
|
|
505
|
+
)
|
|
506
|
+
|
|
507
|
+
for obj in response.objects:
|
|
508
|
+
print(obj.properties)
|
|
509
|
+
```
|
|
510
|
+
|
|
511
|
+
### near_vector Search
|
|
512
|
+
|
|
513
|
+
```python
|
|
514
|
+
from weaviate.classes.query import MetadataQuery
|
|
515
|
+
|
|
516
|
+
query_vector = [0.1, 0.2, 0.3, ...] # Your query vector
|
|
517
|
+
|
|
518
|
+
response = articles.query.near_vector(
|
|
519
|
+
near_vector=query_vector,
|
|
520
|
+
limit=5,
|
|
521
|
+
return_metadata=MetadataQuery(distance=True)
|
|
522
|
+
)
|
|
523
|
+
|
|
524
|
+
for obj in response.objects:
|
|
525
|
+
print(obj.properties)
|
|
526
|
+
```
|
|
527
|
+
|
|
528
|
+
### near_object Search
|
|
529
|
+
|
|
530
|
+
```python
|
|
531
|
+
from weaviate.classes.query import MetadataQuery
|
|
532
|
+
|
|
533
|
+
response = articles.query.near_object(
|
|
534
|
+
near_object=existing_uuid,
|
|
535
|
+
limit=5,
|
|
536
|
+
return_metadata=MetadataQuery(distance=True)
|
|
537
|
+
)
|
|
538
|
+
|
|
539
|
+
for obj in response.objects:
|
|
540
|
+
print(obj.properties)
|
|
541
|
+
```
|
|
542
|
+
|
|
543
|
+
---
|
|
544
|
+
|
|
545
|
+
## Hybrid Search
|
|
546
|
+
|
|
547
|
+
Hybrid search combines keyword (BM25) and vector search.
|
|
548
|
+
|
|
549
|
+
### Basic Hybrid Search
|
|
550
|
+
|
|
551
|
+
```python
|
|
552
|
+
articles = client.collections.get("Article")
|
|
553
|
+
|
|
554
|
+
response = articles.query.hybrid(
|
|
555
|
+
query="artificial intelligence",
|
|
556
|
+
limit=10,
|
|
557
|
+
alpha=0.5 # 0 = pure keyword, 1 = pure vector, 0.5 = balanced
|
|
558
|
+
)
|
|
559
|
+
|
|
560
|
+
for obj in response.objects:
|
|
561
|
+
print(obj.properties)
|
|
562
|
+
```
|
|
563
|
+
|
|
564
|
+
### Hybrid Search with Filters
|
|
565
|
+
|
|
566
|
+
```python
|
|
567
|
+
from weaviate.classes.query import Filter, MetadataQuery
|
|
568
|
+
from datetime import datetime
|
|
569
|
+
|
|
570
|
+
response = articles.query.hybrid(
|
|
571
|
+
query="deep learning",
|
|
572
|
+
limit=5,
|
|
573
|
+
alpha=0.75,
|
|
574
|
+
filters=Filter.by_property("publish_date").greater_than(
|
|
575
|
+
datetime(2024, 1, 1).isoformat()
|
|
576
|
+
),
|
|
577
|
+
return_metadata=MetadataQuery(score=True)
|
|
578
|
+
)
|
|
579
|
+
|
|
580
|
+
for obj in response.objects:
|
|
581
|
+
print(obj.properties)
|
|
582
|
+
print(f"Score: {obj.metadata.score}")
|
|
583
|
+
```
|
|
584
|
+
|
|
585
|
+
### Hybrid Search with Vector
|
|
586
|
+
|
|
587
|
+
```python
|
|
588
|
+
response = articles.query.hybrid(
|
|
589
|
+
query="machine learning",
|
|
590
|
+
limit=5,
|
|
591
|
+
alpha=0.5,
|
|
592
|
+
vector=[0.1, 0.2, 0.3, ...] # Optional manual vector
|
|
593
|
+
)
|
|
594
|
+
```
|
|
595
|
+
|
|
596
|
+
---
|
|
597
|
+
|
|
598
|
+
## Filters
|
|
599
|
+
|
|
600
|
+
### Basic Filters
|
|
601
|
+
|
|
602
|
+
```python
|
|
603
|
+
from weaviate.classes.query import Filter
|
|
604
|
+
|
|
605
|
+
articles = client.collections.get("Article")
|
|
606
|
+
|
|
607
|
+
# Equal
|
|
608
|
+
Filter.by_property("author").equal("John Doe")
|
|
609
|
+
|
|
610
|
+
# Not Equal
|
|
611
|
+
Filter.by_property("author").not_equal("Jane Smith")
|
|
612
|
+
|
|
613
|
+
# Greater Than
|
|
614
|
+
Filter.by_property("view_count").greater_than(1000)
|
|
615
|
+
|
|
616
|
+
# Greater Than or Equal
|
|
617
|
+
Filter.by_property("view_count").greater_or_equal(500)
|
|
618
|
+
|
|
619
|
+
# Less Than
|
|
620
|
+
Filter.by_property("view_count").less_than(2000)
|
|
621
|
+
|
|
622
|
+
# Less Than or Equal
|
|
623
|
+
Filter.by_property("view_count").less_or_equal(1500)
|
|
624
|
+
|
|
625
|
+
# Like (text contains)
|
|
626
|
+
Filter.by_property("title").like("*vector*")
|
|
627
|
+
|
|
628
|
+
# Contains Any
|
|
629
|
+
Filter.by_property("author").contains_any(["John Doe", "Jane Smith"])
|
|
630
|
+
|
|
631
|
+
# Is Null
|
|
632
|
+
Filter.by_property("author").is_null(True)
|
|
633
|
+
```
|
|
634
|
+
|
|
635
|
+
### Combining Filters (AND)
|
|
636
|
+
|
|
637
|
+
```python
|
|
638
|
+
from weaviate.classes.query import Filter
|
|
639
|
+
|
|
640
|
+
response = articles.query.fetch_objects(
|
|
641
|
+
filters=(
|
|
642
|
+
Filter.by_property("view_count").greater_than(1000) &
|
|
643
|
+
Filter.by_property("author").equal("John Doe")
|
|
644
|
+
),
|
|
645
|
+
limit=10
|
|
646
|
+
)
|
|
647
|
+
```
|
|
648
|
+
|
|
649
|
+
### Combining Filters (OR)
|
|
650
|
+
|
|
651
|
+
```python
|
|
652
|
+
from weaviate.classes.query import Filter
|
|
653
|
+
|
|
654
|
+
response = articles.query.fetch_objects(
|
|
655
|
+
filters=(
|
|
656
|
+
Filter.by_property("author").equal("John Doe") |
|
|
657
|
+
Filter.by_property("author").equal("Jane Smith")
|
|
658
|
+
),
|
|
659
|
+
limit=10
|
|
660
|
+
)
|
|
661
|
+
```
|
|
662
|
+
|
|
663
|
+
### Complex Filter Combinations
|
|
664
|
+
|
|
665
|
+
```python
|
|
666
|
+
from weaviate.classes.query import Filter
|
|
667
|
+
from datetime import datetime
|
|
668
|
+
|
|
669
|
+
response = articles.query.fetch_objects(
|
|
670
|
+
filters=(
|
|
671
|
+
(
|
|
672
|
+
Filter.by_property("view_count").greater_than(500) &
|
|
673
|
+
Filter.by_property("publish_date").greater_than(
|
|
674
|
+
datetime(2024, 1, 1).isoformat()
|
|
675
|
+
)
|
|
676
|
+
) |
|
|
677
|
+
Filter.by_property("author").equal("John Doe")
|
|
678
|
+
),
|
|
679
|
+
limit=10
|
|
680
|
+
)
|
|
681
|
+
```
|
|
682
|
+
|
|
683
|
+
---
|
|
684
|
+
|
|
685
|
+
## Generative Search (RAG)
|
|
686
|
+
|
|
687
|
+
Generative search pipes search results through an LLM.
|
|
688
|
+
|
|
689
|
+
### Single Result Generation
|
|
690
|
+
|
|
691
|
+
```python
|
|
692
|
+
articles = client.collections.get("Article")
|
|
693
|
+
|
|
694
|
+
response = articles.generate.near_text(
|
|
695
|
+
query="AI trends",
|
|
696
|
+
single_prompt="Summarize this article in one sentence: {title} - {body}",
|
|
697
|
+
limit=5
|
|
698
|
+
)
|
|
699
|
+
|
|
700
|
+
for obj in response.objects:
|
|
701
|
+
print("Article:", obj.properties)
|
|
702
|
+
print("Generated:", obj.generated)
|
|
703
|
+
```
|
|
704
|
+
|
|
705
|
+
### Grouped Task Generation
|
|
706
|
+
|
|
707
|
+
```python
|
|
708
|
+
response = articles.generate.near_text(
|
|
709
|
+
query="machine learning",
|
|
710
|
+
grouped_task="Write a brief overview summarizing these articles about machine learning",
|
|
711
|
+
limit=5
|
|
712
|
+
)
|
|
713
|
+
|
|
714
|
+
print("Generated summary:", response.generated)
|
|
715
|
+
|
|
716
|
+
for obj in response.objects:
|
|
717
|
+
print("Source article:", obj.properties["title"])
|
|
718
|
+
```
|
|
719
|
+
|
|
720
|
+
### Both Single and Grouped
|
|
721
|
+
|
|
722
|
+
```python
|
|
723
|
+
response = articles.generate.near_text(
|
|
724
|
+
query="neural networks",
|
|
725
|
+
single_prompt="Explain this in simple terms: {body}",
|
|
726
|
+
grouped_task="Create a comprehensive guide based on all these articles",
|
|
727
|
+
limit=5
|
|
728
|
+
)
|
|
729
|
+
|
|
730
|
+
for obj in response.objects:
|
|
731
|
+
print("Article:", obj.properties["title"])
|
|
732
|
+
print("Individual generation:", obj.generated)
|
|
733
|
+
|
|
734
|
+
print("\nGrouped generation:", response.generated)
|
|
735
|
+
```
|
|
736
|
+
|
|
737
|
+
### Generative Search with Filters
|
|
738
|
+
|
|
739
|
+
```python
|
|
740
|
+
from weaviate.classes.query import Filter
|
|
741
|
+
|
|
742
|
+
response = articles.generate.near_text(
|
|
743
|
+
query="databases",
|
|
744
|
+
single_prompt="Summarize: {title}",
|
|
745
|
+
limit=3,
|
|
746
|
+
filters=Filter.by_property("author").equal("John Doe")
|
|
747
|
+
)
|
|
748
|
+
```
|
|
749
|
+
|
|
750
|
+
### Generate with near_vector
|
|
751
|
+
|
|
752
|
+
```python
|
|
753
|
+
query_vector = [0.1, 0.2, 0.3, ...]
|
|
754
|
+
|
|
755
|
+
response = articles.generate.near_vector(
|
|
756
|
+
near_vector=query_vector,
|
|
757
|
+
grouped_task="Create a summary of these related articles",
|
|
758
|
+
limit=5
|
|
759
|
+
)
|
|
760
|
+
|
|
761
|
+
print(response.generated)
|
|
762
|
+
```
|
|
763
|
+
|
|
764
|
+
---
|
|
765
|
+
|
|
766
|
+
## Update Data
|
|
767
|
+
|
|
768
|
+
### Update Object
|
|
769
|
+
|
|
770
|
+
```python
|
|
771
|
+
articles = client.collections.get("Article")
|
|
772
|
+
|
|
773
|
+
articles.data.update(
|
|
774
|
+
uuid=uuid,
|
|
775
|
+
properties={
|
|
776
|
+
"view_count": 2000,
|
|
777
|
+
"title": "Updated Title",
|
|
778
|
+
}
|
|
779
|
+
)
|
|
780
|
+
```
|
|
781
|
+
|
|
782
|
+
### Replace Object
|
|
783
|
+
|
|
784
|
+
Replace replaces the entire object (not a merge).
|
|
785
|
+
|
|
786
|
+
```python
|
|
787
|
+
from datetime import datetime
|
|
788
|
+
|
|
789
|
+
articles.data.replace(
|
|
790
|
+
uuid=uuid,
|
|
791
|
+
properties={
|
|
792
|
+
"title": "Completely New Title",
|
|
793
|
+
"body": "Completely new body",
|
|
794
|
+
"author": "New Author",
|
|
795
|
+
"publish_date": datetime.now().isoformat(),
|
|
796
|
+
"view_count": 0,
|
|
797
|
+
}
|
|
798
|
+
)
|
|
799
|
+
```
|
|
800
|
+
|
|
801
|
+
---
|
|
802
|
+
|
|
803
|
+
## Delete Data
|
|
804
|
+
|
|
805
|
+
### Delete by ID
|
|
806
|
+
|
|
807
|
+
```python
|
|
808
|
+
articles = client.collections.get("Article")
|
|
809
|
+
|
|
810
|
+
articles.data.delete_by_id(uuid)
|
|
811
|
+
print("Object deleted")
|
|
812
|
+
```
|
|
813
|
+
|
|
814
|
+
### Delete Many by Filter
|
|
815
|
+
|
|
816
|
+
```python
|
|
817
|
+
from weaviate.classes.query import Filter
|
|
818
|
+
|
|
819
|
+
response = articles.data.delete_many(
|
|
820
|
+
where=Filter.by_property("view_count").less_than(100)
|
|
821
|
+
)
|
|
822
|
+
|
|
823
|
+
print(f"Deleted {response.successful} objects")
|
|
824
|
+
print(f"Failed: {response.failed}")
|
|
825
|
+
```
|
|
826
|
+
|
|
827
|
+
### Delete All Objects in Collection
|
|
828
|
+
|
|
829
|
+
```python
|
|
830
|
+
from weaviate.classes.query import Filter
|
|
831
|
+
|
|
832
|
+
articles.data.delete_many(
|
|
833
|
+
where=Filter.by_property("title").like("*")
|
|
834
|
+
)
|
|
835
|
+
```
|
|
836
|
+
|
|
837
|
+
---
|
|
838
|
+
|
|
839
|
+
## Aggregations
|
|
840
|
+
|
|
841
|
+
### Count Objects
|
|
842
|
+
|
|
843
|
+
```python
|
|
844
|
+
articles = client.collections.get("Article")
|
|
845
|
+
|
|
846
|
+
response = articles.aggregate.over_all()
|
|
847
|
+
print(f"Total objects: {response.total_count}")
|
|
848
|
+
```
|
|
849
|
+
|
|
850
|
+
### Count with Filter
|
|
851
|
+
|
|
852
|
+
```python
|
|
853
|
+
from weaviate.classes.query import Filter
|
|
854
|
+
|
|
855
|
+
response = articles.aggregate.over_all(
|
|
856
|
+
filters=Filter.by_property("author").equal("John Doe")
|
|
857
|
+
)
|
|
858
|
+
|
|
859
|
+
print(f"Objects by John Doe: {response.total_count}")
|
|
860
|
+
```
|
|
861
|
+
|
|
862
|
+
### Aggregate Numeric Properties
|
|
863
|
+
|
|
864
|
+
```python
|
|
865
|
+
from weaviate.classes.aggregate import Metrics
|
|
866
|
+
|
|
867
|
+
response = articles.aggregate.over_all(
|
|
868
|
+
return_metrics=Metrics("view_count").maximum()
|
|
869
|
+
)
|
|
870
|
+
|
|
871
|
+
print(f"Max views: {response.properties['view_count'].maximum}")
|
|
872
|
+
```
|
|
873
|
+
|
|
874
|
+
### Multiple Aggregations
|
|
875
|
+
|
|
876
|
+
```python
|
|
877
|
+
from weaviate.classes.aggregate import Metrics
|
|
878
|
+
|
|
879
|
+
response = articles.aggregate.over_all(
|
|
880
|
+
return_metrics=[
|
|
881
|
+
Metrics("view_count").sum(),
|
|
882
|
+
Metrics("view_count").average(),
|
|
883
|
+
Metrics("view_count").minimum(),
|
|
884
|
+
Metrics("view_count").maximum(),
|
|
885
|
+
]
|
|
886
|
+
)
|
|
887
|
+
|
|
888
|
+
view_stats = response.properties["view_count"]
|
|
889
|
+
print(f"Sum: {view_stats.sum}")
|
|
890
|
+
print(f"Average: {view_stats.average}")
|
|
891
|
+
print(f"Min: {view_stats.minimum}")
|
|
892
|
+
print(f"Max: {view_stats.maximum}")
|
|
893
|
+
```
|
|
894
|
+
|
|
895
|
+
---
|
|
896
|
+
|
|
897
|
+
## Multi-Tenancy
|
|
898
|
+
|
|
899
|
+
### Create Collection with Multi-Tenancy
|
|
900
|
+
|
|
901
|
+
```python
|
|
902
|
+
from weaviate.classes.config import Configure, Property, DataType
|
|
903
|
+
|
|
904
|
+
client.collections.create(
|
|
905
|
+
name="Article",
|
|
906
|
+
multi_tenancy_config=Configure.multi_tenancy(enabled=True),
|
|
907
|
+
properties=[
|
|
908
|
+
Property(name="title", data_type=DataType.TEXT),
|
|
909
|
+
Property(name="body", data_type=DataType.TEXT),
|
|
910
|
+
],
|
|
911
|
+
)
|
|
912
|
+
```
|
|
913
|
+
|
|
914
|
+
### Add Tenants
|
|
915
|
+
|
|
916
|
+
```python
|
|
917
|
+
from weaviate.classes.tenants import Tenant
|
|
918
|
+
|
|
919
|
+
articles = client.collections.get("Article")
|
|
920
|
+
|
|
921
|
+
articles.tenants.create(
|
|
922
|
+
tenants=[
|
|
923
|
+
Tenant(name="tenant1"),
|
|
924
|
+
Tenant(name="tenant2"),
|
|
925
|
+
Tenant(name="tenant3"),
|
|
926
|
+
]
|
|
927
|
+
)
|
|
928
|
+
```
|
|
929
|
+
|
|
930
|
+
### Get Tenant Reference
|
|
931
|
+
|
|
932
|
+
```python
|
|
933
|
+
tenant1_articles = articles.with_tenant("tenant1")
|
|
934
|
+
```
|
|
935
|
+
|
|
936
|
+
### Insert Data for Tenant
|
|
937
|
+
|
|
938
|
+
```python
|
|
939
|
+
tenant1_articles.data.insert(
|
|
940
|
+
properties={
|
|
941
|
+
"title": "Tenant 1 Article",
|
|
942
|
+
"body": "This belongs to tenant 1",
|
|
943
|
+
}
|
|
944
|
+
)
|
|
945
|
+
```
|
|
946
|
+
|
|
947
|
+
### Query Tenant Data
|
|
948
|
+
|
|
949
|
+
```python
|
|
950
|
+
response = tenant1_articles.query.fetch_objects(limit=10)
|
|
951
|
+
|
|
952
|
+
for obj in response.objects:
|
|
953
|
+
print(obj.properties)
|
|
954
|
+
```
|
|
955
|
+
|
|
956
|
+
### List Tenants
|
|
957
|
+
|
|
958
|
+
```python
|
|
959
|
+
tenants = articles.tenants.get()
|
|
960
|
+
for tenant in tenants:
|
|
961
|
+
print(tenant.name)
|
|
962
|
+
```
|
|
963
|
+
|
|
964
|
+
### Remove Tenant
|
|
965
|
+
|
|
966
|
+
```python
|
|
967
|
+
articles.tenants.remove(["tenant3"])
|
|
968
|
+
```
|
|
969
|
+
|
|
970
|
+
---
|
|
971
|
+
|
|
972
|
+
## Cross-References
|
|
973
|
+
|
|
974
|
+
### Define Collection with Reference
|
|
975
|
+
|
|
976
|
+
```python
|
|
977
|
+
from weaviate.classes.config import Property, DataType, ReferenceProperty
|
|
978
|
+
|
|
979
|
+
# Create Author collection
|
|
980
|
+
client.collections.create(
|
|
981
|
+
name="Author",
|
|
982
|
+
properties=[
|
|
983
|
+
Property(name="name", data_type=DataType.TEXT),
|
|
984
|
+
Property(name="bio", data_type=DataType.TEXT),
|
|
985
|
+
],
|
|
986
|
+
)
|
|
987
|
+
|
|
988
|
+
# Create Article collection with reference to Author
|
|
989
|
+
client.collections.create(
|
|
990
|
+
name="Article",
|
|
991
|
+
properties=[
|
|
992
|
+
Property(name="title", data_type=DataType.TEXT),
|
|
993
|
+
Property(name="body", data_type=DataType.TEXT),
|
|
994
|
+
],
|
|
995
|
+
references=[
|
|
996
|
+
ReferenceProperty(
|
|
997
|
+
name="has_author",
|
|
998
|
+
target_collection="Author"
|
|
999
|
+
)
|
|
1000
|
+
],
|
|
1001
|
+
)
|
|
1002
|
+
```
|
|
1003
|
+
|
|
1004
|
+
### Insert with Reference
|
|
1005
|
+
|
|
1006
|
+
```python
|
|
1007
|
+
authors = client.collections.get("Author")
|
|
1008
|
+
articles = client.collections.get("Article")
|
|
1009
|
+
|
|
1010
|
+
# Insert author
|
|
1011
|
+
author_uuid = authors.data.insert(
|
|
1012
|
+
properties={
|
|
1013
|
+
"name": "John Doe",
|
|
1014
|
+
"bio": "Expert in AI",
|
|
1015
|
+
}
|
|
1016
|
+
)
|
|
1017
|
+
|
|
1018
|
+
# Insert article with reference
|
|
1019
|
+
articles.data.insert(
|
|
1020
|
+
properties={
|
|
1021
|
+
"title": "AI Article",
|
|
1022
|
+
"body": "Content about AI",
|
|
1023
|
+
},
|
|
1024
|
+
references={
|
|
1025
|
+
"has_author": author_uuid
|
|
1026
|
+
}
|
|
1027
|
+
)
|
|
1028
|
+
```
|
|
1029
|
+
|
|
1030
|
+
### Add Reference to Existing Object
|
|
1031
|
+
|
|
1032
|
+
```python
|
|
1033
|
+
articles.data.reference_add(
|
|
1034
|
+
from_uuid=article_uuid,
|
|
1035
|
+
from_property="has_author",
|
|
1036
|
+
to=author_uuid
|
|
1037
|
+
)
|
|
1038
|
+
```
|
|
1039
|
+
|
|
1040
|
+
### Query with References
|
|
1041
|
+
|
|
1042
|
+
```python
|
|
1043
|
+
from weaviate.classes.query import QueryReference
|
|
1044
|
+
|
|
1045
|
+
response = articles.query.fetch_objects(
|
|
1046
|
+
limit=10,
|
|
1047
|
+
return_references=QueryReference(
|
|
1048
|
+
link_on="has_author",
|
|
1049
|
+
return_properties=["name", "bio"]
|
|
1050
|
+
)
|
|
1051
|
+
)
|
|
1052
|
+
|
|
1053
|
+
for obj in response.objects:
|
|
1054
|
+
print("Article:", obj.properties)
|
|
1055
|
+
if obj.references and "has_author" in obj.references:
|
|
1056
|
+
print("Author:", obj.references["has_author"].objects[0].properties)
|
|
1057
|
+
```
|
|
1058
|
+
|
|
1059
|
+
### Delete Reference
|
|
1060
|
+
|
|
1061
|
+
```python
|
|
1062
|
+
articles.data.reference_delete(
|
|
1063
|
+
from_uuid=article_uuid,
|
|
1064
|
+
from_property="has_author",
|
|
1065
|
+
to=author_uuid
|
|
1066
|
+
)
|
|
1067
|
+
```
|
|
1068
|
+
|
|
1069
|
+
---
|
|
1070
|
+
|
|
1071
|
+
## Advanced Configurations
|
|
1072
|
+
|
|
1073
|
+
### Collection with Custom Vectorizer Settings
|
|
1074
|
+
|
|
1075
|
+
```python
|
|
1076
|
+
from weaviate.classes.config import (
|
|
1077
|
+
Configure,
|
|
1078
|
+
Property,
|
|
1079
|
+
DataType,
|
|
1080
|
+
VectorDistances
|
|
1081
|
+
)
|
|
1082
|
+
|
|
1083
|
+
client.collections.create(
|
|
1084
|
+
name="Article",
|
|
1085
|
+
vectorizer_config=Configure.Vectorizer.text2vec_openai(
|
|
1086
|
+
model="text-embedding-3-large",
|
|
1087
|
+
dimensions=1536,
|
|
1088
|
+
vectorize_collection_name=False,
|
|
1089
|
+
),
|
|
1090
|
+
vector_index_config=Configure.VectorIndex.hnsw(
|
|
1091
|
+
distance_metric=VectorDistances.COSINE,
|
|
1092
|
+
ef_construction=128,
|
|
1093
|
+
max_connections=64,
|
|
1094
|
+
),
|
|
1095
|
+
properties=[
|
|
1096
|
+
Property(
|
|
1097
|
+
name="title",
|
|
1098
|
+
data_type=DataType.TEXT,
|
|
1099
|
+
vectorize_property_name=False,
|
|
1100
|
+
skip_vectorization=False,
|
|
1101
|
+
),
|
|
1102
|
+
Property(
|
|
1103
|
+
name="body",
|
|
1104
|
+
data_type=DataType.TEXT,
|
|
1105
|
+
),
|
|
1106
|
+
Property(
|
|
1107
|
+
name="metadata",
|
|
1108
|
+
data_type=DataType.TEXT,
|
|
1109
|
+
skip_vectorization=True, # Don't vectorize this property
|
|
1110
|
+
),
|
|
1111
|
+
],
|
|
1112
|
+
)
|
|
1113
|
+
```
|
|
1114
|
+
|
|
1115
|
+
### Collection with Inverted Index Config
|
|
1116
|
+
|
|
1117
|
+
```python
|
|
1118
|
+
from weaviate.classes.config import Configure, Property, DataType, Tokenization
|
|
1119
|
+
|
|
1120
|
+
client.collections.create(
|
|
1121
|
+
name="Article",
|
|
1122
|
+
properties=[
|
|
1123
|
+
Property(
|
|
1124
|
+
name="title",
|
|
1125
|
+
data_type=DataType.TEXT,
|
|
1126
|
+
tokenization=Tokenization.WORD,
|
|
1127
|
+
index_filterable=True,
|
|
1128
|
+
index_searchable=True,
|
|
1129
|
+
),
|
|
1130
|
+
Property(
|
|
1131
|
+
name="sku",
|
|
1132
|
+
data_type=DataType.TEXT,
|
|
1133
|
+
tokenization=Tokenization.FIELD, # Exact match only
|
|
1134
|
+
index_filterable=True,
|
|
1135
|
+
index_searchable=False,
|
|
1136
|
+
),
|
|
1137
|
+
],
|
|
1138
|
+
inverted_index_config=Configure.inverted_index(
|
|
1139
|
+
index_timestamps=True,
|
|
1140
|
+
index_null_state=True,
|
|
1141
|
+
index_property_length=True,
|
|
1142
|
+
),
|
|
1143
|
+
)
|
|
1144
|
+
```
|
|
1145
|
+
|
|
1146
|
+
---
|
|
1147
|
+
|
|
1148
|
+
## Error Handling
|
|
1149
|
+
|
|
1150
|
+
### Try-Except Pattern
|
|
1151
|
+
|
|
1152
|
+
```python
|
|
1153
|
+
import weaviate
|
|
1154
|
+
from weaviate.classes.init import Auth
|
|
1155
|
+
|
|
1156
|
+
try:
|
|
1157
|
+
client = weaviate.connect_to_weaviate_cloud(
|
|
1158
|
+
cluster_url=WEAVIATE_URL,
|
|
1159
|
+
auth_credentials=Auth.api_key(WEAVIATE_API_KEY)
|
|
1160
|
+
)
|
|
1161
|
+
|
|
1162
|
+
articles = client.collections.get("Article")
|
|
1163
|
+
|
|
1164
|
+
response = articles.query.near_text(
|
|
1165
|
+
query="AI",
|
|
1166
|
+
limit=5
|
|
1167
|
+
)
|
|
1168
|
+
|
|
1169
|
+
for obj in response.objects:
|
|
1170
|
+
print(obj.properties)
|
|
1171
|
+
|
|
1172
|
+
client.close()
|
|
1173
|
+
|
|
1174
|
+
except Exception as e:
|
|
1175
|
+
print(f"Error: {e}")
|
|
1176
|
+
```
|
|
1177
|
+
|
|
1178
|
+
### Batch Insert Error Handling
|
|
1179
|
+
|
|
1180
|
+
```python
|
|
1181
|
+
articles = client.collections.get("Article")
|
|
1182
|
+
|
|
1183
|
+
with articles.batch.dynamic() as batch:
|
|
1184
|
+
for data_obj in data_objects:
|
|
1185
|
+
batch.add_object(properties=data_obj)
|
|
1186
|
+
|
|
1187
|
+
if batch.number_errors > 0:
|
|
1188
|
+
print(f"Number of errors: {batch.number_errors}")
|
|
1189
|
+
|
|
1190
|
+
for failed_obj in batch.failed_objects:
|
|
1191
|
+
print(f"Failed object: {failed_obj.object_}")
|
|
1192
|
+
print(f"Error message: {failed_obj.message}")
|
|
1193
|
+
```
|
|
1194
|
+
|
|
1195
|
+
---
|
|
1196
|
+
|
|
1197
|
+
## Complete Example
|
|
1198
|
+
|
|
1199
|
+
```python
|
|
1200
|
+
import weaviate
|
|
1201
|
+
from weaviate.classes.init import Auth
|
|
1202
|
+
from weaviate.classes.config import Configure, Property, DataType
|
|
1203
|
+
from weaviate.classes.query import Filter, MetadataQuery
|
|
1204
|
+
from weaviate.classes.aggregate import Metrics
|
|
1205
|
+
from datetime import datetime
|
|
1206
|
+
import os
|
|
1207
|
+
|
|
1208
|
+
# Connect
|
|
1209
|
+
client = weaviate.connect_to_weaviate_cloud(
|
|
1210
|
+
cluster_url=os.getenv("WEAVIATE_URL"),
|
|
1211
|
+
auth_credentials=Auth.api_key(os.getenv("WEAVIATE_API_KEY")),
|
|
1212
|
+
headers={
|
|
1213
|
+
"X-OpenAI-Api-Key": os.getenv("OPENAI_API_KEY")
|
|
1214
|
+
}
|
|
1215
|
+
)
|
|
1216
|
+
|
|
1217
|
+
print("Connected to Weaviate")
|
|
1218
|
+
|
|
1219
|
+
# Create collection
|
|
1220
|
+
client.collections.create(
|
|
1221
|
+
name="Article",
|
|
1222
|
+
vectorizer_config=Configure.Vectorizer.text2vec_openai(
|
|
1223
|
+
model="text-embedding-3-small"
|
|
1224
|
+
),
|
|
1225
|
+
generative_config=Configure.Generative.openai(
|
|
1226
|
+
model="gpt-4"
|
|
1227
|
+
),
|
|
1228
|
+
properties=[
|
|
1229
|
+
Property(name="title", data_type=DataType.TEXT),
|
|
1230
|
+
Property(name="body", data_type=DataType.TEXT),
|
|
1231
|
+
Property(name="author", data_type=DataType.TEXT),
|
|
1232
|
+
Property(name="publish_date", data_type=DataType.DATE),
|
|
1233
|
+
Property(name="view_count", data_type=DataType.INT),
|
|
1234
|
+
],
|
|
1235
|
+
)
|
|
1236
|
+
|
|
1237
|
+
print("Collection created")
|
|
1238
|
+
|
|
1239
|
+
# Get collection reference
|
|
1240
|
+
articles = client.collections.get("Article")
|
|
1241
|
+
|
|
1242
|
+
# Insert data
|
|
1243
|
+
data_objects = [
|
|
1244
|
+
{
|
|
1245
|
+
"title": "Vector Databases Explained",
|
|
1246
|
+
"body": "A comprehensive guide to understanding vector databases and their applications in modern AI systems.",
|
|
1247
|
+
"author": "John Doe",
|
|
1248
|
+
"publish_date": datetime(2024, 1, 15).isoformat(),
|
|
1249
|
+
"view_count": 1250,
|
|
1250
|
+
},
|
|
1251
|
+
{
|
|
1252
|
+
"title": "Machine Learning in Production",
|
|
1253
|
+
"body": "Best practices for deploying machine learning models in production environments.",
|
|
1254
|
+
"author": "Jane Smith",
|
|
1255
|
+
"publish_date": datetime(2024, 2, 20).isoformat(),
|
|
1256
|
+
"view_count": 2100,
|
|
1257
|
+
},
|
|
1258
|
+
{
|
|
1259
|
+
"title": "Introduction to RAG",
|
|
1260
|
+
"body": "Retrieval Augmented Generation combines search with language models for better responses.",
|
|
1261
|
+
"author": "John Doe",
|
|
1262
|
+
"publish_date": datetime(2024, 3, 10).isoformat(),
|
|
1263
|
+
"view_count": 890,
|
|
1264
|
+
},
|
|
1265
|
+
]
|
|
1266
|
+
|
|
1267
|
+
with articles.batch.dynamic() as batch:
|
|
1268
|
+
for data_obj in data_objects:
|
|
1269
|
+
batch.add_object(properties=data_obj)
|
|
1270
|
+
|
|
1271
|
+
print(f"Batch insert completed")
|
|
1272
|
+
|
|
1273
|
+
# Vector search
|
|
1274
|
+
print("\n--- Vector Search ---")
|
|
1275
|
+
search_response = articles.query.near_text(
|
|
1276
|
+
query="AI and databases",
|
|
1277
|
+
limit=2,
|
|
1278
|
+
return_metadata=MetadataQuery(distance=True, score=True)
|
|
1279
|
+
)
|
|
1280
|
+
|
|
1281
|
+
for obj in search_response.objects:
|
|
1282
|
+
print(f"\nTitle: {obj.properties['title']}")
|
|
1283
|
+
print(f"Author: {obj.properties['author']}")
|
|
1284
|
+
print(f"Score: {obj.metadata.score}")
|
|
1285
|
+
|
|
1286
|
+
# Filtered search
|
|
1287
|
+
print("\n--- Filtered Search ---")
|
|
1288
|
+
filtered_response = articles.query.near_text(
|
|
1289
|
+
query="machine learning",
|
|
1290
|
+
limit=3,
|
|
1291
|
+
filters=Filter.by_property("view_count").greater_than(1000)
|
|
1292
|
+
)
|
|
1293
|
+
|
|
1294
|
+
for obj in filtered_response.objects:
|
|
1295
|
+
print(f"\nTitle: {obj.properties['title']}")
|
|
1296
|
+
print(f"Views: {obj.properties['view_count']}")
|
|
1297
|
+
|
|
1298
|
+
# Hybrid search
|
|
1299
|
+
print("\n--- Hybrid Search ---")
|
|
1300
|
+
hybrid_response = articles.query.hybrid(
|
|
1301
|
+
query="production deployment",
|
|
1302
|
+
limit=2,
|
|
1303
|
+
alpha=0.5
|
|
1304
|
+
)
|
|
1305
|
+
|
|
1306
|
+
for obj in hybrid_response.objects:
|
|
1307
|
+
print(f"\nTitle: {obj.properties['title']}")
|
|
1308
|
+
|
|
1309
|
+
# Generative search (RAG)
|
|
1310
|
+
print("\n--- Generative Search ---")
|
|
1311
|
+
rag_response = articles.generate.near_text(
|
|
1312
|
+
query="vector databases",
|
|
1313
|
+
single_prompt="Summarize this article in one sentence: {title}",
|
|
1314
|
+
grouped_task="Write a brief paragraph about these articles",
|
|
1315
|
+
limit=2
|
|
1316
|
+
)
|
|
1317
|
+
|
|
1318
|
+
print("\nGrouped generation:")
|
|
1319
|
+
print(rag_response.generated)
|
|
1320
|
+
|
|
1321
|
+
for obj in rag_response.objects:
|
|
1322
|
+
print(f"\n{obj.properties['title']}")
|
|
1323
|
+
print(f"Summary: {obj.generated}")
|
|
1324
|
+
|
|
1325
|
+
# Aggregate
|
|
1326
|
+
print("\n--- Aggregations ---")
|
|
1327
|
+
agg_response = articles.aggregate.over_all(
|
|
1328
|
+
return_metrics=[
|
|
1329
|
+
Metrics("view_count").sum(),
|
|
1330
|
+
Metrics("view_count").average(),
|
|
1331
|
+
Metrics("view_count").maximum(),
|
|
1332
|
+
]
|
|
1333
|
+
)
|
|
1334
|
+
|
|
1335
|
+
view_stats = agg_response.properties["view_count"]
|
|
1336
|
+
print(f"Total articles: {agg_response.total_count}")
|
|
1337
|
+
print(f"Total views: {view_stats.sum}")
|
|
1338
|
+
print(f"Average views: {view_stats.average}")
|
|
1339
|
+
print(f"Max views: {view_stats.maximum}")
|
|
1340
|
+
|
|
1341
|
+
# Update
|
|
1342
|
+
response = articles.query.fetch_objects(limit=1)
|
|
1343
|
+
if response.objects:
|
|
1344
|
+
first_uuid = response.objects[0].uuid
|
|
1345
|
+
articles.data.update(
|
|
1346
|
+
uuid=first_uuid,
|
|
1347
|
+
properties={"view_count": 1500}
|
|
1348
|
+
)
|
|
1349
|
+
print("\nUpdated first article view count")
|
|
1350
|
+
|
|
1351
|
+
# Delete by filter
|
|
1352
|
+
delete_response = articles.data.delete_many(
|
|
1353
|
+
where=Filter.by_property("view_count").less_than(900)
|
|
1354
|
+
)
|
|
1355
|
+
print(f"\nDeleted {delete_response.successful} low-view articles")
|
|
1356
|
+
|
|
1357
|
+
# Cleanup
|
|
1358
|
+
client.collections.delete("Article")
|
|
1359
|
+
print("\nCollection deleted")
|
|
1360
|
+
|
|
1361
|
+
client.close()
|
|
1362
|
+
print("Connection closed")
|
|
1363
|
+
```
|
|
1364
|
+
|
|
1365
|
+
---
|
|
1366
|
+
|
|
1367
|
+
## Type Hints
|
|
1368
|
+
|
|
1369
|
+
```python
|
|
1370
|
+
from typing import List, Dict, Any
|
|
1371
|
+
import weaviate
|
|
1372
|
+
from weaviate.collections.collection import Collection
|
|
1373
|
+
from weaviate.collections.classes.grpc import QueryReturn
|
|
1374
|
+
|
|
1375
|
+
# Collection reference with type hint
|
|
1376
|
+
articles: Collection = client.collections.get("Article")
|
|
1377
|
+
|
|
1378
|
+
# Query result type
|
|
1379
|
+
response: QueryReturn = articles.query.near_text(
|
|
1380
|
+
query="AI",
|
|
1381
|
+
limit=5
|
|
1382
|
+
)
|
|
1383
|
+
|
|
1384
|
+
# Iterate with type hints
|
|
1385
|
+
for obj in response.objects:
|
|
1386
|
+
properties: Dict[str, Any] = obj.properties
|
|
1387
|
+
print(properties)
|
|
1388
|
+
```
|