-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsample_indexer.py
executable file
·324 lines (284 loc) · 10.3 KB
/
sample_indexer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#!/usr/bin/env python3
"""
Sample Indexer for Ignition RAG
This script adds some sample data to the Chroma database for testing.
Run it after starting the Chroma service to have some data to query.
"""
import hashlib
import os
import sys
from datetime import datetime
# Use the Chroma client directly
import chromadb
import numpy as np
import requests
from chromadb.config import Settings
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
# Mock embedding function to create sample embeddings
def mock_embedding(text):
"""Create deterministic mock embeddings"""
# Generate a deterministic hash of the text
text_hash = hashlib.md5(text.encode()).hexdigest()
# Use the hash to seed a random generator for deterministic embeddings
seed = int(text_hash, 16) % (2**32 - 1)
rng = np.random.RandomState(seed)
# Generate a random vector of length 1536 (same as text-embedding-ada-002)
embedding = rng.rand(1536).astype(np.float32)
# Normalize to unit length for cosine similarity
norm = np.linalg.norm(embedding)
if norm > 0:
embedding = embedding / norm
return embedding.tolist()
# Sample content for Ignition tags
SAMPLE_TAG_DATA = [
{
"name": "Tank1Level",
"type": "tag",
"content": """
{
"name": "Tank1Level",
"tagType": "AtomicTag",
"dataType": "Float8",
"value": 75.5,
"alarms": [
{"name": "High", "setpoint": 90.0, "mode": "GreaterThan"},
{"name": "Low", "setpoint": 10.0, "mode": "LessThan"}
],
"engineering_units": "gallons",
"description": "Level sensor for main storage tank 1"
}
""",
"metadata": {
"filepath": "tags/Tanks/Tank1Level.json",
"type": "tag",
"folder": "Tanks",
"created": "2023-01-15",
"modified": "2023-02-20",
},
},
{
"name": "Pump1Status",
"type": "tag",
"content": """
{
"name": "Pump1Status",
"tagType": "AtomicTag",
"dataType": "Boolean",
"value": true,
"alarms": [
{"name": "Offline", "mode": "StateChange"}
],
"description": "Status indicator for main pump 1"
}
""",
"metadata": {
"filepath": "tags/Pumps/Pump1Status.json",
"type": "tag",
"folder": "Pumps",
"created": "2023-01-10",
"modified": "2023-03-05",
},
},
]
# Sample content for Ignition perspective views
SAMPLE_PERSPECTIVE_DATA = [
{
"name": "TankView",
"type": "perspective",
"content": """
{
"name": "TankView",
"type": "view",
"root": {
"type": "flex-container",
"children": [
{
"type": "tank-level-indicator",
"props": {
"tankTag": "Tank1Level",
"height": 300,
"width": 150,
"showLabels": true
}
},
{
"type": "numeric-display",
"props": {
"value": "{Tank1Level}",
"units": "gallons",
"decimalPlaces": 1
}
}
]
}
}
""",
"metadata": {
"filepath": "perspective/views/TankView.json",
"type": "perspective",
"component": "view",
"created": "2023-02-01",
"modified": "2023-03-10",
},
},
{
"name": "PumpControl",
"type": "perspective",
"content": """
{
"name": "PumpControl",
"type": "view",
"root": {
"type": "flex-container",
"children": [
{
"type": "button",
"props": {
"text": "Start Pump",
"action": {
"actionType": "script",
"script": "system.tag.write('Pump1Status', true)"
}
}
},
{
"type": "button",
"props": {
"text": "Stop Pump",
"action": {
"actionType": "script",
"script": "system.tag.write('Pump1Status', false)"
}
}
},
{
"type": "status-indicator",
"props": {
"status": "{Pump1Status}",
"onLabel": "Running",
"offLabel": "Stopped"
}
}
]
}
}
""",
"metadata": {
"filepath": "perspective/views/PumpControl.json",
"type": "perspective",
"component": "view",
"created": "2023-02-05",
"modified": "2023-03-15",
},
},
]
def check_chroma_status(host, port):
"""Check if Chroma is up and retrieve server configuration info."""
try:
# Check heartbeat
heartbeat_url = f"http://{host}:{port}/api/v1/heartbeat"
heartbeat_response = requests.get(heartbeat_url)
heartbeat_data = heartbeat_response.json()
print(f"Chroma heartbeat: {heartbeat_data}")
# List available tenants (this endpoint might not exist in all versions)
try:
tenants_url = f"http://{host}:{port}/api/v1/tenants"
tenants_response = requests.get(tenants_url)
if tenants_response.status_code == 200:
tenants_data = tenants_response.json()
print(f"Available tenants: {tenants_data}")
else:
print(f"Could not retrieve tenants: {tenants_response.status_code}")
except Exception as e:
print(f"Error checking tenants: {e}")
# Try to create default tenant for backward compatibility
try:
create_tenant_url = f"http://{host}:{port}/api/v1/tenants"
create_tenant_data = {"name": "default_tenant"}
create_response = requests.post(create_tenant_url, json=create_tenant_data)
print(f"Create tenant response: {create_response.status_code} - {create_response.text}")
except Exception as e:
print(f"Error creating tenant: {e}")
return True
except Exception as e:
print(f"Error checking Chroma status: {e}")
return False
def main():
"""Main function to populate the database"""
print("Starting sample data indexing...")
# Connect to Chroma
try:
chroma_host = os.getenv("CHROMA_HOST", "localhost")
chroma_port = int(os.getenv("CHROMA_PORT", "8000"))
print(f"Connecting to Chroma at {chroma_host}:{chroma_port}...")
# Check basic connectivity
try:
heartbeat_url = f"http://{chroma_host}:{chroma_port}/api/v1/heartbeat"
heartbeat_response = requests.get(heartbeat_url)
print(f"Chroma is responding with heartbeat: {heartbeat_response.json()}")
except Exception as e:
print(f"Couldn't connect to Chroma at {chroma_host}:{chroma_port}: {e}")
return 1
print("Attempting to connect to the Chroma client...")
# Attempt to create tenant first
try:
create_tenant_url = f"http://{chroma_host}:{chroma_port}/api/v1/tenants"
create_tenant_data = {"name": "default_tenant"}
requests.post(create_tenant_url, json=create_tenant_data)
print("Created or verified default_tenant")
except Exception as e:
print(f"Note: Couldn't create tenant (may already exist): {e}")
# Connect to latest Chroma version with tenant
client = chromadb.HttpClient(
host=chroma_host,
port=chroma_port,
tenant="default_tenant",
settings=Settings(
anonymized_telemetry=False,
allow_reset=True,
),
)
# Test if connection works
heartbeat = client.heartbeat()
print(f"Connected to Chroma. Heartbeat: {heartbeat}")
print("Connected to Chroma successfully.")
# Create or get collection
collection_name = os.getenv("COLLECTION_NAME", "ignition_project")
try:
print(f"Getting collection '{collection_name}'...")
collection = client.get_collection(collection_name)
print(f"Found existing collection with {collection.count()} documents.")
except Exception as e:
print(f"Creating new collection '{collection_name}'...")
print(f"Error was: {e}")
collection = client.create_collection(collection_name)
# Combine sample data
sample_data = SAMPLE_TAG_DATA + SAMPLE_PERSPECTIVE_DATA
# Add sample data to collection
docs = []
ids = []
embeddings = []
metadatas = []
for item in sample_data:
doc_id = f"{item['type']}_{item['name']}_{datetime.now().timestamp()}"
doc_content = item["content"].strip()
doc_embedding = mock_embedding(doc_content)
docs.append(doc_content)
ids.append(doc_id)
embeddings.append(doc_embedding)
metadatas.append(item["metadata"])
# Add to collection
print(f"Adding {len(docs)} documents to collection...")
collection.add(documents=docs, embeddings=embeddings, metadatas=metadatas, ids=ids)
# Verify documents were added
count = collection.count()
print(f"Collection now has {count} documents.")
print("Sample data indexed successfully!")
return 0
except Exception as e:
print(f"Error indexing sample data: {e}")
return 1
if __name__ == "__main__":
sys.exit(main())