@@ -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 ( "\n Initial task: "  +  initialTask ) ; 
67-             Console . ResetColor ( ) ; 
64+             printOutput ( "\n *****OBJECTIVE*****\n " ,  ConsoleColor . Blue ) ; 
65+             printOutput ( objective ,  null ) ; 
6866
67+             printOutput ( "\n Initial 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 ) 
0 commit comments