Skip to contents

Writing an AI function

A formula, a sentence saying what it does, and a codebook of its fields.

ai()
Write a function a language model does the work of
choice()
One of a set of answers
record()
A record: several named fields
optional()
An input or output that may be missing
described()
Describe a field
defaults_to()
An input the caller may leave out
json_shape()
A field written as JSON Schema
ai_tool()
A tool the model may call

Settings

ai_config() with_ai_config() local_ai_config()
Settings for every AI function
update(<functai_fn>)
Change an AI function's settings
model_capabilities()
What FunctAI declares a model can do

Using it on data

predict(<functai_fn>) augment(<functai_fn>)
Predictions for a data frame
ai_problems()
The calls that failed in the last call of an AI function

How often is it right?

evaluate()
How often is a model right?
tidy(<functai_evaluation>) glance(<functai_evaluation>) augment(<functai_evaluation>)
Tidy an evaluation
exact_match()
Is an answer right?
score_interval()
The mean of scores and its 95% interval
normalize_text()
Text as exact_match compares it
casefold_full()
Unicode full case folding

Making it better

labeled_few_shot() bootstrap_few_shot()
Worked examples from rows with known answers
gepa()
Rewrite the instruction from the function's mistakes (GEPA)
random_search() instruction_search()
Try several sets of worked examples, keep the best
ai_trials()
The search that improved a function
with_demos() with_instructions() ai_demos()
Worked examples and instructions
compare()
Two versions compared row by row

As a tidymodels model

ai_model() update(<ai_model>) tunable(<ai_model>)
An AI model, for tidymodels
worked_examples()
How many worked examples, as a tuning parameter

What it takes, sends, and its version

ai_interface()
What a program takes and gives
ai_version() ai_instructions() ai_signature() ai_signature_id() ai_render()
What an AI function sends, and its version

The call log and ratings

calls()
Every logged call
rate()
Say whether calls were right
rated()
Rows with known answers, from people's ratings
default_log_folder()
Where calls are logged when no folder is named

Saving, and other languages’ functions

read_ai()
Run a function saved in any language
write_ai()
Save an AI function for any language

Programs and streams

Your own code around AI functions, as one call; a call watched while it is made.

ai_program() program_from_interface()
Your own code around AI functions, as one program
opaque()
Any R value
predict(<functai_program>) augment(<functai_program>)
Predictions of a program for a data frame
ai_stream() stop_stream() ai_text()
Watch a call while it is made

Conversations

Calls that remember each other, kept in a store; tools that ask a person first.

ai_conversation()
A conversation with an AI function or a program
all_turns() last_turns()
Which earlier turns a turn is shown
remember()
What an AI function inside a program's conversation remembers
ai_turns() ai_turn()
The turns of a conversation
continue_from()
Continue a conversation from an earlier turn
move_head()
Make a turn the conversation's head, for everyone who opens it
wait_turn() stop_turn() abandon_turn() resume_turn()
What can be done with a turn
approve() deny()
Answer a person's question about a tool call
earlier()
The conversation so far, inside a program's turn
folder_store() memory_conversations()
Where conversations are kept

Plugins

Hooks that change calls, every change recorded.

ai_plugin() on_hook()
A plugin: hooks that change what programs do
ai_change()
What a hook changes
ai_ask()
Ask a person whether a tool may run
keep_entry() entries()
A plugin's entries in a conversation
branch_turns()
The done turns of an ended turn's branch, for a turn_end hook
compaction()
Keep a long conversation short: older turns folded into a summary
delegate()
Another program as a tool
load_plugin()
A plugin from a file

Serving, and programs served elsewhere

ai_service() ai_serve() serve_request()
Serve a program over HTTP
ai_remote()
A program served elsewhere, used here

Long runs

The reply cache, a smaller call log, a judge’s evidence.

reply_key()
The reply cache's key of a request
clear_replies()
Forget kept replies
default_reply_cache()
Where cache_replies = "disk" keeps replies
prune_calls()
Make the call log smaller, keeping what ratings need
quotes_found()
Whether a judge's quotes are really in the text

Watching and keeping call trees’ logs

Observers, journals and stores, by the contract’s rules.

ai_journal()
Keep each call tree's log while it is written
memory_store() store_refuse()
A store of call trees' logs
settle()
What a journal kept of an end it could not confirm
follower() follow_event() follow_state() follow_recover()
Follow call trees' logs live
outside_view()
What a caller outside a program sees of its log
turn_events()
A turn's events, from its store

Baking

The examples every trainer reads, and students trained anywhere.

bake_examples() export_examples()
The training conversations of an AI function
baked()
A model baked anywhere, served by an OpenAI-compatible server

Signing in, and what was sent

ai_login() ai_logins() ai_logout()
Sign in to a provider
inspect_history() phistory()
The last requests of this session

Datasets

tickets
Customer support messages, with the team each belongs to
refunds
Refund requests, with the decision the shop's rules give
field_notes
Bird survey field notes, with species, count and behaviour

Internals

ai_model_fit() ai_model_predict()
Fit an AI model (parsnip's fit function for the "functai" engine)
as_field()
Turn a type into a field
reexports augment tidy glance tunable
Objects exported from other packages
functai functai-package
functai: Functions Whose Body a Language Model Writes, for the Tidyverse