Skip to main content

LangChain VectorStore integrations for Azure Database for PostgreSQL

Project description

langchain-azure-postgresql

langchain-azure-postgresql is a Python package that implements both asynchronous and synchronous VectorStore support for Azure Database for PostgreSQL. Specifically, this package adds support for

  1. Microsoft Entra ID (formerly Azure AD) authentication when connecting to your Azure Database for PostgreSQL instances, and,
  2. DiskANN indexing algorithm when indexing your (semantic) vectors.

This way, you can leverage your Azure Database for PostgreSQL instances as secure and fast vector stores for your LangChain workflows.

[!NOTE] langchain-azure-postgresql currently supports Python 3.10 and above.

Installation

To install langchain-azure-postgresql, you need to install the necessary Python packages:

$ python3 -m pip install langchain langchain-azure-postgresql langchain-openai
# logs stripped for brevity

Usage

Once the packages are installed, you can use Azure Database for PostgreSQL instances as vector stores:

import os

from langchain_core.documents import Document
from langchain_openai import OpenAIEmbeddings

from langchain_azure_postgresql.common import AzurePGConnectionPool, ConnectionInfo
from langchain_azure_postgresql.langchain import AzurePGVectorStore

documents = [
    Document(
        page_content="Dogs are great companions, known for their loyalty and friendliness.",
        metadata={"source": "mammal-pets-doc"},
    ),
    Document(
        page_content="Cats are independent pets that often enjoy their own space.",
        metadata={"source": "mammal-pets-doc"},
    ),
]

host = os.getenv("PGHOST", "localhost")
connection_pool = AzurePGConnectionPool(azure_conn_info=ConnectionInfo(host=host))

embedding = OpenAIEmbeddings(model="text-embedding-3-small")

vector_store = AzurePGVectorStore(connection_pool=connection_pool, embedding=embedding)

vector_store.add_documents(documents)

The code snippet above will try to connect to your PostgreSQL host, as defined by the environment variable PGHOST, and fall back to connecting to localhost if the environment variable is not defined. By default, ConnectionInfo objects try to use Microsoft Entra ID to login to the PostgreSQL instances/hosts.

Please see the documentation for more details on configuring various classes provided by langchain-azure-postgresql.

Development

The development environment for this package is managed by uv, an up-and-coming and versatile Python package and project manager.

To create the development environment, you first need to install uv in your development environment (unless you are using the development container setup as provided by this repository). Once uv is properly installed and set up, you can run the following commands to synchronize and activate the development environment:

$ uv sync --all-extras # synchronize the development environment from uv.lock
# logs stripped for brevity
$ source .venv/bin/activate # or, as appropriate for your shell, e.g., fish

Once the development environment is synchronized and activated, you can start contributing changes to the package. For test automation, the package leverages tox, a test automation and standardization framework in Python.

There are some pre-defined test environments and labels managed by tox:

$ tox list
default environments:
lint    -> Run lint checks on the code base
package -> Run packaging checks on the code base
type    -> Run type checks on the code base
3.10    -> Run tests under Python 3.10
3.11    -> Run tests under Python 3.11
3.12    -> Run tests under Python 3.12
3.13    -> Run tests under Python 3.13

The default environments are for running lint checks, packaging checks, type checks, and end-to-end tests for different Python versions, respectively. You can selectively run an environment via, e.g., tox run -e lint, or, otherwise, run the full suite of tests via tox. There is a special label called test, which will run only the end-to-end tests for all the supported Python versions and can be run via the following:

$ PGHOST=<host>.postgres.database.azure.com PGPASSWORD='your_password' tox run -m test
# logs stripped for brevity

For more information on the supported test flags and environment variables, please check the output of pytest --help.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

langchain_azure_postgresql-1.0.0.tar.gz (36.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

langchain_azure_postgresql-1.0.0-py3-none-any.whl (41.1 kB view details)

Uploaded Python 3

File details

Details for the file langchain_azure_postgresql-1.0.0.tar.gz.

File metadata

File hashes

Hashes for langchain_azure_postgresql-1.0.0.tar.gz
Algorithm Hash digest
SHA256 ae53d1dc548c687e5f84c02cfeb35f09a859cfdd0df0e9d461b349998f3132e9
MD5 e2ed7a9a96195aa4e4bebf578ceb926a
BLAKE2b-256 e224149bee8e3062c43eb4f218efb4c4d74ff2d9231ad78d6eca1e4d0dae4d8e

See more details on using hashes here.

File details

Details for the file langchain_azure_postgresql-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for langchain_azure_postgresql-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 511e1d65d1941f23b3184f4e2fdcb20940d6844ad6121de5457e39757b849b34
MD5 092aa58cb60aac79acb0e3cecf9ac10f
BLAKE2b-256 6fc7a568f00635c2e090f48a30b3ffaa7d09b03a76d9b28b8db3b4097aa32f23

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page