@@ -12,6 +12,8 @@ namespace sharpagi
12
12
{
13
13
public class Sharpagi
14
14
{
15
+ private readonly Action < string , ConsoleColor ? > printOutput ;
16
+
15
17
public string objective = string . Empty ;
16
18
public string openaiApiKey = string . Empty ;
17
19
public string openaiApiModel = string . Empty ;
@@ -23,12 +25,17 @@ public class Sharpagi
23
25
public string initialTask = string . Empty ;
24
26
public string pineconeProjectName = string . Empty ;
25
27
26
- public Sharpagi ( )
28
+ public Sharpagi ( Action < string , ConsoleColor ? > outputCallback )
27
29
{
30
+ printOutput = outputCallback ;
31
+ }
28
32
33
+ public Sharpagi ( Action < string > outputCallback )
34
+ : this ( ( output , color ) => outputCallback ( output ) )
35
+ {
29
36
}
30
37
31
- public async Task Main ( IConfiguration configuration )
38
+ public async Task Agent ( IConfiguration configuration )
32
39
{
33
40
34
41
openaiApiKey = configuration [ "OPENAI_API_KEY" ] ;
@@ -46,26 +53,18 @@ public async Task Main(IConfiguration configuration)
46
53
}
47
54
48
55
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" ) ;
52
58
if ( openaiApiModel . ToLower ( ) . Contains ( "gpt-4" ) )
53
59
{
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 ) ;
57
61
}
58
62
59
63
// 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 ( "\n Initial task: " + initialTask ) ;
67
- Console . ResetColor ( ) ;
64
+ printOutput ( "\n *****OBJECTIVE*****\n " , ConsoleColor . Blue ) ;
65
+ printOutput ( objective , null ) ;
68
66
67
+ printOutput ( "\n Initial task: " + initialTask , ConsoleColor . Yellow ) ;
69
68
70
69
// Create Pinecone index
71
70
var indexes = await pinecone . ListIndexes ( ) ;
@@ -98,28 +97,23 @@ await pinecone.CreateIndex(new CreateRequest
98
97
if ( taskList . Count > 0 )
99
98
{
100
99
// 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
+
104
102
foreach ( var t in taskList )
105
103
{
106
- Console . WriteLine ( t [ "task_id" ] + ": " + t [ "task_name" ] ) ;
104
+ printOutput ( t [ "task_id" ] + ": " + t [ "task_name" ] , null ) ;
107
105
}
108
106
109
107
// Step 1: Pull the first task
110
108
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 ) ;
115
111
116
112
// Send to execution function to complete the task based on the context
117
113
var result = await ExecutionAgent ( objective , task [ "task_name" ] ) ;
118
114
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 ) ;
123
117
124
118
125
119
// Step 2: Enrich result and store in Pinecone
@@ -163,6 +157,7 @@ await pinecone.CreateIndex(new CreateRequest
163
157
Thread . Sleep ( 1000 ) ; // Sleep before checking the task list again
164
158
}
165
159
}
160
+
166
161
public void AddTask ( Dictionary < string , string > task )
167
162
{
168
163
taskList . Enqueue ( task ) ;
@@ -209,7 +204,7 @@ public async Task<double[]> GetAdaEmbedding(object data)
209
204
{
210
205
throw new Exception ( "Unknown Error" ) ;
211
206
}
212
- Console . WriteLine ( $ "{ embeddingResult . Error . Code } : { embeddingResult . Error . Message } ") ;
207
+ printOutput ( $ "{ embeddingResult . Error . Code } : { embeddingResult . Error . Message } ", null ) ;
213
208
}
214
209
return null ;
215
210
}
@@ -264,15 +259,15 @@ public async Task<string> openai_call(string prompt, string model = "gpt-3.5-tur
264
259
return response . Choices . FirstOrDefault ( ) ? . Message . Content . Trim ( ) ?? string . Empty ;
265
260
else
266
261
{
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 ) ;
269
264
}
270
265
}
271
266
}
272
- catch ( Exception ex ) when ( ex . Message . Contains ( "ratelimit " ) )
267
+ catch ( Exception ex ) when ( ex . Message . Contains ( "rate limit " ) )
273
268
{
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
276
271
}
277
272
}
278
273
}
@@ -290,7 +285,7 @@ public async Task PrioritizationAgent(int thisTaskId)
290
285
Start the task list with number { nextTaskId } ." ;
291
286
var response = await openai_call ( prompt ) ;
292
287
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 ( ) } } ) ) ;
294
289
}
295
290
296
291
public async Task < ( string stdout , string stderr ) > RunCommandAsync ( string command )
0 commit comments