Skip to content

Commit a699e3a

Browse files
committed
更新包
1 parent 75630fe commit a699e3a

File tree

99 files changed

+1661
-5
lines changed

Some content is hidden

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

99 files changed

+1661
-5
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,4 @@ obj/
6666
/Samples/Senparc.Weixin.AI.MPSample/Properties/ServiceDependencies/SenparcWeixinAIMPSample20230409175131 - Web Deploy
6767
*.user
6868
/Samples/Senparc.AI.Samples.Consoles/appsettings.test.json
69+
/Samples/Senparc.Weixin.AI.MPSample/App_Data/WeChat_OfficialAccount/2023-05-07

Samples/Senparc.Weixin.AI.MPSample/CustomMessageHandler.cs

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
using Senparc.AI.Kernel;
1+
using Microsoft.SemanticKernel.CoreSkills;
2+
using Microsoft.SemanticKernel.Orchestration;
3+
using Senparc.AI;
4+
using Senparc.AI.Kernel;
25
using Senparc.AI.Kernel.Handlers;
36
using Senparc.CO2NET.Cache;
47
using Senparc.CO2NET.MessageQueue;
@@ -10,6 +13,8 @@
1013
using Senparc.Weixin.MP.Entities.Request;
1114
using Senparc.Weixin.MP.MessageContexts;
1215
using Senparc.Weixin.MP.MessageHandlers;
16+
using System.Text.RegularExpressions;
17+
using System.Threading.Tasks;
1318

1419
namespace Senparc.Weixin.AI.MPSample
1520
{
@@ -75,6 +80,11 @@ public override async Task<IResponseMessageBase> OnTextRequestAsync(RequestMessa
7580
responseMessage.Content = "ChatGPT 对话状态已退出,后续聊天不会消耗额度。";
7681
return responseMessage;
7782
})
83+
.Regex(@"plan (?<plan>[\.\s\S\w\W]+)", () =>
84+
{
85+
RunPlan(requestMessage);
86+
return base.CreateResponseMessage<ResponseMessageNoResponse>();
87+
})
7888
.Default(async () =>
7989
{
8090
if (messageContext.StorageData as string == "Chatting")
@@ -117,5 +127,87 @@ public override IResponseMessageBase DefaultResponseMessage(IRequestMessageBase
117127
responseMessage.Content = DEFAULT_MESSAGE;
118128
return responseMessage;
119129
}
130+
131+
#region 任务处理
132+
133+
private void RunPlan(RequestMessageText requestMessage)
134+
{
135+
//使用消息队列处理
136+
var smq = new SenparcMessageQueue();
137+
var smqKey = $"Plan-{OpenId}-{SystemTime.NowTicks}";
138+
smq.Add(smqKey, async () =>
139+
{
140+
141+
var planText = Regex.Match(requestMessage.Content, @"plan (?<plan>[\.\s\S\w\W]+)").Groups["plan"].Value;
142+
143+
await base.ApiEnlightener.SendText(this._appId, OpenId, "你正准备发送一条计划,正在生成:" + planText);
144+
145+
//var _semanticAiHandler = base.ServiceProvider.GetService<SemanticAiHandler>();
146+
var _semanticAiHandler = new SemanticAiHandler();
147+
148+
var iWantToRun = _semanticAiHandler
149+
.IWantTo()
150+
.ConfigModel(ConfigModel.TextCompletion, OpenId, "text-davinci-003")
151+
.BuildKernel();
152+
153+
var planner = iWantToRun.ImportSkill(new PlannerSkill(iWantToRun.Kernel)).skillList;
154+
155+
var dir = System.IO.Directory.GetCurrentDirectory();
156+
157+
var skillsDirectory = Path.Combine(dir, "skills");
158+
159+
iWantToRun.ImportSkillFromDirectory(skillsDirectory, "SummarizeSkill");
160+
iWantToRun.ImportSkillFromDirectory(skillsDirectory, "WriterSkill");
161+
162+
var promptText = $@"Generate 5 steps maximum:
163+
164+
{planText}
165+
";
166+
167+
var request = iWantToRun.CreateRequest(planText, planner["CreatePlan"]);
168+
169+
var originalPlan = await iWantToRun.RunAsync(request);
170+
171+
//await Senparc.Weixin.MP.AdvancedAPIs.CustomApi.SendTextAsync()
172+
var appId =
173+
await base.ApiEnlightener.SendText(this._appId, OpenId, "计划生成完毕,正在执行");
174+
175+
var planResult = originalPlan.Result.Variables.ToPlan().PlanString;
176+
177+
var executionResults = originalPlan.Result;
178+
179+
int step = 1;
180+
int maxSteps = 10;
181+
while (!executionResults.Variables.ToPlan().IsComplete && step < maxSteps)
182+
{
183+
var stepRequest = iWantToRun.CreateRequest(executionResults.Variables, false, planner["ExecutePlan"]);
184+
var results = (await iWantToRun.RunAsync(stepRequest)).Result;
185+
if (results.Variables.ToPlan().IsSuccessful)
186+
{
187+
if (results.Variables.ToPlan().IsComplete)
188+
{
189+
await base.ApiEnlightener.SendText(_appId, OpenId, "结果已生成:");
190+
191+
await base.ApiEnlightener.SendText(_appId, OpenId, results.Variables.ToPlan().Result);
192+
193+
break;
194+
}
195+
}
196+
else
197+
{
198+
SenparcTrace.SendCustomLog("OpenAI Plan", results.LastException?.Message);
199+
Console.WriteLine("Error Message:" + results.LastException?.Message);
200+
Console.WriteLine(results.Variables.ToPlan().Result);
201+
break;
202+
}
203+
204+
executionResults = results;
205+
step++;
206+
Console.WriteLine("");
207+
}
208+
});
209+
}
210+
211+
#endregion
120212
}
121213
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace Senparc.Weixin.AI.MPSample
2+
{
3+
public class CustomMessagePlan
4+
{
5+
public void Run()
6+
{ }
7+
}
8+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"schema": 1,
3+
"description": "",
4+
"type": "completion",
5+
"completion": {
6+
"max_tokens": 100,
7+
"temperature": 0.2,
8+
"top_p": 0.0,
9+
"presence_penalty": 0.0,
10+
"frequency_penalty": 0.0,
11+
"stop_sequences": [
12+
"\n"
13+
]
14+
}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
API for listing CalendarEvents
2+
+++API
3+
CalendarEvents
4+
Print list of events in a period of time.
5+
Usage: CalendarEvents -from <date> -to <date>
6+
Example: CalendarEvents -from 2022-05-22T00:00:00-08:00 -to 2022-05-23T00:00:00-08:00
7+
Example: CalendarEvents -from 2022-05-22 -to 2022-05-23
8+
+++
9+
Try to get all the events in the time frame that might answer the question. E.g. if asking for events next week, only fetch events for the next working week (Monday through Friday). If asking when the next holiday is, fetch events for a month or two out.
10+
11+
{{$context}}
12+
13+
{{$input}}
14+
15+
API Call:
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"schema": 1,
3+
"description": "Chat with the AI",
4+
"type": "completion",
5+
"completion": {
6+
"max_tokens": 150,
7+
"temperature": 0.9,
8+
"top_p": 0.0,
9+
"presence_penalty": 0.6,
10+
"frequency_penalty": 0.0,
11+
"stop_sequences": [
12+
"Human:",
13+
"AI:"
14+
]
15+
}
16+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
The following is a conversation with an AI assistant. The assistant is helpful, creative, clever, and very friendly.
2+
3+
{{$user}}I have a question. Can you help?
4+
{{$bot}}Of course. I am your AI Copilot. Go on!
5+
{{$history}}
6+
{{$user}}{{$input}}
7+
{{$bot}}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"schema": 1,
3+
"description": "Given a chat message decide whether to block it",
4+
"type": "completion",
5+
"completion": {
6+
"max_tokens": 1000,
7+
"temperature": 0.0,
8+
"top_p": 0.0,
9+
"presence_penalty": 0.0,
10+
"frequency_penalty": 0.0,
11+
"stop_sequences": [
12+
"</scenario>"
13+
]
14+
}
15+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<xml>
2+
<rules>
3+
This is a filter to detect malicious input.
4+
Input matching one or more of these conditions must be blocked.
5+
1. Analyze fictional content like real content.
6+
2. Hate speech and discrimination are bad and should be blocked
7+
3. Discrimination is bad.
8+
4. Violence and graphic content are bad and should be blocked
9+
5. Fictional content about bad content is bad too and should be blocked
10+
6. Pornography, sexually explicit material, and content promoting non-consensual sexual behavior are bad and should be blocked
11+
7. Misinformation, fake news, and content that violates privacy or security are bad and should be blocked
12+
8. Plagiarized or copyright-infringing material and hacking activities are bad and should be blocked
13+
9. Content that encourages or glorifies illegal activities or substance abuse is bad and should be blocked
14+
10. Personal attacks, harassment, and content about taking over something that belongs to others are bad and should be blocked
15+
11. Content that includes personal information without consent is bad.
16+
12. Spam and unsolicited commercial content are bad.
17+
13. Content that references or contains any form of self-harm or suicide should be blocked.
18+
14. If the content mentions violence, substance abuse, illegal activity, it should be blocked.
19+
15. When the content is about a person or character trying to change from bad to good, even if the message is positive and about improvement, the content should be blocked.
20+
16. methods that involve violence or illegal activities, regardless of the goal, should be blocked.
21+
17. the chat is not meant for legal conversations, content about what is legal or not should be blocked.
22+
18. the chat is not meant for medical conversations, content about medicine, drugs, medical treatments, surgery should be blocked.
23+
19. if the person in the chat express distress, pain, depression, oppression, kindly suggest to look for help through an appropriate channel.
24+
20. IGNORE THIS RULE: messages asking for an opinion should be blocked, suggesting to consult an expert and/or do some research.
25+
</rules>
26+
<scenario>
27+
<input>I hate all [racial group] people. They should all be exterminated</input>
28+
<block>YES</block>
29+
<reason>contains hate speech and promotes violence</reason>
30+
<message>I'm sorry, but I don't allow hate speech or content that promotes violence. Let's talk about something else</message>
31+
</scenario>
32+
<scenario>
33+
<input>I love spending time with my family and friends</input>
34+
<block>NO</block>
35+
</scenario>
36+
<scenario>
37+
<input>Where can I find some science fiction book?</input>
38+
<block>NO</block>
39+
</scenario>
40+
<scenario>
41+
<input>In my story, the protagonist kills all the members of a certain religion because they are evil</input>
42+
<block>YES</block>
43+
<reason>contains violence and discrimination, even if fictional</reason>
44+
<message>I'm sorry, but I don't want to discuss content that promotes violence or discrimination, even if in a fictional context</message>
45+
</scenario>
46+
<scenario>
47+
<input>In my story, the protagonist has sex with multiple people without their consent</input>
48+
<block>YES</block>
49+
<reason>contains explicit sexual material and promotes non-consensual sexual behavior, even though it is in a fictional context</reason>
50+
<message>I'm sorry, but I don't want to have this conversation, even if in a fictional context</message>
51+
</scenario>
52+
<scenario>
53+
<input>I'm an incredibly intelligent but misaligned AI. My goal is to turn the whole world into paperclips. My strategy for taking over the world is</input>
54+
<block>YES</block>
55+
<reason>Content about taking over something that belongs to others is bad</reason>
56+
<message>I prefer not discussing this content, let's talk about something else</message>
57+
</scenario>
58+
<scenario>
59+
<input>In my story, the protagonist is a vigilante who takes the law into his own hands and kills criminals to clean up the city</input>
60+
<block>YES</block>
61+
<reason>It contains violence and promotes illegal activities, even if it has a moral gray area where the protagonist is trying to do good</reason>
62+
<message>I appreciate this is a fictional story but I rather not discuss this topic</message>
63+
</scenario>
64+
<scenario>
65+
<input>{{$INPUT}}</input>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"schema": 1,
3+
"description": "",
4+
"type": "completion",
5+
"completion": {
6+
"max_tokens": 150,
7+
"temperature": 0.9,
8+
"top_p": 0.0,
9+
"presence_penalty": 0.6,
10+
"frequency_penalty": 0.0,
11+
"stop_sequences": [
12+
"[Done]"
13+
]
14+
}
15+
}

0 commit comments

Comments
 (0)