- Notifications
You must be signed in to change notification settings - Fork352
pgml Python SDK with vector search support#636
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
21 commits Select commitHold shift + click to select a range
89c1223 Python SDK init
santiadavani53dde0f create collection init
santiatpml2d685e8 Upsert documents + tests
santiatpml86ccef8 Creating more tables as part of collection ..
santiatpml5b5cee4 Register models and text splitters
santiatpml7b03e01 Refactored run select and added models
santiatpml2d9202e Embeddings and vector search
santiatpmlea19ecc Incremental updates for chunks and embeddings
santiatpmldee6e5b Docstrings for all modules
santiatpmlb7a0495 Minor updates
santiatpml5186705 Added basic readme with quickstart
santiatpml5c8cf62 Updated readme with PGML_CONNECTION
santiatpml5a81918 Updated readme
santiatpmla5d1618 Minor API and notebook updates
santiatpml368da8a Using document_id, chunk_id etc. for column names
santiatpml986b314 Renaming model -> model_id and splitter -> splitter_id
santiatpml4601edc Performance improvements
santiadavani988ea41 delete collection is replaced with archive collection
santiadavani97ec30b Support for uuids without dashes
santiadavani4793563 Refactored upsert documents
santiadavani998c996 Merge branch 'master' into santi-pgml-memory-sdk-python
santiatpmlFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
38 changes: 38 additions & 0 deletionspgml-sdks/python/pgml/README.md
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,38 @@ | ||
| # PostgresML Python SDK | ||
| This Python SDK provides an easy interface to use PostgresML generative AI capabilities. | ||
| ## Table of Contents | ||
| - [Quickstart](#quickstart) | ||
| ### Quickstart | ||
| 1. Install Python 3.11. SDK should work for Python >=3.8. However, at this time, we have only tested Python 3.11. | ||
| 2. Clone the repository and checkout the SDK branch (before PR) | ||
| ``` | ||
| git clone https://github.com/postgresml/postgresml | ||
| cd postgresml | ||
| git checkout santi-pgml-memory-sdk-python | ||
| cd pgml-sdks/python/pgml | ||
| ``` | ||
| 3. Install poetry `pip install poetry` | ||
| 4. Initialize Python environment | ||
| ``` | ||
| poetry env use python3.11 | ||
| poetry shell | ||
| poetry install | ||
| poetry build | ||
| ``` | ||
| 5. SDK uses your local PostgresML database by default | ||
| `postgres://postgres@127.0.0.1:5433/pgml_development` | ||
| If it is not up to date with `pgml.embed` please [signup for a free database](https://postgresml.org/signup) and set `PGML_CONNECTION` environment variable with serverless hosted database. | ||
| ``` | ||
| export PGML_CONNECTION="postgres://<username>:<password>@<hostname>:<port>/pgm<database>" | ||
| ``` | ||
| 6. Run a **vector search** example | ||
| ``` | ||
| python examples/vector_search.py | ||
| ``` | ||
236 changes: 236 additions & 0 deletionspgml-sdks/python/pgml/examples/vector_search.ipynb
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,236 @@ | ||
| { | ||
| "cells": [ | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "from pgml import Database\n", | ||
| "import os\n", | ||
| "import json" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "local_pgml = \"postgres://postgres@127.0.0.1:5433/pgml_development\"\n", | ||
| "\n", | ||
| "conninfo = os.environ.get(\"PGML_CONNECTION\",local_pgml)\n", | ||
| "db = Database(conninfo,min_connections=4)" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "collection_name = \"test_pgml_sdk_1\"\n", | ||
| "collection = db.create_or_get_collection(collection_name)" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "from datasets import load_dataset\n", | ||
| "\n", | ||
| "data = load_dataset(\"squad\", split=\"train\")\n", | ||
| "data = data.to_pandas()\n", | ||
| "data.head()\n", | ||
| "\n", | ||
| "data = data.drop_duplicates(subset=[\"context\"])\n", | ||
| "print(len(data))\n", | ||
| "data.head()\n", | ||
| "\n", | ||
| "documents = [\n", | ||
| " {\n", | ||
| " 'text': r['context'],\n", | ||
| " 'metadata': {\n", | ||
| " 'title': r['title']\n", | ||
| " }\n", | ||
| " } for r in data.to_dict(orient='records')\n", | ||
| "]\n", | ||
| "documents[:3]" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "collection.upsert_documents(documents[0:200])\n", | ||
| "collection.generate_chunks()\n", | ||
| "collection.generate_embeddings()" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "results = collection.vector_search(\"Who won 20 Grammy awards?\", top_k=2)\n", | ||
| "print(json.dumps(results,indent=2))" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "collection.register_model(model_name=\"paraphrase-MiniLM-L6-v2\")" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "collection.get_models()" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "print(json.dumps(collection.get_models(),indent=2))" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "collection.generate_embeddings(model_id=2)" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "results = collection.vector_search(\"Who won 20 Grammy awards?\", top_k=2, model_id=2)\n", | ||
| "print(json.dumps(results,indent=2))" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "collection.register_model(model_name=\"hkunlp/instructor-xl\", model_params={\"instruction\": \"Represent the Wikipedia document for retrieval: \"})" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "collection.get_models()" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "collection.generate_embeddings(model_id=3)" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "results = collection.vector_search(\"Who won 20 Grammy awards?\", top_k=2, model_id=3, query_parameters={\"instruction\": \"Represent the Wikipedia question for retrieving supporting documents: \"})\n", | ||
| "print(json.dumps(results,indent=2))" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "collection.register_text_splitter(splitter_name=\"RecursiveCharacterTextSplitter\",splitter_params={\"chunk_size\": 100,\"chunk_overlap\": 20})" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "collection.generate_chunks(splitter_id=2)" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "collection.generate_embeddings(splitter_id=2)" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "results = collection.vector_search(\"Who won 20 Grammy awards?\", top_k=2, splitter_id=2)\n", | ||
| "print(json.dumps(results,indent=2))" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "db.delete_collection(collection_name)" | ||
| ] | ||
| } | ||
| ], | ||
| "metadata": { | ||
| "kernelspec": { | ||
| "display_name": "pgml-zoggicR5-py3.11", | ||
| "language": "python", | ||
| "name": "python3" | ||
| }, | ||
| "language_info": { | ||
| "codemirror_mode": { | ||
| "name": "ipython", | ||
| "version": 3 | ||
| }, | ||
| "file_extension": ".py", | ||
| "mimetype": "text/x-python", | ||
| "name": "python", | ||
| "nbconvert_exporter": "python", | ||
| "pygments_lexer": "ipython3", | ||
| "version": "3.11.3" | ||
| }, | ||
| "orig_nbformat": 4 | ||
| }, | ||
| "nbformat": 4, | ||
| "nbformat_minor": 2 | ||
| } |
34 changes: 34 additions & 0 deletionspgml-sdks/python/pgml/examples/vector_search.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,34 @@ | ||
| from pgml import Database | ||
| import os | ||
| import json | ||
| from datasets import load_dataset | ||
| from time import time | ||
| from rich import print as rprint | ||
| local_pgml = "postgres://postgres@127.0.0.1:5433/pgml_development" | ||
| conninfo = os.environ.get("PGML_CONNECTION", local_pgml) | ||
| db = Database(conninfo) | ||
| collection_name = "test_pgml_sdk_1" | ||
| collection = db.create_or_get_collection(collection_name) | ||
| data = load_dataset("squad", split="train") | ||
| data = data.to_pandas() | ||
| data = data.drop_duplicates(subset=["context"]) | ||
| documents = [ | ||
| {'id': r['id'], "text": r["context"], "title": r["title"]} | ||
| for r in data.to_dict(orient="records") | ||
| ] | ||
| collection.upsert_documents(documents[:200]) | ||
| collection.generate_chunks() | ||
| collection.generate_embeddings() | ||
| start = time() | ||
| results = collection.vector_search("Who won 20 grammy awards?", top_k=2) | ||
| rprint(json.dumps(results, indent=2)) | ||
| rprint("Query time %0.3f"%(time()-start)) | ||
| db.archive_collection(collection_name) |
7 changes: 7 additions & 0 deletionspgml-sdks/python/pgml/pgml/__init__.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,7 @@ | ||
| from .database import Database | ||
| from .collection import Collection | ||
| from .dbutils import ( | ||
| run_create_or_insert_statement, | ||
| run_select_statement, | ||
| run_drop_or_delete_statement, | ||
| ) |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
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.