-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathingest_scenes.py
330 lines (312 loc) · 9.93 KB
/
ingest_scenes.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
325
326
327
328
329
330
import os, json
from azure.core.credentials import AzureKeyCredential
from azure.search.documents import SearchClient
from azure.search.documents.indexes import SearchIndexClient
from azure.search.documents.indexes.models import SearchIndex
# Set up logging
import logging
logging.basicConfig(level=logging.INFO)
# Generate embeddings
from openai import AzureOpenAI
api_endpoint = "https://aoai-eastus2-eliza.openai.azure.com/"
api_key = ""
client = AzureOpenAI(api_key=api_key,
api_version="2024-06-01",
azure_endpoint=api_endpoint)
def generate_embedding(text):
response = client.embeddings.create(
input=text,
model="text-embedding-3-large" # deployment name
)
return response.data[0].embedding
# 读取guitantou.json文件
# json_file_path = 'actionSummary-P36-1-en.json'
json_file_path = 'actionSummary.json'
with open(json_file_path, 'r', encoding='utf-8') as file:
documents = json.load(file)
# 生成向量并添加到文档
for doc in documents:
# Add vectors to the document
doc['summaryVector'] = generate_embedding(doc['summary'])
doc['actionsVector'] = generate_embedding(doc['actions'])
doc['characterVector'] = generate_embedding(doc['characters'])
doc['keyobjectsVector'] = generate_embedding(doc['key_objects'])
doc['keyactionsVector'] = generate_embedding(doc['key_actions'])
doc['nextactionVector'] = generate_embedding(doc['next_action'])
# Start Ingesting...
# Define your service and index names
service_name = "cobra-video-search-eliz"
admin_key = ""
# Define the name of the index
index_name = "complexscene2_index"
# Create a SearchIndexClient to manage the index
index_client = SearchIndexClient(
endpoint=f"https://{service_name}.search.windows.net",
credential=AzureKeyCredential(admin_key),
)
# Define the schema of the index
index_schema = {
"name": "complexscene2_index",
"fields": [
{
"name": "id",
"type": "Edm.String",
"key": True,
"sortable": True,
"filterable": True,
},
{
"name": "Start_Timestamp",
"type": "Edm.String",
"searchable": True,
"filterable": True,
"sortable": True,
"facetable": True,
},
{
"name": "End_Timestamp",
"type": "Edm.String",
"searchable": True,
"filterable": True,
"sortable": True,
"facetable": True,
},
{
"name": "sentiment",
"type": "Edm.String",
"searchable": True,
"filterable": True,
"sortable": True,
"facetable": True,
},
{
"name": "scene_theme",
"type": "Edm.String",
"searchable": True,
"filterable": True,
"sortable": True,
"facetable": True,
},
{
"name": "characters",
"type": "Edm.String",
"searchable": True,
"filterable": True,
"facetable": False,
},
{
"name": "characterVector",
"type": "Collection(Edm.Single)",
"searchable": True,
"filterable": False,
"facetable": False,
"vectorSearchDimensions": 3072,
"vectorSearchProfile": "vector-profile",
},
{
"name": "summary",
"type": "Edm.String",
"searchable": True,
"filterable": False,
"facetable": False,
},
{
"name": "summaryVector",
"type": "Collection(Edm.Single)",
"searchable": True,
"filterable": False,
"facetable": False,
"vectorSearchDimensions": 3072,
"vectorSearchProfile": "vector-profile",
},
{
"name": "actions",
"type": "Edm.String",
"searchable": True,
"filterable": False,
"facetable": False,
},
{
"name": "actionsVector",
"type": "Collection(Edm.Single)",
"searchable": True,
"filterable": False,
"facetable": False,
"vectorSearchDimensions": 3072,
"vectorSearchProfile": "vector-profile",
},
{
"name": "key_objects",
"type": "Edm.String",
"searchable": True,
"filterable": False,
"facetable": False,
},
{
"name": "keyobjectsVector",
"type": "Collection(Edm.Single)",
"searchable": True,
"filterable": False,
"facetable": False,
"vectorSearchDimensions": 3072,
"vectorSearchProfile": "vector-profile",
},
{
"name": "key_actions",
"type": "Edm.String",
"searchable": True,
"filterable": False,
"facetable": False,
},
{
"name": "keyactionsVector",
"type": "Collection(Edm.Single)",
"searchable": True,
"filterable": False,
"facetable": False,
"vectorSearchDimensions": 3072,
"vectorSearchProfile": "vector-profile",
},
{
"name": "next_action",
"type": "Edm.String",
"searchable": True,
"filterable": False,
"facetable": False,
},
{
"name": "nextactionVector",
"type": "Collection(Edm.Single)",
"searchable": True,
"filterable": False,
"facetable": False,
"vectorSearchDimensions": 3072,
"vectorSearchProfile": "vector-profile",
},
],
"suggesters": [
{
"name": "sg",
"searchMode": "analyzingInfixMatching",
"sourceFields": [
"scene_theme",
"characters",
"summary",
"actions",
"key_objects",
],
}
],
"scoringProfiles": [],
"defaultScoringProfile": None,
"corsOptions": {"allowedOrigins": ["*"], "maxAgeInSeconds": 300},
"analyzers": [],
"charFilters": [],
"tokenFilters": [],
"tokenizers": [],
"semantic": {
"configurations": [
{
"name": "complex_scene_semantic_config",
"prioritizedFields": {
"titleField": {"fieldName": "summary"},
"prioritizedContentFields": [
{"fieldName": "summary"},
{"fieldName": "actions"},
{"fieldName": "characters"},
{"fieldName": "key_objects"},
{"fieldName": "key_actions"},
{"fieldName": "next_action"},
],
},
}
]
},
"vectorSearch": {
"algorithms": [
{
"name": "vector-config-hnsw",
"kind": "hnsw",
"hnswParameters": {
"metric": "cosine",
"m": 4,
"efConstruction": 400,
"efSearch": 500
},
"exhaustiveKnnParameters": None
}
],
"profiles": [
{
"name": "vector-profile",
"algorithm": "vector-config-hnsw",
"vectorizer": "text-embedding-3-large",
}
],
"vectorizers": [
{
"name": "text-embedding-3-large",
"kind": "azureOpenAI",
"azureOpenAIParameters": {
"resourceUri": "https://aoai-eastus2-eliza.openai.azure.com/",
"deploymentId": "text-embedding-3-large",
"apiKey": "",
"modelName": "text-embedding-3-large",
"authIdentity": None
},
"customWebApiParameters": None,
"aiServicesVisionParameters": None,
"amlParameters": None
}
],
}
}
# Create or update the index
try:
index = SearchIndex.deserialize(index_schema)
index_client.create_or_update_index(index)
print("Index created or updated successfully.")
except Exception as e:
print(f"Failed to create or update index: {e}")
# Create a SearchClient to upload documents
search_client = SearchClient(
endpoint=f"https://{service_name}.search.windows.net",
index_name=index_name,
credential=AzureKeyCredential(admin_key),
)
# Add IDs to documents
for i, doc in enumerate(documents):
doc["id"] = str(i)
# 构建索引操作
upload_documents = [
{
"@search.action": "upload", # 或者 "mergeOrUpload" 或 "delete"
"id": doc["id"],
"Start_Timestamp": doc.get("Start_Timestamp", ""),
"End_Timestamp": doc.get("End_Timestamp", ""),
"sentiment": doc.get("sentiment", ""),
"scene_theme": doc.get("scene_theme", ""),
"characters": doc.get("characters", ""),
"summary": doc.get("summary", ""),
"actions": doc.get("actions", ""),
"key_objects": doc.get("key_objects", ""),
"key_actions": doc.get("key_actions", ""),
"next_action": doc.get("next_action", ""),
"summaryVector": doc.get("summaryVector", []),
"actionsVector": doc.get("actionsVector", []),
"characterVector": doc.get("characterVector", []),
"keyobjectsVector": doc.get("keyobjectsVector", []),
"keyactionsVector": doc.get("keyactionsVector", []),
"nextactionVector": doc.get("nextactionVector", []),
}
for doc in documents
]
# Upload documents to the index
try:
result = search_client.upload_documents(documents=upload_documents)
if result[0].succeeded:
print(f"Documents uploaded successfully: {result}")
else:
logging.error(f"Failed to upload documents: {result[0].error_message}")
except Exception as e:
print(f"Failed to upload documents: {e}")