Reference Integrations
ScriptedModel
A Messages implementation that plays back the replies you script, from the
anthropic4s-testkit module. Agents run against it with no network and no API key.
Reference#
ScriptedModel.of(replies*)#
def of[F[_]: Concurrent](replies: Message*): F[ScriptedModel[F]]
def scripted[F[_]: Concurrent](steps: Either[Throwable, Message]*): F[ScriptedModel[F]]
replies: the replies, in order: one per request.steps: replies or failures, to test how the code handles errors.
ScriptedModel[F]#
final class ScriptedModel[F[_]] private (
script: Ref[F, List[Either[Throwable, Message]]],
seen: Ref[F, Vector[MessageRequest]]
)(implicit F: Concurrent[F])
extends Messages[F]
def requests: F[Vector[MessageRequest]]
requests returns every request it received, to assert on what your code sent.
Caveats
countTokensalways returns 0.
Replies#
def text(text: String): Message
def toolUse(name: String, input: Json, id: String = "toolu_1"): Message
def toolUses(calls: (String, String, Json)*): Message
def stopped(reason: StopReason): Message
def structured[A](value: A)(implicit schema: anthropic4s.schema.Schema[A]): Message
def of(message: Message): List[StreamEvent]
Ready-made replies: text, tool calls, a given stop reason, or a structured value. StreamEvents.of turns a reply into the events of its stream.
Usage#
Testing an agent#
for {
model <- ScriptedModel.of[IO](Replies.toolUse("get_weather", json"""{"city":"Brussels"}"""), Replies.text("Sunny."))
result <- model.toolRunner(List(weatherTool)).run(request)
sent <- model.requests
} yield assertEquals(result.text, "Sunny.")
for {
model <- ScriptedModel.of[IO](Replies.toolUse("get_weather", Json.obj("city" -> "Ghent".asJson)), Replies.text("Rainy."))
result <- model.toolRunner(List(weather)).run(request)
sent <- model.requests // inspect what your code sent
} yield assertEquals(result.text, "Rainy.")
Every signature and example on this page is copied, character for character, from the public source of anthropic4s and its Scaladoc. The complete API, every type included, is in the Scaladoc of each module.