ChatScript

The script of a ScriptedChatModel: each call adds the answer to one request, in order.

ScriptedChatModel {
says("Hello!") // text
calls("menu_price", MenuLookup("cola")) // one tool call
calls(call("menu_price", MenuLookup("cola")), call("opening_hours")) // several in one answer
answers(Verdict(Category.REFUND, "The pizza was cold.")) // JSON, for `ask<Verdict>`
fails("API error 529: Overloaded") // a call that throws
responds { request -> ChatResponse(ChatMessage.Assistant("You said: ${request.messages.last().text}")) }
}

Functions

Link copied to clipboard
inline fun <Value> answers(value: Value, usage: TokenUsage? = null)

The model answers with value as JSON, for code that asks with ChatModel.ask. Value is a @Serializable class.

Link copied to clipboard
fun call(tool: String): ToolCall

Returns a call of the tool named tool without an input.

inline fun <Input> call(tool: String, input: Input): ToolCall

Returns a call of the tool named tool with input, for calls. Its id is the next of call-1, call-2 and so on.

Link copied to clipboard
fun calls(vararg calls: ToolCall, text: String = "", usage: TokenUsage? = null)

The model answers with calls, several tool calls in one answer. Build each with call.

inline fun <Input> calls(tool: String, input: Input, text: String = "", usage: TokenUsage? = null)

The model answers with one call of the tool named tool. See call for what input may be.

Link copied to clipboard
fun fails(message: String, partialText: String = "")

The call to the model fails with a ChatModelException that has message, as when the provider is overloaded. Use it to test a retry, or what the app does with a failed run.

fun fails(failure: Throwable, partialText: String = "")

The call to the model throws failure. See the overload with a message.

Link copied to clipboard
fun responds(answer: suspend (ChatRequest) -> ChatResponse)

The model answers with what answer returns for the request, for an answer that depends on what was asked.

Link copied to clipboard
fun says(text: String, usage: TokenUsage? = null, truncated: Boolean = false)

The model answers with text.