Files
init_dataParser/create_ai_index.py
T
2025-01-18 15:32:25 +03:30

140 lines
3.8 KiB
Python

from time import time
import requests
from general_functions import *
from config_base import *
def create_ai_Index(elasticHelper, index_name, app_key, map_file="") :
elasticHelper.createIndex(index_name, app_key, 'general/', map_file )
def import_data_embeding(elasticHelper, ai_url_vec, index_name_i = "mj_qa_section", index_name_o = "mj_qa_section_ai", field_content="content") :
counter = 0
total = 0
id = ""
body={}
dicIds = {}
try:
try:
list = elasticHelper.es_iterate_all_documents(index_name_i, body)
except Exception as e:
save_error(0, e)
count = 0
for mentry in list:
count += 1
entry = mentry["source"]
id = entry.get("id", "")
content = entry.get(field_content, "")
data = entry
if content :
payload={
'text':content
}
response = requests.request("POST", ai_url_vec, headers=HEADERS, json=payload)
if response.status_code == 500 :
print(response.text)
bulk_data = []
continue
resp = json.loads(response.text)
data["embeddings"]=resp['result']
update = False
eid = id
try:
resp = elasticHelper.update_index_doc(update, index_name_o, eid, data)
if not update :
dicIds[eid] = True
except Exception as e:
save_error(id, e)
except Exception as e:
save_error(id, e)
# print("Exception: " + str(e))
# print( e)
# print("%s -> %.2f " % (id , (counter / total) if total > 0 else 0))
# try:
# print('ts_date:'+ts_date)
# except:
# print('not definded')
print("=" * 10, "setting finished")
def import_classification(elasticHelper, ai_url_class, index_name_o = "mj_qa_section_ai") :
counter = 0
total = 0
id = ""
body={
"query": {
"bool": {
"must_not": [
{
"exists": {
"field": "code-ai"
}
}
]
}
}
}
dicIds = {}
try:
try:
list = elasticHelper.es_iterate_all_documents(index_name_o, body)
except Exception as e:
save_error(0, e)
count = 0
print('*' * 20 , ' count=', elasticHelper.total)
for mentry in list:
count += 1
entry = mentry["source"]
id = entry.get("id", "")
content = entry.get("content", "")
data = {}
if content :
payload={
'text':content
}
response = requests.request("POST", ai_url_class, headers=HEADERS, json=payload)
if response.status_code == 500 :
print(response.text)
continue
resp = json.loads(response.text)
data["code-ai"]=resp['result']
update = True
eid = id
try:
resp = elasticHelper.update_index_doc(update, index_name_o, eid, data)
if not update :
dicIds[eid] = True
except Exception as e:
save_error(id, e)
except Exception as e:
save_error(id, e)
# print("Exception: " + str(e))
# print( e)
# print("%s -> %.2f " % (id , (counter / total) if total > 0 else 0))
# try:
# print('ts_date:'+ts_date)
# except:
# print('not definded')
print("=" * 10, " finished")