ChatSession API
Overview
ChatSession bundles an InferenceRuntime, Tokenizer, and ModelMetadata to provide chat and tool calling capabilities for any model.
It lives in the llm-agent module and has no dependencies on specific runners.
Constructor
llm-agent/src/commonMain/kotlin/sk/ainet/apps/kllama/chat/ChatSession.ktclass ChatSession<T : DType>(
val runtime: InferenceRuntime<T>,
val tokenizer: Tokenizer,
val metadata: ModelMetadata = ModelMetadata(),
templateName: String? = null // override auto-detection
)
Methods
runSingleTurn
Run a single agent round with the given prompt and tools.
fun runSingleTurn(
prompt: String,
tools: List<Tool> = emptyList(),
maxTokens: Int = 256,
temperature: Float = 0.7f,
listener: AgentListener? = null
): String
createAgentLoop
Create an AgentLoop configured for this session.
fun createAgentLoop(
toolRegistry: ToolRegistry,
maxTokens: Int = 512,
temperature: Float = 0.7f
): AgentLoop<T>
Usage Example
// Any runner can create a ChatSession
val session = ChatSession(
runtime = myRuntime,
tokenizer = myTokenizer,
metadata = ModelMetadata(family = "qwen", architecture = "qwen3")
)
// Single-shot tool calling
val answer = session.runSingleTurn(
prompt = "What is 42 * 7?",
tools = listOf(CalculatorTool())
)
// Multi-turn agent
val registry = ToolRegistry()
registry.register(CalculatorTool())
val loop = session.createAgentLoop(registry)
val messages = mutableListOf(
ChatMessage(role = ChatRole.SYSTEM, content = "You are helpful."),
ChatMessage(role = ChatRole.USER, content = "Calculate 42 * 7")
)
loop.runWithEncoder(messages, encode = { session.encode(it) })