File size: 1,354 Bytes
9fdb4cf
4a3f00d
bcc6e2c
9fdb4cf
 
 
 
4a3f00d
 
539078c
4a3f00d
bcc6e2c
e1a830c
d8d5c48
e1a830c
 
4a3f00d
bcc6e2c
 
 
 
 
 
 
 
e1a830c
bcc6e2c
e1a830c
 
 
9fdb4cf
d8d5c48
9fdb4cf
d8d5c48
e1a830c
 
4a3f00d
 
539078c
4a3f00d
 
e1a830c
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
#!/usr/bin/env python3
import yaml
from pathlib import Path
from langchain_community.embeddings import HuggingFaceEmbeddings
from langchain_community.vectorstores import FAISS

class QueryEngine:
    def __init__(self):
        with open('config.yaml') as f:
            cfg = yaml.safe_load(f)

        # Embeddings
        self.embeddings = HuggingFaceEmbeddings(
            model_name=cfg.get('embedding_model', 'sentence-transformers/all-MiniLM-L6-v2'),
            model_kwargs={'device': 'cpu'}
        )

        # ✅ PATH CORRETO HARDCODED!
        faiss_path = '/home/user/app/faiss_index'

        # Verifica se existe
        if not Path(faiss_path).exists():
            raise FileNotFoundError(f"FAISS index não encontrado em {faiss_path}")

        # Carrega FAISS
        self.vectorstore = FAISS.load_local(
            faiss_path,
            self.embeddings,
            allow_dangerous_deserialization=True
        )

    def search_by_embedding(self, query: str, top_k: int = 10):
        results = self.vectorstore.similarity_search_with_score(query, k=top_k)

        return {
            'query': query,
            'total': len(results),
            'results': [
                {'id': doc.metadata.get('id'), 'ementa': doc.page_content, 'score': float(score)}
                for doc, score in results
            ]
        }