Skip to content

Commit

Permalink
feat: rewrite to accept json data from cli args other than env var (#7)
Browse files Browse the repository at this point in the history

* update readme
  • Loading branch information
sigoden authored May 21, 2024
1 parent e6642b5 commit ef43b22
Show file tree
Hide file tree
Showing 8 changed files with 263 additions and 180 deletions.
170 changes: 49 additions & 121 deletions Argcfile.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,13 @@ LANG_CMDS=( \
)

# @cmd Call the function
# @arg func![`_choice_func`] The function name
# @arg args~[?`_choice_func_args`] The function args
# @arg cmd![`_choice_cmd`] The function command
# @arg json The json data
call() {
basename="${argc_func%.*}"
lang="${argc_func##*.}"
func_path="./$lang/$basename.$lang"
if [[ ! -e "$func_path" ]]; then
_die "error: not found $argc_func"
fi
if [[ "$lang" == "sh" ]]; then
"$func_path" "${argc_args[@]}"
else
"$(_lang_to_cmd "$lang")" "./cmd/cmd.$lang" "$argc_func"
if _is_win; then
ext=".cmd"
fi
"$BIN_DIR/$argc_cmd$ext" "$argc_json"
}

# @cmd Build the project
Expand All @@ -49,35 +42,29 @@ build-bin() {
mkdir -p "$BIN_DIR"
rm -rf "$BIN_DIR"/*
names=($(cat "$argc_names_file"))
invalid_names=()
not_found_funcs=()
for name in "${names[@]}"; do
basename="${name%.*}"
lang="${name##*.}"
func_file="$lang/$name"
if [[ -f "$func_file" ]]; then
if _is_win; then
bin_file="$BIN_DIR/$basename.cmd"
if [[ "$lang" == sh ]]; then
_build_win_sh > "$bin_file"
else
_build_win_lang $lang "$(_lang_to_cmd "$lang")" > "$bin_file"
fi
_build_win_shim $lang > "$bin_file"
else
bin_file="$BIN_DIR/$basename"
if [[ "$lang" == sh ]]; then
ln -s "$PWD/$func_file" "$bin_file"
else
ln -s "$PWD/cmd/cmd.$lang" "$bin_file"
fi
ln -s "$PWD/cmd/cmd.$lang" "$bin_file"
fi
else
invalid_names+=("$name")
not_found_funcs+=("$name")
fi
done
if [[ -n "$invalid_names" ]]; then
_die "error: missing following functions: ${invalid_names[*]}"
if [[ -n "$not_found_funcs" ]]; then
_die "error: not founds functions: ${not_found_funcs[*]}"
fi
echo "Build bin"
for name in "$BIN_DIR"/*; do
echo "Build $name"
done
}

# @cmd Build declarations.json
Expand Down Expand Up @@ -125,11 +112,7 @@ build-single-declaration() {
func="$1"
lang="${func##*.}"
cmd="$(_lang_to_cmd "$lang")"
if [[ "$lang" == sh ]]; then
argc --argc-export "$lang/$func" | _parse_argc_declaration
else
LLM_FUNCTION_DECLARATE=1 "$cmd" "cmd/cmd.$lang" "$func"
fi
LLM_FUNCTION_ACTION=declarate "$cmd" "cmd/cmd.$lang" "$func"
}

# @cmd List functions that can be put into functions.txt
Expand All @@ -146,36 +129,37 @@ test() {
func_names_file=functions.txt.test
argc list-functions > "$func_names_file"
argc build --names-file "$func_names_file"
argc test-call-functions
argc test-functions
rm -rf "$func_names_file"
}


# @cmd Test call functions
test-call-functions() {
test-functions() {
if _is_win; then
ext=".cmd"
fi
"./bin/may_execute_command$ext" --command 'echo "bash works"'
argc call may_execute_command.sh --command 'echo "bash works"'

if command -v node &> /dev/null; then
export LLM_FUNCTION_DATA='{"code":"console.log(\"javascript works\")"}'
"./bin/may_execute_js_code$ext"
argc call may_execute_js_code.js
fi

if command -v python &> /dev/null; then
export LLM_FUNCTION_DATA='{"code":"print(\"python works\")"}'
"./bin/may_execute_py_code$ext"
argc call may_execute_py_code.py
fi

if command -v ruby &> /dev/null; then
export LLM_FUNCTION_DATA='{"code":"puts \"ruby works\""}'
"./bin/may_execute_rb_code$ext"
argc call may_execute_rb_code.rb
fi
test_cases=( \
'sh#may_execute_command#{"command":"echo \"✓\""}' \
'js#may_execute_js_code#{"code":"console.log(\"✓\")"}' \
'py#may_execute_py_code#{"code":"print(\"✓\")"}' \
'rb#may_execute_rb_code#{"code":"puts \"✓\""}' \
)

for test_case in "${test_cases[@]}"; do
IFS='#' read -r lang func data <<<"${test_case}"
cmd="$(_lang_to_cmd "$lang")"
cmd_path="$BIN_DIR/$func$ext"
if command -v "$cmd" &> /dev/null; then
"$cmd_path" "$data" | {
echo "Test $cmd_path: $(cat)"
}
if ! _is_win; then
"$cmd" "cmd/cmd.$lang" "$func" "$data" | {
echo "Test $cmd cmd/cmd.$lang $func: $(cat)"
}
fi
fi
done
}

# @cmd Install this repo to aichat functions_dir
Expand All @@ -199,49 +183,6 @@ version() {
curl --version | head -n 1
}

_parse_argc_declaration() {
jq -r '
def parse_description(flag_option):
if flag_option.describe == "" then
{}
else
{ "description": flag_option.describe }
end;
def parse_enum(flag_option):
if flag_option.choice.type == "Values" then
{ "enum": flag_option.choice.data }
else
{}
end;
def parse_property(flag_option):
[
{ condition: (flag_option.flag == true), result: { type: "boolean" } },
{ condition: (flag_option.multiple_occurs == true), result: { type: "array", items: { type: "string" } } },
{ condition: (flag_option.notations[0] == "INT"), result: { type: "integer" } },
{ condition: (flag_option.notations[0] == "NUM"), result: { type: "number" } },
{ condition: true, result: { type: "string" } } ]
| map(select(.condition) | .result) | first
| (. + parse_description(flag_option))
| (. + parse_enum(flag_option))
;
def parse_parameter(flag_options):
{
type: "object",
properties: (reduce flag_options[] as $item ({}; . + { ($item.id | sub("-"; "_"; "g")): parse_property($item) })),
required: [flag_options[] | select(.required == true) | .id],
};
{
name: (.name | sub("-"; "_"; "g")),
description: .describe,
parameters: parse_parameter([.flag_options[] | select(.id != "help" and .id != "version")])
}'
}

_lang_to_cmd() {
match_lang="$1"
for item in "${LANG_CMDS[@]}"; do
Expand All @@ -252,24 +193,14 @@ _lang_to_cmd() {
done
}

_build_win_sh() {
cat <<-'EOF'
@echo off
setlocal
set "bin_dir=%~dp0"
for %%i in ("%bin_dir:~0,-1%") do set "script_dir=%%~dpi"
set "script_name=%~n0"
set "script_name=%script_name%.sh"
for /f "delims=" %%a in ('argc --argc-shell-path') do set "_bash_prog=%%a"
"%_bash_prog%" --noprofile --norc "%script_dir%sh\%script_name%" %*
EOF
}

_build_win_lang() {
_build_win_shim() {
lang="$1"
cmd="$2"
cmd="$(_lang_to_cmd "$lang")"
if [[ "$lang" == "sh" ]]; then
run="\"$(cygpath -w "$(which $cmd)")\" --noprofile --norc"
else
run="\"$(cygpath -w "$(which $cmd)")\""
fi
cat <<-EOF
@echo off
setlocal
Expand All @@ -278,7 +209,7 @@ set "bin_dir=%~dp0"
for %%i in ("%bin_dir:~0,-1%") do set "script_dir=%%~dpi"
set "script_name=%~n0"
$cmd "%script_dir%cmd\cmd.$lang" "%script_name%.$lang" %*
$run "%script_dir%cmd\cmd.$lang" "%script_name%.$lang" %*
EOF
}

Expand All @@ -300,11 +231,8 @@ _choice_func() {
done
}

_choice_func_args() {
args=( "${argc__positionals[@]}" )
if [[ "${args[0]}" == *.sh ]]; then
argc --argc-compgen generic "sh/${args[0]}" "${args[@]}"
fi
_choice_cmd() {
ls -1 "$BIN_DIR" | sed -e 's/\.cmd$//'
}

_die() {
Expand Down
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# LLM Functions

This project allows you to enhance large language models (LLMs) with custom functions written in Bash/Js/Python/Ruby. Imagine your LLM being able to execute system commands, access web APIs, or perform other complex tasks – all triggered by simple, natural language prompts.
This project allows you to enhance large language models (LLMs) with custom functions written in bash/js/python/ruby. Imagine your LLM being able to execute system commands, access web APIs, or perform other complex tasks – all triggered by simple, natural language prompts.

## Prerequisites

Expand Down Expand Up @@ -46,7 +46,8 @@ AIChat will automatically load `functions.json` and execute functions located in

Now you can interact with your LLM using natural language prompts that trigger your defined functions.

![image](https://github.com/sigoden/llm-functions/assets/4012553/867b7b2a-25fb-4c74-9b66-3701eaabbd1f)
![function-showcase](https://github.com/sigoden/llm-functions/assets/4012553/391867dd-577c-4aaa-9ff2-c9e67fb0f3a3)


## Function Types

Expand All @@ -56,13 +57,17 @@ The function returns JSON data to LLM for further processing.

AIChat does not ask permission to run the function or print the output.

![retrieve-type-showcase](https://github.com/sigoden/llm-functions/assets/4012553/7e628834-9863-444a-bad8-7b51bfb18dff)

### Execute Type

The function does not return data to LLM. Instead, they enable more complex actions, such as showing a progress bar or running a TUI application.
The function does not have to return JSON data.

The function can perform dangerous tasks like creating/deleting files, changing network adapter, and setting a scheduled task...

AIChat will ask permission before running the function.

![image](https://github.com/sigoden/aichat/assets/4012553/711067b8-dd23-443d-840a-5556697ab075)
![execute-type-showcase](https://github.com/sigoden/llm-functions/assets/4012553/1dbc345f-daf9-4d65-a49f-3df8c7df1727)

**AIChat categorizes functions starting with `may_` as `execute type` and all others as `retrieve type`.**

Expand Down
53 changes: 36 additions & 17 deletions cmd/cmd.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,55 @@
#!/usr/bin/env node

function loadModule() {
const path = require("path");
let func_name = process.argv[1];
if (func_name.endsWith("cmd.js")) {
func_name = process.argv[2]
const path = require("path");

function parseArgv() {
let func_file = process.argv[1];
let func_data = null;

if (func_file.endsWith("cmd.js")) {
func_file = process.argv[2]
func_data = process.argv[3]
} else {
func_name = path.basename(func_name)
func_file = path.basename(func_file)
func_data = process.argv[2];
}
if (!func_name.endsWith(".js")) {
func_name += '.js'

if (!func_file.endsWith(".js")) {
func_file += '.js'
}
const func_path = path.resolve(__dirname, `../js/${func_name}`)

return [func_file, func_data]
}

function loadFunc(func_file) {
const func_path = path.resolve(__dirname, `../js/${func_file}`)
try {
return require(func_path);
} catch {
console.log(`Invalid js function: ${func_name}`)
console.log(`Invalid function: ${func_file}`)
process.exit(1)
}
}

if (process.env["LLM_FUNCTION_DECLARATE"]) {
const { declarate } = loadModule();
const [func_file, func_data] = parseArgv();

if (process.env["LLM_FUNCTION_ACTION"] == "declarate") {
const { declarate } = loadFunc(func_file);
console.log(JSON.stringify(declarate(), null, 2))
} else {
let data = null;
if (!func_data) {
console.log("No json data");
process.exit(1)
}

let args;
try {
data = JSON.parse(process.env["LLM_FUNCTION_DATA"])
args = JSON.parse(func_data)
} catch {
console.log("Invalid LLM_FUNCTION_DATA")
console.log("Invalid json data")
process.exit(1)
}
const { execute } = loadModule();
execute(data)

const { execute } = loadFunc(func_file);
execute(args)
}
Loading

0 comments on commit ef43b22

Please sign in to comment.