Spaces:
Sleeping
Sleeping
Update pinecone_utils.py
Browse files- pinecone_utils.py +19 -26
pinecone_utils.py
CHANGED
@@ -1,34 +1,27 @@
|
|
1 |
# pinecone_utils.py
|
2 |
|
3 |
-
import
|
4 |
from config import PINECONE_API_KEY, PINECONE_ENVIRONMENT, INDEX_NAME, CONTEXT_FIELDS
|
5 |
-
import torch
|
6 |
|
7 |
# Conectar a Pinecone
|
8 |
def connect_to_pinecone():
|
9 |
-
|
10 |
-
|
11 |
-
return index
|
12 |
-
|
13 |
-
# Realizar búsqueda vectorial
|
14 |
-
def vector_search(query, embedding_model, index):
|
15 |
-
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
16 |
-
# Generar el embedding utilizando el modelo de embeddings
|
17 |
-
xq = embedding_model.encode(query, convert_to_tensor=True, device=device)
|
18 |
|
19 |
-
#
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
-
#
|
23 |
-
|
24 |
-
|
25 |
-
return [
|
26 |
-
{
|
27 |
-
'content': ' '.join(f"{k}: {v}" for k, v in match['metadata'].items() if k in CONTEXT_FIELDS and k != 'Tag'),
|
28 |
-
'metadata': match['metadata'],
|
29 |
-
'score': match.get('score', 0)
|
30 |
-
}
|
31 |
-
for match in res['matches']
|
32 |
-
if 'metadata' in match
|
33 |
-
]
|
34 |
-
return []
|
|
|
1 |
# pinecone_utils.py
|
2 |
|
3 |
+
from pinecone import Pinecone, ServerlessSpec
|
4 |
from config import PINECONE_API_KEY, PINECONE_ENVIRONMENT, INDEX_NAME, CONTEXT_FIELDS
|
|
|
5 |
|
6 |
# Conectar a Pinecone
|
7 |
def connect_to_pinecone():
|
8 |
+
# Crear una instancia de Pinecone
|
9 |
+
pc = Pinecone(api_key=PINECONE_API_KEY)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
+
# Verificar si el índice existe
|
12 |
+
index_names = pc.list_indexes().names()
|
13 |
+
if INDEX_NAME not in index_names:
|
14 |
+
# Si el índice no existe, crearlo
|
15 |
+
pc.create_index(
|
16 |
+
name=INDEX_NAME,
|
17 |
+
dimension=1024, # Asegúrate de que esta dimensión coincida con la de tus embeddings
|
18 |
+
metric='cosine', # Puedes cambiar el métrico según tus necesidades
|
19 |
+
spec=ServerlessSpec(
|
20 |
+
cloud='aws',
|
21 |
+
region=PINECONE_ENVIRONMENT # Asegúrate de que este sea el entorno correcto
|
22 |
+
)
|
23 |
+
)
|
24 |
|
25 |
+
# Conectar al índice
|
26 |
+
index = pc.Index(INDEX_NAME)
|
27 |
+
return index
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|