Skip to content

Commit

Permalink
更新使用案例
Browse files Browse the repository at this point in the history
支持修改prompt
优化提示词
依赖SK
  • Loading branch information
239573049 committed Oct 30, 2024
1 parent a29a44f commit da750e1
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<VersionSuffix>8</VersionSuffix>
<VersionSuffix>9</VersionSuffix>
<VersionPrefix>1.0</VersionPrefix>
<Version>$(VersionPrefix).$(VersionSuffix)</Version>
<Authors>AIDotNet</Authors>
Expand Down
2 changes: 1 addition & 1 deletion src/AutoGpt.Tests/AutoGpt.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.2" />
</ItemGroup>

<ItemGroup>
Expand Down
28 changes: 26 additions & 2 deletions src/AutoGpt.Tests/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,34 @@

var autoGptClient = serviceProvider.GetRequiredService<AutoGptClient>();

const string prompt =
"""
如果一只蜗牛白天爬上 10 英尺高的杆子,然后晚上从 6 英尺高的杆子上滑下来,那么蜗牛需要多少天才能到达顶端?
""";
// 周长为 18 英尺。 (矩形周长的公式为 P = 2 *(长度 + 宽度)。在本例中,P = 2 * (4 + 5) = 2 * 9 = 18 英尺。)
var sw = Stopwatch.StartNew();
await foreach (var make in autoGptClient.GenerateResponseAsync(
"使用c#设计工厂模式,并且给出代码案例", "", "gpt-4o-2024-05-13",
2000))
prompt, "", "gpt-4o",
500, promptHandler: (pairs) =>
{
foreach (KeyValuePair<string, string> pair in pairs)
{
if (pair.Key == "system")
{
pairs[pair.Key] =
"""
You are an expert AI assistant that explains your reasoning step by step. For each step, provide a title that describes what you're doing in that step, along with the content. Decide if you need another step or if you're ready to give the final answer. Respond in JSON format with 'title', 'content', and 'next_action' (either 'continue' or 'final_answer') keys. USE AS MANY REASONING STEPS AS POSSIBLE. AT LEAST 3. BE AWARE OF YOUR LIMITATIONS AS AN LLM AND WHAT YOU CAN AND CANNOT DO. IN YOUR REASONING, INCLUDE EXPLORATION OF ALTERNATIVE ANSWERS. CONSIDER YOU MAY BE WRONG, AND IF YOU ARE WRONG IN YOUR REASONING, WHERE IT WOULD BE. FULLY TEST ALL OTHER POSSIBILITIES. YOU CAN BE WRONG. WHEN YOU SAY YOU ARE RE-EXAMINING, ACTUALLY RE-EXAMINE, AND USE ANOTHER APPROACH TO DO SO. DO NOT JUST SAY YOU ARE RE-EXAMINING. USE AT LEAST 3 METHODS TO DERIVE THE ANSWER. USE BEST PRACTICES.
Example of a valid JSON response:
```json
{
"title": "Identifying Key Information",
"content": "To begin solving this problem, we need to carefully examine the given information and identify the crucial elements that will guide our solution process. This involves...",
"next_action": "continue"
}```
""";
}
}
}))
{
if (make.Type == MakeResultDto.MakeResultType.FinalAnswer)
{
Expand Down
9 changes: 4 additions & 5 deletions src/AutoGpt/AutoGpt.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Azure.AI.OpenAI" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="8.0.0" />
<PackageReference Include="OpenAI" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.2" />
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.Options" Version="8.0.2" />
<PackageReference Include="Microsoft.SemanticKernel" Version="1.25.0" />
</ItemGroup>

</Project>
12 changes: 9 additions & 3 deletions src/AutoGpt/AutoGptClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@ public class AutoGptClient(
/// <param name="apiKey"></param>
/// <param name="model"></param>
/// <param name="maxToken"></param>
/// <param name="promptHandler">提示词处理回调</param>
/// <returns></returns>
public async IAsyncEnumerable<MakeResultDto> GenerateResponseAsync(
string prompt, string apiKey, string model, int maxToken = 800, float? temperature = null)
string prompt, string apiKey, string model, int maxToken = 800, float? temperature = null,
Action<Dictionary<string, string>>? promptHandler = null)
{
var chat = new List<ChatMessage> { ChatMessage.CreateUserMessage(prompt) };

await foreach (var item in GenerateResponseAsync(chat, apiKey, model, maxToken, temperature))
await foreach (var item in GenerateResponseAsync(chat, apiKey, model, maxToken, temperature,promptHandler))
{
yield return item;
}
Expand All @@ -55,9 +57,13 @@ public async IAsyncEnumerable<MakeResultDto> GenerateResponseAsync(
/// <param name="temperature"></param>
/// <returns></returns>
public async IAsyncEnumerable<MakeResultDto> GenerateResponseAsync(
List<ChatMessage> chatMessages, string apiKey, string model, int maxToken = 800, float? temperature = null)
List<ChatMessage> chatMessages, string apiKey, string model, int maxToken = 800, float? temperature = null,
Action<Dictionary<string, string>>? promptHandler = null)
{
var chatHistory = new List<ChatMessage>();

promptHandler?.Invoke(promptManager.Prompts);

foreach (var item in promptManager.Prompts)
{
switch (item.Key)
Expand Down

0 comments on commit da750e1

Please sign in to comment.