-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
@kk502
committed
Jul 29, 2024
1 parent
34631da
commit 1290470
Showing
32 changed files
with
3,090 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ""; | ||
} | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
Oops, something went wrong.