IMPROVEMENT: changed structure of tests

This commit is contained in:
Frederico @ VilaRosa02 2025-09-11 10:58:00 +00:00
parent 436e0e57c5
commit 06392538b8
8 changed files with 52 additions and 133 deletions

View File

@ -1,8 +1,10 @@
<?php
require_once __DIR__."/lib/index.php";
$debug_level = 0;
function LlamaCli(string $prompt, string $system = "You are a helpful assistant", array $options = []) {
global $debug_level;
// Get the options passed
extract($options);
@ -17,8 +19,10 @@ function LlamaCli(string $prompt, string $system = "You are a helpful assistant"
$previousConversation = $previousConversation ?? []; // expects: [["role"=>"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); }

View File

@ -1,52 +0,0 @@
<?php
$actions = [];
class Action {
private $function_name;
private $no_of_args;
private $args_type;
public function setFunctionName(string $s) { $this->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;
}

View File

@ -1,74 +1,2 @@
<?php require_once __DIR__."/LlamaCli.func.php";
// TEST 01: Simple query.
// $out = LlamaCli("What is the capital of France?"); print_r($out);
// 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."]]]); 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","");

View File

@ -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);

View File

@ -1,6 +1,6 @@
<?php require_once __DIR__."/../LlamaCli.func.php";
// TEST 03: Simple query with SYSTEM PRIMING message
// TEST 03: Simple query with SYSTEM PRIMING message, and chaining
$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");

View File

@ -1,6 +1,6 @@
<?php require_once __DIR__."/../LlamaCli.func.php";
TEST 04: RECURSIVE QUERY, to produce long-content from short-one. LONG-WRITING. ( GOAL: Learning from model deep knowledge )
// TEST 04: RECURSIVE QUERY, to produce long-content from short-one. LONG-WRITING. ( GOAL: Learning from model deep knowledge )
// -------- BOOK EXAMPLE ---------

View File

@ -4,9 +4,13 @@
$summaries = [];
$content = file_get_contents("test_input_data.2.txt");
$chunks = array_chunk(explode("\n",$content),80);
$chunks = array_chunk(explode("\n",$content),100);
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");
$summaries[] = LlamaCli_raw(
implode("\n",$chunk),
"You are a legal executive assistant that will count how many times the ex-husband Frederico has visited the kids in Tampere. ",
["context_size" => 8192]
);
}
print_r($summaries);

View File

@ -0,0 +1,16 @@
<?php require_once __DIR__."/../LlamaCli.func.php";
// TEST 06: Expand FETCHER COMMANDS
echo LlamaCli_raw("say hello to ( (@echo_text Frederico) @echo_text Falcao)");
echo "\n\n\n";
echo LlamaCli_raw("say hello to @echo_text Frederico @echo_text Falcao");
//echo LlamaCli_raw("translate this title to portuguese and finnish and quote the original in english: @getFirstTitleFromRSSFeed https://www.hs.fi/rss/tuoreimmat.xml","");
//echo LlamaCli_raw("translate this wiki summary to portuguse and show the result also in english @wikiFullArticle Bitcoin","");
// echo LlamaCli_raw("Give me a one paragraph description of what you know about Pablo Escobar","");
// echo LlamaCli_raw("did you know this about Pablo Escobar? @wikiSummary Pablo-Escobar \n\n\nlet me know your impressions.","");
// echo LlamaCli_raw("Based on this forecasted weather for tomorrow @weather_tomorrow Lisbon give me a plan on what to do, since you know I'm on vacation","");