-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
225 lines (185 loc) · 5.99 KB
/
Program.cs
File metadata and controls
225 lines (185 loc) · 5.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
using Microsoft.Data.Sqlite;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.SemanticKernel;
using System.Text;
using System.Text.Json;
var kernel = BuildKernel();
SetupDatabase();
string dbSchema = GetDatabaseSchema();
const string dbPath = "local_company.db";
Console.WriteLine("Database Schema:");
Console.WriteLine(dbSchema);
Console.WriteLine("\nChat with your database! Type 'exit' to quit.");
var textToSqlFunction = CreateTextToSqlFunction(kernel, dbSchema);
var finalAnswerFunction = CreateFinalAnswerFunction(kernel);
while (true)
{
Console.Write("> ");
string userInput = Console.ReadLine() ?? "";
if (userInput.Equals("exit", StringComparison.OrdinalIgnoreCase))
{
break;
}
try
{
var sqlResult = await textToSqlFunction.InvokeAsync(kernel, new() { ["input"] = userInput });
string sqlQuery = sqlResult.GetValue<string>()!.Trim();
Console.WriteLine($"\n🤖 Generated SQL: {sqlQuery}");
string dbData = ExecuteQueryAndFormatResults(dbPath, sqlQuery);
if (string.IsNullOrWhiteSpace(dbData))
{
Console.WriteLine("🤖 I couldn't find any data for that query.\n");
continue;
}
Console.WriteLine($"🗃️ Query Result:\n{dbData}");
var finalAnswerResult = await finalAnswerFunction.InvokeAsync(kernel, new()
{
["input"] = userInput,
["data"] = dbData
});
Console.WriteLine($"\n💬 Answer: {finalAnswerResult.GetValue<string>()}\n");
}
catch (Exception ex)
{
Console.WriteLine($"\nAn error occurred: {ex.Message}\n");
}
}
#pragma warning disable SKEXP0070
Kernel BuildKernel()
{
var builder = Kernel.CreateBuilder();
builder.AddOllamaChatCompletion(
modelId: "phi3",
endpoint: new Uri("http://localhost:11434")
);
return builder.Build();
}
KernelFunction CreateTextToSqlFunction(Kernel kernel, string dbSchema)
{
const string prompt = @"
Given the following database schema, your job is to convert the user's question into a valid SQLite SQL query.
- ONLY output the SQL query.
- Do not add any other text, explanations, or markdown formatting like ```sql.
- Be careful with data types and column names.
Schema:
---
{{$schema}}
---
User Question: {{$input}}
SQL Query:
";
var executionSettings = new PromptExecutionSettings()
{
ExtensionData = new Dictionary<string, object>()
{
{ "temperature", 0.0 }
}
};
var promptConfig = new PromptTemplateConfig
{
Template = prompt.Replace("{{$schema}}", dbSchema),
ExecutionSettings = new Dictionary<string, PromptExecutionSettings>
{
{ "default", executionSettings }
}
};
return KernelFunctionFactory.CreateFromPrompt(promptConfig);
}
KernelFunction CreateFinalAnswerFunction(Kernel kernel)
{
const string prompt = @"
Answer the following user's question based ONLY on the provided data.
If the data is empty or irrelevant, say you could not find an answer.
Be friendly and concise.
Data:
---
{{$data}}
---
User Question: {{$input}}
Answer:
";
var executionSettings = new PromptExecutionSettings()
{
ExtensionData = new Dictionary<string, object>()
{
{ "temperature", 0.2 }
}
};
var promptConfig = new PromptTemplateConfig
{
Template = prompt,
ExecutionSettings = new Dictionary<string, PromptExecutionSettings>
{
{ "default", executionSettings }
}
};
return KernelFunctionFactory.CreateFromPrompt(promptConfig);
}
string ExecuteQueryAndFormatResults(string dbPath, string sqlQuery)
{
var resultBuilder = new StringBuilder();
using var connection = new SqliteConnection($"Data Source={dbPath}");
connection.Open();
var command = connection.CreateCommand();
command.CommandText = sqlQuery;
using var reader = command.ExecuteReader();
for (int i = 0; i < reader.FieldCount; i++)
{
resultBuilder.Append(reader.GetName(i) + (i == reader.FieldCount - 1 ? "" : "\t"));
}
resultBuilder.AppendLine();
while (reader.Read())
{
for (int i = 0; i < reader.FieldCount; i++)
{
resultBuilder.Append(reader.GetValue(i) + (i == reader.FieldCount - 1 ? "" : "\t"));
}
resultBuilder.AppendLine();
}
return resultBuilder.ToString();
}
static void SetupDatabase()
{
const string dbPath = "local_company.db";
if (File.Exists(dbPath)) File.Delete(dbPath);
using var connection = new SqliteConnection($"Data Source={dbPath}");
connection.Open();
var createTableCommand = connection.CreateCommand();
createTableCommand.CommandText =
@"
CREATE TABLE Employees (
Id INTEGER PRIMARY KEY,
Name TEXT NOT NULL,
Department TEXT NOT NULL,
Salary INTEGER NOT NULL,
HireDate TEXT NOT NULL
);
";
createTableCommand.ExecuteNonQuery();
var insertCommand = connection.CreateCommand();
insertCommand.CommandText =
@"
INSERT INTO Employees (Name, Department, Salary, HireDate) VALUES
('Alice Johnson', 'Engineering', 95000, '2022-01-15'),
('Bob Smith', 'Sales', 82000, '2021-11-30'),
('Charlie Brown', 'Engineering', 110000, '2020-05-20'),
('Diana Prince', 'Sales', 78000, '2022-08-01'),
('Eve Adams', 'HR', 65000, '2023-02-10');
";
insertCommand.ExecuteNonQuery();
Console.WriteLine("Database 'local_company.db' created and populated.");
}
static string GetDatabaseSchema()
{
const string dbPath = "local_company.db";
using var connection = new SqliteConnection($"Data Source={dbPath}");
connection.Open();
var command = connection.CreateCommand();
command.CommandText = "SELECT sql FROM sqlite_master WHERE type='table' AND name='Employees';";
using var reader = command.ExecuteReader();
if (reader.Read())
{
return reader.GetString(0);
}
return string.Empty;
}