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 @@
|
||||
# product_search.py
|
||||
|
||||
import faiss
|
||||
import pickle
|
||||
import difflib
|
||||
import os
|
||||
import oracledb
|
||||
import numpy as np
|
||||
import difflib
|
||||
from rapidfuzz import fuzz
|
||||
from langchain_community.embeddings import OCIGenAIEmbeddings
|
||||
|
||||
@@ -11,19 +9,26 @@ from langchain_community.embeddings import OCIGenAIEmbeddings
|
||||
class BuscaProdutoSimilar:
|
||||
def __init__(
|
||||
self,
|
||||
faiss_index_path="faiss_index.bin",
|
||||
id_map_path="produto_id_map.pkl",
|
||||
top_k=5,
|
||||
distancia_minima=1.0,
|
||||
model_id="cohere.embed-english-light-v3.0",
|
||||
service_endpoint="https://inference.generativeai.us-chicago-1.oci.oraclecloud.com",
|
||||
compartment_id="ocid1.compartment.oc1..aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
|
||||
auth_profile="DEFAULT"
|
||||
compartment_id="ocid1.compartment.oc1..aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
|
||||
auth_profile="DEFAULT",
|
||||
wallet_path="/WALLET_PATH/Wallet_oradb23ai",
|
||||
db_alias="oradb23ai_high",
|
||||
username="USER",
|
||||
password="Password"
|
||||
):
|
||||
print("📦 Carregando índice vetorial...")
|
||||
self.index = faiss.read_index(faiss_index_path)
|
||||
with open(id_map_path, "rb") as f:
|
||||
self.id_map = pickle.load(f)
|
||||
os.environ["TNS_ADMIN"] = wallet_path
|
||||
self.conn = oracledb.connect(
|
||||
user=username,
|
||||
password=password,
|
||||
dsn=db_alias,
|
||||
config_dir=wallet_path,
|
||||
wallet_location=wallet_path,
|
||||
wallet_password=password
|
||||
)
|
||||
self.top_k = top_k
|
||||
self.distancia_minima = distancia_minima
|
||||
self.embedding = OCIGenAIEmbeddings(
|
||||
@@ -33,8 +38,27 @@ class BuscaProdutoSimilar:
|
||||
auth_profile=auth_profile
|
||||
)
|
||||
|
||||
print("📦 Carregando vetores do Oracle...")
|
||||
self._carregar_embeddings()
|
||||
|
||||
def _carregar_embeddings(self):
|
||||
cursor = self.conn.cursor()
|
||||
cursor.execute("SELECT id, codigo, descricao, vetor FROM embeddings_produtos")
|
||||
self.vetores = []
|
||||
self.produtos = []
|
||||
for row in cursor.fetchall():
|
||||
id_, codigo, descricao, blob = row
|
||||
vetor = np.frombuffer(blob.read(), dtype=np.float32)
|
||||
self.vetores.append(vetor)
|
||||
self.produtos.append({
|
||||
"id": id_,
|
||||
"codigo": codigo,
|
||||
"descricao": descricao
|
||||
})
|
||||
self.vetores = np.array(self.vetores)
|
||||
|
||||
def _corrigir_input(self, input_usuario):
|
||||
descricoes = [p["descricao"] for p in self.id_map]
|
||||
descricoes = [p["descricao"] for p in self.produtos]
|
||||
sugestoes = difflib.get_close_matches(input_usuario, descricoes, n=1, cutoff=0.6)
|
||||
return sugestoes[0] if sugestoes else input_usuario
|
||||
|
||||
@@ -50,12 +74,16 @@ class BuscaProdutoSimilar:
|
||||
}
|
||||
|
||||
consulta_emb = self.embedding.embed_query(descricao_corrigida)
|
||||
consulta_emb = np.array([consulta_emb])
|
||||
distances, indices = self.index.search(consulta_emb, self.top_k)
|
||||
consulta_emb = np.array(consulta_emb)
|
||||
|
||||
for i, dist in zip(indices[0], distances[0]):
|
||||
# Cálculo de distância euclidiana
|
||||
dists = np.linalg.norm(self.vetores - consulta_emb, axis=1)
|
||||
top_indices = np.argsort(dists)[:self.top_k]
|
||||
|
||||
for idx in top_indices:
|
||||
dist = dists[idx]
|
||||
if dist < self.distancia_minima:
|
||||
match = self.id_map[i]
|
||||
match = self.produtos[idx]
|
||||
similaridade = 1 / (1 + dist)
|
||||
resultados["semanticos"].append({
|
||||
"id": match["id"],
|
||||
@@ -67,7 +95,7 @@ class BuscaProdutoSimilar:
|
||||
|
||||
if not resultados["semanticos"]:
|
||||
melhores_fuzz = []
|
||||
for produto in self.id_map:
|
||||
for produto in self.produtos:
|
||||
score = fuzz.token_sort_ratio(descricao_corrigida, produto["descricao"])
|
||||
melhores_fuzz.append((produto, score))
|
||||
melhores_fuzz.sort(key=lambda x: x[1], reverse=True)
|
||||
|
||||
Reference in New Issue
Block a user