As AI-driven applications continue to evolve, the need for efficient vector-based search capabilities is greater than ever. Microsoft Semantic Kernel makes it easy to integrate these capabilities with PostgreSQL databases using the Postgres
connector. Whether you’re leveraging cloud-hosted PostgreSQL instances on Amazon Web Services or Google Cloud, this connector enables seamless interaction, allowing you to store and query vectorized data for tasks like recommendation systems, semantic search, and more.
Compatible Databases
Semantic Kernel Postgres Connector is compatible with PostgreSQL instances hosted locally or in cloud including but not limited to:
Setup Instructions
Enable pgvector extension
Once your database is configured on AWS or GCP, enable vector similarity search by installing the pgvector
extension:
CREATE EXTENSION IF NOT EXISTS vector;
Install the Semantic Kernel PostgreSQL Connector
.NET
dotnet add package Microsoft.SemanticKernel.Connectors.Postgres --prerelease
Python
pip install semantic-kernel[postgres]
Define Your Data Model
.NET
using Microsoft.Extensions.VectorData;
public class Hotel
{
[VectorStoreRecordKey]
public ulong HotelId { get; set; }
[VectorStoreRecordData(IsFilterable = true)]
public string HotelName { get; set; }
[VectorStoreRecordData(IsFullTextSearchable = true)]
public string Description { get; set; }
[VectorStoreRecordVector(4, DistanceFunction.CosineDistance, IndexKind.Hnsw)]
public ReadOnlyMemory? DescriptionEmbedding { get; set; }
[VectorStoreRecordData(IsFilterable = true)]
public string[] Tags { get; set; }
}
Python
from uuid import uuid4
from dataclasses import dataclass, field
from typing import Annotated
from semantic_kernel.data import (
DistanceFunction, IndexKind, VectorStoreRecordDataField,
VectorStoreRecordKeyField, VectorStoreRecordVectorField, vectorstoremodel
)
@vectorstoremodel
@dataclass
class Hotel:
hotel_id: Annotated[str, VectorStoreRecordKeyField()] = field(default_factory=lambda: str(uuid4()))
hotel_name: Annotated[str, VectorStoreRecordDataField(is_filterable=True)]
description: Annotated[str, VectorStoreRecordDataField(is_full_text_searchable=True)]
description_embedding: Annotated[list[float], VectorStoreRecordVectorField(dimensions=4, distance_function=DistanceFunction.COSINE, index_kind=IndexKind.HNSW)]
tags: Annotated[list[str], VectorStoreRecordDataField(is_filterable=True)]
Configure the Vector Store
.NET
using Microsoft.SemanticKernel.Connectors.Postgres;
using Npgsql;
NpgsqlDataSourceBuilder dataSourceBuilder = new("Host=<endpoint_from_cloud_provider>;Port=5432;Username=<username>;Password=<password>;Database=<database_name>;");
dataSourceBuilder.UseVector();
var dataSource = dataSourceBuilder.Build();
var vectorStore = new PostgresVectorStore(dataSource);
Python
from semantic_kernel.connectors.memory.postgres import PostgresSettings, PostgresStore
from collections.abc import AsyncGenerator
from pydantic import SecretStr
settings = PostgresSettings(
host="<endpoint_from_cloud_provider>",
port=5432,
user="<username>",
password=SecretStr("<password>"),
dbname="<database_name>")
async def get_vector_store() -> AsyncGenerator[PostgresStore, None]:
async with await settings.create_connection_pool() as pool:
yield PostgresStore(connection_pool=pool)
Perform a Vector Search
.NET
// Placeholder embedding generation method.
async Task<ReadOnlyMemory> GenerateEmbeddingAsync(string textToVectorize)
{
// your logic here
}
IVectorStoreRecordCollection<ulong, Hotel> collection = vectorStore.GetCollection<ulong, Hotel>("hotels");
// Generate a vector for your search text.
ReadOnlyMemory searchVector = await GenerateEmbeddingAsync("I'm looking for a hotel where customer happiness is the priority.");
// Perform the search.
var searchResult = await collection.VectorizedSearchAsync(searchVector, new() { Top = 1 });
// Display results.
await foreach (var record in searchResult.Results)
{
Console.WriteLine("Found hotel description: " + record.Record.Description);
}
Python
from semantic_kernel.data.vector_search import VectorSearchOptions
async def vector_search():
store = await get_vector_store()
collection = store.get_collection("hotels", Hotel)
# Generate a vector for your search text.
vector = await generate_vector("I'm looking for a hotel where customer happiness is the priority.")
search_results = await collection.vectorized_search(
vector=vector, options=VectorSearchOptions(vector_field_name="vector")
)
hotels = [record.record async for record in search_results.results]
print(f"Found hotels: {hotels}")
More information
- What are Semantic Kernel Vector Store connectors?
- Defining your data model
- Vector search using Semantic Kernel Vector Store connectors
- Using the Postgres Vector Store connector
Summary
This example demonstrates how to leverage Microsoft Semantic Kernel with PostgreSQL databases on AWS and GCP to perform efficient vector searches. By setting up the pgvector
extension and integrating the Semantic Kernel connector, you can store and retrieve vectorized data with ease. Whether you’re building recommendation systems, semantic search applications, or AI-powered insights, this approach ensures scalable and effective vector storage and retrieval.
We’re always interested in hearing from you. If you have feedback, questions or want to discuss further, feel free to reach out to us and the community on the discussion boards on GitHub! We would also love your support, if you’ve enjoyed using Semantic Kernel, give us a star on GitHub.
0 comments
Be the first to start the discussion.