---
title: "GenerativeModel"
sidebar_label: "GenerativeModel"
description: "Kotlin binding reference: GenerativeModel."
---

<!-- Generated by tools/api_reference/generate_api_docs.py. Do not edit. -->

//[clika-runtime](../../../index.md)/[io.clika.modelverse](../index.md)/[GenerativeModel](index.md)

# GenerativeModel

[common]\
class [GenerativeModel](index.md) : [PreTrainedModel](../PreTrainedModel/index.md)

A text model loaded from its files on disk, on the CPU.

Open it with [open](Companion/open.md) from a snapshot directory (the layout a Hugging Face download leaves: `config.json`, the tokenizer files and the weights) or from one GGUF file. Read [capabilities](capabilities.md) to learn what it takes. Send a conversation with [chat](chat.md); the reply streams through the listener on the model's own worker thread, one request at a time per model, and the returned handle cancels it. [close](close.md) frees the weights; a request sent after it ends in [GenerationListener.onError](../GenerationListener/onError.md).

The chat template is the checkpoint's: the turns are rendered through it, so a system turn, the user turns and the earlier assistant turns reach the model as its publisher intended.

[generate](generate.md) is the blocking form of [chat](chat.md), answering the reply's text with [report](report.md) filled in; [chatSession](chatSession.md) keeps a conversation over the model, its tools channel included; [render](render.md) shows the prompt the template writes; and [setStageObserver](setStageObserver.md) hears the pipeline's stages as they run.

## Types

| Name | Summary |
|---|---|
| [Companion](Companion/index.md) | [common]<br>object [Companion](Companion/index.md) |

## Properties

| Name | Summary |
|---|---|
| [capabilities](capabilities.md) | [common]<br>val [capabilities](capabilities.md): [Capabilities](../Capabilities/index.md)<br>What the model takes and gives, read when it opened. |
| [config](config.md) | [common]<br>open override val [config](config.md): [PretrainedConfig](../PretrainedConfig/index.md)<br>The checkpoint's identity, read when the model opened. |
| [report](report.md) | [common]<br>var [report](report.md): [GenerationReport](../GenerationReport/index.md)?<br>The last reply's report, from [generate](generate.md), [chat](chat.md) or a [ChatSession](../ChatSession/index.md) over this model; null before the first reply ends. |

## Functions

| Name | Summary |
|---|---|
| [chat](chat.md) | [common]<br>fun [chat](chat.md)(messages: [List](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-list/index.html)&lt;[ChatMessage](../ChatMessage/index.md)&gt;, config: [GenerationConfig](../GenerationConfig/index.md), listener: [GenerationListener](../GenerationListener/index.md)): [GenerationHandle](../GenerationHandle/index.md)<br>Stream a reply to [messages](chat.md) under [config](chat.md). Returns at once; the worker runs the request and calls [listener](chat.md) on its thread. A second [chat](chat.md) while one runs waits for the first to end. After [close](close.md) the listener receives [GenerationListener.onError](../GenerationListener/onError.md) and nothing else. |
| [chatSession](chatSession.md) | [common]<br>fun [chatSession](chatSession.md)(system: [String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)? = null, tools: [List](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-list/index.html)&lt;[ToolDef](../ToolDef/index.md)&gt; = emptyList()): [ChatSession](../ChatSession/index.md)<br>A conversation over this model, opened with [system](chatSession.md) as its system turn when given and [tools](chatSession.md) offered on every turn (a turn may offer its own). |
| [close](close.md) | [common]<br>open fun [close](close.md)() |
| [generate](generate.md) | [common]<br>fun [generate](generate.md)(prompt: [String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)? = null, messages: [List](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-list/index.html)&lt;[ChatMessage](../ChatMessage/index.md)&gt;? = null, config: [GenerationConfig](../GenerationConfig/index.md) = capabilities.defaults, images: [List](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-list/index.html)&lt;[ByteArray](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte-array/index.html)&gt; = emptyList(), tools: [List](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-list/index.html)&lt;[ToolDef](../ToolDef/index.md)&gt; = emptyList(), toolChoice: [ToolChoice](../ToolChoice/index.md) = ToolChoice.Auto, stageObserver: [StageObserver](../StageObserver/index.md)? = null): [String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)<br>One reply, blocking: [prompt](generate.md) as the user turn (with [images](generate.md)), or [messages](generate.md) as the conversation; exactly one of the two. A model with a chat template renders it, one without reads the prompt as text. Answers the reply's visible text; [report](report.md) then says how it ended and what it cost. [tools](generate.md) are offered under [toolChoice](generate.md), and the calls the reply made are on the report. [stageObserver](generate.md) hears this call's stages alone, beside the model's own ([setStageObserver](setStageObserver.md)). A failure throws [ModelverseException](../ModelverseException/index.md) with the library's message (an offer of tools to a model that takes none among them); a [close](close.md) while the reply runs ends it as [FinishReason.CANCELED](../FinishReason/CANCELED/index.md) with the text so far. |
| [render](render.md) | [common]<br>fun [render](render.md)(messages: [List](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-list/index.html)&lt;[ChatMessage](../ChatMessage/index.md)&gt;, tools: [List](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-list/index.html)&lt;[ToolDef](../ToolDef/index.md)&gt; = emptyList()): [String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)<br>The prompt the chat template writes for [messages](render.md) with [tools](render.md) offered, as a request would send it (the generation prompt appended); a model without a template answers the turns' text joined by newlines. |
| [residency](residency.md) | [common]<br>open override fun [residency](residency.md)(): [ResidencyReport](../ResidencyReport/index.md)<br>Free the model. A running request is canceled first and the call waits for it to return; called from inside a listener callback, the free runs right after that callback's request ends instead. Idempotent. |
| [setStageObserver](setStageObserver.md) | [common]<br>fun [setStageObserver](setStageObserver.md)(observer: [StageObserver](../StageObserver/index.md)?)<br>Hear every request's pipeline stages through [observer](setStageObserver.md) ([StageEvent](../StageEvent/index.md)), on the stage's own thread; null detaches it. Takes effect at once, for a request already running too; an event already in flight may still reach the observer it replaced. |