-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* ignore chroma db on project * add pinecone dependency * base pinecone client * remove namespace as it is unnecesary * add pinecone variables * add vscode debugging * final fixes on pinecone client * add documentation
- Loading branch information
1 parent
52176e1
commit 48b2ca9
Showing
11 changed files
with
383 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -189,3 +189,5 @@ terraform.rc | |
|
||
.history | ||
|
||
# Ignore ChromaDB | ||
chroma.sqlite3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
// Use IntelliSense to learn about possible attributes. | ||
// Hover to view descriptions of existing attributes. | ||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"name": "Python: FastAPI", | ||
"console": "integratedTerminal", | ||
"type": "python", | ||
"request": "launch", | ||
"module": "uvicorn", | ||
"envFile": "${workspaceFolder}/apps/semantic_search/.env", | ||
"args": ["semantic_search:app", "--reload"], | ||
"jinja": true, | ||
"justMyCode": true, | ||
"pythonArgs": ["-Xfrozen_modules=off"] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
poetry.lock | ||
.env.example | ||
chroma.sqlite3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import uuid | ||
from typing import Any | ||
|
||
import pinecone | ||
from pinecone import Index, Vector | ||
from pydantic import BaseModel | ||
from stores.base import EmbeddingsStore, SearchResult, StoreRequest | ||
|
||
|
||
class PineconeEmbeddingsStoreSettings(BaseModel): | ||
key: str | ||
environment: str | ||
index: str | ||
|
||
|
||
class PineconeMatch(BaseModel): | ||
id: str | ||
score: float | ||
values: list[float] | ||
metadata: dict[str, Any] | ||
|
||
|
||
class PineconeEmbeddingsStore(EmbeddingsStore): | ||
def __init__(self, settings: PineconeEmbeddingsStoreSettings) -> None: | ||
pinecone.init(api_key=settings.key, environment=settings.environment) | ||
|
||
self.index = Index(settings.index) | ||
|
||
def store(self, embeddings: list[StoreRequest]) -> list[str]: | ||
self._validate_configuration() | ||
|
||
pinecone_vectors: list[Vector] = [] | ||
for embedding in embeddings: | ||
pinecone_metadata = embedding.metadata.copy() | ||
pinecone_metadata["cluster_id"] = embedding.cluster_id | ||
vector = Vector( | ||
id=str(uuid.uuid4()), | ||
values=embedding.embedding, | ||
metadata=pinecone_metadata, | ||
) | ||
pinecone_vectors.append(vector) | ||
|
||
result = self.index.upsert(vectors=pinecone_vectors) | ||
|
||
result.get("upsertedData") | ||
if result.get("upsertedData") == 0: | ||
raise Exception("Error inserting: No rows added") | ||
|
||
return [vector["id"] for vector in pinecone_vectors] | ||
|
||
def search( | ||
self, | ||
embedding: list[float], | ||
cluster_ids: list[str], | ||
match_threshold: float = 0.8, | ||
limit: int = 10, | ||
) -> list[SearchResult]: | ||
self._validate_configuration() | ||
|
||
query_response = self.index.query( | ||
top_k=limit, | ||
include_values=True, | ||
include_metadata=True, | ||
vector=embedding, | ||
filter={"cluster_id": {"$in": cluster_ids}}, | ||
) | ||
|
||
matches: list[PineconeMatch] = query_response.get("matches") | ||
search_results: list[SearchResult] = [] | ||
for match in matches: | ||
if match.score < match_threshold: | ||
continue | ||
|
||
cluster_id = match.metadata.pop("cluster_id") | ||
search_result = SearchResult( | ||
id=match.id, | ||
metadata=match.metadata, | ||
score=match.score, | ||
cluster_id=cluster_id, | ||
) | ||
search_results.append(search_result) | ||
return search_results | ||
|
||
def _validate_configuration(self): | ||
if not self.index: | ||
raise ValueError("Pinecone index is required.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters