override suspend fun execute()

in prompt/prompt-executor/prompt-executor-clients/prompt-executor-ollama-client/src/commonMain/kotlin/ai/koog/prompt/executor/ollama/client/OllamaClient.kt [165:221]


    override suspend fun execute(
        prompt: Prompt,
        model: LLModel,
        tools: List<ToolDescriptor>
    ): List<Message.Response> {
        require(model.provider == LLMProvider.Ollama) { "Model not supported by Ollama" }

        val ollamaTools = if (tools.isNotEmpty()) {
            tools.map {
                OllamaToolDTO(
                    type = "function",
                    function = Definition(
                        name = it.name,
                        description = it.description,
                        parameters = toolDescriptorConverter.generate(it)
                    )
                )
            }
        } else {
            null
        }

        val request = ollamaJson.encodeToString(
            OllamaChatRequestDTOSerializer,
            OllamaChatRequestDTO(
                model = model.id,
                messages = prompt.toOllamaChatMessages(model),
                tools = ollamaTools,
                format = prompt.extractOllamaJsonFormat(),
                options = extractOllamaOptions(prompt, model),
                stream = false,
                additionalProperties = prompt.params.additionalProperties
            )
        )

        val response = client.post(DEFAULT_MESSAGE_PATH) {
            setBody(request)
        }

        if (response.status.isSuccess()) {
            return parseResponse(response.body<OllamaChatResponseDTO>())
        } else {
            // TODO: after the update to the KoogHttpClient, delegate this logic to the http client

            val httpClientException = KoogHttpClientException(
                statusCode = response.status.value,
                errorBody = response.bodyAsText(),
            )
            val exception = LLMClientException(
                clientName = clientName,
                message = httpClientException.message,
                cause = httpClientException,
            )
            logger.error(exception) { exception.message }
            throw exception
        }
    }