Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Use Pinecone library list functionality #403

Merged
merged 4 commits into from
Aug 29, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 3 additions & 35 deletions semantic_router/index/pinecone.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,39 +363,13 @@
if self.index is None:
raise ValueError("Index is None, could not retrieve vector IDs.")
all_vector_ids = []
next_page_token = None

if prefix:
prefix_str = f"?prefix={prefix}"
else:
prefix_str = ""

# Construct the request URL for listing vectors. Adjust parameters as needed.
list_url = f"https://{self.host}/vectors/list{prefix_str}"
params: Dict = {}
if self.namespace:
params["namespace"] = self.namespace
headers = {"Api-Key": self.api_key}
metadata = []

while True:
if next_page_token:
params["paginationToken"] = next_page_token

# Make the request to list vectors. Adjust headers and parameters as needed.
response = requests.get(list_url, params=params, headers=headers)
response_data = response.json()
for ids in self.index.list(prefix=prefix):
jamescalam marked this conversation as resolved.
Show resolved Hide resolved
all_vector_ids.extend(ids)

Check warning on line 369 in semantic_router/index/pinecone.py

View check run for this annotation

Codecov / codecov/patch

semantic_router/index/pinecone.py#L368-L369

Added lines #L368 - L369 were not covered by tests

# Extract vector IDs from the response and add them to the list
vector_ids = [vec["id"] for vec in response_data.get("vectors", [])]
# check that there are vector IDs, otherwise break the loop
if not vector_ids:
break
all_vector_ids.extend(vector_ids)

# if we need metadata, we fetch it
if include_metadata:
for id in vector_ids:
for id in ids:

Check warning on line 372 in semantic_router/index/pinecone.py

View check run for this annotation

Codecov / codecov/patch

semantic_router/index/pinecone.py#L372

Added line #L372 was not covered by tests
res_meta = (
self.index.fetch(ids=[id], namespace=self.namespace)
if self.index
Expand All @@ -404,12 +378,6 @@
metadata.extend(
[x["metadata"] for x in res_meta["vectors"].values()]
)
# extract metadata only

# Check if there's a next page token; if not, break the loop
next_page_token = response_data.get("pagination", {}).get("next")
if not next_page_token:
break

return all_vector_ids, metadata

Expand Down
Loading