mirror of
https://github.com/hoshikawa2/agent-ai-mcp-server.git
synced 2026-07-10 09:14:21 +00:00
adjustments
This commit is contained in:
@@ -1,9 +1,7 @@
|
||||
import oracledb
|
||||
import os
|
||||
from sentence_transformers import SentenceTransformer
|
||||
import faiss
|
||||
import numpy as np
|
||||
import pickle
|
||||
|
||||
# === CONFIGURAÇÃO ORACLE COM WALLET ===
|
||||
WALLET_PATH = "/WALLET_PATH/Wallet_oradb23ai"
|
||||
@@ -14,7 +12,14 @@ PASSWORD = "Password"
|
||||
os.environ["TNS_ADMIN"] = WALLET_PATH
|
||||
|
||||
# === CONECTANDO USANDO oracledb (modo thin) ===
|
||||
connection = oracledb.connect(user=USERNAME, password=PASSWORD, dsn=DB_ALIAS, config_dir=WALLET_PATH, wallet_location=WALLET_PATH, wallet_password=PASSWORD)
|
||||
connection = oracledb.connect(
|
||||
user=USERNAME,
|
||||
password=PASSWORD,
|
||||
dsn=DB_ALIAS,
|
||||
config_dir=WALLET_PATH,
|
||||
wallet_location=WALLET_PATH,
|
||||
wallet_password=PASSWORD
|
||||
)
|
||||
|
||||
cursor = connection.cursor()
|
||||
|
||||
@@ -26,22 +31,52 @@ ids = []
|
||||
descricoes = []
|
||||
|
||||
for row in rows:
|
||||
ids.append({"id": row[0], "codigo": row[1], "descricao": row[2]})
|
||||
descricoes.append(row[2]) # Usado no embedding
|
||||
ids.append((row[0], row[1], row[2]))
|
||||
descricoes.append(row[2])
|
||||
|
||||
# === GERAÇÃO DE EMBEDDINGS COM SENTENCE TRANSFORMERS ===
|
||||
# === GERAÇÃO DOS EMBEDDINGS ===
|
||||
model = SentenceTransformer('all-MiniLM-L6-v2')
|
||||
embeddings = model.encode(descricoes, convert_to_numpy=True)
|
||||
|
||||
# === CRIAÇÃO DO ÍNDICE FAISS ===
|
||||
dim = embeddings.shape[1]
|
||||
index = faiss.IndexFlatL2(dim)
|
||||
index.add(embeddings)
|
||||
# === CRIAÇÃO DA TABELA DE EMBEDDINGS (caso não exista) ===
|
||||
cursor.execute("""
|
||||
BEGIN
|
||||
EXECUTE IMMEDIATE '
|
||||
CREATE TABLE embeddings_produtos (
|
||||
id NUMBER PRIMARY KEY,
|
||||
codigo VARCHAR2(100),
|
||||
descricao VARCHAR2(4000),
|
||||
vetor BLOB
|
||||
)';
|
||||
EXCEPTION
|
||||
WHEN OTHERS THEN
|
||||
IF SQLCODE != -955 THEN
|
||||
RAISE;
|
||||
END IF;
|
||||
END;
|
||||
""")
|
||||
|
||||
# === SALVANDO O ÍNDICE E O MAPA DE PRODUTOS ===
|
||||
faiss.write_index(index, "faiss_index.bin")
|
||||
# === INSERÇÃO OU ATUALIZAÇÃO DOS DADOS ===
|
||||
for (id_, codigo, descricao), vetor in zip(ids, embeddings):
|
||||
vetor_bytes = vetor.astype(np.float32).tobytes()
|
||||
cursor.execute("""
|
||||
MERGE INTO embeddings_produtos tgt
|
||||
USING (SELECT :id AS id FROM dual) src
|
||||
ON (tgt.id = src.id)
|
||||
WHEN MATCHED THEN
|
||||
UPDATE SET codigo = :codigo, descricao = :descricao, vetor = :vetor
|
||||
WHEN NOT MATCHED THEN
|
||||
INSERT (id, codigo, descricao, vetor)
|
||||
VALUES (:id, :codigo, :descricao, :vetor)
|
||||
""", {
|
||||
"id": id_,
|
||||
"codigo": codigo,
|
||||
"descricao": descricao,
|
||||
"vetor": vetor_bytes
|
||||
})
|
||||
|
||||
with open("produto_id_map.pkl", "wb") as f:
|
||||
pickle.dump(ids, f)
|
||||
connection.commit()
|
||||
cursor.close()
|
||||
connection.close()
|
||||
|
||||
print("✅ Vetores gerados e salvos com sucesso.")
|
||||
print("✅ Vetores gravados com sucesso no banco Oracle.")
|
||||
Reference in New Issue
Block a user