diff --git a/LlamaCli.func.php b/LlamaCli.func.php index bf0800f..e51bcdd 100644 --- a/LlamaCli.func.php +++ b/LlamaCli.func.php @@ -1,8 +1,10 @@ "assistant","content"=>"How can I help you?"],["role"=>"user","content"=>"Tell me the capital of France"]] $debug_level = $debug_level ?? 0; + $context_size = $context_size ?? 4096; + // 1. Parse the prompt and resolve actions "@" - $prompt = ResolveActions($prompt); + $prompt = ExpandFetcherCommands($prompt); // $llm_stdin = $prompt; @@ -40,12 +44,18 @@ function LlamaCli(string $prompt, string $system = "You are a helpful assistant" - $command = "$llamacpp_bin -m $llamacpp_model_path/$llamacpp_model -f $input_file_name -no-cnv --simple-io 2> $debug_file_name "; + $command = "$llamacpp_bin \ + -m $llamacpp_model_path/$llamacpp_model \ + -f $input_file_name \ + -c $context_size \ + -no-cnv \ + --simple-io \ + 2> $debug_file_name "; if ($debug_level >= 3) file_put_contents("php://stderr","Attempting command: $command\n"); $output = shell_exec($command); - // + // Save the DEBUG data from the LLM $debug_data = file_get_contents($debug_file_name); if ($debug_level >= 5) file_put_contents("php://stderr","debug_file_name $debug_file_name content: $debug_data\n"); @@ -74,4 +84,4 @@ function LlamaCli(string $prompt, string $system = "You are a helpful assistant" return $output; } -function LlamaCli_raw(string $prompt, string $system, array $options = []) { $options["parseCodeInOutput"] = false; return LlamaCli($prompt, $system,$options); } +function LlamaCli_raw(string $prompt, string $system = "You are a helpful assistant", array $options = []) { $options["parseCodeInOutput"] = false; return LlamaCli($prompt, $system,$options); } diff --git a/lib/ResolveActions.function.php b/lib/ResolveActions.function.php deleted file mode 100644 index 63e1d5f..0000000 --- a/lib/ResolveActions.function.php +++ /dev/null @@ -1,52 +0,0 @@ -function_name = $s; return $this; } - public function setArgumentsTypes(array $a) { $this->args_type = $a; $this->no_of_args = count($a); return $this; } - public function getExpectedNoOfArguments() { return $this->no_of_args; } - public function getFunctionName() { return $this->function_name; } -} -function registerAction(Action $a) { global $actions; $actions[$a->getFunctionName()] = $a; return true;} -// Register all actions available -require_once __DIR__."/actions/index.php"; - -function ResolveActions(string $text) { - global $actions; - - $function_name_regex_format = "@([a-zA-Z_][a-zA-Z0-9_]+)"; - $arguments_regex_format = "([\w:\/\.-]+)"; - - - if (preg_match("/$function_name_regex_format( |$)/",$text,$matches)) { - $function_name = $matches[1]; - - if ( !isset($actions[$function_name]) || - !function_exists($function_name )) return "[[ Action $function_name not available. ]]"; - - $action = $actions[$function_name]; - - // Fetch the arguments - $reg_exp = array_fill(0,$action->getExpectedNoOfArguments(),"([\w:\/\.-]+)"); - array_unshift($reg_exp, $function_name_regex_format); - - if (!preg_match("/".implode(" ",$reg_exp)."/",$text,$matches)) - return "[[ Action $function_name was passed with wrong number of arguemnts. Expected: ".$a->getExpectedNoOfArguments()." ]]"; - - $full_action_requested_string = $matches[0]; - - array_shift($matches); // Clip the first whole-string result - array_shift($matches); // Clip the function name - $arguments = $matches; - $actionResult = call_user_func_array($function_name, $arguments); - - $text = str_replace($full_action_requested_string,$actionResult,$text); - } - - return $text; - -} diff --git a/test.php b/test.php index 95a53d3..b75213b 100644 --- a/test.php +++ b/test.php @@ -1,74 +1,2 @@ [["role"=>"user","content"=>"What is the capital of France?"],["role"=>"assistant","content"=>"Paris."]]]); print_r($out); - - -// TEST 03: Simple query with SYSTEM PRIMING message - -//$summary = LlamaCli(file_get_contents("test_input_data.3.txt"),"You are an executive assistant that will summarize english text in 1 paragraph"); -//$title = LlamaCli($summary[0],"You are an talented journalist that will produce a provocative headline title based on a summary of a text"); - - -// TEST 04: RECURSIVE QUERY, to produce long-content from short-one. LONG-WRITING. ( GOAL: Learning from model deep knowledge ) - -// // -------- BOOK EXAMPLE --------- -// -// // 0. CATEGORY -// $category = "learning finnish"; -// $expert = "teacher"; -// $total_no_of_chapters = 3; -// -// -// // 1. TITLE -// $book_title = LlamaCli_raw("Write me a title for my new book about $category. Output title only, no options, no chat.","You are an expert $expert."); -// -// -// // 2. CHAPTER TITLES -// $sys_msg = "You are an expert $expert writing a book with the title $book_title with $total_no_of_chapters chapters"; -// $chapters = []; $conv_hist = []; -// -// $msg = "Write me the title of my first chapter. Output title only, no options, no chat."; -// $conv_hist[] = ["role"=>"user","content"=>$msg]; -// $conv_hist[] = ["role"=>"assistant","content"=>($chapters[] = LlamaCli_raw($msg,$sys_msg,["debug_level"=>0]))]; -// -// for($no=1; $no < $total_no_of_chapters; $no++) { -// $msg = "Write me the title of chapter number $no."; -// $conv_hist[] = ["role"=>"user","content"=>$msg]; -// $conv_hist[] = ["role"=>"assistant","content"=>($chapters[] = LlamaCli_raw($msg,$sys_msg,["previousConversation"=>$conv_hist, "debug_level"=>0]))]; -// -// } -// -// // 3. CHAPTER CONTENTS -// $content = []; -// foreach($chapters as $chapter_title) -// $content[$chapter_title] = LlamaCli_raw( -// "Write 2 paragraphs for a chapter titled $chapter_title in a book called $book_title. Output content only, no chat.", -// "You are an expert $expert." -// ); -// -// print_r([$book_title, $content]); - - - -// TEST 05: CHAINED QUERY, to parse long-content and produce short one. SUMMARIZATION. ( GOAL: ) - - -// $summaries = []; -// $content = file_get_contents("test_input_data.2.txt"); -// $chunks = array_chunk(explode("\n",$content),80); -// -// foreach($chunks as $chunk) { -// $summaries[] = LlamaCli_raw(implode("\n",$chunk),"You are a legal executive assistant that will summarize a conversation in english between ex-husband and wife in 1 paragraph"); -// } -// print_r($summaries); - - -// TEST 06: Resolve ACTIONS - -echo LlamaCli_raw("translate this title to portuguese and finnish: @rss_reader https://feeds.bbci.co.uk/news/rss.xml cde def",""); diff --git a/tests/02_previous_background_info.php b/tests/02_previous_background_info.php index e3638f5..9c8b8b5 100644 --- a/tests/02_previous_background_info.php +++ b/tests/02_previous_background_info.php @@ -3,7 +3,20 @@ // TEST 02: Simple query with previous background conversation -$out = LlamaCli("Portugal?","",["previousConversation"=>[["role"=>"user","content"=>"What is the capital of France?"],["role"=>"assistant","content"=>"Paris."]]]); +$out = LlamaCli("Portugal?","", + [ + "previousConversation"=>[ + [ + "role"=>"user", + "content"=>"What is the capital of France?" + ], + [ + "role"=>"assistant", + "content"=>"Paris." + ] + ] + ] +); print_r($out); diff --git a/tests/03_system_major_role.php b/tests/03_system_major_role.php index ffe699e..9cb740d 100644 --- a/tests/03_system_major_role.php +++ b/tests/03_system_major_role.php @@ -1,6 +1,6 @@ 8192] + ); } print_r($summaries); diff --git a/tests/06_ExpandFetcherCommands.php b/tests/06_ExpandFetcherCommands.php new file mode 100644 index 0000000..c8383c0 --- /dev/null +++ b/tests/06_ExpandFetcherCommands.php @@ -0,0 +1,16 @@ +