IMPROVEMENT: separated possible fetching-actions, for easier scalability

This commit is contained in:
Frederico @ VilaRosa02 2025-09-11 10:59:25 +00:00
parent 06392538b8
commit 902ead9855
4 changed files with 88 additions and 12 deletions

View File

@ -0,0 +1,57 @@
<?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 ExpandFetcherCommands(string $text) {
global $actions;
// Make it recursive, while you have parenthesis
while(preg_match("/\(([^()]+)\)/",$text, $matches))
$text = str_replace($matches[0],ExpandFetcherCommands($matches[1]),$text);
$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;
}

2
lib/actions/Makefile Normal file
View File

@ -0,0 +1,2 @@
%.action.php: %.to-be.action
~/llama.cpp/build/bin/llama-cli -m ~/llama.cpp/models/Meta-Llama-3.1-8B-Instruct-Q4_K_M.gguf -sys "User will pass a sample template of code, and you'll adapt to the following request: $(shell cat $< | tr "\n" " ")" -f action.sample.php -no-cnv 2> llama.err.log > $@

View File

@ -0,0 +1,29 @@
<?php
/**
* SIMPLE ACTION: Echo
* -------------------------
* Demonstrates how to register and implement a simple echo action.
*
* Call format:
* @echo HelloWorld
*
* Expected output:
* "HelloWorld"
*/
// 1. Register the action with expected argument types
registerAction(
(new Action())
->setFunctionName("echo_text")
->setArgumentsTypes(["input"]) // one argument
);
/**
* 2. Implement the function
*
* @param string $input
* @return string
*/
function echo_text(string $input): string {
return $input;
}

View File

@ -1,12 +0,0 @@
<?php require_once __DIR__."/../LlamaCli.func.php";
// TEST 06: Resolve FETCHING ACTIONS
//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","");