org.llm4s.repl

package org.llm4s.repl

Members list

Type members

Classlikes

@Experimental
final class CallStats

What one model call cost: the model that answered, the tokens the provider reported, how long the call took, and the estimated cost in US dollars when llm4s knows the model's price. Printed as one line:

What one model call cost: the model that answered, the tokens the provider reported, how long the call took, and the estimated cost in US dollars when llm4s knows the model's price. Printed as one line:

gpt-4o-mini: 12 prompt + 8 completion = 20 tokens, 312 ms, $0.000007

Value parameters

estimatedCost

the estimated cost in US dollars, when the model's price is known

latency

the time from the request to the last byte of the answer

model

the model that answered, as the provider named it

usage

the token counts, when the provider reports them

Attributes

Supertypes
class Object
trait Matchable
class Any
@Experimental
final class History

The conversation a session has had so far, as ask and chat() recorded it: each question, each answer, and the tool calls and results in between. A REPL prints it as a transcript, one message per line:

The conversation a session has had so far, as ask and chat() recorded it: each question, each answer, and the tool calls and results in between. A REPL prints it as a transcript, one message per line:

user: What is the weather in Paris?
assistant: -> get_weather({"city":"Paris"})
tool: <- {"temperature":21}
assistant: It is 21 degrees in Paris.

Value parameters

messages

the messages, oldest first; the session's system prompt is not among them

Attributes

Supertypes
class Object
trait Matchable
class Any
@Experimental
enum Outcome[+A]

What a REPL helper returns: the value, or the error that stopped it. A REPL prints it as the value itself, or as error: <kind>: <message and hint>, never as a stack trace. A script that wants a Result calls toResult, or uses the helpers of strict.

What a REPL helper returns: the value, or the error that stopped it. A REPL prints it as the value itself, or as error: <kind>: <message and hint>, never as a stack trace. A script that wants a Result calls toResult, or uses the helpers of strict.

val answer = ask("What is a monad?")
answer.getOrElse("")      // the text, or "" when the call failed
answer.toResult           // Right(text) or Left(error)

Attributes

Supertypes
trait Enum
trait Serializable
trait Product
trait Equals
class Object
trait Matchable
class Any
Show all
@Experimental
final class Session

What the helpers of org.llm4s.repl talk to: a client, its completion options, the conversation so far and the statistics of the last call. Set it once as a context parameter and every helper finds it:

What the helpers of org.llm4s.repl talk to: a client, its completion options, the conversation so far and the statistics of the last call. Set it once as a context parameter and every helper finds it:

import org.llm4s.repl.*

given Session = Session.fromConfig()       // the `llm4s.providers.provider` section of application.conf
ask("Explain variance in Scala 3 in two sentences")

A session whose client cannot be built (no application.conf, a missing API key, an unregistered provider) is still a session: every helper prints the configuration error with its troubleshooting hint and returns a failure, so a worksheet never stops on a stack trace. failure says whether that is the case.

A session is for a REPL, a worksheet or a script, not for production code: it keeps the conversation in memory, prints to the console, and its helpers are not meant to be called concurrently. Each with* method returns a new session with the same client and an empty history.

Attributes

Companion
object
Supertypes
class Object
trait Matchable
class Any
object Session

Attributes

Companion
class
Supertypes
class Object
trait Matchable
class Any
Self type
Session.type
@Experimental
object strict

The helpers for scripts: the same calls, returning Result so that a script can stop on the first failure with a for comprehension. They print nothing on failure; strict.ask still streams the answer.

The helpers for scripts: the same calls, returning Result so that a script can stop on the first failure with a for comprehension. They print nothing on failure; strict.ask still streams the answer.

Attributes

Supertypes
class Object
trait Matchable
class Any
Self type
strict.type

Value members

Concrete methods

def ask(prompt: String, tools: ToolRegistry)(using session: Session): Outcome[String]

Asks the model prompt, after the conversation so far, and streams the answer to the session's output as it arrives. The question and answer are added to the history.

Asks the model prompt, after the conversation so far, and streams the answer to the session's output as it arrives. The question and answer are added to the history.

With tools, each tool call the model makes is run, printed (-> name(arguments), then <- result) and sent back, until the model answers in text.

A failed call prints error: <kind>: <message>, with the troubleshooting hint llm4s attaches, and returns Outcome.Failed; nothing is thrown. strict.ask returns a Result instead.

Value parameters

prompt

the question

tools

the tools the model may call; none by default

Attributes

Returns

the answer's text, or the error

def chat()(using session: Session): Unit

A small read-eval loop over the session: each line read is asked with ask, so the conversation carries on from one line to the next. :q (or the end of the input) leaves, :history prints the conversation so far and :reset forgets it.

A small read-eval loop over the session: each line read is asked with ask, so the conversation carries on from one line to the next. :q (or the end of the input) leaves, :history prints the conversation so far and :reset forgets it.

Attributes

def extract[A](text: String)(using session: Session, schema: SchemaOf[A], reader: Reader[A]): Outcome[A]

Asks the model for an A read from text, with the JSON schema derived from A (completeStructuredOf):

Asks the model for an A read from text, with the JSON schema derived from A (completeStructuredOf):

case class Invoice(vendor: String, amount: Double, currency: String) derives SchemaOf, ReadWriter
val invoice = extract[Invoice]("ACME, 12.50 EUR, two widgets")

The call is not added to the history. A failure is printed and returned as Outcome.Failed.

Type parameters

A

a case class or sum type that derives SchemaOf and has a uPickle Reader

Value parameters

text

the text to read the value from

Attributes

def history(using session: Session): History

The conversation so far: what ask and chat sent and received. history.show prints it.

The conversation so far: what ask and chat sent and received. history.show prints it.

Attributes

def lastUsage(using session: Session): Option[CallStats]

The model, tokens, latency and estimated cost of the session's last successful model call, if there was one.

The model, tokens, latency and estimated cost of the session's last successful model call, if there was one.

Attributes