Python, фильтровать векторы из хранилища векторов Pinecone на основе поля, сохраненного в метаданных этих векторов.Python

Программы на Python
Ответить Пред. темаСлед. тема
Anonymous
 Python, фильтровать векторы из хранилища векторов Pinecone на основе поля, сохраненного в метаданных этих векторов.

Сообщение Anonymous »

У меня есть векторы, хранящиеся в векторном хранилище Pinecone, каждый вектор представляет содержание файла PDF: < /p>

metadata ::
hash_code: "D53D7EC8B0E66E9A83A97ACDA09EDD3FE9867CADB42833F9BF5525CC3B89FE2D"
ID: "CC54FFBE-9CBA-4DE9-9F30-A114E4C3C3FB" поле в метаданных, которое HASH_CODE содержания PDF, чтобы не добавлять тот же файл снова и снова в векторный хранилище. хочу добавить, тогда я хочу сканировать существующие, чтобы найти, если кто -либо из них уже существует, а затем отфильтровать. Еще не удалось достичь моей цели: < /p>
Первый метод: < /p>
def filter_existing_docs(index_name, docs):
# Initialize the Pinecone index
index = pinecone_client.Index(index_name)

# Extract hash_codes from the docs list using the appropriate method for your Document objects
hash_codes = [doc.metadata['hash_code'] for doc in docs] # Accessing 'metadata' if it's an attribute
print("Hash Codes:", hash_codes)

# Fetch by list of hash_codes (ensure hash_codes are valid ids)
fetch_response = index.fetch(ids=hash_codes)
print("Fetch Response:", fetch_response)

# Get the existing hash_codes that are already in the Pinecone index
existing_hash_codes = set(fetch_response.get('vectors', {}).keys()) # Extract existing IDs from the response
print("1 -----------> Existing Hash Codes:", len(existing_hash_codes))

# Filter out the docs that have already been added to Pinecone
filtered_docs = [doc for doc in docs if doc.metadata['hash_code'] not in existing_hash_codes]
print("2 -----------> Filtered Docs:", len(filtered_docs))

return filtered_docs

Затем попробовал другой подход:
def filter_existing_docs(index_name, docs):
# Initialize the Pinecone index
index = pinecone_client.Index(index_name)

# Extract hash_codes from the docs list using the appropriate method for your Document objects
hash_codes = [doc.metadata['hash_code'] for doc in docs] # Accessing 'metadata' if it's an attribute
print("Hash Codes:", hash_codes)

# We need to query Pinecone using `top_k` and search through the index
query_response = index.query(
top_k=100, # Set a suitable `top_k` to return a reasonable number of documents
include_metadata=True,
#namespace=namespace
)

# Debug: Print the query response to see its structure
print("Query Response:", query_response)

# Extract the hash_codes of the existing documents in Pinecone
existing_hash_codes = {item['metadata']['hash_code'] for item in query_response['matches']}
print("1 -----------> Existing Hash Codes:", len(existing_hash_codes))

# Filter out the docs that have already been added to Pinecone based on hash_code
filtered_docs = [doc for doc in docs if str(doc.metadata['hash_code']) not in existing_hash_codes]
print("2 -----------> Filtered Docs:", len(filtered_docs))

return filtered_docs


Подробнее здесь: https://stackoverflow.com/questions/793 ... ved-in-the
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «Python»