Skip to content

Commit

Permalink
v1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
@kk502 committed Jul 29, 2024
1 parent 34631da commit 1290470
Show file tree
Hide file tree
Showing 32 changed files with 3,090 additions and 103 deletions.
69 changes: 69 additions & 0 deletions SQL-Truck/AIService/AIServer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using AIService.Dtos;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using RestSharp;
using System.Security.Principal;

namespace AIService
{
public class AIServer : IAIService
{
private readonly IConfiguration _configuration;

public AIServer(IConfiguration configuration)
{
_configuration = configuration;
}
public ResultSQL GetSQLConvert(string systemPrompt, string prompt)
{
ResultSQL result = new ResultSQL();
string callAIResult = CallAI(systemPrompt, prompt);
if (!string.IsNullOrEmpty(callAIResult))
result = JsonConvert.DeserializeObject<ResultSQL>(callAIResult);
return result;
}
private string CallAI(string systemPrompt, string prompt)
{
string baseUrl = _configuration["OpenAI:BaseUrl"];
if (!string.IsNullOrEmpty(baseUrl))
baseUrl = baseUrl.TrimEnd('/');
string apiKey = _configuration["OpenAI:ApiKey"];
string model = _configuration["OpenAI:Model"];
var url = $"{baseUrl}/v1/chat/completions";
var client = new RestClient(url);
var request = new RestRequest("", Method.Post);

request.AddHeader("Authorization", $"Bearer {apiKey}");
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Accept", "*/*");
request.AddHeader("Connection", "keep-alive");

var requestBody = new
{
model = model,
response_format = new { type = "json_object" },
messages = new[]
{
new { role = "system", content = systemPrompt },
new { role = "user", content = prompt }
},
stream = false
};
string jsonBody = JsonConvert.SerializeObject(requestBody);
request.AddParameter("application/json", jsonBody, ParameterType.RequestBody);
RestResponse response = client.Execute(request);
if (response.IsSuccessful)
{
JObject jsonObj = JsonConvert.DeserializeObject<JObject>(response.Content);
string content = jsonObj["choices"][0]["message"]["content"].ToString();
return content;
}
else
{
return "";
}
}

}
}
15 changes: 15 additions & 0 deletions SQL-Truck/AIService/AIService.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="RestSharp" Version="111.4.0" />
</ItemGroup>

</Project>
18 changes: 18 additions & 0 deletions SQL-Truck/AIService/Dtos/AIResultDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AIService.Dtos
{
public class ResultSQL
{
public List<AIResultDto> AIResultDto { get; set; }
}
public class AIResultDto
{
public string DatabaseName { get; set; }
public string SQL { get; set; }
}
}
14 changes: 14 additions & 0 deletions SQL-Truck/AIService/IAIService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using AIService.Dtos;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AIService
{
public interface IAIService
{
ResultSQL GetSQLConvert(string systemPrompt, string prompt);
}
}
17 changes: 16 additions & 1 deletion SQL-Truck/SQL-Truck.sln
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@

Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SQL-Truck", "SQL-Truck\SQL-Truck.csproj", "{B5A05942-7618-4849-B519-8CD5F9D657B2}"
# Visual Studio Version 17
VisualStudioVersion = 17.8.34408.163
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SQL-Truck", "SQL-Truck\SQL-Truck.csproj", "{B5A05942-7618-4849-B519-8CD5F9D657B2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AIService", "AIService\AIService.csproj", "{2344DD0C-2541-4DC6-BFDB-8D7162842A00}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -12,5 +17,15 @@ Global
{B5A05942-7618-4849-B519-8CD5F9D657B2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B5A05942-7618-4849-B519-8CD5F9D657B2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B5A05942-7618-4849-B519-8CD5F9D657B2}.Release|Any CPU.Build.0 = Release|Any CPU
{2344DD0C-2541-4DC6-BFDB-8D7162842A00}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2344DD0C-2541-4DC6-BFDB-8D7162842A00}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2344DD0C-2541-4DC6-BFDB-8D7162842A00}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2344DD0C-2541-4DC6-BFDB-8D7162842A00}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {CCFDA7D4-52AF-4EFE-893D-624CB01EFD03}
EndGlobalSection
EndGlobal
53 changes: 53 additions & 0 deletions SQL-Truck/SQL-Truck/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,65 @@
using System.Diagnostics;
using AIService;
using Microsoft.AspNetCore.Mvc;

namespace SQL_Truck.Controllers;

public class HomeController : Controller
{
private readonly IAIService _aiservice;
public HomeController(IAIService aiservice)
{
_aiservice = aiservice;
}
public IActionResult Index()
{
return View();
}
public IActionResult GetSQLConvert(List<string> databasesName, string codeEditorValue)
{
if (databasesName.Count == 0 || string.IsNullOrEmpty(codeEditorValue))
return Json(new
{
success = false
});
string systemPrompt = @$"# 你是一个数据库专家,你的工作是将用户提供的SQL语句修改为指定数据库类型的SQL语句。这几种类型请你全部转换:{string.Join(", ", databasesName)}
# 任务:根据用户输入的SQL语句和指定的数据库类型,转换SQL语句以适应该类型的数据库。
# 输出应以JSON格式返回,格式为List<AIResultDto>,请必须返回一个JSON数组,其中每个AIResultDto包含数据库名称和修改后的SQL语句,SQL语句请使用markdown代码块包裹。
# AIResultDto是一个类,定义如下:
```csharp
public class AIResultDto
{{
public string DatabaseName {{ get; set; }}
public string SQL {{ get; set; }}
}}
```
返回数据示例(JSON格式):
```json
{{
'AIResultDto':[
{{
'DatabaseName': 'MySQL',
'SQL': '```sql
ALTER TABLE Example MODIFY COLUMN a VARCHAR(255);
```'
}},
{{
'DatabaseName': 'PostgreSQL',
'SQL': '```sql
ALTER TABLE Example ALTER COLUMN a TYPE VARCHAR(255);
```'
}}
]
}}
```
";
string prompt = @$"# 待转换的SQL语句:{codeEditorValue}
# 请提供我这几种数据库的SQL语句,以JSON数组方式发给我:{string.Join(", ", databasesName)}";
var result = _aiservice.GetSQLConvert(systemPrompt, prompt);
return Json(new
{
success = true,
data = result
});
}
}
4 changes: 3 additions & 1 deletion SQL-Truck/SQL-Truck/Program.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using AIService;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
Expand All @@ -12,7 +13,8 @@ public static void Main(string[] args)

// Add services to the container.
builder.Services.AddControllersWithViews();

// 注册AI服务
builder.Services.AddScoped<IAIService, AIServer>();
var app = builder.Build();

// Configure the HTTP request pipeline.
Expand Down
4 changes: 4 additions & 0 deletions SQL-Truck/SQL-Truck/SQL-Truck.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@
<RootNamespace>SQL_Truck</RootNamespace>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\AIService\AIService.csproj" />
</ItemGroup>

</Project>
9 changes: 9 additions & 0 deletions SQL-Truck/SQL-Truck/SQL-Truck.csproj.user
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DebuggerFlavor>ProjectDebugger</DebuggerFlavor>
</PropertyGroup>
<PropertyGroup>
<ActiveDebugProfile>IIS Express</ActiveDebugProfile>
</PropertyGroup>
</Project>
Loading

0 comments on commit 1290470

Please sign in to comment.