-
Notifications
You must be signed in to change notification settings - Fork 3
Pgvector port #182
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
Open
tozzen0121
wants to merge
10
commits into
master
Choose a base branch
from
pgvector-port
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Pgvector port #182
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
592922a
Converted smart search from pinecone to postgre
tozzen0121 848d68c
pgvector
tozzen0121 7d903aa
upgrade minor change for the coding style.
tozzen0121 9ff1d54
fix spelling issue and python library version
tozzen0121 0dde005
update readme about installing pgvector extension
tozzen0121 88f10e1
minor change for the readme
tozzen0121 34d380b
minor change again
tozzen0121 850ace7
changed readme finally
tozzen0121 799e160
abstract layer for the vector database
tozzen0121 7f2e58f
remove dead code
tozzen0121 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
9 changes: 0 additions & 9 deletions
9
src/semantic_search/semantic_search/external_services/pinecone.py
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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
Empty file.
42 changes: 42 additions & 0 deletions
42
src/semantic_search/semantic_search/external_services/vector_databases/pinecone.py
This file contains hidden or 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,42 @@ | ||
| import pinecone | ||
| from .vector_database import VectorDatabase | ||
| from ...config import get_pinecone_key, get_pinecone_environment, get_pinecone_index_name | ||
|
|
||
| class Pinecone(VectorDatabase): | ||
| def __init__(self): | ||
| pinecone.init(api_key=get_pinecone_key(), environment=get_pinecone_environment()) | ||
|
|
||
| # overriding abstract method | ||
| def insert(self, embeddings, chunk, namespace): | ||
| items = [] | ||
|
|
||
| for i in range(len(chunk)): | ||
| items.append({ | ||
| 'id': chunk[i].id, | ||
| 'values': embeddings[i], | ||
| 'metadata': chunk[i].to_metadata() | ||
| }) | ||
|
|
||
| self.get_pinecone_index().upsert( | ||
| vectors=items, | ||
| namespace=namespace | ||
| ) | ||
|
|
||
| # overriding abstract method | ||
| def delete(self, ids, namespace): | ||
| self.get_pinecone_index().delete(ids=ids, namespace=namespace) | ||
|
|
||
| # overriding abstract method | ||
| def select(self, query_vector, namespace): | ||
| query_results = self.get_pinecone_index().query( | ||
| queries=[query_vector], | ||
| top_k=50, | ||
| namespace=namespace, | ||
| include_values=False, | ||
| includeMetadata=True | ||
| ) | ||
| return query_results['results'][0]['matches'] | ||
|
|
||
| def get_pinecone_index() -> 'pinecone.Index': | ||
| return pinecone.Index(get_pinecone_index_name()) | ||
|
|
50 changes: 50 additions & 0 deletions
50
src/semantic_search/semantic_search/external_services/vector_databases/postgres.py
This file contains hidden or 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,50 @@ | ||
| from pgvector.psycopg2 import register_vector | ||
| import psycopg2 | ||
| from .vector_database import VectorDatabase | ||
| import json | ||
| import numpy as np | ||
|
|
||
| from ...config import get_postgres_host, get_postgres_port, get_postgres_database, get_postgres_user, get_postgres_password | ||
|
|
||
| class Postgres(VectorDatabase): | ||
| def __init__(self): | ||
| try: | ||
| self.conn = psycopg2.connect( | ||
| host=get_postgres_host(), | ||
| database=get_postgres_database(), | ||
| user=get_postgres_user(), | ||
| password=get_postgres_password(), | ||
| port=get_postgres_port()) | ||
|
|
||
| self.cur = self.conn.cursor() | ||
|
|
||
| self.cur.execute('CREATE EXTENSION IF NOT EXISTS vector') | ||
| register_vector(self.cur) | ||
|
|
||
| self.cur.execute('CREATE TABLE IF NOT EXISTS embedding (id bigserial PRIMARY KEY, namespace text, chunk_id text, metadata text, values vector)') | ||
| self.conn.commit() | ||
|
|
||
| except (Exception, psycopg2.DatabaseError) as error: | ||
| print(error) | ||
|
|
||
| # overriding abstract method | ||
| def insert(self, embeddings, chunk, namespace): | ||
| for i in range(len(chunk)): | ||
| metadata = json.dumps(chunk[i].to_metadata()) | ||
| self.cur.execute('INSERT INTO embedding (namespace, chunk_id, metadata, values) VALUES (%s, %s, %s, %s)', (namespace, chunk[i].id, metadata, embeddings[i])) | ||
|
|
||
| self.conn.commit() | ||
|
|
||
| # overriding abstract method | ||
| def delete(self, ids, namespace): | ||
| self.cur.execute('DELETE FROM embedding WHERE chunk_id IN (%s) AND namespace=%s',(ids, namespace)) | ||
| self.conn.commit() | ||
|
|
||
| # overriding abstract method | ||
| def select(self, query_vector): | ||
| self.cur.execute('SELECT * FROM embedding ORDER BY values <-> %s LIMIT 50', (np.array(query_vector),)) | ||
| embeddings = self.cur.fetchall() | ||
| output = [dict(id=chunk_id, metadata=json.loads(metadata)) for id, namespace, chunk_id, metadata, values in embeddings] | ||
| return output | ||
|
|
||
|
|
14 changes: 14 additions & 0 deletions
14
src/semantic_search/semantic_search/external_services/vector_databases/vector_database.py
This file contains hidden or 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,14 @@ | ||
| from abc import ABC, abstractmethod | ||
|
|
||
| class VectorDatabase(ABC): | ||
| @abstractmethod | ||
| def insert(self, embeddings, chunk, namespace): | ||
| pass | ||
|
|
||
| @abstractmethod | ||
| def delete(self, ids, namespace): | ||
| pass | ||
|
|
||
| @abstractmethod | ||
| def select(self, query_vector): | ||
| pass |
17 changes: 17 additions & 0 deletions
17
src/semantic_search/semantic_search/external_services/vector_databases/vector_instance.py
This file contains hidden or 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,17 @@ | ||
| from ...config import get_postgres_host, get_pinecone_environment | ||
| from .postgres import Postgres | ||
| from .pinecone import Pinecone | ||
|
|
||
| postgres_instance = None | ||
| pinecone_instance = None | ||
|
|
||
| def get_db_instance(): | ||
| global postgres_instance, pinecone_instance | ||
| if get_postgres_host(): | ||
| if postgres_instance is None: | ||
| postgres_instance = Postgres() | ||
| return postgres_instance | ||
| else: | ||
| if pinecone_instance is None: | ||
| pinecone_instance = Pinecone() | ||
| return pinecone_instance |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.