Skip to main content
Neon Postgres Docs
current

Search documentation

Type to search this documentation.

On this pageOverview

Semantic Kernel

Semantic Kernel is an open source SDK developed by Microsoft that enables the integration of large language models (LLMs) with traditional programming constructs. It allows developers to build AI powe...

Semantic Kernel is an open-source SDK developed by Microsoft that enables the integration of large language models (LLMs) with traditional programming constructs. It allows developers to build AI-powered applications by combining natural language processing, planning, and memory capabilities. Semantic Kernel supports orchestration of AI workflows, plugin-based extensibility, and vector-based memory storage for retrieval-augmented generation (RAG) use cases. It is commonly used to create intelligent agents, chatbots, and automation tools that leverage LLMs like OpenAI’s GPT models.

Semantic Kernel supports using Neon as a vector store, using the pgvector extension and existing Postgres Vector Store connector to access and manage data in Neon. It establishes a Neon connection, enables vector support, and initializes a vector store for AI-driven search and retrieval tasks

Here's how you can initialize Postgres Vector Store with Semantic Kernel in .NET using Microsoft.SemanticKernel.Connectors.Postgres NuGet package:

C#
// File: Program.cs

using Microsoft.SemanticKernel.Connectors.Postgres;
using Npgsql;

class Program
{
    static void Main()
    {
        var connectionString = "Host=myhost;Username=myuser;Password=mypass;Database=mydb";
        var dataSourceBuilder = new NpgsqlDataSourceBuilder(connectionString);
        dataSourceBuilder.UseVector();
        using var dataSource = dataSourceBuilder.Build();

        var vectorStore = new PostgresVectorStore(dataSource);
        Console.WriteLine("Vector store created successfully.");
    }
}

You can generate text embeddings using Azure OpenAI in the same .NET application.

C#
// File: Program.cs

using Microsoft.SemanticKernel.Connectors.Postgres;
using Microsoft.SemanticKernel.Connectors.AzureOpenAI;
using Npgsql;
using System;
using System.Threading.Tasks;

class Program
{
    static async Task Main()
    {
        string connectionString = "Host=myhost;Username=myuser;Password=mypass;Database=mydb";

        // Create and configure the vector store
        var dataSourceBuilder = new NpgsqlDataSourceBuilder(connectionString);
        dataSourceBuilder.UseVector();
        using var dataSource = dataSourceBuilder.Build();
        var vectorStore = new PostgresVectorStore(dataSource);
        Console.WriteLine("Vector store created successfully.");

        // Generate embeddings using Azure OpenAI
        var embeddingService = new AzureOpenAITextEmbeddingGenerationService(
            deploymentName: "your-deployment-name",
            endpoint: "https://api.openai.com",
            apiKey: "your-api-key"
        );

        string text = "This is an example sentence for embedding.";
        var embedding = await embeddingService.GenerateEmbeddingsAsync(new[] { text });

        Console.WriteLine($"Generated Embedding: [{string.Join(", ", embedding[0].AsReadOnlySpan().Slice(0, 5))}...]");
    }
}

Here is how you can run a chat completion query with Azure OpenAI and Semantic Kernel

C#
// File: Program.cs

using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Connectors.Postgres;
using Microsoft.SemanticKernel.Connectors.AzureOpenAI;
using Npgsql;
using System;
using System.Threading.Tasks;

class Program
{
    static async Task Main()
    {
        string connectionString = "Host=myhost;Username=myuser;Password=mypass;Database=mydb";

        // Step 1: Create and configure the vector store
        var dataSourceBuilder = new NpgsqlDataSourceBuilder(connectionString);
        dataSourceBuilder.UseVector();
        using var dataSource = dataSourceBuilder.Build();
        var vectorStore = new PostgresVectorStore(dataSource);
        Console.WriteLine("✅ Vector store created successfully.");

        // Step 2: Generate embeddings using Azure OpenAI
        var embeddingService = new AzureOpenAITextEmbeddingGenerationService(
            deploymentName: "your-deployment-name",
            endpoint: "https://api.openai.com",
            apiKey: "your-api-key"
        );

        string text = "This is an example sentence for embedding.";
        var embedding = await embeddingService.GenerateEmbeddingsAsync(new[] { text });

        Console.WriteLine($"✅ Generated Embedding: [{string.Join(", ", embedding[0].AsReadOnlySpan().Slice(0, 5))}...]");

        // Step 3: Perform chat completion using Azure OpenAI
        var kernel = Kernel.CreateBuilder()
            .AddAzureOpenAIChatCompletion(
                deploymentName: "your-chat-deployment-name",
                endpoint: "https://api.openai.com",
                apiKey: "your-api-key"
            ).Build();

        string userPrompt = "Explain Retrieval-Augmented Generation (RAG) in simple terms.";
        var response = await kernel.InvokePromptAsync(userPrompt);

        Console.WriteLine("✅ Chat Completion Response:");
        Console.WriteLine(response);
    }
}

Explore examples and sample code for using SemanticKernel with Neon Serverless Postgres.

Suggest an edit

Propose a replacement for this page. The site team reviews it before applying any changes.

Export
Documentation menu