Skip to content

Commit 2626ce8

Browse files
committed
Sharpagi library and test project changes
1 parent 86a27dd commit 2626ce8

File tree

7 files changed

+128
-76
lines changed

7 files changed

+128
-76
lines changed

Program.cs

Lines changed: 0 additions & 32 deletions
This file was deleted.

README.md

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,35 @@ SharpAGI is a C# library based on the BabyAGI project by [@yoheinakajima].
66

77
## Quickstart
88

9-
Use these settings in user secrets to run the code.
9+
To create a Sharpagi client to run the agent in console application
10+
11+
```csharp
12+
Sharpagi sharpagi = new Sharpagi((output,consolecolor) =>
13+
{
14+
if (consolecolor.HasValue)
15+
{
16+
Console.ForegroundColor = consolecolor.Value;
17+
}
18+
Console.WriteLine(output);
19+
if (consolecolor.HasValue)
20+
{
21+
Console.ResetColor();
22+
}
23+
});
24+
await sharpagi.Agent(configuration);
25+
```
26+
27+
To cerate a Sharpagi client in other application types
28+
```csharp
29+
StringBuilder printOutput = new StringBuilder();
30+
Sharpagi sharpagi = new Sharpagi((output) =>
31+
{
32+
printOutput.AppendLine(output);
33+
});
34+
await sharpagi.Agent(configuration);
35+
```
36+
37+
Use sharpagi_test console application to test the agi agent with this user secret
1038

1139
```csharp
1240
{
@@ -23,8 +51,8 @@ Use these settings in user secrets to run the code.
2351
}
2452
```
2553

26-
License
54+
## License
2755
This plugin is based on the BabyAGI project by @yoheinakajima (https://github.com/yoheinakajima). Please refer to their repository for licensing information.
2856

29-
Acknowledgments
57+
## Acknowledgments
3058
This plugin is based on the BabyAGI project by [@yoheinakajima] A big thank you to the author for their original work.

sharpagi.sln

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,24 @@ Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio Version 17
44
VisualStudioVersion = 17.5.33530.505
55
MinimumVisualStudioVersion = 10.0.40219.1
6-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "sharpagi", "sharpagi.csproj", "{4D2045ED-8623-41EC-9834-5ED038ED9EFF}"
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "sharpagi_test", "sharpagi_test\sharpagi_test.csproj", "{2E6C0B34-0CF8-4FEF-BAED-33B2DF419E2B}"
7+
EndProject
8+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "sharpagi", "sharpagi\sharpagi.csproj", "{7257BE27-97E9-4407-BB2E-5E13D7071005}"
79
EndProject
810
Global
911
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1012
Debug|Any CPU = Debug|Any CPU
1113
Release|Any CPU = Release|Any CPU
1214
EndGlobalSection
1315
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14-
{4D2045ED-8623-41EC-9834-5ED038ED9EFF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15-
{4D2045ED-8623-41EC-9834-5ED038ED9EFF}.Debug|Any CPU.Build.0 = Debug|Any CPU
16-
{4D2045ED-8623-41EC-9834-5ED038ED9EFF}.Release|Any CPU.ActiveCfg = Release|Any CPU
17-
{4D2045ED-8623-41EC-9834-5ED038ED9EFF}.Release|Any CPU.Build.0 = Release|Any CPU
16+
{2E6C0B34-0CF8-4FEF-BAED-33B2DF419E2B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17+
{2E6C0B34-0CF8-4FEF-BAED-33B2DF419E2B}.Debug|Any CPU.Build.0 = Debug|Any CPU
18+
{2E6C0B34-0CF8-4FEF-BAED-33B2DF419E2B}.Release|Any CPU.ActiveCfg = Release|Any CPU
19+
{2E6C0B34-0CF8-4FEF-BAED-33B2DF419E2B}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{7257BE27-97E9-4407-BB2E-5E13D7071005}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{7257BE27-97E9-4407-BB2E-5E13D7071005}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{7257BE27-97E9-4407-BB2E-5E13D7071005}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{7257BE27-97E9-4407-BB2E-5E13D7071005}.Release|Any CPU.Build.0 = Release|Any CPU
1824
EndGlobalSection
1925
GlobalSection(SolutionProperties) = preSolution
2026
HideSolutionNode = FALSE

sharpagi.cs renamed to sharpagi/sharpagi.cs

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ namespace sharpagi
1212
{
1313
public class Sharpagi
1414
{
15+
private readonly Action<string, ConsoleColor?> printOutput;
16+
1517
public string objective = string.Empty;
1618
public string openaiApiKey = string.Empty;
1719
public string openaiApiModel = string.Empty;
@@ -23,12 +25,17 @@ public class Sharpagi
2325
public string initialTask = string.Empty;
2426
public string pineconeProjectName = string.Empty;
2527

26-
public Sharpagi()
28+
public Sharpagi(Action<string, ConsoleColor?> outputCallback)
2729
{
30+
printOutput = outputCallback;
31+
}
2832

33+
public Sharpagi(Action<string> outputCallback)
34+
: this((output, color) => outputCallback(output))
35+
{
2936
}
3037

31-
public async Task Main(IConfiguration configuration)
38+
public async Task Agent(IConfiguration configuration)
3239
{
3340

3441
openaiApiKey = configuration["OPENAI_API_KEY"];
@@ -46,26 +53,18 @@ public async Task Main(IConfiguration configuration)
4653
}
4754

4855

49-
// Check if we know what we are doing
50-
Debug.Assert(!string.IsNullOrEmpty(openaiApiKey), "OPENAI_API_KEY environment variable is missing from UserSecrets");
51-
Debug.Assert(!string.IsNullOrEmpty(openaiApiModel), "OPENAI_API_MODEL environment variable is missing from UserSecrets");
56+
if(string.IsNullOrEmpty(openaiApiKey)) throw new ArgumentException("OPENAI_API_KEY environment variable is missing from UserSecrets");
57+
if(string.IsNullOrEmpty(openaiApiModel)) throw new ArgumentException("OPENAI_API_MODEL environment variable is missing from UserSecrets");
5258
if (openaiApiModel.ToLower().Contains("gpt-4"))
5359
{
54-
Console.ForegroundColor = ConsoleColor.Red;
55-
Console.WriteLine("*****USING GPT-4. POTENTIALLY EXPENSIVE. MONITOR YOUR COSTS*****");
56-
Console.ResetColor();
60+
printOutput("*****USING GPT-4. POTENTIALLY EXPENSIVE. MONITOR YOUR COSTS*****", ConsoleColor.Red);
5761
}
5862

5963
// Print OBJECTIVE
60-
Console.ForegroundColor = ConsoleColor.Blue;
61-
Console.WriteLine("\n*****OBJECTIVE*****\n");
62-
Console.ResetColor();
63-
Console.WriteLine(objective);
64-
65-
Console.ForegroundColor = ConsoleColor.Yellow;
66-
Console.WriteLine("\nInitial task: " + initialTask);
67-
Console.ResetColor();
64+
printOutput("\n*****OBJECTIVE*****\n", ConsoleColor.Blue);
65+
printOutput(objective, null);
6866

67+
printOutput("\nInitial task: " + initialTask, ConsoleColor.Yellow);
6968

7069
// Create Pinecone index
7170
var indexes = await pinecone.ListIndexes();
@@ -98,28 +97,23 @@ await pinecone.CreateIndex(new CreateRequest
9897
if (taskList.Count > 0)
9998
{
10099
// Print the task list
101-
Console.ForegroundColor = ConsoleColor.Magenta;
102-
Console.WriteLine("\n*****TASK LIST*****\n");
103-
Console.ResetColor();
100+
printOutput("\n*****TASK LIST*****\n", ConsoleColor.Magenta);
101+
104102
foreach (var t in taskList)
105103
{
106-
Console.WriteLine(t["task_id"] + ": " + t["task_name"]);
104+
printOutput(t["task_id"] + ": " + t["task_name"], null);
107105
}
108106

109107
// Step 1: Pull the first task
110108
var task = taskList.Dequeue();
111-
Console.ForegroundColor = ConsoleColor.Green;
112-
Console.WriteLine("\n*****NEXT TASK*****\n");
113-
Console.ResetColor();
114-
Console.WriteLine(task["task_id"] + ": " + task["task_name"]);
109+
printOutput("\n*****NEXT TASK*****\n", ConsoleColor.Green);
110+
printOutput(task["task_id"] + ": " + task["task_name"], null);
115111

116112
// Send to execution function to complete the task based on the context
117113
var result = await ExecutionAgent(objective, task["task_name"]);
118114
var thisTaskId = int.Parse(task["task_id"]);
119-
Console.ForegroundColor = ConsoleColor.Yellow;
120-
Console.WriteLine("\n*****TASK RESULT*****\n");
121-
Console.ResetColor();
122-
Console.WriteLine(result);
115+
printOutput("\n*****TASK RESULT*****\n", ConsoleColor.Yellow);
116+
printOutput(result?.ToString(), null);
123117

124118

125119
// Step 2: Enrich result and store in Pinecone
@@ -163,6 +157,7 @@ await pinecone.CreateIndex(new CreateRequest
163157
Thread.Sleep(1000); // Sleep before checking the task list again
164158
}
165159
}
160+
166161
public void AddTask(Dictionary<string, string> task)
167162
{
168163
taskList.Enqueue(task);
@@ -209,7 +204,7 @@ public async Task<double[]> GetAdaEmbedding(object data)
209204
{
210205
throw new Exception("Unknown Error");
211206
}
212-
Console.WriteLine($"{embeddingResult.Error.Code}: {embeddingResult.Error.Message}");
207+
printOutput($"{embeddingResult.Error.Code}: {embeddingResult.Error.Message}", null);
213208
}
214209
return null;
215210
}
@@ -264,15 +259,15 @@ public async Task<string> openai_call(string prompt, string model = "gpt-3.5-tur
264259
return response.Choices.FirstOrDefault()?.Message.Content.Trim() ?? string.Empty;
265260
else
266261
{
267-
if (response.Error.Message.Contains("ratelimit", StringComparison.OrdinalIgnoreCase))
268-
await Task.Delay(10000);
262+
if (response.Error.Message.Contains("rate limit", StringComparison.OrdinalIgnoreCase))
263+
await Task.Delay(20000);
269264
}
270265
}
271266
}
272-
catch (Exception ex) when (ex.Message.Contains("ratelimit"))
267+
catch (Exception ex) when (ex.Message.Contains("rate limit"))
273268
{
274-
Console.WriteLine("The OpenAI API rate limit has been exceeded. Waiting 10 seconds and trying again.");
275-
await Task.Delay(10000); // Wait 10 seconds and try again
269+
printOutput("The OpenAI API rate limit has been exceeded. Waiting 10 seconds and trying again.", null);
270+
await Task.Delay(20000); // Wait 10 seconds and try again
276271
}
277272
}
278273
}
@@ -290,7 +285,7 @@ public async Task PrioritizationAgent(int thisTaskId)
290285
Start the task list with number {nextTaskId}.";
291286
var response = await openai_call(prompt);
292287
var newTasks = response.Split('\n');
293-
taskList = new Queue<Dictionary<string, string>>(newTasks.Select((t, index) => new Dictionary<string, string> { { "task_id", (index + nextTaskId).ToString() }, { "task_name", (t.Trim().Length>=2)?t.Trim().Substring(2):t.Trim() } }));
288+
taskList = new Queue<Dictionary<string, string>>(newTasks.Select((t, index) => new Dictionary<string, string> { { "task_id", (index + nextTaskId).ToString() }, { "task_name", (t.Trim().Length >= 2) ? t.Trim().Substring(2) : t.Trim() } }));
294289
}
295290

296291
public async Task<(string stdout, string stderr)> RunCommandAsync(string command)

sharpagi.csproj renamed to sharpagi/sharpagi.csproj

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<OutputType>Exe</OutputType>
4+
<OutputType>Library</OutputType>
55
<TargetFramework>net6.0</TargetFramework>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Nullable>enable</Nullable>
88
<UserSecretsId>50c01620-9aba-4847-9fa1-0f06066b9d14</UserSecretsId>
99
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<Compile Remove="sharpagi\**" />
13+
<Compile Remove="sharpagi_test\**" />
14+
<EmbeddedResource Remove="sharpagi\**" />
15+
<EmbeddedResource Remove="sharpagi_test\**" />
16+
<None Remove="sharpagi\**" />
17+
<None Remove="sharpagi_test\**" />
18+
</ItemGroup>
1019

1120
<ItemGroup>
1221
<PackageReference Include="Betalgo.OpenAI" Version="6.8.5" />

sharpagi_test/Program.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// See https://aka.ms/new-console-template for more information
2+
using Microsoft.Extensions.Configuration;
3+
using sharpagi;
4+
5+
Console.WriteLine("Hello, World!");
6+
// Load environment variables from UserSecrets in Visual Studio
7+
var builder = new ConfigurationBuilder()
8+
.AddUserSecrets<Program>();
9+
var configuration = builder.Build();
10+
11+
Sharpagi sharpagi = new Sharpagi((output, consolecolor) =>
12+
{
13+
if (consolecolor.HasValue)
14+
{
15+
Console.ForegroundColor = consolecolor.Value;
16+
}
17+
Console.WriteLine(output);
18+
if (consolecolor.HasValue)
19+
{
20+
Console.ResetColor();
21+
}
22+
});
23+
await sharpagi.Agent(configuration);

sharpagi_test/sharpagi_test.csproj

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
<UserSecretsId>79855906-63dd-4e12-b308-95501dab288a</UserSecretsId>
9+
</PropertyGroup>
10+
11+
12+
<ItemGroup>
13+
<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.1" />
14+
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="6.0.1" />
15+
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.1" />
16+
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
17+
</ItemGroup>
18+
19+
20+
<ItemGroup>
21+
<ProjectReference Include="..\sharpagi\sharpagi.csproj" />
22+
</ItemGroup>
23+
</Project>

0 commit comments

Comments
 (0)