Skip to content

Commit 6bfb7cc

Browse files
fix: Add pg vector support?
1 parent 4bc14dd commit 6bfb7cc

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

EssentialCSharp.Chat.Shared/Extensions/ServiceCollectionExtensions.cs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,7 @@ public static IServiceCollection AddAzureOpenAIServices(
102102
/// <summary>
103103
/// Adds PostgreSQL vector store with managed identity authentication support.
104104
/// NOTE: Token is obtained once at startup and will expire after ~1 hour.
105-
/// For long-running applications, consider restarting the application periodically
106-
/// or implementing a background service to refresh the connection.
105+
/// For long-running applications, consider implementing token refresh logic.
107106
/// </summary>
108107
/// <param name="services">The service collection to add services to</param>
109108
/// <param name="connectionString">The PostgreSQL connection string (without password)</param>
@@ -136,13 +135,22 @@ private static IServiceCollection AddPostgresVectorStoreWithManagedIdentity(
136135
{
137136
builder.SslMode = SslMode.Require;
138137
}
139-
138+
140139
connectionString = builder.ToString();
141140
}
142141

143-
// Register the vector store using the connection string
142+
// Register NpgsqlDataSource with UseVector() enabled - this is critical for pgvector support
143+
services.AddSingleton<NpgsqlDataSource>(sp =>
144+
{
145+
var dataSourceBuilder = new NpgsqlDataSourceBuilder(connectionString);
146+
// IMPORTANT: UseVector() must be called to enable pgvector support
147+
dataSourceBuilder.UseVector();
148+
return dataSourceBuilder.Build();
149+
});
150+
151+
// Register the vector store using the NpgsqlDataSource from DI
144152
#pragma warning disable SKEXP0010 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
145-
services.AddPostgresVectorStore(connectionString);
153+
services.AddPostgresVectorStore();
146154
#pragma warning restore SKEXP0010
147155

148156
return services;
@@ -190,8 +198,17 @@ public static IServiceCollection AddAzureOpenAIServicesWithApiKey(
190198
aiOptions.Endpoint,
191199
apiKey);
192200

193-
// Add PostgreSQL vector store (standard connection string with password)
194-
services.AddPostgresVectorStore(postgresConnectionString);
201+
// Register NpgsqlDataSource with UseVector() enabled for API key scenario as well
202+
services.AddSingleton<NpgsqlDataSource>(sp =>
203+
{
204+
var dataSourceBuilder = new NpgsqlDataSourceBuilder(postgresConnectionString);
205+
// IMPORTANT: UseVector() must be called to enable pgvector support
206+
dataSourceBuilder.UseVector();
207+
return dataSourceBuilder.Build();
208+
});
209+
210+
// Add PostgreSQL vector store using the NpgsqlDataSource from DI
211+
services.AddPostgresVectorStore();
195212

196213
services.AddAzureOpenAIEmbeddingGenerator(
197214
aiOptions.VectorGenerationDeploymentName,

0 commit comments

Comments
 (0)