beetlebug▾
/

ztcl::agent run

Runs the loop of the model and gives a dict of the result, the log, and the counts.

ztcl::agent run ?-option value ...?

Description

Starts a thread with thread::create -caps {}, with no file, socket, program, clock, or thread command, and runs the loop: the host posts a request to the endpoint, and each call of a tool in the reply runs a tool, until the model calls the tool result. A reply with no call of a tool gets one more user message that asks for the result tool, and a second such reply is the error ZTCL AGENT MODEL. A status other than 200 is also ZTCL AGENT MODEL.

The result is a dict: result, the checked result; log, a list of dicts, one for each model call, tool call, and the result; turns; calls; input_tokens; and output_tokens. An entry of the log has kind: model with turn, status, input_tokens, and output_tokens; tool with where (host or thread), name, args, ok, result, and errorcode; and result with ok, and error when the result does not match the schema. The model gets each error of a tool as the result of its call, with the error code and errorInfo.

Options

OptionTypeDefaultDescription
-endpointURLnoneGives the one URL of the model.
-headerlist of names and valuesnoneGives the headers of the host, such as the key.
-modelstringnoneGives the model field of each request.
-systemstringnoneGives the system text.
-promptstringnoneGives the first user text.
-datalist of names and valuesnoneGives data for the first user message.
-toolslist of command namesnoneGives the host commands that the model can call.
-locallist of command namesnoneGives the commands of the thread that the model can call.
-setupscriptnoneGives a script that runs in the thread before the loop.
-apimessages or openaimessagesGives the form of the requests.
-resultresult schemaanswer stringGives the schema of the result.
-isolationprocess or interpprocessGives the isolation level of the thread.
-computeintegerthe limit of the thread groupGives the compute limit of the thread.
-metermeter namenoneCounts the thread in a meter.
-turnsinteger8Gives the most model calls.
-callsinteger16Gives the most tool calls, of the host and of the thread.
-tokensinteger100,000Gives the most tokens of the replies.
-max-tokensinteger4,096Gives the max_tokens field of each request.
-logcommand prefixnoneGives a command that gets each entry of the log at once.

-endpoint URL

Gives the one URL of the model.

An http:// or https:// URL. It is needed.

% replies [call result {{"answer":"hi"}}]
> ztcl::agent run -endpoint $url -isolation interp
> lindex [requests] end 0 0
POST /v1/messages HTTP/1.1
% ztcl::agent run -prompt hi
ztcl::agent run needs -endpoint
-errorcode ZTCL AGENT USAGE

% ztcl::agent run -endpoint ftp://x -isolation interp
bad endpoint "ftp://x"
-errorcode ZTCL AGENT ENDPOINT

-header LIST

Gives the headers of the host, such as the key.

The headers that the host adds to each request. The thread never gets them.

% replies [call result {{"answer":"x"}}]
> ztcl::agent run -endpoint $url -isolation interp -header {x-api-key K1}
> lsearch -inline [lindex [requests] end 0] x-api-key*
x-api-key: K1
% ztcl::agent run -endpoint $url -isolation interp -header {a "b\nc"}
a header has a line end
-errorcode ZTCL AGENT HEADER

-model NAME

Gives the model field of each request.

The host puts NAME in the field model of each request.

% replies [call result {{"answer":"x"}}]
> ztcl::agent run -endpoint $url -isolation interp -model m1
> string match {*"model":"m1"*} [lindex [requests] end 1]
1

Each name is valid.

-system TEXT

Gives the system text.

The field system of each request.

% replies [call result {{"answer":"x"}}]
> ztcl::agent run -endpoint $url -isolation interp -system S1
> string match {*"system":"S1"*} [lindex [requests] end 1]
1

Each text is valid.

-prompt TEXT

Gives the first user text.

The text of the first user message.

% replies [call result {{"answer":"x"}}]
> ztcl::agent run -endpoint $url -isolation interp -prompt hi
> expr {[string first {"messages":[{"role":"user","content":"hi"}]} [lindex [requests] end 1]] >= 0}
1

Each text is valid.

-data LIST

Gives data for the first user message.

Each value goes in the first user message as <data name="NAME">, after the prompt. An odd list is an error.

% replies [call result {{"answer":"x"}}]
> ztcl::agent run -endpoint $url -isolation interp -data {email hello}
> string match {*<data name=\\"email\\">\\nhello\\n</data>*} [lindex [requests] end 1]
1
% ztcl::agent run -endpoint $url -isolation interp -data {a}
-data needs a list of names and values
-errorcode ZTCL AGENT USAGE

-tools LIST

Gives the host commands that the model can call.

The thread gets a command of each name, and a call runs the command of the host at the global level with the words from the input of the model. The spec of each tool is the one of describe. A tool cannot have the name result.

% replies [call add {{"a":"2","b":"3"}}] [call result {{"answer":"5"}}]
> set r [ztcl::agent run -endpoint $url -isolation interp -tools add]
> list [dict get $r result] [lindex [dict get $r log] 1]
{answer 5} {kind tool where host name add args {2 3} ok 1 result 5 errorcode {}}

% replies [call add {{"a":"x","b":"1"}}] [call result {{"answer":"e"}}]
> dict get [lindex [dict get [ztcl::agent run -endpoint $url -isolation interp -tools add] log] 1] errorcode
ARITH DOMAIN {non-numeric string}
% ztcl::agent run -endpoint $url -tools nosuch
no command "nosuch" for a tool
-errorcode ZTCL AGENT USAGE

% proc result {} {}
> ztcl::agent run -endpoint $url -tools result
the tool name "result" is for the result
-errorcode ZTCL AGENT USAGE

-local LIST

Gives the commands of the thread that the model can call.

The names of commands of the thread, made by -setup.

% replies [call twice {{"n":"4"}}] [call result {{"answer":"8"}}]
> set r [ztcl::agent run -endpoint $url -isolation interp -setup {proc twice {n} {expr {$n * 2}}} -local twice]
> list [dict get $r result] [lindex [dict get $r log] 1]
{answer 8} {kind tool where thread name twice args 4 ok 1 result 8 errorcode {}}

A name with no command of the thread is a call that fails, in the log.

-setup SCRIPT

Gives a script that runs in the thread before the loop.

The script can use ::agent::doc in place of ztcl::agent doc. It runs with the limits of the thread.

% replies [call twice {{"n":"1"}}] [call result {{"answer":"2"}}]
> dict get [ztcl::agent run -endpoint $url -isolation interp -setup {proc twice {n} {expr {$n * 2}}} -local twice] result
answer 2
% ztcl::agent run -endpoint $url -isolation interp -setup {while 1 {}} -compute 1000
the agent thread ended: compute limit reached
-errorcode ZTCL AGENT LIMIT thread

-api FORM

Gives the form of the requests.

messages is the Messages API: messages with tool_use and tool_result blocks, tools with input_schema, and the counts usage.input_tokens and usage.output_tokens. openai is chat completions, as llama-server serves it: messages with tool_calls and tool messages, tools of type function, and the counts usage.prompt_tokens and usage.completion_tokens.

Values: messages, openai.

% replies {{"choices":[{"message":{"role":"assistant","tool_calls":[{"id":"c1","type":"function","function":{"name":"result","arguments":"{\"answer\":\"ok\"}"}}]}}],"usage":{"prompt_tokens":7,"completion_tokens":2}}}
> set r [ztcl::agent run -endpoint $url -isolation interp -api openai]
> list [dict get $r result] [dict get $r input_tokens] [dict get $r output_tokens]
{answer ok} 7 2
% ztcl::agent run -endpoint $url -api bogus
bad api "bogus": must be messages or openai
-errorcode ZTCL AGENT USAGE

-result SCHEMA

Gives the schema of the result.

The model gives its result with the tool result, whose input schema is ztcl::agent schema SCHEMA, and run checks it as check does.

% replies [call result {{"n":3,"tags":["a"]}}]
> dict get [ztcl::agent run -endpoint $url -isolation interp -result {n int tags {list string}}] result
n 3 tags a
% ztcl::agent run -endpoint $url -isolation interp -result {n bogus}
bad type "bogus"
-errorcode ZTCL AGENT SCHEMA

% replies [call result {{"answer":"x"}}]
> ztcl::agent run -endpoint $url -isolation interp -result {n int}
result has no key "n"
-errorcode ZTCL AGENT RESULT

-isolation LEVEL

Gives the isolation level of the thread.

The level of thread::create -isolation: process runs the thread in a child process, and interp in an interpreter of this process.

Values: process, interp.

% replies [call result {{"answer":"x"}}]
> dict get [ztcl::agent run -endpoint $url -isolation interp] result
answer x
% ztcl::agent run -endpoint $url -isolation bogus
bad isolation level "bogus": must be interp, process, or vm
-errorcode ZTCL AGENT USAGE

-compute UNITS

Gives the compute limit of the thread.

The thread ends at UNITS compute units, and run gives ZTCL AGENT LIMIT thread. The thread also ends at its memory limit or at a crash of its child process.

% replies [call result {{"answer":"x"}}]
> dict get [ztcl::agent run -endpoint $url -isolation interp -compute 1000000] result
answer x
% ztcl::agent run -endpoint $url -isolation interp -setup {while 1 {}} -compute 1000
the agent thread ended: compute limit reached
-errorcode ZTCL AGENT LIMIT thread

-meter NAME

Counts the thread in a meter.

The thread counts in the meter NAME of ztcl::meter, which needs the capability meter. With no capability meter, -meter is an error of ZTCL AGENT USAGE.

% replies [call result {{"answer":"x"}}]
> ztcl::agent run -endpoint $url -isolation interp -meter m
> expr {[dict get [ztcl::meter get m] units] > 0}
1

A new name makes the meter.

-turns N

Gives the most model calls.

At the end of N model calls with no result, run gives ZTCL AGENT LIMIT turns. run checks that N is an integer before the first request, as it does for -calls, -tokens, -max-tokens, and -compute.

% replies [call add {{"a":"1","b":"1"}}] [call add {{"a":"1","b":"1"}}]
> catch {ztcl::agent run -endpoint $url -isolation interp -tools add -turns 1} msg
> set msg
the agent used its 1 model calls
% replies [call add {{"a":"1","b":"1"}}]
> ztcl::agent run -endpoint $url -isolation interp -tools add -turns 1
the agent used its 1 model calls
-errorcode ZTCL AGENT LIMIT turns

-calls N

Gives the most tool calls, of the host and of the thread.

At the call after N tool calls, run gives ZTCL AGENT LIMIT calls.

% replies [call add {{"a":"1","b":"1"}}] [call result {{"answer":"2"}}]
> dict get [ztcl::agent run -endpoint $url -isolation interp -tools add -calls 1] calls
1
% replies [call add {{"a":"1","b":"1"}}] [call add {{"a":"1","b":"1"}}] [call result {{"answer":"x"}}]
> ztcl::agent run -endpoint $url -isolation interp -tools add -calls 1
the agent used its 1 tool calls
-errorcode ZTCL AGENT LIMIT calls

-tokens N

Gives the most tokens of the replies.

The most of the sum of usage.input_tokens and usage.output_tokens of the replies. Past it, run gives ZTCL AGENT LIMIT tokens.

% replies [call result {{"answer":"x"}}]
> dict get [ztcl::agent run -endpoint $url -isolation interp -tokens 8] input_tokens
5
% replies [call add {{"a":"1","b":"1"}}] [call result {{"answer":"x"}}]
> ztcl::agent run -endpoint $url -isolation interp -tools add -tokens 5
the agent used more than 5 tokens
-errorcode ZTCL AGENT LIMIT tokens

-max-tokens N

Gives the max_tokens field of each request.

The host puts N in the field max_tokens of each request.

% replies [call result {{"answer":"x"}}]
> ztcl::agent run -endpoint $url -isolation interp -max-tokens 50
> string match {*"max_tokens":50,*} [lindex [requests] end 1]
1

The value goes to the endpoint as it is.

-log CMD

Gives a command that gets each entry of the log at once.

run calls CMD with each entry of the log as it comes, before the end of the run.

% set ::L {}
> replies [call result {{"answer":"x"}}]
> ztcl::agent run -endpoint $url -isolation interp -log {lappend ::L}
> set ::L
{kind model turn 1 status 200 input_tokens 5 output_tokens 3} {kind result ok 1}

An error of CMD is not checked here.

Returns

A dict.

Examples

Each example of this page runs after package require ztcl::agent # A fake model on 127.0.0.1, on a port of the system, in a thread: each # request gets the next reply of ::replies, and ::requests keeps them. set srv [thread::create { proc serve {c a p} { fconfigure $c -translation binary set len 0 set hs {} while {[gets $c line] >= 0 && [set line [string trim $line]] ne ""} { lappend hs $line regexp -nocase {^content-length: *(\d+)} $line -> len } lappend ::requests [list $hs [read $c $len]] set body [lindex $::replies 0] set ::replies [lrange $::replies 1 end] puts -nonewline $c "HTTP/1.1 $::status OK\r\nContent-Length: [string length $body]\r\nConnection: close\r\n\r\n$body" close $c } set ::status 200 set ::replies {} set ::requests {} set ::port [lindex [fconfigure [socket -server serve -myaddr 127.0.0.1 0] -sockname] 2] thread::wait }] set url http://127.0.0.1:[thread::send $srv {set ::port}]/v1/messages # Sets the replies of the fake model, and gives the requests that it got. proc replies {args} { thread::send $::srv [list set ::replies $args] } proc requests {} { thread::send $::srv {set ::requests} } # A reply of the Messages API with one call of a tool. proc call {name input} { return "{\"content\":\[{\"type\":\"tool_use\",\"id\":\"t[incr ::id]\",\"name\":\"$name\",\"input\":$input}\],\"usage\":{\"input_tokens\":5,\"output_tokens\":3}}" } proc add {a b} { expr {$a + $b} } .

% replies [call result {{"answer":"hi"}}]
> set r [ztcl::agent run -endpoint $url -isolation interp]
> list [dict keys $r] [dict get $r log]
{result log turns calls input_tokens output_tokens} {{kind model turn 1 status 200 input_tokens 5 output_tokens 3} {kind result ok 1}}

% replies [call nosuch {{}}] [call result {{"answer":"x"}}]
> lindex [dict get [ztcl::agent run -endpoint $url -isolation interp] log] 1
kind tool where thread name nosuch args {} ok 0 result {invalid command name "nosuch"} errorcode {TCL LOOKUP COMMAND}

Errors

% ztcl::agent run -endpoint $url -bogus 1
bad option "-bogus": must be -api, -calls, -compute, -data, -endpoint, -header, -isolation, -local, -log, -max-tokens, -meter, -model, -prompt, -result, -setup, -system, -tokens, -tools, or -turns
-errorcode ZTCL AGENT USAGE

% thread::send $srv {set ::status 500}
> ztcl::agent run -endpoint $url -isolation interp
the model endpoint gave status 500
-errorcode ZTCL AGENT MODEL

% thread::send $srv {set ::status 200}
> set text {{"content":[{"type":"text","text":"no"}],"usage":{"input_tokens":1,"output_tokens":1}}}
> replies $text $text
> ztcl::agent run -endpoint $url -isolation interp
the model gave no result
-errorcode ZTCL AGENT MODEL

Facts

Capability
thread. A new interpreter does not have it until the host turns it on.
Tcl release
Tcl 9.1 and 8.6.
Since
ztcl 1.0.0-beta.1

See also