ScriptedChatModel

A ChatModel for tests: it gives the answers of its script, in the order they were written, and keeps every request it received. A graph with an agent is tested with it without an API key and without a network.

val model = ScriptedChatModel {
calls("order_status", OrderLookup(customer = "Ana")) // the first answer asks for a tool
says("Your pizza is 5 minutes away.") // the second one is the final text
}

val state = toolAgent(model, listOf(orderStatus)).invoke(AgentState("Where is my pizza?")).state

assertEquals("Your pizza is 5 minutes away.", state.answer)
assertEquals(2, model.requests.size)

See ChatScript for what a script can hold.

A model that is asked for more answers than its script has throws an AssertionError that says what it was asked. The engine passes an AssertionError on as it is, so the test fails with that message.

In a streamed run, the model writes the text of an answer word by word, so stream emits one ChatEvent.TextDelta for each word. The pieces joined are the text of the answer.

When nodes that run at the same time ask the same model, which of them gets which answer is not fixed. Give each of them a model of its own, or write the script with ChatScript.responds.

Parameters

script

the answers of the model, in order.

Constructors

Link copied to clipboard
constructor(script: ChatScript.() -> Unit)

Properties

Link copied to clipboard

How many answers of the script the model has not given yet. 0 when the whole script was used.

Link copied to clipboard

Every request the model received so far, oldest first, also those of a call that failed.

Functions

Link copied to clipboard
open suspend override fun chat(request: ChatRequest): ChatResponse
Link copied to clipboard
open override fun stream(request: ChatRequest): Flow<ChatEvent>