ChatModel

fun interface ChatModel

A language model that continues a conversation. It is the only thing the rest of this module needs from a model provider, so a graph written against it runs on every Kotlin target and with any provider.

Use an implementation from an integration module (telar-anthropic, telar-openai, telar-langchain4j), or write one. In a test, a lambda is enough:

val model = ChatModel { request -> ChatResponse(ChatMessage.Assistant("Hello!")) }

chat waits for the whole answer and stream delivers it piece by piece. A model of your own only needs chat.

Functions

Link copied to clipboard
inline suspend fun <Answer> ChatModel.ask(request: ChatRequest, attempts: Int = 2): TypedResponse<Answer>

Returns the model's next message for request as an instance of the @Serializable class Answer, with the message it was read from. Use it to ask for a value at the end of a conversation. See the overload with a prompt for how the model is asked.

inline suspend fun <Answer> ChatModel.ask(prompt: String, system: String? = null, attempts: Int = 2): Answer

Sends prompt as a single user message and returns the answer as an instance of the @Serializable class Answer, for a node that needs a value from the model and not a text.

suspend fun <Answer> ChatModel.ask(serializer: KSerializer<Answer>, request: ChatRequest, attempts: Int = 2): TypedResponse<Answer>

Returns the model's next message for request as a value read with serializer. See the overload with a reified type.

Link copied to clipboard
abstract suspend fun chat(request: ChatRequest): ChatResponse

Returns the model's next message for request.

Link copied to clipboard
suspend fun ChatModel.chat(prompt: String, system: String? = null): String

Sends prompt as a single user message and returns the text of the answer, for a node that only needs a piece of text from the model. Call ChatModel.chat with a ChatRequest when you need to know whether the answer was cut off.

Link copied to clipboard

Returns the model's next message for request, like ChatModel.chat, and lets a streamed run watch the model write it. Use it in a node in place of ChatModel.chat:

suspend fun ChatModel.chatWithProgress(prompt: String, system: String? = null): String

Sends prompt as a single user message and returns the text of the answer, like chat with a prompt, and lets a streamed run watch the model write it. See the overload with a ChatRequest.

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

Returns the model's next message for request as the model writes it: a ChatEvent.TextDelta for each piece of text, and then one ChatEvent.Completed with the whole answer. The flow is cold: the model is asked when the flow is collected.