Skip to content

Commit 4748340

Browse files
committed
Init 初始化项目
1 parent 9c35767 commit 4748340

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+4354
-1
lines changed

.gitattributes

+62-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,63 @@
1-
# Auto detect text files and perform LF normalization
1+
###############################################################################
2+
# Set default behavior to automatically normalize line endings.
3+
###############################################################################
24
* text=auto
5+
6+
###############################################################################
7+
# Set default behavior for command prompt diff.
8+
#
9+
# This is need for earlier builds of msysgit that does not have it on by
10+
# default for csharp files.
11+
# Note: This is only used by command line
12+
###############################################################################
13+
#*.cs diff=csharp
14+
15+
###############################################################################
16+
# Set the merge driver for project and solution files
17+
#
18+
# Merging from the command prompt will add diff markers to the files if there
19+
# are conflicts (Merging from VS is not affected by the settings below, in VS
20+
# the diff markers are never inserted). Diff markers may cause the following
21+
# file extensions to fail to load in VS. An alternative would be to treat
22+
# these files as binary and thus will always conflict and require user
23+
# intervention with every merge. To do so, just uncomment the entries below
24+
###############################################################################
25+
#*.sln merge=binary
26+
#*.csproj merge=binary
27+
#*.vbproj merge=binary
28+
#*.vcxproj merge=binary
29+
#*.vcproj merge=binary
30+
#*.dbproj merge=binary
31+
#*.fsproj merge=binary
32+
#*.lsproj merge=binary
33+
#*.wixproj merge=binary
34+
#*.modelproj merge=binary
35+
#*.sqlproj merge=binary
36+
#*.wwaproj merge=binary
37+
38+
###############################################################################
39+
# behavior for image files
40+
#
41+
# image files are treated as binary by default.
42+
###############################################################################
43+
#*.jpg binary
44+
#*.png binary
45+
#*.gif binary
46+
47+
###############################################################################
48+
# diff behavior for common document formats
49+
#
50+
# Convert binary document formats to text before diffing them. This feature
51+
# is only available from the command line. Turn it on by uncommenting the
52+
# entries below.
53+
###############################################################################
54+
#*.doc diff=astextplain
55+
#*.DOC diff=astextplain
56+
#*.docx diff=astextplain
57+
#*.DOCX diff=astextplain
58+
#*.dot diff=astextplain
59+
#*.DOT diff=astextplain
60+
#*.pdf diff=astextplain
61+
#*.PDF diff=astextplain
62+
#*.rtf diff=astextplain
63+
#*.RTF diff=astextplain

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
SKdemo示例
2+
主要介绍了SK插件使用
3+
原生函数
4+
嵌套函数
5+
Plan规划
6+
意图识别
7+
依赖注入等SK基础功能

Xzy.SK.sln

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
Microsoft Visual Studio Solution File, Format Version 12.00
2+
# Visual Studio Version 17
3+
VisualStudioVersion = 17.7.34031.279
4+
MinimumVisualStudioVersion = 10.0.40219.1
5+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Xzy.SK.Api", "src\Xzy.SK.Api\Xzy.SK.Api.csproj", "{B7138D00-A84E-4B56-821C-A71B836440E4}"
6+
EndProject
7+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Xzy.SK.Domain", "src\Xzy.SK.Domain\Xzy.SK.Domain.csproj", "{7730A0E2-25B1-483F-8F7B-00DEBDB7135B}"
8+
EndProject
9+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Xzy.SK.Test", "src\Xzy.SK.Test\Xzy.SK.Test.csproj", "{161E3478-8E2C-44FA-88C1-A3C0A010CEA2}"
10+
EndProject
11+
Global
12+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
13+
Debug|Any CPU = Debug|Any CPU
14+
Release|Any CPU = Release|Any CPU
15+
EndGlobalSection
16+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
17+
{B7138D00-A84E-4B56-821C-A71B836440E4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
18+
{B7138D00-A84E-4B56-821C-A71B836440E4}.Debug|Any CPU.Build.0 = Debug|Any CPU
19+
{B7138D00-A84E-4B56-821C-A71B836440E4}.Release|Any CPU.ActiveCfg = Release|Any CPU
20+
{B7138D00-A84E-4B56-821C-A71B836440E4}.Release|Any CPU.Build.0 = Release|Any CPU
21+
{7730A0E2-25B1-483F-8F7B-00DEBDB7135B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
22+
{7730A0E2-25B1-483F-8F7B-00DEBDB7135B}.Debug|Any CPU.Build.0 = Debug|Any CPU
23+
{7730A0E2-25B1-483F-8F7B-00DEBDB7135B}.Release|Any CPU.ActiveCfg = Release|Any CPU
24+
{7730A0E2-25B1-483F-8F7B-00DEBDB7135B}.Release|Any CPU.Build.0 = Release|Any CPU
25+
{161E3478-8E2C-44FA-88C1-A3C0A010CEA2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
26+
{161E3478-8E2C-44FA-88C1-A3C0A010CEA2}.Debug|Any CPU.Build.0 = Debug|Any CPU
27+
{161E3478-8E2C-44FA-88C1-A3C0A010CEA2}.Release|Any CPU.ActiveCfg = Release|Any CPU
28+
{161E3478-8E2C-44FA-88C1-A3C0A010CEA2}.Release|Any CPU.Build.0 = Release|Any CPU
29+
EndGlobalSection
30+
GlobalSection(SolutionProperties) = preSolution
31+
HideSolutionNode = FALSE
32+
EndGlobalSection
33+
GlobalSection(ExtensibilityGlobals) = postSolution
34+
SolutionGuid = {68D21C10-BBBC-4C5B-ADAA-E88C2B026776}
35+
EndGlobalSection
36+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using Azure.AI.OpenAI;
2+
using Azure;
3+
using Microsoft.AspNetCore.Http;
4+
using Microsoft.AspNetCore.Mvc;
5+
using Microsoft.SemanticKernel;
6+
using System;
7+
using System.IO;
8+
using System.Threading.Tasks;
9+
using Xzy.SK.Domain.Common.Options;
10+
using Xzy.SK.Domain.Domain.DTO.Chat;
11+
using System.Linq;
12+
using System.Collections.Generic;
13+
14+
namespace Xzy.SK.Api.Controllers
15+
{
16+
[Route("api/[controller]")]
17+
[ApiController]
18+
public class OpenAIController : ControllerBase
19+
{
20+
public OpenAIController()
21+
{
22+
23+
}
24+
25+
/// <summary>
26+
/// 测试翻译
27+
/// </summary>
28+
/// <param name="input"></param>
29+
/// <returns></returns>
30+
[HttpPost]
31+
public async Task<IActionResult> Translate(string msg)
32+
{
33+
string prompt = $"Translate this into English:\r\n{msg}\r\n";
34+
35+
OpenAIClient client = new OpenAIClient(new Uri(OpenAIOptions.Endpoint), new AzureKeyCredential(OpenAIOptions.Key), new OpenAIClientOptions());
36+
ChatCompletionsOptions completionsOptions = new ChatCompletionsOptions();
37+
38+
completionsOptions.Messages.Add(new ChatMessage("user", prompt));
39+
completionsOptions.MaxTokens = 300;
40+
completionsOptions.Temperature = 0;
41+
var result= await client.GetChatCompletionsAsync(OpenAIOptions.Model, completionsOptions);
42+
if (result != null)
43+
{
44+
return Ok(result.Value.Choices.First().Message.Content);
45+
}
46+
else
47+
{
48+
return Ok();
49+
}
50+
}
51+
52+
}
53+
}
+231
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,231 @@
1+
using Microsoft.AspNetCore.Http;
2+
using Microsoft.AspNetCore.Mvc;
3+
using Microsoft.SemanticKernel;
4+
using Microsoft.SemanticKernel.Orchestration;
5+
using Microsoft.SemanticKernel.Planning;
6+
using Microsoft.SemanticKernel.SkillDefinition;
7+
using Microsoft.SemanticKernel.Skills.Core;
8+
using Newtonsoft.Json;
9+
using System;
10+
using System.IO;
11+
using System.Text.Json;
12+
using System.Threading.Tasks;
13+
using Xzy.SK.Api.plugins;
14+
using Xzy.SK.Api.plugins.MathPlugin;
15+
using Xzy.SK.Domain.Common.Model;
16+
using Xzy.SK.Domain.Common.Options;
17+
18+
namespace Xzy.SK.Api.Controllers
19+
{
20+
/// <summary>
21+
/// SK
22+
/// </summary>
23+
[Route("api/[controller]/[action]")]
24+
[ApiController]
25+
public class SKController : ControllerBase
26+
{
27+
private readonly IKernel _kernel;
28+
public SKController(IKernel kernel)
29+
{
30+
_kernel = kernel;
31+
}
32+
33+
/// <summary>
34+
/// 测试翻译
35+
/// </summary>
36+
/// <param name="input"></param>
37+
/// <returns></returns>
38+
[HttpPost]
39+
public async Task<IActionResult> Translate(string input,string language)
40+
{
41+
//导入本地技能
42+
var pluginsDirectory = Path.Combine(System.IO.Directory.GetCurrentDirectory(), "plugins");
43+
var writerPlugin = _kernel
44+
.ImportSemanticSkillFromDirectory(pluginsDirectory, "Translate");
45+
46+
var result = await _kernel.RunAsync(input, writerPlugin[language]);
47+
48+
return Ok(result.Result);
49+
}
50+
51+
/// <summary>
52+
/// 测试计算
53+
/// </summary>
54+
/// <param name="num1"></param>
55+
/// <param name="num2"></param>
56+
/// <returns></returns>
57+
[HttpPost]
58+
public async Task<IActionResult> Calculate(string num1, string num2)
59+
{
60+
//导入本地技能,多参数
61+
62+
var pluginsDirectory = Path.Combine(System.IO.Directory.GetCurrentDirectory(), "plugins");
63+
var calculatePlugin = _kernel
64+
.ImportSemanticSkillFromDirectory(pluginsDirectory, "Calculate");
65+
66+
var variables = new ContextVariables
67+
{
68+
["num1"] = num1,
69+
["num2"] = num2
70+
};
71+
var result = await _kernel.RunAsync(variables, calculatePlugin["Addition"]);
72+
73+
return Ok(result.Result);
74+
}
75+
76+
/// <summary>
77+
/// 原生测试
78+
/// </summary>
79+
/// <param name="num1"></param>
80+
/// <param name="num2"></param>
81+
/// <returns></returns>
82+
[HttpPost]
83+
public async Task<IActionResult> Native(string num1, string num2)
84+
{
85+
//导入原生函数
86+
87+
var mathPlugin = _kernel.ImportSkill(new MathSK(), "MathPlugin");
88+
89+
var variables = new ContextVariables
90+
{
91+
["num1"] = num1,
92+
["num2"] = num2
93+
};
94+
var result = await _kernel.RunAsync(variables, mathPlugin["Subtraction"]);
95+
96+
return Ok(result.Result);
97+
}
98+
99+
/// <summary>
100+
/// 嵌套函数
101+
/// </summary>
102+
/// <param name="num1"></param>
103+
/// <param name="num2"></param>
104+
/// <returns></returns>
105+
[HttpPost]
106+
public async Task<IActionResult> Nested(string num1, string num2)
107+
{
108+
109+
//嵌套函数使用,在prompty中使用 {{Plugin.Fun}} 可以嵌套调用
110+
var pluginsDirectory = Path.Combine(System.IO.Directory.GetCurrentDirectory(), "plugins");
111+
var calculatePlugin = _kernel
112+
.ImportSemanticSkillFromDirectory(pluginsDirectory, "Calculate");
113+
//MathPlugin Multiplication 中可以嵌套其他函数
114+
var mathPlugin = _kernel.ImportSkill(new MathSK(), "MathPlugin");
115+
116+
var variables = new ContextVariables
117+
{
118+
["num1"] = num1,
119+
["num2"] = num2
120+
};
121+
var result = await _kernel.RunAsync(variables, calculatePlugin["Multiplication"]);
122+
123+
return Ok(result.Result);
124+
}
125+
126+
127+
128+
/// <summary>
129+
/// 原生嵌套,通过自然语义先找到最大和最小的2个值,然后用最大值减去最小值得到结果返回
130+
/// </summary>
131+
/// <param name="msg"></param>
132+
/// <returns></returns>
133+
134+
[HttpPost]
135+
public async Task<IActionResult> NativeNested(string msg)
136+
{
137+
138+
139+
var NativeNested = _kernel.ImportSkill(new NativeNested(_kernel), "NativeNested");
140+
141+
var result = await _kernel.RunAsync(msg, NativeNested["Test"]);
142+
143+
return Ok(result.Result);
144+
}
145+
146+
147+
/// <summary>
148+
/// 计划
149+
/// </summary>
150+
/// <param name="msg">小明有7个冰淇淋,我有2个冰淇淋,他比我多几个冰淇淋?</param>
151+
/// <param name="msg">小明有7个冰淇淋,我有2个冰淇淋,我们一共有几个冰淇淋?</param>
152+
/// <returns></returns>
153+
[HttpPost]
154+
public async Task<IActionResult> Plan(string msg)
155+
{
156+
157+
158+
var planner = new SequentialPlanner(_kernel);
159+
160+
var pluginsDirectory = Path.Combine(System.IO.Directory.GetCurrentDirectory(), "plugins");
161+
var calculatePlugin = _kernel
162+
.ImportSemanticSkillFromDirectory(pluginsDirectory, "Calculate");
163+
164+
var plan = await planner.CreatePlanAsync(msg);
165+
Console.WriteLine("Plan:\n");
166+
Console.WriteLine(JsonConvert.SerializeObject(plan));
167+
168+
var result = (await _kernel.RunAsync(plan)).Result;
169+
return Ok(result);
170+
}
171+
172+
173+
174+
/// <summary>
175+
/// 意图识别
176+
/// </summary>
177+
/// <param name="msg"></param>
178+
/// <returns></returns>
179+
[HttpPost]
180+
public async Task<IActionResult> Intent(string msg)
181+
{
182+
183+
//对话摘要 SK.Skills.Core 核心技能
184+
_kernel.ImportSkill(new ConversationSummarySkill(_kernel), "ConversationSummarySkill");
185+
186+
var pluginsDirectory = Path.Combine(System.IO.Directory.GetCurrentDirectory(), "plugins");
187+
var intentPlugin = _kernel
188+
.ImportSemanticSkillFromDirectory(pluginsDirectory, "BasePlugin");
189+
var travelPlugin = _kernel
190+
.ImportSemanticSkillFromDirectory(pluginsDirectory, "Travel");
191+
192+
var NativeNested = _kernel.ImportSkill(new UtilsPlugin(_kernel), "UtilsPlugin");
193+
var getIntentVariables = new ContextVariables
194+
{
195+
["input"] = msg,
196+
["options"] = "Attractions, Delicacy,Traffic,Weather,SendEmail"
197+
};
198+
string intent = (await _kernel.RunAsync(getIntentVariables, intentPlugin["GetIntent"])).Result.Trim();
199+
ISKFunction MathFunction;
200+
switch (intent)
201+
{
202+
case "Attractions":
203+
MathFunction = _kernel.Skills.GetFunction("Travel", "Attractions");
204+
break;
205+
case "Delicacy":
206+
MathFunction = _kernel.Skills.GetFunction("Travel", "Delicacy");
207+
break;
208+
case "Traffic":
209+
MathFunction = _kernel.Skills.GetFunction("Travel", "Traffic");
210+
break;
211+
case "Weather":
212+
MathFunction = _kernel.Skills.GetFunction("Travel", "Weather");
213+
break;
214+
case "SendEmail":
215+
var sendEmailVariables = new ContextVariables
216+
{
217+
["input"] = msg,
218+
["example"] = JsonConvert.SerializeObject(new { send_user = "xzy", receiver_user = "xzy", body = "hello" })
219+
};
220+
msg = (await _kernel.RunAsync(sendEmailVariables, intentPlugin["JSON"])).Result;
221+
MathFunction = _kernel.Skills.GetFunction("UtilsPlugin", "SendEmail");
222+
break;
223+
default:
224+
return Ok("对不起我不知道");
225+
}
226+
var result = await _kernel.RunAsync(msg, MathFunction);
227+
228+
return Ok(result.Result);
229+
}
230+
}
231+
}

0 commit comments

Comments
 (0)