From dc460a952d3e38d25e159e63b2a3e90366736d44 Mon Sep 17 00:00:00 2001 From: Marco Minerva Date: Mon, 23 Oct 2023 10:34:14 +0200 Subject: [PATCH 1/4] Libraries update --- src/DatabaseGpt/DatabaseGpt.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/DatabaseGpt/DatabaseGpt.csproj b/src/DatabaseGpt/DatabaseGpt.csproj index 999b16c..3ede0c6 100644 --- a/src/DatabaseGpt/DatabaseGpt.csproj +++ b/src/DatabaseGpt/DatabaseGpt.csproj @@ -9,7 +9,7 @@ - + From 5a1bd2613a4f9a2dfe86a3a5ad7290ad0e6bc794 Mon Sep 17 00:00:00 2001 From: Marco Minerva Date: Wed, 25 Oct 2023 12:22:15 +0200 Subject: [PATCH 2/4] Some bugfixes #14 --- samples/DatabaseGptConsole/Application.cs | 8 ++-- .../DatabaseGptConsole.csproj | 1 - samples/DatabaseGptConsole/Program.cs | 23 +++++++----- samples/DatabaseGptConsole/appsettings.json | 2 +- .../Exceptions/DatabaseGptException.cs | 13 +------ .../IDatabaseGptProvider.cs | 7 +--- .../NpgsqlDatabaseGptExtensions.cs | 8 ++-- .../NpgsqlDatabaseGptProvider.cs | 32 ++++++++-------- .../SqlServerDatabaseGptExtensions.cs | 8 ++-- .../SqlServerDatabaseGptProvider.cs | 37 ++++++++----------- .../TypeHandlers/DateOnlyTypeHandler.cs | 16 -------- .../TypeHandlers/TimeOnlyTypeHandler.cs | 16 -------- src/DatabaseGpt/DatabaseGpt.csproj | 1 - src/DatabaseGpt/DatabaseGptClient.cs | 21 +++++------ .../DatabaseGptServiceCollectionExtensions.cs | 18 ++++++--- .../Exceptions/InvalidSqlException.cs | 6 +-- .../Exceptions/NoTableFoundException.cs | 6 +-- .../Settings/DatabaseGptSettings.cs | 8 ++-- 18 files changed, 92 insertions(+), 139 deletions(-) delete mode 100644 src/DatabaseGpt.SqlServer/TypeHandlers/DateOnlyTypeHandler.cs delete mode 100644 src/DatabaseGpt.SqlServer/TypeHandlers/TimeOnlyTypeHandler.cs diff --git a/samples/DatabaseGptConsole/Application.cs b/samples/DatabaseGptConsole/Application.cs index 6a85355..2b7d0ed 100644 --- a/samples/DatabaseGptConsole/Application.cs +++ b/samples/DatabaseGptConsole/Application.cs @@ -3,7 +3,6 @@ using DatabaseGpt.Extensions; using DatabaseGpt.Models; using DatabaseGpt.Settings; -using Microsoft.Extensions.Options; using Spectre.Console; namespace DatabaseGptConsole; @@ -13,10 +12,10 @@ internal class Application private readonly IDatabaseGptClient databaseGptClient; private readonly DatabaseGptSettings databaseSettings; - public Application(IDatabaseGptClient databaseGptClient, IOptions databaseSettingsOptions) + public Application(IDatabaseGptClient databaseGptClient, DatabaseGptSettings databaseSettings) { this.databaseGptClient = databaseGptClient; - databaseSettings = databaseSettingsOptions.Value; + this.databaseSettings = databaseSettings; } public async Task ExecuteAsync() @@ -46,7 +45,7 @@ public async Task ExecuteAsync() AnsiConsole.WriteLine(); AnsiConsole.WriteLine(); - AnsiConsole.WriteLine($"I think the following tables might be useful: {string.Join(", ", args.Tables)}."); + AnsiConsole.Write($"I think the following tables might be useful: {string.Join(", ", args.Tables)}."); return default; }, @@ -57,6 +56,7 @@ public async Task ExecuteAsync() AnsiConsole.WriteLine("The query to answer the question should be the following:"); + AnsiConsole.WriteLine(); AnsiConsole.WriteLine(args.Sql); AnsiConsole.WriteLine(); diff --git a/samples/DatabaseGptConsole/DatabaseGptConsole.csproj b/samples/DatabaseGptConsole/DatabaseGptConsole.csproj index 8e12a3e..494a531 100644 --- a/samples/DatabaseGptConsole/DatabaseGptConsole.csproj +++ b/samples/DatabaseGptConsole/DatabaseGptConsole.csproj @@ -5,7 +5,6 @@ net7.0 enable enable - cace36d6-2904-4a1a-ae73-7f5071a723ed diff --git a/samples/DatabaseGptConsole/Program.cs b/samples/DatabaseGptConsole/Program.cs index b731d54..f42789e 100644 --- a/samples/DatabaseGptConsole/Program.cs +++ b/samples/DatabaseGptConsole/Program.cs @@ -28,16 +28,19 @@ static void ConfigureServices(HostBuilderContext context, IServiceCollection services) { services.AddSingleton(); + services.AddDatabaseGpt(database => - { - // For using SQL Server - database.UseConfiguration(context.Configuration) - .UseSqlServer(context.Configuration["ConnectionStrings:SqlConnection"]); + { + // For SQL Server. + database.UseConfiguration(context.Configuration) + .UseSqlServer(context.Configuration["ConnectionStrings:SqlConnection"]); - // For using Postgres - // database.UseConfiguration(context.Configuration) - // .UseNpgsql(context.Configuration["ConnectionStrings:SqlConnection"]); - }, - chatgpt => chatgpt.UseConfiguration(context.Configuration) - ); + // For Postgre SQL. + //database.UseConfiguration(context.Configuration) + // .UseNpgsql(context.Configuration["ConnectionStrings:NpgsqlConnection"]); + }, + chatGpt => + { + chatGpt.UseConfiguration(context.Configuration); + }); } diff --git a/samples/DatabaseGptConsole/appsettings.json b/samples/DatabaseGptConsole/appsettings.json index 54f5cc5..ca07d3d 100644 --- a/samples/DatabaseGptConsole/appsettings.json +++ b/samples/DatabaseGptConsole/appsettings.json @@ -12,7 +12,7 @@ "MessageLimit": 20, "MessageExpiration": "00:30:00" }, - "DatabaseSettings": { + "DatabaseGptSettings": { "IncludedTables": [ ], "ExcludedTables": [ ], "ExcludedColumns": [ ], diff --git a/src/DatabaseGpt.Abstractions/Exceptions/DatabaseGptException.cs b/src/DatabaseGpt.Abstractions/Exceptions/DatabaseGptException.cs index 39f4002..7294abf 100644 --- a/src/DatabaseGpt.Abstractions/Exceptions/DatabaseGptException.cs +++ b/src/DatabaseGpt.Abstractions/Exceptions/DatabaseGptException.cs @@ -1,11 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Runtime.Serialization; -using System.Text; -using System.Threading.Tasks; - -namespace DatabaseGpt.Abstractions.Exceptions; +namespace DatabaseGpt.Abstractions.Exceptions; public class DatabaseGptException : Exception { @@ -20,8 +13,4 @@ public DatabaseGptException(string? message) : base(message) public DatabaseGptException(string? message, Exception? innerException) : base(message, innerException) { } - - protected DatabaseGptException(SerializationInfo info, StreamingContext context) : base(info, context) - { - } } diff --git a/src/DatabaseGpt.Abstractions/IDatabaseGptProvider.cs b/src/DatabaseGpt.Abstractions/IDatabaseGptProvider.cs index f7a2c2c..0fe2e69 100644 --- a/src/DatabaseGpt.Abstractions/IDatabaseGptProvider.cs +++ b/src/DatabaseGpt.Abstractions/IDatabaseGptProvider.cs @@ -1,9 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Data; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using System.Data; namespace DatabaseGpt.Abstractions; diff --git a/src/DatabaseGpt.Npgsql/NpgsqlDatabaseGptExtensions.cs b/src/DatabaseGpt.Npgsql/NpgsqlDatabaseGptExtensions.cs index 992b243..080f25b 100644 --- a/src/DatabaseGpt.Npgsql/NpgsqlDatabaseGptExtensions.cs +++ b/src/DatabaseGpt.Npgsql/NpgsqlDatabaseGptExtensions.cs @@ -1,13 +1,15 @@ using DatabaseGpt.Abstractions; using DatabaseGpt.Npgsql; -using Microsoft.Extensions.DependencyInjection; namespace DatabaseGpt; public static class NpgsqlDatabaseGptExtensions { - public static void UseNpgsql(this IDatabaseGptSettings databaseGptSettings, string connectionString) + public static void UseNpgsql(this IDatabaseGptSettings databaseGptSettings, string? connectionString) { - databaseGptSettings.SetDatabaseGptProviderFactory(() => new NpgsqlDatabaseGptProvider(new() { ConnectionString = connectionString })); + databaseGptSettings.SetDatabaseGptProviderFactory(() => new NpgsqlDatabaseGptProvider(new() + { + ConnectionString = connectionString + })); } } \ No newline at end of file diff --git a/src/DatabaseGpt.Npgsql/NpgsqlDatabaseGptProvider.cs b/src/DatabaseGpt.Npgsql/NpgsqlDatabaseGptProvider.cs index 62631fc..949f481 100644 --- a/src/DatabaseGpt.Npgsql/NpgsqlDatabaseGptProvider.cs +++ b/src/DatabaseGpt.Npgsql/NpgsqlDatabaseGptProvider.cs @@ -12,14 +12,13 @@ public class NpgsqlDatabaseGptProvider : IDatabaseGptProvider, IDisposable private readonly NpgsqlConnection connection; private bool disposedValue; - public string Name => "Postgres"; + public string Name => "PostgreSQL"; - public string Language => "Postgres SQL"; + public string Language => "PL/pgSQL"; public NpgsqlDatabaseGptProvider(NpgsqlDatabaseGptProviderConfiguration settings) { connection = new NpgsqlConnection(settings.ConnectionString); - connection.Open(); } public async Task> GetTablesAsync(IEnumerable includedTables, IEnumerable excludedTables) @@ -32,27 +31,16 @@ WHERE TABLE_SCHEMA NOT IN ('pg_catalog', 'information_schema') if (includedTables?.Any() ?? false) { - tables = tables.Where(t => includedTables.Contains(t)); + tables = tables.Where(t => includedTables.Contains(t, StringComparer.InvariantCultureIgnoreCase)); } else if (excludedTables?.Any() ?? false) { - tables = tables.Where(t => !excludedTables.Contains(t)); + tables = tables.Where(t => !excludedTables.Contains(t, StringComparer.InvariantCultureIgnoreCase)); } return tables; } - public async Task ExecuteQueryAsync(string query) - { - try - { - return await connection.ExecuteReaderAsync(query); - } - catch (NpgsqlException ex) - { - throw new DatabaseGptException("An error occurred while executing the query. See the inner exception for details", ex); - } - } public async Task GetCreateTablesScriptAsync(IEnumerable tables, IEnumerable excludedColumns) { var result = new StringBuilder(); @@ -82,6 +70,18 @@ FROM INFORMATION_SCHEMA.COLUMNS return result.ToString(); } + public async Task ExecuteQueryAsync(string query) + { + try + { + return await connection.ExecuteReaderAsync(query); + } + catch (NpgsqlException ex) + { + throw new DatabaseGptException("An error occurred while executing the query. See the inner exception for details", ex); + } + } + protected virtual void Dispose(bool disposing) { if (!disposedValue) diff --git a/src/DatabaseGpt.SqlServer/SqlServerDatabaseGptExtensions.cs b/src/DatabaseGpt.SqlServer/SqlServerDatabaseGptExtensions.cs index 3e8c6bd..901b5de 100644 --- a/src/DatabaseGpt.SqlServer/SqlServerDatabaseGptExtensions.cs +++ b/src/DatabaseGpt.SqlServer/SqlServerDatabaseGptExtensions.cs @@ -1,13 +1,15 @@ using DatabaseGpt.Abstractions; using DatabaseGpt.SqlServer; -using Microsoft.Extensions.DependencyInjection; namespace DatabaseGpt; public static class SqlServerDatabaseGptExtensions { - public static void UseSqlServer(this IDatabaseGptSettings databaseGptSettings, string connectionString) + public static void UseSqlServer(this IDatabaseGptSettings databaseGptSettings, string? connectionString) { - databaseGptSettings.SetDatabaseGptProviderFactory(() => new SqlServerDatabaseGptProvider(new() { ConnectionString = connectionString })); + databaseGptSettings.SetDatabaseGptProviderFactory(() => new SqlServerDatabaseGptProvider(new() + { + ConnectionString = connectionString + })); } } \ No newline at end of file diff --git a/src/DatabaseGpt.SqlServer/SqlServerDatabaseGptProvider.cs b/src/DatabaseGpt.SqlServer/SqlServerDatabaseGptProvider.cs index d807ac3..820a0b3 100644 --- a/src/DatabaseGpt.SqlServer/SqlServerDatabaseGptProvider.cs +++ b/src/DatabaseGpt.SqlServer/SqlServerDatabaseGptProvider.cs @@ -3,7 +3,6 @@ using Dapper; using DatabaseGpt.Abstractions; using DatabaseGpt.Abstractions.Exceptions; -using DatabaseGpt.SqlServer.TypeHandlers; using Microsoft.Data.SqlClient; namespace DatabaseGpt.SqlServer; @@ -17,16 +16,9 @@ public class SqlServerDatabaseGptProvider : IDatabaseGptProvider, IDisposable public string Language => "T-SQL"; - static SqlServerDatabaseGptProvider() - { - SqlMapper.AddTypeHandler(new DateOnlyTypeHandler()); - SqlMapper.AddTypeHandler(new TimeOnlyTypeHandler()); - } - public SqlServerDatabaseGptProvider(SqlServerDatabaseGptProviderConfiguration settings) { connection = new SqlConnection(settings.ConnectionString); - connection.Open(); } public async Task> GetTablesAsync(IEnumerable includedTables, IEnumerable excludedTables) @@ -35,27 +27,16 @@ public async Task> GetTablesAsync(IEnumerable includ if (includedTables?.Any() ?? false) { - tables = tables.Where(t => includedTables.Contains(t)); + tables = tables.Where(t => includedTables.Contains(t, StringComparer.InvariantCultureIgnoreCase)); } else if (excludedTables?.Any() ?? false) { - tables = tables.Where(t => !excludedTables.Contains(t)); + tables = tables.Where(t => !excludedTables.Contains(t, StringComparer.InvariantCultureIgnoreCase)); } return tables; } - public async Task ExecuteQueryAsync(string query) - { - try - { - return await connection.ExecuteReaderAsync(query); - } - catch (SqlException ex) - { - throw new DatabaseGptException("An error occurred while executing the query. See the inner exception for details", ex); - } - } public async Task GetCreateTablesScriptAsync(IEnumerable tables, IEnumerable excludedColumns) { var result = new StringBuilder(); @@ -82,13 +63,25 @@ FOR XML PATH('')), 1, 1, '' ); """; - var columns = await connection.QueryAsync(query, new { schema = table.Schema, table = table.Name, ExcludedColumns = excludedColumns }); + var columns = await connection.QueryFirstAsync(query, new { schema = table.Schema, table = table.Name, ExcludedColumns = excludedColumns }); result.AppendLine($"CREATE TABLE [{table.Schema}].[{table.Name}] ({columns});"); } return result.ToString(); } + public async Task ExecuteQueryAsync(string query) + { + try + { + return await connection.ExecuteReaderAsync(query); + } + catch (SqlException ex) + { + throw new DatabaseGptException("An error occurred while executing the query. See the inner exception for details", ex); + } + } + protected virtual void Dispose(bool disposing) { if (!disposedValue) diff --git a/src/DatabaseGpt.SqlServer/TypeHandlers/DateOnlyTypeHandler.cs b/src/DatabaseGpt.SqlServer/TypeHandlers/DateOnlyTypeHandler.cs deleted file mode 100644 index 0595333..0000000 --- a/src/DatabaseGpt.SqlServer/TypeHandlers/DateOnlyTypeHandler.cs +++ /dev/null @@ -1,16 +0,0 @@ -using System.Data; -using Dapper; - -namespace DatabaseGpt.SqlServer.TypeHandlers; - -internal class DateOnlyTypeHandler : SqlMapper.TypeHandler -{ - public override DateOnly Parse(object value) - => DateOnly.FromDateTime(Convert.ToDateTime(value)); - - public override void SetValue(IDbDataParameter parameter, DateOnly value) - { - parameter.DbType = DbType.Date; - parameter.Value = value.ToDateTime(TimeOnly.MinValue); - } -} \ No newline at end of file diff --git a/src/DatabaseGpt.SqlServer/TypeHandlers/TimeOnlyTypeHandler.cs b/src/DatabaseGpt.SqlServer/TypeHandlers/TimeOnlyTypeHandler.cs deleted file mode 100644 index 82f846c..0000000 --- a/src/DatabaseGpt.SqlServer/TypeHandlers/TimeOnlyTypeHandler.cs +++ /dev/null @@ -1,16 +0,0 @@ -using System.Data; -using Dapper; - -namespace DatabaseGpt.SqlServer.TypeHandlers; - -internal class TimeOnlyTypeHandler : SqlMapper.TypeHandler -{ - public override TimeOnly Parse(object value) - => TimeOnly.FromTimeSpan(TimeSpan.Parse(value.ToString()!)); - - public override void SetValue(IDbDataParameter parameter, TimeOnly value) - { - parameter.DbType = DbType.Time; - parameter.Value = value.ToTimeSpan(); - } -} \ No newline at end of file diff --git a/src/DatabaseGpt/DatabaseGpt.csproj b/src/DatabaseGpt/DatabaseGpt.csproj index 805a02f..465fb7b 100644 --- a/src/DatabaseGpt/DatabaseGpt.csproj +++ b/src/DatabaseGpt/DatabaseGpt.csproj @@ -8,7 +8,6 @@ - diff --git a/src/DatabaseGpt/DatabaseGptClient.cs b/src/DatabaseGpt/DatabaseGptClient.cs index e430fc0..27dfcc5 100644 --- a/src/DatabaseGpt/DatabaseGptClient.cs +++ b/src/DatabaseGpt/DatabaseGptClient.cs @@ -4,7 +4,6 @@ using DatabaseGpt.Exceptions; using DatabaseGpt.Models; using DatabaseGpt.Settings; -using Microsoft.Extensions.Options; using Polly; using Polly.Registry; @@ -18,13 +17,13 @@ internal class DatabaseGptClient : IDatabaseGptClient private readonly ResiliencePipeline pipeline; private readonly DatabaseGptSettings databaseGptSettings; - public DatabaseGptClient(IChatGptClient chatGptClient, ResiliencePipelineProvider pipelineProvider - , IServiceProvider serviceProvider, DatabaseGptSettings databaseGptSettings) + public DatabaseGptClient(IChatGptClient chatGptClient, ResiliencePipelineProvider pipelineProvider, IServiceProvider serviceProvider, DatabaseGptSettings databaseGptSettings) { this.chatGptClient = chatGptClient; this.serviceProvider = serviceProvider; this.databaseGptSettings = databaseGptSettings; - provider = this.databaseGptSettings.CreateProvider(); + + provider = databaseGptSettings.CreateProvider(); pipeline = pipelineProvider.GetPipeline(nameof(DatabaseGptClient)); } @@ -36,9 +35,9 @@ public async Task ExecuteNaturalLanguageQueryAsync(Guid sessionId, var tables = await provider.GetTablesAsync(databaseGptSettings.IncludedTables, databaseGptSettings.ExcludedTables); var systemMessage = $""" - You are an assistant that answers questions using the information stored in a {provider.Name} database. + You are an assistant that answers questions using the information stored in a {provider.Name} database and the {provider.Language} language. Your answers can only reference one or more of the following tables: '{string.Join(',', tables)}'. - You can create only SELECT queries. Never create INSERT, UPDATE nor DELETE command. + You can create only {provider.Language} SELECT queries. Never create INSERT, UPDATE nor DELETE command. When you create a {provider.Language} query, consider the following information: {databaseGptSettings.SystemMessage} """; @@ -49,7 +48,7 @@ public async Task ExecuteNaturalLanguageQueryAsync(Guid sessionId, var reader = await pipeline.ExecuteAsync(async cancellationToken => { var request = $""" - You must answer the following question, '{question}', using a {provider.Language} query. Take into account also the previous messages. + You must answer the following question, '{question}', using a {provider.Language} query for a {provider.Name} database. Take into account also the previous messages. From the comma separated list of tables available in the database, select those tables that might be useful in the generated {provider.Language} query. The selected tables should be returned in a comma separated list. Your response should just contain the comma separated list of selected tables. If there are no tables that might be useful, return only the string 'NONE', without any other words. You shouldn't never explain the reason why you haven't found any table. @@ -79,14 +78,14 @@ The selected tables should be returned in a comma separated list. Your response var createTableScripts = await provider.GetCreateTablesScriptAsync(tables, databaseGptSettings.ExcludedColumns); request = $""" - A database contains the following tables and columns: + A {provider.Name} database contains the following tables and columns: {createTableScripts} - Generate a {provider.Language} query to answer the question: '{question}' - the query must only reference table names and column names that appear in this request. + Generate a {provider.Language} query for a {provider.Name} database to answer the question: '{question}' - The query must only reference table names and column names that appear in this request. For example, if the request contains the following CREATE TABLE statements: CREATE TABLE Table1 (Column1 VARCHAR(255), Column2 VARCHAR(255)) CREATE TABLE Table2 (Column3 VARCHAR(255), Column4 VARCHAR(255)) - Then you should only reference Tables Table1 and Table2 and the query should only reference columns Column1, Column2, Column3 and Column4. - Your response should just contain the {provider.Language} query, no other information is required. For example, never explain the meaning of the query nor explain how to use the query. + Then you should only reference tables Table1 and Table2 and the query should only reference columns Column1, Column2, Column3 and Column4. + Your response should just contain the {provider.Language} query for a {provider.Name} database, no other information is required. For example, never explain the meaning of the query nor explain how to use the query. You can create only SELECT queries. Never create INSERT, UPDATE nor DELETE commands. If the question of the user requires an INSERT, UPDATE or DELETE command, then return only the string 'NONE', without any other words. You shouldn't never explain the reason why you haven't created the query. """; diff --git a/src/DatabaseGpt/DatabaseGptServiceCollectionExtensions.cs b/src/DatabaseGpt/DatabaseGptServiceCollectionExtensions.cs index 9bed05a..c56771b 100644 --- a/src/DatabaseGpt/DatabaseGptServiceCollectionExtensions.cs +++ b/src/DatabaseGpt/DatabaseGptServiceCollectionExtensions.cs @@ -12,23 +12,25 @@ public static class DatabaseGptServiceCollectionExtensions { public static IServiceCollection AddDatabaseGpt(this IServiceCollection services, Action configureDatabaseGpt, Action configureChatGpt, ServiceLifetime lifetime = ServiceLifetime.Scoped) { + ArgumentNullException.ThrowIfNull(services); + ArgumentNullException.ThrowIfNull(configureDatabaseGpt); + ArgumentNullException.ThrowIfNull(configureChatGpt); + var settings = new DatabaseGptSettings(); configureDatabaseGpt(settings); - services.AddSingleton(settings); + services.Add(new ServiceDescriptor(typeof(IDatabaseGptClient), typeof(DatabaseGptClient), lifetime)); + services.AddChatGpt(configureChatGpt); + services.AddResiliencePipeline(nameof(DatabaseGptClient), (builder) => { builder.AddRetry(new RetryStrategyOptions { MaxRetryAttempts = settings!.MaxRetries, Delay = TimeSpan.Zero, - ShouldHandle = new PredicateBuilder() - .Handle() - .Handle() - .Handle(), - OnRetry = args => default + ShouldHandle = new PredicateBuilder().Handle().Handle().Handle() }); }); @@ -37,6 +39,10 @@ public static IServiceCollection AddDatabaseGpt(this IServiceCollection services public static DatabaseGptSettings UseConfiguration(this DatabaseGptSettings settings, IConfiguration configuration, string databaseGptSettingsSectionName = "DatabaseGptSettings") { + ArgumentNullException.ThrowIfNull(settings); + ArgumentNullException.ThrowIfNull(configuration); + ArgumentException.ThrowIfNullOrEmpty(databaseGptSettingsSectionName); + configuration.GetSection(databaseGptSettingsSectionName).Bind(settings); return settings; } diff --git a/src/DatabaseGpt/Exceptions/InvalidSqlException.cs b/src/DatabaseGpt/Exceptions/InvalidSqlException.cs index 0260dea..987d835 100644 --- a/src/DatabaseGpt/Exceptions/InvalidSqlException.cs +++ b/src/DatabaseGpt/Exceptions/InvalidSqlException.cs @@ -1,8 +1,6 @@ -using DatabaseGpt.Abstractions.Exceptions; +namespace DatabaseGpt.Exceptions; -namespace DatabaseGpt.Exceptions; - -public class InvalidSqlException : DatabaseGptException +public class InvalidSqlException : Exception { public InvalidSqlException() { diff --git a/src/DatabaseGpt/Exceptions/NoTableFoundException.cs b/src/DatabaseGpt/Exceptions/NoTableFoundException.cs index 0a8a388..67c441d 100644 --- a/src/DatabaseGpt/Exceptions/NoTableFoundException.cs +++ b/src/DatabaseGpt/Exceptions/NoTableFoundException.cs @@ -1,8 +1,6 @@ -using DatabaseGpt.Abstractions.Exceptions; +namespace DatabaseGpt.Exceptions; -namespace DatabaseGpt.Exceptions; - -public class NoTableFoundException : DatabaseGptException +public class NoTableFoundException : Exception { public NoTableFoundException() { diff --git a/src/DatabaseGpt/Settings/DatabaseGptSettings.cs b/src/DatabaseGpt/Settings/DatabaseGptSettings.cs index 56629c3..b99d0ef 100644 --- a/src/DatabaseGpt/Settings/DatabaseGptSettings.cs +++ b/src/DatabaseGpt/Settings/DatabaseGptSettings.cs @@ -4,9 +4,9 @@ namespace DatabaseGpt.Settings; public class DatabaseGptSettings : IDatabaseGptSettings { - private Func providerFactory; + private Func providerFactory = null!; - public string SystemMessage { get; set; } = "You are an AI assistant that helps people find information."; + public string? SystemMessage { get; set; } public string[] IncludedTables { get; set; } = Array.Empty(); @@ -18,8 +18,10 @@ public class DatabaseGptSettings : IDatabaseGptSettings public void SetDatabaseGptProviderFactory(Func providerFactory) { + ArgumentNullException.ThrowIfNull(providerFactory); + this.providerFactory = providerFactory; } - internal IDatabaseGptProvider CreateProvider() => providerFactory(); + internal IDatabaseGptProvider CreateProvider() => providerFactory.Invoke(); } From a36438b90b8a02a3ecab4123d60725e5e0c5f5cc Mon Sep 17 00:00:00 2001 From: Marco Minerva Date: Wed, 25 Oct 2023 12:23:11 +0200 Subject: [PATCH 3/4] Libraries update --- src/DatabaseGpt.Npgsql/DatabaseGpt.Npgsql.csproj | 2 +- src/DatabaseGpt.SqlServer/DatabaseGpt.SqlServer.csproj | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/DatabaseGpt.Npgsql/DatabaseGpt.Npgsql.csproj b/src/DatabaseGpt.Npgsql/DatabaseGpt.Npgsql.csproj index 8751418..c1824b7 100644 --- a/src/DatabaseGpt.Npgsql/DatabaseGpt.Npgsql.csproj +++ b/src/DatabaseGpt.Npgsql/DatabaseGpt.Npgsql.csproj @@ -7,7 +7,7 @@ - + diff --git a/src/DatabaseGpt.SqlServer/DatabaseGpt.SqlServer.csproj b/src/DatabaseGpt.SqlServer/DatabaseGpt.SqlServer.csproj index aa1722b..efcc3c4 100644 --- a/src/DatabaseGpt.SqlServer/DatabaseGpt.SqlServer.csproj +++ b/src/DatabaseGpt.SqlServer/DatabaseGpt.SqlServer.csproj @@ -8,7 +8,7 @@ - + From 6ddb9b43b8252872955d34114b983646890121b9 Mon Sep 17 00:00:00 2001 From: Marco Minerva Date: Wed, 25 Oct 2023 12:24:31 +0200 Subject: [PATCH 4/4] Fix a typo #14 --- src/DatabaseGpt.Npgsql/NpgsqlDatabaseGptProvider.cs | 2 +- src/DatabaseGpt.SqlServer/SqlServerDatabaseGptProvider.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/DatabaseGpt.Npgsql/NpgsqlDatabaseGptProvider.cs b/src/DatabaseGpt.Npgsql/NpgsqlDatabaseGptProvider.cs index 949f481..a543bef 100644 --- a/src/DatabaseGpt.Npgsql/NpgsqlDatabaseGptProvider.cs +++ b/src/DatabaseGpt.Npgsql/NpgsqlDatabaseGptProvider.cs @@ -78,7 +78,7 @@ public async Task ExecuteQueryAsync(string query) } catch (NpgsqlException ex) { - throw new DatabaseGptException("An error occurred while executing the query. See the inner exception for details", ex); + throw new DatabaseGptException("An error occurred while executing the query. See the inner exception for details.", ex); } } diff --git a/src/DatabaseGpt.SqlServer/SqlServerDatabaseGptProvider.cs b/src/DatabaseGpt.SqlServer/SqlServerDatabaseGptProvider.cs index 820a0b3..354d5d0 100644 --- a/src/DatabaseGpt.SqlServer/SqlServerDatabaseGptProvider.cs +++ b/src/DatabaseGpt.SqlServer/SqlServerDatabaseGptProvider.cs @@ -78,7 +78,7 @@ public async Task ExecuteQueryAsync(string query) } catch (SqlException ex) { - throw new DatabaseGptException("An error occurred while executing the query. See the inner exception for details", ex); + throw new DatabaseGptException("An error occurred while executing the query. See the inner exception for details.", ex); } }