├── .nvmrc
├── modules
├── build.gradle.kts
├── client
│ ├── src
│ │ └── commonMain
│ │ │ └── kotlin
│ │ │ ├── api
│ │ │ ├── HttpStatusCode.kt
│ │ │ ├── VersionResponse.kt
│ │ │ ├── RateLimitException.kt
│ │ │ ├── InvalidRequestException.kt
│ │ │ ├── MonitoringResponse.kt
│ │ │ ├── PermissionException.kt
│ │ │ ├── AuthenticationException.kt
│ │ │ ├── FailureResponse.kt
│ │ │ ├── ListModelsResponse.kt
│ │ │ ├── EmbeddedInput.kt
│ │ │ ├── UnknownAPIException.kt
│ │ │ ├── ApiException.kt
│ │ │ ├── ModelCapability.kt
│ │ │ ├── OllamaException.kt
│ │ │ ├── Role.kt
│ │ │ ├── OllamaIOException.kt
│ │ │ ├── ToolCall.kt
│ │ │ ├── ProgressResponse.kt
│ │ │ ├── Format.kt
│ │ │ ├── ModelDetails.kt
│ │ │ ├── Message.kt
│ │ │ ├── ShowModelResponse.kt
│ │ │ ├── DoneReason.kt
│ │ │ ├── EmbedResponse.kt
│ │ │ ├── Tool.kt
│ │ │ ├── OllamaModel.kt
│ │ │ ├── CheckBlobRequest.kt
│ │ │ ├── ProcessResponse.kt
│ │ │ ├── CreateBlobRequest.kt
│ │ │ ├── DeleteModelRequest.kt
│ │ │ ├── ShowModelRequest.kt
│ │ │ ├── ChatResponse.kt
│ │ │ ├── CopyModelRequest.kt
│ │ │ ├── OctetByteArray.kt
│ │ │ ├── ChatCompletionResponse.kt
│ │ │ ├── PushModelRequest.kt
│ │ │ ├── EmbedRequest.kt
│ │ │ ├── PullModelRequest.kt
│ │ │ ├── ChatRequest.kt
│ │ │ ├── Options.kt
│ │ │ ├── CreateModelRequest.kt
│ │ │ └── ChatCompletionRequest.kt
│ │ │ ├── dsl
│ │ │ └── OllamaDslMarker.kt
│ │ │ └── annotations
│ │ │ ├── Version.kt
│ │ │ ├── Since.kt
│ │ │ ├── ExperimentalSince.kt
│ │ │ ├── InternalApi.kt
│ │ │ └── DeprecatedSince.kt
│ └── build.gradle.kts
└── client-ktor
│ ├── src
│ ├── commonMain
│ │ └── kotlin
│ │ │ └── client
│ │ │ └── ktor
│ │ │ ├── OllamaClientConfig.kt
│ │ │ ├── OllamaClient.kt
│ │ │ ├── HttpTransport.kt
│ │ │ └── OllamaService.kt
│ └── commonTest
│ │ └── kotlin
│ │ ├── MockHttpClientEngineFactory.kt
│ │ ├── VersionClientTest.kt
│ │ ├── MonitoringClientTest.kt
│ │ ├── EmbedClientTest.kt
│ │ └── ChatCompletionsClientTest.kt
│ ├── build.gradle.kts
│ └── api
│ └── client-ktor.api
├── samples
├── build.gradle.kts
├── README.md
├── client-sample-nodejs
│ ├── src
│ │ └── jsMain
│ │ │ └── kotlin
│ │ │ └── Application.kt
│ └── build.gradle.kts
└── client-sample-cio
│ ├── src
│ └── jvmMain
│ │ └── kotlin
│ │ └── Application.kt
│ └── build.gradle.kts
├── publishing
├── build.gradle.kts
├── README.md
├── version-catalog
│ └── build.gradle.kts
└── bom
│ ├── build.gradle.kts
│ └── README.md
├── reporting
├── build.gradle.kts
├── README.md
└── kover
│ └── build.gradle.kts
├── .github
├── FUNDING.yml
├── ISSUE_TEAMPLATE
│ ├── custom.md
│ ├── feature_request.md
│ └── bug_report.md
├── workflows
│ ├── run-build.yml
│ ├── ci-cleanup.yml
│ ├── ci-publish.yml
│ ├── ci-weekly.yml
│ ├── ci-main.yml
│ ├── ci-branch.yml
│ ├── ci-pr.yml
│ ├── run-workflow-cleanup.yml
│ ├── build-jvm.yml
│ ├── build-native-linux.yml
│ ├── build-native-macos.yml
│ ├── build-native-windows.yml
│ ├── ci-codeql.yml
│ └── run-publish.yml
├── PULL_REQUEST_TEMPLATE.md
├── CONTRIBUTING.md
├── SECURITY.md
├── actions
│ ├── pr-title-validation
│ │ └── action.yml
│ ├── commit-messages-validation
│ │ └── action.yml
│ └── setup
│ │ └── action.yml
└── COMPATIBILITY.md
├── CHANGELOG.md
├── gradle
├── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── js
│ └── karma
│ │ └── conf.js
├── detekt
│ └── detekt.yml
├── wasm
│ └── yarn.lock
└── catalogs
│ └── libraries.versions.toml
├── jitpack.yml
├── .sdkmanrc
├── .idea
├── codeStyles
│ ├── codeStyleConfig.xml
│ └── Project.xml
├── inspectionProfiles
│ └── Project_Default.xml
└── dictionaries
│ └── ProjectDictionary.xml
├── .editorconfig
├── settings.gradle.kts
├── gradle.properties
├── .gitattributes
├── gradlew.bat
├── .gitignore
├── README.md
└── gradlew
/.nvmrc:
--------------------------------------------------------------------------------
1 | v22
2 |
--------------------------------------------------------------------------------
/modules/build.gradle.kts:
--------------------------------------------------------------------------------
1 | description = "Modules"
2 |
--------------------------------------------------------------------------------
/samples/build.gradle.kts:
--------------------------------------------------------------------------------
1 | description = "Samples"
2 |
--------------------------------------------------------------------------------
/publishing/build.gradle.kts:
--------------------------------------------------------------------------------
1 | description = "Publishing"
2 |
--------------------------------------------------------------------------------
/reporting/build.gradle.kts:
--------------------------------------------------------------------------------
1 | description = "Reporting"
2 |
--------------------------------------------------------------------------------
/reporting/README.md:
--------------------------------------------------------------------------------
1 | # Reporting
2 |
3 | Aggregation of reporting tools.
4 |
--------------------------------------------------------------------------------
/.github/FUNDING.yml:
--------------------------------------------------------------------------------
1 | # These are supported funding model platforms
2 | buy_me_a_coffee: kkadete
3 |
--------------------------------------------------------------------------------
/CHANGELOG.md:
--------------------------------------------------------------------------------
1 |
2 | # v0.1.1
3 |
4 | ## Changes
5 |
6 | Initial Ollama client implementation.
7 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/nirmato/nirmato-ollama/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/jitpack.yml:
--------------------------------------------------------------------------------
1 | # JitPack configuration file
2 | # See https://docs.jitpack.io/building/#java-version
3 | jdk:
4 | - openjdk21
5 |
--------------------------------------------------------------------------------
/.sdkmanrc:
--------------------------------------------------------------------------------
1 | # Enable auto-env through the sdkman_auto_env config
2 | # Add key=value pairs of SDKs to use below
3 | java=21.0.8-zulu
4 |
--------------------------------------------------------------------------------
/modules/client/src/commonMain/kotlin/api/HttpStatusCode.kt:
--------------------------------------------------------------------------------
1 | package org.nirmato.ollama.api
2 |
3 | public typealias HttpStatusCode = Int
4 |
--------------------------------------------------------------------------------
/publishing/README.md:
--------------------------------------------------------------------------------
1 | # Publishing
2 |
3 | This module's primary purpose is to publish library artifacts to Maven Central for user-friendly dependency management.
4 |
--------------------------------------------------------------------------------
/.github/ISSUE_TEAMPLATE/custom.md:
--------------------------------------------------------------------------------
1 | ---
2 | name: Custom issue template
3 | about: Describe this issue template's purpose here.
4 | title: ''
5 | labels: ''
6 | assignees: ''
7 |
8 | ---
9 |
--------------------------------------------------------------------------------
/.idea/codeStyles/codeStyleConfig.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/gradle/js/karma/conf.js:
--------------------------------------------------------------------------------
1 | config.client = config.client || {}
2 | config.client.mocha = config.client.mocha || {}
3 | config.client.mocha.timeout = '300s'
4 | config.browserNoActivityTimeout = 300000
5 | config.browserDisconnectTimeout = 300000
6 |
--------------------------------------------------------------------------------
/.github/workflows/run-build.yml:
--------------------------------------------------------------------------------
1 | name: Run build
2 |
3 | on:
4 | workflow_dispatch:
5 | workflow_call:
6 |
7 | permissions:
8 | contents: read
9 |
10 | jobs:
11 | build:
12 | uses: ./.github/workflows/build-jvm.yml
13 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | distributionBase=GRADLE_USER_HOME
2 | distributionPath=wrapper/dists
3 | distributionUrl=https\://services.gradle.org/distributions/gradle-9.2.0-all.zip
4 | networkTimeout=10000
5 | validateDistributionUrl=true
6 | zipStoreBase=GRADLE_USER_HOME
7 | zipStorePath=wrapper/dists
8 |
--------------------------------------------------------------------------------
/modules/client/src/commonMain/kotlin/dsl/OllamaDslMarker.kt:
--------------------------------------------------------------------------------
1 | package org.nirmato.ollama.dsl
2 |
3 | /**
4 | * A marker annotations for DSLs.
5 | */
6 | @DslMarker
7 | @Target(AnnotationTarget.CLASS, AnnotationTarget.TYPEALIAS, AnnotationTarget.TYPE, AnnotationTarget.FUNCTION)
8 | public annotation class OllamaDslMarker
9 |
--------------------------------------------------------------------------------
/gradle/detekt/detekt.yml:
--------------------------------------------------------------------------------
1 | naming:
2 | InvalidPackageDeclaration:
3 | active: false
4 | complexity:
5 | TooManyFunctions:
6 | active: true
7 | thresholdInFiles: 20
8 | thresholdInClasses: 15
9 | style:
10 | MaxLineLength:
11 | active: true
12 | maxLineLength: 180
13 |
--------------------------------------------------------------------------------
/.idea/inspectionProfiles/Project_Default.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/modules/client/src/commonMain/kotlin/api/VersionResponse.kt:
--------------------------------------------------------------------------------
1 | package org.nirmato.ollama.api
2 |
3 | import kotlinx.serialization.SerialName
4 | import kotlinx.serialization.Serializable
5 |
6 | @Serializable
7 | public data class VersionResponse(
8 | @SerialName(value = "version")
9 | public val version: String,
10 | )
11 |
--------------------------------------------------------------------------------
/modules/client/src/commonMain/kotlin/api/RateLimitException.kt:
--------------------------------------------------------------------------------
1 | package org.nirmato.ollama.api
2 |
3 | /**
4 | * Represents an exception thrown when the Ollama API rate limit is exceeded.
5 | */
6 | public class RateLimitException(
7 | statusCode: HttpStatusCode,
8 | error: FailureResponse,
9 | throwable: Throwable? = null,
10 | ) : ApiException(statusCode, error, throwable)
11 |
--------------------------------------------------------------------------------
/modules/client/src/commonMain/kotlin/api/InvalidRequestException.kt:
--------------------------------------------------------------------------------
1 | package org.nirmato.ollama.api
2 |
3 | /**
4 | * Represents an exception thrown when an invalid request is made to the Ollama API.
5 | */
6 | public class InvalidRequestException(
7 | statusCode: HttpStatusCode,
8 | error: FailureResponse,
9 | throwable: Throwable? = null,
10 | ) : ApiException(statusCode, error, throwable)
11 |
--------------------------------------------------------------------------------
/modules/client/src/commonMain/kotlin/api/MonitoringResponse.kt:
--------------------------------------------------------------------------------
1 | package org.nirmato.ollama.api
2 |
3 | import kotlinx.serialization.Contextual
4 | import kotlinx.serialization.SerialName
5 | import kotlinx.serialization.Serializable
6 |
7 | @Serializable
8 | public data class MonitoringResponse(
9 |
10 | @SerialName(value = "status")
11 | @Contextual
12 | public val status: HttpStatusCode,
13 | )
14 |
--------------------------------------------------------------------------------
/modules/client/src/commonMain/kotlin/annotations/Version.kt:
--------------------------------------------------------------------------------
1 | package org.nirmato.ollama.annotations
2 |
3 | @InternalApi
4 | public enum class Version {
5 | Unreleased,
6 | V0_1_0;
7 |
8 | override fun toString(): String {
9 | return if (this == Unreleased) {
10 | name.lowercase()
11 | } else {
12 | name.drop(1).replace('_', '.')
13 | }
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/modules/client/src/commonMain/kotlin/api/PermissionException.kt:
--------------------------------------------------------------------------------
1 | package org.nirmato.ollama.api
2 |
3 | /**
4 | * Represents an exception thrown when a permission error occurs while interacting with the Ollama API.
5 | */
6 | public class PermissionException(
7 | statusCode: HttpStatusCode,
8 | error: FailureResponse,
9 | throwable: Throwable? = null,
10 | ) : ApiException(statusCode, error, throwable)
11 |
--------------------------------------------------------------------------------
/modules/client-ktor/src/commonMain/kotlin/client/ktor/OllamaClientConfig.kt:
--------------------------------------------------------------------------------
1 | package org.nirmato.ollama.client.ktor
2 |
3 | import kotlinx.serialization.json.Json
4 |
5 | /**
6 | * Configuration settings for [OllamaClient].
7 | *
8 | * @property jsonConfig JSON serialization configuration used for requests and responses.
9 | */
10 | public class OllamaClientConfig(
11 | public val jsonConfig: Json = Json,
12 | )
13 |
--------------------------------------------------------------------------------
/modules/client/src/commonMain/kotlin/api/AuthenticationException.kt:
--------------------------------------------------------------------------------
1 | package org.nirmato.ollama.api
2 |
3 | /**
4 | * Represents an exception thrown when an authentication error occurs while interacting with the Ollama API.
5 | */
6 | public class AuthenticationException(
7 | statusCode: HttpStatusCode,
8 | error: FailureResponse,
9 | throwable: Throwable? = null,
10 | ) : ApiException(statusCode, error, throwable)
11 |
--------------------------------------------------------------------------------
/.idea/dictionaries/ProjectDictionary.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Dfile
5 | Dkotlin
6 | Dorg
7 | Foojay
8 | Konan
9 | codeql
10 | kkadete
11 | llava
12 | nirmato
13 | ollama
14 | tinyllama
15 |
16 |
17 |
--------------------------------------------------------------------------------
/modules/client/src/commonMain/kotlin/api/FailureResponse.kt:
--------------------------------------------------------------------------------
1 | package org.nirmato.ollama.api
2 |
3 | import kotlinx.serialization.SerialName
4 | import kotlinx.serialization.Serializable
5 |
6 | /**
7 | * Represents an error response from the Ollama API.
8 | */
9 | @Serializable
10 | public data class FailureResponse(
11 | /** Information about the error that occurred. */
12 | @SerialName("error")
13 | public val message: String? = null,
14 | )
15 |
--------------------------------------------------------------------------------
/.github/PULL_REQUEST_TEMPLATE.md:
--------------------------------------------------------------------------------
1 |
2 |
3 | - [ ] Have you followed the [contributing guidelines](CONTRIBUTING.md)?
4 | - [ ] Have you added tests that validate the new capability or bug fix?
5 | - [ ] Does your submission align with the current code style?
6 | - [ ] Have you checked that there aren't other open [pull requests](https://github.com/nirmato/nirmato-ollama/pulls) for the same change?
7 |
--------------------------------------------------------------------------------
/.github/workflows/ci-cleanup.yml:
--------------------------------------------------------------------------------
1 | name: CI Cleanup
2 |
3 | on:
4 | workflow_dispatch:
5 | schedule:
6 | - cron: '33 8 * * 0' # Runs weekly at 08:33 on Sunday UTC
7 |
8 | permissions:
9 | contents: read
10 | actions: write
11 |
12 | concurrency:
13 | group: ci-cleanup-${{ github.workflow }}-${{ github.ref }}
14 | cancel-in-progress: true
15 |
16 | jobs:
17 | build:
18 | uses: ./.github/workflows/run-workflow-cleanup.yml
19 |
--------------------------------------------------------------------------------
/modules/client/src/commonMain/kotlin/api/ListModelsResponse.kt:
--------------------------------------------------------------------------------
1 | package org.nirmato.ollama.api
2 |
3 | import kotlinx.serialization.SerialName
4 | import kotlinx.serialization.Serializable
5 |
6 | /**
7 | * Response class for the list models endpoint.
8 | */
9 | @Serializable
10 | public data class ListModelsResponse(
11 |
12 | /** List of models available locally. */
13 | @SerialName(value = "models")
14 | val models: List? = null,
15 | )
16 |
--------------------------------------------------------------------------------
/.editorconfig:
--------------------------------------------------------------------------------
1 | # EditorConfig is awesome: https://editorconfig.org/
2 |
3 | # This is the file in the root of the project.
4 | root = true
5 |
6 | [*]
7 | charset = utf-8
8 | end_of_line = lf
9 | max_line_length=180
10 | indent_style = space
11 | indent_size = 4
12 | tab_width = 4
13 | trim_trailing_whitespace = true
14 | insert_final_newline = true
15 |
16 | [*.{cmd,bat}]
17 | end_of_line = crlf
18 |
19 | [*.md]
20 | max_line_length = off
21 | trim_trailing_whitespace = false
22 |
--------------------------------------------------------------------------------
/samples/README.md:
--------------------------------------------------------------------------------
1 | # Samples
2 |
3 | JVM, JS and Native sample applications.
4 |
5 | ### Running the apps
6 |
7 | | Target | command |
8 | |-------------------------|----------------------------------------------------------------|
9 | | client-sample-cio-jvm | `./gradlew :samples:client-sample-cio:jvmRun` |
10 | | client-sample-nodejs-js | `./gradlew :samples:client-sample-nodejs:jsNodeDevelopmentRun` |
11 |
--------------------------------------------------------------------------------
/modules/client/src/commonMain/kotlin/api/EmbeddedInput.kt:
--------------------------------------------------------------------------------
1 | package org.nirmato.ollama.api
2 |
3 | import kotlin.jvm.JvmInline
4 | import kotlinx.serialization.Serializable
5 |
6 | @Serializable
7 | public sealed interface EmbeddedInput {
8 |
9 | @JvmInline
10 | @Serializable
11 | public value class EmbeddedText(public val text: String) : EmbeddedInput
12 |
13 | @JvmInline
14 | @Serializable
15 | public value class EmbeddedList(public val texts: List) : EmbeddedInput
16 | }
17 |
--------------------------------------------------------------------------------
/modules/client/src/commonMain/kotlin/api/UnknownAPIException.kt:
--------------------------------------------------------------------------------
1 | package org.nirmato.ollama.api
2 |
3 | /**
4 | * Represents an exception thrown when an unknown error occurs while interacting with the Ollama API.
5 | * This exception is used when the specific type of error is not covered by the existing subclasses.
6 | */
7 | public class UnknownAPIException(
8 | statusCode: HttpStatusCode,
9 | error: FailureResponse,
10 | throwable: Throwable? = null,
11 | ) : ApiException(statusCode, error, throwable)
12 |
--------------------------------------------------------------------------------
/modules/client/src/commonMain/kotlin/annotations/Since.kt:
--------------------------------------------------------------------------------
1 | package org.nirmato.ollama.annotations
2 |
3 | import kotlin.annotation.AnnotationRetention.SOURCE
4 | import kotlin.annotation.AnnotationTarget.CLASS
5 | import kotlin.annotation.AnnotationTarget.FUNCTION
6 | import kotlin.annotation.AnnotationTarget.PROPERTY
7 | import kotlin.annotation.AnnotationTarget.TYPEALIAS
8 |
9 | @InternalApi
10 | @MustBeDocumented
11 | @Retention(SOURCE)
12 | @Target(CLASS, FUNCTION, PROPERTY, TYPEALIAS)
13 | public annotation class Since(val version: Version)
14 |
--------------------------------------------------------------------------------
/modules/client/src/commonMain/kotlin/api/ApiException.kt:
--------------------------------------------------------------------------------
1 | package org.nirmato.ollama.api
2 |
3 | /**
4 | * Represents an exception thrown when an error occurs while interacting with the Ollama API.
5 | */
6 | public sealed class ApiException(
7 | /** The HTTP status code associated with the error. */
8 | public val statusCode: HttpStatusCode,
9 | /** The error that occurred. */
10 | public val failure: FailureResponse,
11 | throwable: Throwable? = null,
12 | ) : OllamaException(message = failure.message, throwable = throwable)
13 |
--------------------------------------------------------------------------------
/modules/client-ktor/src/commonTest/kotlin/MockHttpClientEngineFactory.kt:
--------------------------------------------------------------------------------
1 | package org.nirmato.ollama.client.ktor
2 |
3 | import io.ktor.client.engine.HttpClientEngine
4 | import io.ktor.client.engine.HttpClientEngineFactory
5 | import io.ktor.client.engine.mock.MockEngine
6 | import io.ktor.client.engine.mock.MockEngineConfig
7 |
8 | internal class MockHttpClientEngineFactory : HttpClientEngineFactory {
9 | override fun create(block: MockEngineConfig.() -> Unit): HttpClientEngine {
10 | return MockEngine.Companion.create(block)
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/modules/client/src/commonMain/kotlin/api/ModelCapability.kt:
--------------------------------------------------------------------------------
1 | package org.nirmato.ollama.api
2 |
3 | import kotlinx.serialization.SerialName
4 | import kotlinx.serialization.Serializable
5 |
6 | @Serializable
7 | public enum class ModelCapability {
8 | @SerialName("completion")
9 | Completion,
10 |
11 | @SerialName("tools")
12 | Tools,
13 |
14 | @SerialName("insert")
15 | Insert,
16 |
17 | @SerialName("vision")
18 | Vision,
19 |
20 | @SerialName("embedding")
21 | Embedding,
22 |
23 | @SerialName("thinking")
24 | Thinking,
25 | }
26 |
--------------------------------------------------------------------------------
/.github/workflows/ci-publish.yml:
--------------------------------------------------------------------------------
1 | name: CI Publish
2 |
3 | on:
4 | workflow_dispatch:
5 | inputs:
6 | version:
7 | description: 'Version to publish'
8 | required: true
9 |
10 | permissions:
11 | contents: read
12 |
13 | concurrency:
14 | group: ci-publish-${{ github.workflow }}-${{ github.event.ref }}
15 | cancel-in-progress: true
16 |
17 | jobs:
18 | publish:
19 | uses: ./.github/workflows/run-publish.yml
20 | secrets: inherit
21 | with:
22 | version: ${{ github.event.inputs.version }}
23 |
--------------------------------------------------------------------------------
/modules/client/src/commonMain/kotlin/api/OllamaException.kt:
--------------------------------------------------------------------------------
1 | package org.nirmato.ollama.api
2 |
3 | /** Ollama client exception */
4 | public sealed class OllamaException(message: String? = null, throwable: Throwable? = null) : RuntimeException(message, throwable)
5 |
6 | /** Runtime Http Client exception */
7 | public class OllamaClientException(throwable: Throwable? = null) : OllamaException(throwable?.message, throwable)
8 |
9 | /** An exception thrown in case of a server error */
10 | public class OllamaServerException(throwable: Throwable? = null) : OllamaException(throwable?.message, throwable)
11 |
--------------------------------------------------------------------------------
/.github/CONTRIBUTING.md:
--------------------------------------------------------------------------------
1 | # Contributing to the repository
2 |
3 | Please feel free to submit a pull request. Contributions are welcome!
4 |
5 | * [Submitting bug reports](https://github.com/nirmato/nirmato-ollama/issues/new?labels=bug&template=bug-report.md)
6 | * [Submitting feature requests](https://github.com/nirmato/nirmato-ollama/issues/new?labels=enhancement&template=feature-request.md) to enhance the existing data structures of this library
7 |
8 | ## License
9 | By contributing, you agree that your contributions will be licensed under the [Apache-2.0 License](http://www.apache.org/licenses/LICENSE-2.0) of this library.
10 |
--------------------------------------------------------------------------------
/modules/client/src/commonMain/kotlin/api/Role.kt:
--------------------------------------------------------------------------------
1 | package org.nirmato.ollama.api
2 |
3 | import kotlinx.serialization.SerialName
4 | import kotlinx.serialization.Serializable
5 |
6 | /**
7 | * The role of the message, either `system`, `user`, `assistant`, or `tool`.
8 | */
9 | @Serializable
10 | public enum class Role(public val value: String) {
11 | @SerialName(value = "system")
12 | SYSTEM("system"),
13 |
14 | @SerialName(value = "user")
15 | USER("user"),
16 |
17 | @SerialName(value = "assistant")
18 | ASSISTANT("assistant"),
19 |
20 | @SerialName(value = "tool")
21 | TOOL("tool");
22 | }
23 |
--------------------------------------------------------------------------------
/modules/client/src/commonMain/kotlin/api/OllamaIOException.kt:
--------------------------------------------------------------------------------
1 | package org.nirmato.ollama.api
2 |
3 | /** An exception thrown in case of an I/O error */
4 | public sealed class OllamaIOException(throwable: Throwable? = null) : OllamaException(message = throwable?.message, throwable = throwable)
5 |
6 | /** An exception thrown in case a request times out. */
7 | public class OllamaTimeoutException(throwable: Throwable? = null) : OllamaIOException(throwable = throwable)
8 |
9 | /** An exception thrown in case of an I/O error */
10 | public class OllamaGenericIOException(throwable: Throwable? = null) : OllamaIOException(throwable = throwable)
11 |
--------------------------------------------------------------------------------
/.github/ISSUE_TEAMPLATE/feature_request.md:
--------------------------------------------------------------------------------
1 | ---
2 | name: Feature request
3 | about: Suggest an idea to make this library even more awesome
4 | title: ''
5 | labels: enhancement
6 | assignees: ''
7 |
8 | ---
9 |
10 | **Is your feature request related to a problematic scenario? Please describe.**
11 | A clear and concise description of a simplified scenario.
12 |
13 | **Describe the solution you'd like**
14 | A clear and concise description of what you want to happen.
15 |
16 | **Describe alternatives you've considered**
17 | A clear and concise description of any alternative solutions or features you've considered.
18 |
19 | **Additional context**
20 | Add any other context about the feature request here.
21 |
--------------------------------------------------------------------------------
/modules/client/src/commonMain/kotlin/annotations/ExperimentalSince.kt:
--------------------------------------------------------------------------------
1 | package org.nirmato.ollama.annotations
2 |
3 | import kotlin.RequiresOptIn.Level
4 | import kotlin.annotation.AnnotationRetention.BINARY
5 | import kotlin.annotation.AnnotationTarget.CLASS
6 | import kotlin.annotation.AnnotationTarget.FUNCTION
7 | import kotlin.annotation.AnnotationTarget.PROPERTY
8 | import kotlin.annotation.AnnotationTarget.TYPEALIAS
9 |
10 | @RequiresOptIn(
11 | message = "An API that is still in the experimental stage.",
12 | level = Level.WARNING,
13 | )
14 | @MustBeDocumented
15 | @InternalApi
16 | @Retention(BINARY)
17 | @Target(CLASS, FUNCTION, PROPERTY, TYPEALIAS)
18 | public annotation class ExperimentalSince(val version: Version)
19 |
--------------------------------------------------------------------------------
/modules/client/src/commonMain/kotlin/api/ToolCall.kt:
--------------------------------------------------------------------------------
1 | package org.nirmato.ollama.api
2 |
3 | import kotlinx.serialization.SerialName
4 | import kotlinx.serialization.Serializable
5 | import kotlinx.serialization.json.JsonElement
6 |
7 | /**
8 | * A tool call in a message.
9 | */
10 | @Serializable
11 | public data class ToolCall(
12 | @SerialName(value = "function")
13 | public val function: FunctionCall,
14 | ) {
15 | /**
16 | * Function details for a tool call.
17 | */
18 | @Serializable
19 | public data class FunctionCall(
20 | @SerialName(value = "name")
21 | public val name: String,
22 |
23 | @SerialName(value = "arguments")
24 | public val arguments: Map,
25 | )
26 | }
27 |
--------------------------------------------------------------------------------
/.github/SECURITY.md:
--------------------------------------------------------------------------------
1 | # Security Policy
2 |
3 | ## Supported Versions
4 |
5 | We focus development efforts on the latest release of the library to reduce maintenance overhead and increase the pace of improvement.
6 |
7 | We recommend staying up to date with the latest version of this library as that will provide the most secure and best experience.
8 |
9 | ## Supported Versions
10 |
11 | | Version | Supported |
12 | |---------|--------------------|
13 | | 1.0.x | :white_check_mark: |
14 | | < 1.0.0 | :x: |
15 |
16 | ## Reporting a Vulnerability
17 |
18 | Please create a [Bug Report](https://github.com/nirmato/nirmato-ollama/issues/new?labels=bug&template=bug_report.md) documenting the security vulnerability clearly along with examples of how to encounter it.
19 |
--------------------------------------------------------------------------------
/modules/client/src/commonMain/kotlin/annotations/InternalApi.kt:
--------------------------------------------------------------------------------
1 | package org.nirmato.ollama.annotations
2 |
3 | import kotlin.annotation.AnnotationRetention.BINARY
4 | import kotlin.annotation.AnnotationTarget.CLASS
5 | import kotlin.annotation.AnnotationTarget.CONSTRUCTOR
6 | import kotlin.annotation.AnnotationTarget.FUNCTION
7 | import kotlin.annotation.AnnotationTarget.PROPERTY
8 | import kotlin.annotation.AnnotationTarget.TYPEALIAS
9 |
10 | @RequiresOptIn(
11 | message = "An API for internal use only. It may be changed or deleted at any time and compatibility is not guaranteed.",
12 | level = RequiresOptIn.Level.ERROR,
13 | )
14 | @MustBeDocumented
15 | @Retention(BINARY)
16 | @Target(CLASS, PROPERTY, CONSTRUCTOR, FUNCTION, TYPEALIAS)
17 | public annotation class InternalApi
18 |
--------------------------------------------------------------------------------
/settings.gradle.kts:
--------------------------------------------------------------------------------
1 | import org.gradle.api.provider.gradleBooleanProperty
2 |
3 | pluginManagement {
4 | includeBuild("build-settings-logic")
5 | includeBuild("build-logic")
6 | }
7 |
8 | plugins {
9 | id("build-settings")
10 | id("build-foojay-toolchains")
11 | }
12 |
13 | rootProject.name = "nirmato-ollama"
14 |
15 | include("modules:client")
16 | include("modules:client-ktor")
17 |
18 | include("publishing:bom")
19 | include("publishing:version-catalog")
20 |
21 | include("reporting:kover")
22 |
23 | if(providers.gradleBooleanProperty("kotlin.targets.jvm.enabled").get()) {
24 | include("samples:client-sample-cio")
25 | }
26 |
27 | if(providers.gradleBooleanProperty("kotlin.targets.js.enabled").get()) {
28 | include("samples:client-sample-nodejs")
29 | }
30 |
--------------------------------------------------------------------------------
/.github/workflows/ci-weekly.yml:
--------------------------------------------------------------------------------
1 | name: 'Stale Pull Requests'
2 |
3 | on:
4 | workflow_dispatch:
5 |
6 | permissions:
7 | issues: none
8 | pull-requests: write
9 |
10 | jobs:
11 | weekly:
12 | runs-on: ubuntu-latest
13 | steps:
14 | - uses: actions/stale@3a9db7e6a41a89f618792c92c0e97cc736e1b13f #v10.0.0
15 | id: stale
16 | with:
17 | stale-pr-message: 'This pull request has been open and inactive. It will be closed shortly.'
18 | days-before-stale: 60
19 | days-before-close: 7
20 | exempt-draft-pr: true
21 | exempt-pr-labels: 'WIP'
22 | - name: Print outputs
23 | run: echo ${{ join(steps.stale.outputs.*, ',') }}
24 |
--------------------------------------------------------------------------------
/publishing/version-catalog/build.gradle.kts:
--------------------------------------------------------------------------------
1 | plugins {
2 | id("version-catalog")
3 | id("build-maven-publish")
4 | }
5 |
6 | description = "Version Catalog"
7 |
8 | catalog {
9 | versionCatalog {
10 | version("kotlin", libraries.versions.kotlin.get())
11 |
12 | rootProject.subprojects.forEach { subproject ->
13 | if (subproject.plugins.hasPlugin("maven-publish") && subproject.name != name) {
14 | subproject.publishing.publications.withType().configureEach {
15 | if (!artifactId.endsWith("-metadata") && !artifactId.endsWith("-kotlinMultiplatform")) {
16 | library(artifactId, "$groupId:$artifactId:$version")
17 | }
18 | }
19 | }
20 | }
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/modules/client/src/commonMain/kotlin/annotations/DeprecatedSince.kt:
--------------------------------------------------------------------------------
1 | package org.nirmato.ollama.annotations
2 |
3 | import kotlin.RequiresOptIn.Level
4 | import kotlin.annotation.AnnotationRetention.BINARY
5 | import kotlin.annotation.AnnotationTarget.CLASS
6 | import kotlin.annotation.AnnotationTarget.FUNCTION
7 | import kotlin.annotation.AnnotationTarget.PROPERTY
8 | import kotlin.annotation.AnnotationTarget.TYPEALIAS
9 |
10 | @RequiresOptIn(
11 | message = "An API that is deprecated stage.",
12 | level = Level.WARNING,
13 | )
14 | @MustBeDocumented
15 | @InternalApi
16 | @Retention(BINARY)
17 | @Target(CLASS, FUNCTION, PROPERTY, TYPEALIAS)
18 | public annotation class DeprecatedSince(
19 | val warningSince: Version,
20 | val errorSince: Version = Version.Unreleased,
21 | val hiddenSince: Version = Version.Unreleased,
22 | )
23 |
--------------------------------------------------------------------------------
/publishing/bom/build.gradle.kts:
--------------------------------------------------------------------------------
1 | plugins {
2 | id("java-platform")
3 | id("build-maven-publish")
4 | }
5 |
6 | description = "BOM"
7 |
8 | val me = project
9 | rootProject.subprojects {
10 | if (name != me.name) {
11 | me.evaluationDependsOn(path)
12 | }
13 | }
14 |
15 | dependencies {
16 | constraints {
17 | rootProject.subprojects.forEach { subproject ->
18 | if (subproject.plugins.hasPlugin("maven-publish") && !subproject.name.endsWith("version-catalog") && subproject.name != name) {
19 | subproject.publishing.publications.withType().configureEach {
20 | if (!artifactId.endsWith("-metadata") && !artifactId.endsWith("-kotlinMultiplatform")) {
21 | api("$groupId:$artifactId:$version")
22 | }
23 | }
24 | }
25 | }
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | org.gradle.version=9.2.0
2 | org.gradle.jvmargs=-Dfile.encoding=UTF-8
3 | org.gradle.java.installations.auto-detect=true
4 | org.gradle.java.installations.auto-download=false
5 | org.gradle.publications.signing.enabled=true
6 |
7 | kotlin.code.style=official
8 |
9 | kotlin.javaToolchain.mainJvmCompiler=21
10 |
11 | kotlin.compilerOptions.apiVersion=2.0
12 | kotlin.compilerOptions.languageVersion=2.0
13 |
14 | kotlin.incremental.wasm=true
15 |
16 | kotlin.experimental.tryNext=false
17 |
18 | kotlin.targets.jvm.enabled=true
19 | kotlin.targets.jvm.target=11
20 | kotlin.targets.js.enabled=false
21 | kotlin.targets.wasmJs.enabled=false
22 | kotlin.targets.wasmWasi.enabled=false
23 | kotlin.targets.windows.enabled=false
24 | kotlin.targets.linux.enabled=false
25 | kotlin.targets.macos.enabled=false
26 | kotlin.targets.tvos.enabled=false
27 | kotlin.targets.watchos.enabled=false
28 | kotlin.targets.ios.enabled=false
29 |
--------------------------------------------------------------------------------
/.github/ISSUE_TEAMPLATE/bug_report.md:
--------------------------------------------------------------------------------
1 | ---
2 | name: Bug report
3 | about: Report a bug to help us improve
4 | title: ''
5 | labels: bug
6 | assignees: ''
7 |
8 | ---
9 |
10 | **Describe the bug**
11 | A clear and concise description of what the bug is.
12 |
13 | **To Reproduce**
14 | Sample code snippet to reproduce the problem:
15 | ```kotlin
16 | val ollamaClient = OllamaClient(CIO)
17 |
18 | val request = chatRequest {
19 | model("tinyllama")
20 | messages(listOf(Message(role = USER, content = "Why is the sky blue?")))
21 | }
22 |
23 | val response = ollamaClient.chat(request)
24 |
25 | println(response)
26 | ```
27 |
28 | **Expected behavior**
29 | A clear and concise description of what you expected to happen.
30 |
31 | **Environment (please complete the following information):**
32 | - Kotlin version: [eg. 2.2.0]
33 | - JDK: [eg. OpenJDK 17]
34 |
35 | **Additional context**
36 | Add any other context about the problem here.
37 |
--------------------------------------------------------------------------------
/samples/client-sample-nodejs/src/jsMain/kotlin/Application.kt:
--------------------------------------------------------------------------------
1 | package org.nirmato.ollama.client.samples
2 |
3 | import io.ktor.client.engine.js.Js
4 | import io.ktor.client.plugins.defaultRequest
5 | import org.nirmato.ollama.api.ChatRequest.Companion.chatRequest
6 | import org.nirmato.ollama.api.Message
7 | import org.nirmato.ollama.api.Role.USER
8 | import org.nirmato.ollama.client.ktor.OllamaClient
9 |
10 | suspend fun main() {
11 | val ollamaClient = OllamaClient(Js) {
12 | httpClient {
13 | defaultRequest {
14 | url("http://localhost:11434/api/")
15 | }
16 | }
17 | }
18 |
19 | val request = chatRequest {
20 | model("tinyllama")
21 | messages(listOf(Message(role = USER, content = "Why is the sky blue?")))
22 | }
23 |
24 | val response = ollamaClient.chat(request)
25 |
26 | println(response.toString())
27 |
28 | ollamaClient.close()
29 | }
30 |
--------------------------------------------------------------------------------
/.github/workflows/ci-main.yml:
--------------------------------------------------------------------------------
1 | name: CI Main
2 |
3 | on:
4 | workflow_dispatch:
5 | push:
6 | branches:
7 | - main
8 | paths-ignore:
9 | - "**.md"
10 | - ".idea/**"
11 | - ".editorconfig"
12 | - ".gitattributes"
13 | - ".gitignore"
14 | - "oas/**"
15 |
16 | permissions:
17 | contents: read
18 |
19 | concurrency:
20 | group: ci-main-${{ github.workflow }}-${{ github.ref }}
21 | cancel-in-progress: true
22 |
23 | jobs:
24 | validate:
25 | runs-on: ubuntu-latest
26 |
27 | steps:
28 | - name: Checkout
29 | uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
30 |
31 | - name: Validate commit messages
32 | uses: ./.github/actions/commit-messages-validation
33 |
34 | build:
35 | needs:
36 | - validate
37 | uses: ./.github/workflows/run-build.yml
38 |
--------------------------------------------------------------------------------
/samples/client-sample-cio/src/jvmMain/kotlin/Application.kt:
--------------------------------------------------------------------------------
1 | package org.nirmato.ollama.client.samples
2 |
3 | import kotlinx.coroutines.runBlocking
4 | import io.ktor.client.engine.cio.CIO
5 | import io.ktor.client.plugins.defaultRequest
6 | import org.nirmato.ollama.api.ChatRequest.Companion.chatRequest
7 | import org.nirmato.ollama.api.Message
8 | import org.nirmato.ollama.api.Role.USER
9 | import org.nirmato.ollama.client.ktor.OllamaClient
10 |
11 | fun main() = runBlocking {
12 | val ollamaClient = OllamaClient(CIO) {
13 | httpClient {
14 | defaultRequest {
15 | url("http://localhost:11434/api/")
16 | }
17 | }
18 | }
19 |
20 | val request = chatRequest {
21 | model("tinyllama")
22 | messages(listOf(Message(role = USER, content = "Why is the sky blue?")))
23 | }
24 |
25 | val response = ollamaClient.chat(request)
26 |
27 | println(response.toString())
28 |
29 | ollamaClient.close()
30 | }
31 |
--------------------------------------------------------------------------------
/.github/workflows/ci-branch.yml:
--------------------------------------------------------------------------------
1 | name: CI Branch
2 |
3 | on:
4 | workflow_dispatch:
5 | push:
6 | branches-ignore:
7 | - main
8 | paths-ignore:
9 | - "**.md"
10 | - ".idea/**"
11 | - ".editorconfig"
12 | - ".gitattributes"
13 | - ".gitignore"
14 | - "oas/**"
15 |
16 | permissions:
17 | contents: read
18 |
19 | concurrency:
20 | group: ci-branch-${{ github.workflow }}-${{ github.ref }}
21 | cancel-in-progress: true
22 |
23 | jobs:
24 | validate:
25 | runs-on: ubuntu-latest
26 |
27 | steps:
28 | - name: Checkout
29 | uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
30 |
31 | - name: Validate commit messages
32 | uses: ./.github/actions/commit-messages-validation
33 |
34 | build:
35 | needs:
36 | - validate
37 | uses: ./.github/workflows/run-build.yml
38 |
--------------------------------------------------------------------------------
/modules/client/src/commonMain/kotlin/api/ProgressResponse.kt:
--------------------------------------------------------------------------------
1 | package org.nirmato.ollama.api
2 |
3 | import kotlinx.serialization.SerialName
4 | import kotlinx.serialization.Serializable
5 |
6 | /**
7 | * Response class for pulling a model.
8 | * The first object is the manifest. Then there is a series of downloading responses. Until any of the download is completed, the `completed` key may not be included.
9 | * The number of files to be downloaded depends on the number of layers specified in the manifest.
10 | */
11 | @Serializable
12 | public data class ProgressResponse(
13 |
14 | @SerialName(value = "status")
15 | val status: String? = null,
16 |
17 | /** The model's digest. */
18 | @SerialName(value = "digest")
19 | val digest: String? = null,
20 |
21 | /** Total size of the model. */
22 | @SerialName(value = "total")
23 | val total: Long? = null,
24 |
25 | /** Total bytes transferred. */
26 | @SerialName(value = "completed")
27 | val completed: Long? = null,
28 | )
29 |
--------------------------------------------------------------------------------
/modules/client/src/commonMain/kotlin/api/Format.kt:
--------------------------------------------------------------------------------
1 | package org.nirmato.ollama.api
2 |
3 | import kotlin.jvm.JvmInline
4 | import kotlinx.serialization.Serializable
5 | import kotlinx.serialization.json.JsonObject
6 |
7 | @Serializable
8 | public sealed interface Format {
9 | /**
10 | * The format to return a response in. Currently, the only accepted value is json.
11 | *
12 | * Enable JSON mode by setting the format parameter to json. This will structure the response as valid JSON.
13 | * Note: it's important to instruct the model to use JSON in the prompt. Otherwise, the model may generate large amounts whitespace.
14 | */
15 | @JvmInline
16 | @Serializable
17 | public value class FormatType(public val value: String) : Format {
18 | public companion object {
19 | public val JSON: FormatType = FormatType("json")
20 | }
21 | }
22 |
23 | @JvmInline
24 | @Serializable
25 | public value class FormatSchema(public val schema: JsonObject) : Format
26 | }
27 |
--------------------------------------------------------------------------------
/modules/client/src/commonMain/kotlin/api/ModelDetails.kt:
--------------------------------------------------------------------------------
1 | package org.nirmato.ollama.api
2 |
3 | import kotlinx.serialization.SerialName
4 | import kotlinx.serialization.Serializable
5 |
6 | /**
7 | * Details about a model.
8 | */
9 | @Serializable
10 | public data class ModelDetails(
11 | @SerialName(value = "parent_model")
12 | val parentModel: String,
13 |
14 | /** The format of the model. */
15 | @SerialName(value = "format")
16 | val format: String? = null,
17 |
18 | /** The family of the model. */
19 | @SerialName(value = "family")
20 | val family: String? = null,
21 |
22 | /** The families of the model. */
23 | @SerialName(value = "families")
24 | val families: List? = null,
25 |
26 | /** The size of the model's parameters. */
27 | @SerialName(value = "parameter_size")
28 | val parameterSize: String? = null,
29 |
30 | /** The quantization level of the model. */
31 | @SerialName(value = "quantization_level")
32 | val quantizationLevel: String? = null,
33 | )
34 |
--------------------------------------------------------------------------------
/.gitattributes:
--------------------------------------------------------------------------------
1 | # Set default behavior to automatically normalize line endings.
2 | * text=auto eol=lf
3 |
4 | # Force batch scripts to always use CRLF line endings so that if a repo is accessed
5 | # in Windows via a file share from Linux, the scripts will work.
6 | *.{cmd,[cC][mM][dD]} text eol=crlf
7 | *.{bat,[bB][aA][tT]} text eol=crlf
8 | *.{ps1,[pP][sS]1} text eol=crlf
9 |
10 | # Force bash scripts to always use LF line endings so that if a repo is accessed
11 | # in Unix via a file share from Windows, the scripts will work.
12 | *.sh text eol=lf
13 |
14 | *.md text
15 | *.properties text
16 | *.kt text
17 | *.kts text
18 | *.toml text
19 | *.yaml text
20 | *.yml text
21 |
22 | *.api linguist-generated
23 | *.lock linguist-generated
24 |
25 | .editorconfig text
26 | .gitattributes text
27 | .gitignore text
28 |
29 | *.jar binary
30 |
31 | gradlew text eol=lf linguist-vendored
32 | gradlew.bat text eol=crlf linguist-vendored
33 |
34 | .idea/* linguist-vendored
35 | .githooks/* linguist-vendored
36 |
--------------------------------------------------------------------------------
/modules/client/src/commonMain/kotlin/api/Message.kt:
--------------------------------------------------------------------------------
1 | package org.nirmato.ollama.api
2 |
3 | import kotlinx.serialization.Required
4 | import kotlinx.serialization.SerialName
5 | import kotlinx.serialization.Serializable
6 |
7 | /**
8 | * A message in the chat.
9 | */
10 | @Serializable
11 | public data class Message(
12 | /** The role of the message. */
13 | @SerialName(value = "role")
14 | @Required
15 | val role: Role,
16 |
17 | /** The content of the message. */
18 | @SerialName(value = "content")
19 | @Required
20 | val content: String,
21 |
22 | /** (for thinking models) should the model think before responding? */
23 | @SerialName(value = "thinking")
24 | val thinking: Boolean? = null,
25 |
26 | /** A list of Base64-encoded images to include in the message (for multimodal models such as llava). */
27 | @SerialName(value = "images")
28 | val images: List? = null,
29 |
30 | /** A list of tools in JSON the model wants to use. */
31 | @SerialName(value = "tool_calls")
32 | val tools: List? = null,
33 |
34 | @SerialName(value = "tool_name")
35 | val toolName: String? = null,
36 | )
37 |
--------------------------------------------------------------------------------
/gradle/wasm/yarn.lock:
--------------------------------------------------------------------------------
1 | # THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
2 | # yarn lockfile v1
3 |
4 |
5 | "@js-joda/core@3.2.0":
6 | version "3.2.0"
7 | resolved "https://registry.yarnpkg.com/@js-joda/core/-/core-3.2.0.tgz#3e61e21b7b2b8a6be746df1335cf91d70db2a273"
8 | integrity sha512-PMqgJ0sw5B7FKb2d5bWYIoxjri+QlW/Pys7+Rw82jSH0QN3rB05jZ/VrrsUdh1w4+i2kw9JOejXGq/KhDOX7Kg==
9 |
10 | format-util@^1.0.5:
11 | version "1.0.5"
12 | resolved "https://registry.yarnpkg.com/format-util/-/format-util-1.0.5.tgz#1ffb450c8a03e7bccffe40643180918cc297d271"
13 | integrity sha512-varLbTj0e0yVyRpqQhuWV+8hlePAgaoFRhNFj50BNjEIrw1/DphHSObtqwskVCPWNgzwPoQrZAbfa/SBiicNeg==
14 |
15 | tmp@0.2.4:
16 | version "0.2.4"
17 | resolved "https://registry.yarnpkg.com/tmp/-/tmp-0.2.4.tgz#c6db987a2ccc97f812f17137b36af2b6521b0d13"
18 | integrity sha512-UdiSoX6ypifLmrfQ/XfiawN6hkjSBpCjhKxxZcWlUUmoXLaCKQU0bx4HF/tdDK2uzRuchf1txGvrWBzYREssoQ==
19 |
20 | ws@8.18.3:
21 | version "8.18.3"
22 | resolved "https://registry.yarnpkg.com/ws/-/ws-8.18.3.tgz#b56b88abffde62791c639170400c93dcb0c95472"
23 | integrity sha512-PEIGCY5tSlUt50cqyMXfCzX+oOPqN0vuGqWzbcJ2xvnkzkq46oOpz7dQaTDBdfICb4N14+GARUDw2XV2N4tvzg==
24 |
--------------------------------------------------------------------------------
/modules/client/src/commonMain/kotlin/api/ShowModelResponse.kt:
--------------------------------------------------------------------------------
1 | package org.nirmato.ollama.api
2 |
3 | import kotlinx.serialization.SerialName
4 | import kotlinx.serialization.Serializable
5 | import kotlinx.serialization.json.JsonElement
6 |
7 | /**
8 | * Details about a model including model file, template, parameters, license, and system prompt.
9 | */
10 | @Serializable
11 | public data class ShowModelResponse(
12 |
13 | /** The model's license. */
14 | @SerialName(value = "license")
15 | val license: String? = null,
16 |
17 | /** The model file associated with the model. */
18 | @SerialName(value = "modelfile")
19 | val modelFile: String? = null,
20 |
21 | /** The model parameters. */
22 | @SerialName(value = "parameters")
23 | val parameters: String? = null,
24 |
25 | /** The prompt template for the model. */
26 | @SerialName(value = "template")
27 | val template: String? = null,
28 |
29 | @SerialName(value = "details")
30 | val details: ModelDetails? = null,
31 |
32 | @SerialName(value = "model_info")
33 | val modelInfo: Map? = null,
34 |
35 | @SerialName(value = "capabilities")
36 | val capabilities: List? = null,
37 | )
38 |
--------------------------------------------------------------------------------
/modules/client/src/commonMain/kotlin/api/DoneReason.kt:
--------------------------------------------------------------------------------
1 | package org.nirmato.ollama.api
2 |
3 | import kotlinx.serialization.SerialName
4 | import kotlinx.serialization.Serializable
5 |
6 | /**
7 | * Reason why the model is done generating a response.
8 | */
9 | @Serializable
10 | public enum class DoneReason(public val value: String) {
11 |
12 | @SerialName(value = "stop")
13 | STOP("stop"),
14 |
15 | @SerialName(value = "length")
16 | LENGTH("length"),
17 |
18 | @SerialName(value = "load")
19 | LOAD("load");
20 |
21 | /**
22 | * Override [toString()] to avoid using the enum variable name as the value, and instead use
23 | * the actual value defined in the API spec file.
24 | *
25 | * This solves a problem when the variable name and its value are different, and ensures that
26 | * the client sends the correct enum values to the server always.
27 | */
28 | override fun toString(): String = value
29 |
30 | public companion object {
31 |
32 | /**
33 | * Returns a valid [DoneReason] for [data], null otherwise.
34 | */
35 | public fun fromValueOrNull(data: String?): DoneReason? = data?.let { value -> values().firstOrNull { it.value == value } }
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/.github/workflows/ci-pr.yml:
--------------------------------------------------------------------------------
1 | name: CI Pull Request
2 |
3 | on:
4 | pull_request:
5 | types:
6 | - reopened
7 | - synchronize
8 | - opened
9 | - ready_for_review
10 | branches-ignore:
11 | - main
12 | paths-ignore:
13 | - "**.md"
14 | - ".idea/**"
15 | - ".editorconfig"
16 | - ".gitattributes"
17 | - ".gitignore"
18 | - "oas/**"
19 |
20 | permissions:
21 | contents: read
22 |
23 | concurrency:
24 | group: ci-pr-${{ github.workflow }}-${{ github.event.ref }}-${{ github.event.pull_request.id }}"
25 | cancel-in-progress: true
26 |
27 | jobs:
28 | validate:
29 | runs-on: ubuntu-latest
30 |
31 | steps:
32 | - name: Checkout
33 | uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
34 |
35 | - name: Validate PR title
36 | uses: ./.github/actions/pr-title-validation
37 |
38 | - name: Validate commit messages
39 | uses: ./.github/actions/commit-messages-validation
40 |
41 | build:
42 | needs:
43 | - validate
44 | uses: ./.github/workflows/run-build.yml
45 |
--------------------------------------------------------------------------------
/modules/client/src/commonMain/kotlin/api/EmbedResponse.kt:
--------------------------------------------------------------------------------
1 | package org.nirmato.ollama.api
2 |
3 | import kotlinx.serialization.SerialName
4 | import kotlinx.serialization.Serializable
5 |
6 | /**
7 | * Returns the embedding information.
8 | */
9 | @Serializable
10 | public data class EmbedResponse(
11 |
12 | /**
13 | * The model name.
14 | * Model names follow a model:tag format, where model can have an optional namespace such as example/model.
15 | * Some examples are orca-mini:3b-q4_1 and llama3:70b. The tag is optional and, if not provided, will default to latest.
16 | * The tag is used to identify a specific version.
17 | */
18 | @SerialName(value = "model")
19 | val model: String? = null,
20 |
21 | /** The embedding for the prompt. */
22 | @SerialName(value = "embeddings")
23 | val embeddings: List>? = null,
24 |
25 | /** Time spent generating the response. */
26 | @SerialName(value = "total_duration")
27 | val totalDuration: Long? = null,
28 |
29 | /** Time spent in nanoseconds loading the model. */
30 | @SerialName(value = "load_duration")
31 | val loadDuration: Long? = null,
32 |
33 | /** Number of tokens in the prompt. */
34 | @SerialName(value = "prompt_eval_count")
35 | val promptEvalCount: Int? = null,
36 | )
37 |
--------------------------------------------------------------------------------
/.github/actions/pr-title-validation/action.yml:
--------------------------------------------------------------------------------
1 | name: Pull Request Title Validation
2 |
3 | runs:
4 | using: 'composite'
5 | steps:
6 | - name: Checkout
7 | uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
8 |
9 | - name: Validate PR title
10 | env:
11 | PR_TITLE: ${{ github.event.pull_request.title }}
12 | shell: bash
13 | run: |
14 | # Regex for Conventional Commits specification
15 | PR_TITLE_REGEX="^(build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test)(!)?(\([^\)]*\))?:\s?.+$"
16 |
17 | # Check if PR title matches the regex
18 | if [[ ! "$PR_TITLE" =~ $PR_TITLE_REGEX ]]; then
19 | echo "❌ The PR title does not follow the Conventional Commits specification."
20 | echo "PR Title -> $PR_TITLE"
21 | echo "Valid PR title format: (): "
22 | echo "Example: fix: Bug in insert"
23 | echo "Please check https://www.conventionalcommits.org/en/v1.0.0/#summary"
24 | exit 1
25 | else
26 | echo "🎉 The PR title is following the Conventional Commits specification."
27 | fi
28 |
--------------------------------------------------------------------------------
/modules/client/src/commonMain/kotlin/api/Tool.kt:
--------------------------------------------------------------------------------
1 | package org.nirmato.ollama.api
2 |
3 | import kotlinx.serialization.SerialName
4 | import kotlinx.serialization.Serializable
5 | import kotlinx.serialization.json.JsonObject
6 |
7 | /**
8 | * A tool definition.
9 | */
10 | @Serializable
11 | public data class Tool(
12 | @SerialName(value = "type")
13 | public val type: String,
14 |
15 | @SerialName(value = "function")
16 | public val function: ToolFunction,
17 | ) {
18 |
19 | /**
20 | * Function details for a tool definition.
21 | */
22 | @Serializable
23 | public data class ToolFunction(
24 | @SerialName(value = "name")
25 | public val name: String,
26 |
27 | @SerialName(value = "description")
28 | public val description: String,
29 |
30 | @SerialName(value = "parameters")
31 | public val parameters: ToolParameters? = null,
32 | )
33 |
34 | /**
35 | * Parameters for a tool definition.
36 | */
37 | @Serializable
38 | public data class ToolParameters(
39 | @SerialName(value = "properties")
40 | public val properties: Map? = null,
41 |
42 | @SerialName(value = "required")
43 | public val required: List? = null,
44 | ) {
45 | @SerialName(value = "type")
46 | public val type: String = "object"
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/.github/workflows/run-workflow-cleanup.yml:
--------------------------------------------------------------------------------
1 | name: Run cleanup of old workflow runs
2 |
3 | on:
4 | workflow_dispatch:
5 | workflow_call:
6 |
7 | permissions:
8 | contents: read
9 | actions: write
10 |
11 | jobs:
12 | cleanup:
13 | runs-on: ubuntu-latest
14 | steps:
15 | - name: Checkout
16 | uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
17 |
18 | - name: Install jq
19 | run: sudo apt-get install jq
20 |
21 | - name: List and delete old (>28 days) workflow runs
22 | run: |
23 | gh run list --limit 200 --json databaseId,createdAt |
24 | jq -r '[.[] | select((now - (.createdAt | fromdateiso8601)) > (28 * 24 * 60 * 60))] | .[].databaseId' |
25 | xargs -I ID gh run delete ID
26 | env:
27 | GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
28 |
29 | - name: List and delete all failed workflow runs
30 | run: |
31 | gh run list --limit 200 --json databaseId,status,conclusion |
32 | jq -r '[.[] | select(.status == "completed" and .conclusion == "failure")] | .[].databaseId' |
33 | xargs -I ID gh run delete ID
34 | env:
35 | GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
36 |
--------------------------------------------------------------------------------
/modules/client/src/commonMain/kotlin/api/OllamaModel.kt:
--------------------------------------------------------------------------------
1 | package org.nirmato.ollama.api
2 |
3 | import kotlinx.datetime.Instant
4 | import kotlinx.serialization.SerialName
5 | import kotlinx.serialization.Serializable
6 |
7 | /**
8 | * A model available locally.
9 | */
10 | @Serializable
11 | public data class OllamaModel(
12 | /**
13 | * The model name.
14 | * Model names follow a model:tag format, where model can have an optional namespace such as example/model.
15 | * Some examples are orca-mini:3b-q4_1 and llama3:70b. The tag is optional and, if not provided, will default to latest.
16 | * The tag is used to identify a specific version.
17 | */
18 | @SerialName(value = "name")
19 | val name: String? = null,
20 |
21 | @SerialName(value = "model")
22 | val model: String? = null,
23 |
24 | /** Model modification date. */
25 | @SerialName(value = "modified_at")
26 | val modifiedAt: Instant? = null,
27 |
28 | /** Model expiration date. */
29 | @SerialName(value = "expires_at")
30 | val expiresAt: Instant? = null,
31 |
32 | /** Size of the model on disk. */
33 | @SerialName(value = "size")
34 | val size: Long? = null,
35 |
36 | /** The model's digest. */
37 | @SerialName(value = "digest")
38 | val digest: String? = null,
39 |
40 | @SerialName(value = "details")
41 | val details: ModelDetails? = null,
42 |
43 | /** The model's digest. */
44 | @SerialName(value = "size_vram")
45 | val sizeVram: Long? = null,
46 | )
47 |
--------------------------------------------------------------------------------
/modules/client/src/commonMain/kotlin/api/CheckBlobRequest.kt:
--------------------------------------------------------------------------------
1 | package org.nirmato.ollama.api
2 |
3 | import kotlin.contracts.InvocationKind
4 | import kotlin.contracts.contract
5 | import kotlinx.serialization.Required
6 | import kotlinx.serialization.SerialName
7 | import kotlinx.serialization.Serializable
8 | import org.nirmato.ollama.dsl.OllamaDslMarker
9 |
10 | @Serializable
11 | public data class CheckBlobRequest(
12 | /** The SHA256 digest of the blob */
13 | @SerialName(value = "digest")
14 | @Required
15 | public val digest: String,
16 | ) {
17 | public companion object {
18 | /** A request for checking a blob. */
19 | public fun checkBlobRequest(block: CheckBlobRequestBuilder.() -> Unit): CheckBlobRequest {
20 | contract {
21 | callsInPlace(block, InvocationKind.EXACTLY_ONCE)
22 | }
23 |
24 | return CheckBlobRequestBuilder().apply(block).build()
25 | }
26 |
27 | public fun builder(): CheckBlobRequestBuilder = CheckBlobRequestBuilder()
28 | }
29 |
30 | /** Builder of [CheckBlobRequestBuilder] instances. */
31 | @OllamaDslMarker
32 | public class CheckBlobRequestBuilder() {
33 | private var digest: String? = null
34 |
35 | public fun digest(digest: String): CheckBlobRequestBuilder = apply { this.digest = digest }
36 |
37 | /** Create [CheckBlobRequest] instance. */
38 | public fun build(): CheckBlobRequest = CheckBlobRequest(
39 | digest = requireNotNull(digest) { "digest is required" },
40 | )
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/modules/client-ktor/src/commonTest/kotlin/VersionClientTest.kt:
--------------------------------------------------------------------------------
1 | package org.nirmato.ollama.client.ktor
2 |
3 | import kotlin.test.Test
4 | import kotlinx.coroutines.test.runTest
5 | import io.ktor.client.engine.mock.respond
6 | import io.ktor.client.plugins.logging.DEFAULT
7 | import io.ktor.client.plugins.logging.LogLevel
8 | import io.ktor.client.plugins.logging.Logger
9 | import io.ktor.client.plugins.logging.Logging
10 | import io.ktor.http.HttpHeaders
11 | import io.ktor.http.HttpStatusCode
12 | import io.ktor.http.headers
13 |
14 | internal class VersionClientTest {
15 |
16 | @Test
17 | fun version_validRequest_returnSuccess() = runTest {
18 | val ollamaClient = OllamaClient(MockHttpClientEngineFactory()) {
19 | httpClient {
20 | engine {
21 | addHandler {
22 | respond(
23 | content = """{
24 | "version": "1.2.3"
25 | }""",
26 | status = HttpStatusCode.OK,
27 | headers {
28 | append(HttpHeaders.ContentType, "application/json")
29 | }
30 | )
31 | }
32 | }
33 |
34 | install(Logging) {
35 | level = LogLevel.ALL
36 | logger = Logger.DEFAULT
37 | }
38 | }
39 | }
40 |
41 | val response = ollamaClient.getVersion()
42 |
43 | println(response.toString())
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/modules/client-ktor/src/commonTest/kotlin/MonitoringClientTest.kt:
--------------------------------------------------------------------------------
1 | package org.nirmato.ollama.client.ktor
2 |
3 | import kotlin.test.Test
4 | import kotlinx.coroutines.test.runTest
5 | import io.ktor.client.engine.mock.respond
6 | import io.ktor.client.plugins.logging.DEFAULT
7 | import io.ktor.client.plugins.logging.LogLevel
8 | import io.ktor.client.plugins.logging.Logger
9 | import io.ktor.client.plugins.logging.Logging
10 | import io.ktor.http.HttpHeaders
11 | import io.ktor.http.HttpStatusCode
12 | import io.ktor.http.headers
13 |
14 | internal class MonitoringClientTest {
15 |
16 | @Test
17 | fun monitoring_validRequest_returnSuccess() = runTest {
18 | val ollamaClient = OllamaClient(MockHttpClientEngineFactory()) {
19 | httpClient {
20 | engine {
21 | addHandler {
22 | respond(
23 | content = """{
24 | "status": "500"
25 | }""",
26 | status = HttpStatusCode.OK,
27 | headers {
28 | append(HttpHeaders.ContentType, "application/json")
29 | }
30 | )
31 | }
32 | }
33 |
34 | install(Logging) {
35 | level = LogLevel.ALL
36 | logger = Logger.DEFAULT
37 | }
38 | }
39 | }
40 |
41 | val response = ollamaClient.getMonitoring()
42 |
43 | println(response.toString())
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/.github/workflows/build-jvm.yml:
--------------------------------------------------------------------------------
1 | name: Run build KMP for JVM
2 |
3 | on:
4 | workflow_dispatch:
5 | workflow_call:
6 |
7 | defaults:
8 | run:
9 | shell: bash
10 |
11 | permissions:
12 | contents: read
13 |
14 | env:
15 | GRADLE_OPTS: "-Dorg.gradle.daemon=false -Dkotlin.incremental=false"
16 |
17 | jobs:
18 | build-jvm:
19 | name: Run build on ${{ matrix.os }}
20 |
21 | continue-on-error: false
22 | strategy:
23 | fail-fast: true
24 | matrix:
25 | os: [ 'ubuntu-latest' ]
26 |
27 | runs-on: ${{ matrix.os }}
28 | timeout-minutes: 15
29 |
30 | concurrency:
31 | group: run-build-${{ github.workflow }}-${{ github.ref }}-${{ matrix.os }}
32 | cancel-in-progress: true
33 |
34 | steps:
35 | - name: Checkout
36 | uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
37 |
38 | - name: Setup environment
39 | uses: ./.github/actions/setup
40 | with:
41 | cache-read-only: ${{ github.ref != 'refs/heads/main' }}
42 |
43 | - name: Run check
44 | continue-on-error: false
45 | run: |
46 | ./gradlew -s detekt check
47 |
48 | - name: (Fail-only) Upload reports
49 | if: failure()
50 | uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
51 | with:
52 | name: reports-build-jvm
53 | path: |
54 | **/build/reports/**
55 |
--------------------------------------------------------------------------------
/.github/workflows/build-native-linux.yml:
--------------------------------------------------------------------------------
1 | name: Run build KMP for Linux
2 |
3 | on:
4 | workflow_dispatch:
5 | workflow_call:
6 |
7 | defaults:
8 | run:
9 | shell: bash
10 |
11 | permissions:
12 | contents: read
13 |
14 | env:
15 | GRADLE_OPTS: "-Dorg.gradle.daemon=false -Dkotlin.incremental=false"
16 |
17 | jobs:
18 | build-linux:
19 | name: Run build on ${{ matrix.os }}
20 |
21 | continue-on-error: false
22 | strategy:
23 | fail-fast: true
24 | matrix:
25 | os: [ 'ubuntu-latest' ]
26 |
27 | runs-on: ${{ matrix.os }}
28 | timeout-minutes: 15
29 |
30 | concurrency:
31 | group: run-build-linux-${{ github.workflow }}-${{ github.ref }}-${{ matrix.os }}
32 | cancel-in-progress: true
33 |
34 | steps:
35 | - name: Checkout
36 | uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
37 |
38 | - name: Setup environment
39 | uses: ./.github/actions/setup
40 | with:
41 | cache-read-only: ${{ github.ref != 'refs/heads/main' }}
42 |
43 | - name: Run check
44 | continue-on-error: false
45 | run: |
46 | ./gradlew -Vs detekt checkLinux
47 |
48 | - name: (Fail-only) Upload reports
49 | if: failure()
50 | uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
51 | with:
52 | name: reports-build-linux
53 | path: |
54 | **/build/reports/**
55 |
--------------------------------------------------------------------------------
/.github/workflows/build-native-macos.yml:
--------------------------------------------------------------------------------
1 | name: Run build KMP for MacOS
2 |
3 | on:
4 | workflow_dispatch:
5 | workflow_call:
6 |
7 | defaults:
8 | run:
9 | shell: bash
10 |
11 | permissions:
12 | contents: read
13 |
14 | env:
15 | GRADLE_OPTS: "-Dorg.gradle.daemon=false -Dkotlin.incremental=false"
16 |
17 | jobs:
18 | build-linux:
19 | name: Run build on ${{ matrix.os }}
20 |
21 | continue-on-error: false
22 | strategy:
23 | fail-fast: true
24 | matrix:
25 | os: [ 'ubuntu-latest' ]
26 |
27 | runs-on: ${{ matrix.os }}
28 | timeout-minutes: 15
29 |
30 | concurrency:
31 | group: run-build-macos-${{ github.workflow }}-${{ github.ref }}-${{ matrix.os }}
32 | cancel-in-progress: true
33 |
34 | steps:
35 | - name: Checkout
36 | uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
37 |
38 | - name: Setup environment
39 | uses: ./.github/actions/setup
40 | with:
41 | cache-read-only: ${{ github.ref != 'refs/heads/main' }}
42 |
43 | - name: Run check
44 | continue-on-error: false
45 | run: |
46 | ./gradlew -Vs detekt checkMacos
47 |
48 | - name: (Fail-only) Upload reports
49 | if: failure()
50 | uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
51 | with:
52 | name: reports-build-macos
53 | path: |
54 | **/build/reports/**
55 |
--------------------------------------------------------------------------------
/.github/workflows/build-native-windows.yml:
--------------------------------------------------------------------------------
1 | name: Run build KMP for Windows
2 |
3 | on:
4 | workflow_dispatch:
5 | workflow_call:
6 |
7 | defaults:
8 | run:
9 | shell: bash
10 |
11 | permissions:
12 | contents: read
13 |
14 | env:
15 | GRADLE_OPTS: "-Dorg.gradle.daemon=false -Dkotlin.incremental=false"
16 |
17 | jobs:
18 | build-windows:
19 | name: Run build on ${{ matrix.os }}
20 |
21 | continue-on-error: false
22 | strategy:
23 | fail-fast: true
24 | matrix:
25 | os: [ 'windows-latest' ]
26 |
27 | runs-on: ${{ matrix.os }}
28 | timeout-minutes: 15
29 |
30 | concurrency:
31 | group: run-build-windows-${{ github.workflow }}-${{ github.ref }}-${{ matrix.os }}
32 | cancel-in-progress: true
33 |
34 | steps:
35 | - name: Checkout
36 | uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
37 |
38 | - name: Setup environment
39 | uses: ./.github/actions/setup
40 | with:
41 | cache-read-only: ${{ github.ref != 'refs/heads/main' }}
42 |
43 | - name: Run check
44 | continue-on-error: false
45 | run: |
46 | ./gradlew.bat -Vs detekt checkWindows
47 |
48 | - name: (Fail-only) Upload reports
49 | if: failure()
50 | uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
51 | with:
52 | name: reports-build-windows
53 | path: |
54 | **/build/reports/**
55 |
--------------------------------------------------------------------------------
/reporting/kover/build.gradle.kts:
--------------------------------------------------------------------------------
1 | import kotlinx.kover.gradle.plugin.dsl.AggregationType
2 | import kotlinx.kover.gradle.plugin.dsl.CoverageUnit
3 | import kotlinx.kover.gradle.plugin.dsl.GroupingEntityType
4 |
5 | plugins {
6 | alias(libraries.plugins.kotlinx.kover)
7 | }
8 |
9 | description = "Kover Reporting"
10 |
11 | dependencies {
12 | kover(project(":modules:client"))
13 | kover(project(":modules:client-ktor"))
14 | }
15 |
16 | kover {
17 | reports {
18 | verify {
19 | rule("Global minimum code coverage") {
20 | disabled = false
21 | groupBy = GroupingEntityType.APPLICATION
22 |
23 | bound {
24 | minValue = 60
25 | coverageUnits = CoverageUnit.LINE
26 | aggregationForGroup = AggregationType.COVERED_PERCENTAGE
27 | }
28 | bound {
29 | minValue = 60
30 | coverageUnits = CoverageUnit.BRANCH
31 | aggregationForGroup = AggregationType.COVERED_PERCENTAGE
32 | }
33 | bound {
34 | minValue = 60
35 | coverageUnits = CoverageUnit.INSTRUCTION
36 | aggregationForGroup = AggregationType.COVERED_PERCENTAGE
37 | }
38 | }
39 | }
40 |
41 | total {
42 | html {
43 | charset = Charsets.UTF_8.name()
44 | onCheck = true
45 | }
46 | xml {
47 | onCheck = true
48 | }
49 | binary {
50 | onCheck = true
51 | }
52 | }
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/modules/client/src/commonMain/kotlin/api/ProcessResponse.kt:
--------------------------------------------------------------------------------
1 | package org.nirmato.ollama.api
2 |
3 | import kotlinx.datetime.Instant
4 | import kotlinx.serialization.SerialName
5 | import kotlinx.serialization.Serializable
6 |
7 | /**
8 | * Response class for the list of models running.
9 | */
10 | @Serializable
11 | public data class ProcessResponse(
12 |
13 | /** List of running models. */
14 | @SerialName(value = "models")
15 | val models: List? = null,
16 | ) {
17 | /**
18 | * A model that is currently loaded.
19 | */
20 | @Serializable
21 | public data class ProcessModel(
22 | /**
23 | * The model name.
24 | * Model names follow a model:tag format, where model can have an optional namespace such as example/model.
25 | * Some examples are orca-mini:3b-q4_1 and llama3:70b. The tag is optional and, if not provided, will default to latest.
26 | * The tag is used to identify a specific version.
27 | */
28 | @SerialName(value = "name")
29 | val name: String? = null,
30 |
31 | @SerialName(value = "model")
32 | val model: String? = null,
33 |
34 | /** Size of the model on disk. */
35 | @SerialName(value = "size")
36 | val size: Long? = null,
37 |
38 | /** The model's digest. */
39 | @SerialName(value = "digest")
40 | val digest: String? = null,
41 |
42 | @SerialName(value = "details")
43 | val details: ModelDetails? = null,
44 |
45 | @SerialName(value = "expires_at")
46 | val expiresAt: Instant? = null,
47 |
48 | /** Size of the model on disk. */
49 | @SerialName(value = "size_vram")
50 | val sizeVram: Long? = null,
51 | )
52 | }
53 |
--------------------------------------------------------------------------------
/modules/client/build.gradle.kts:
--------------------------------------------------------------------------------
1 | plugins {
2 | alias(libraries.plugins.kotlinx.serialization)
3 | alias(libraries.plugins.kotlinx.kover)
4 |
5 | id("build-project")
6 | id("build-kotlin-multiplatform")
7 | id("build-maven-publish")
8 | }
9 |
10 | description = "Ollama client API"
11 |
12 | kotlin {
13 | explicitApi()
14 |
15 | compilerOptions {
16 | optIn.addAll(
17 | listOf(
18 | "kotlin.ExperimentalStdlibApi",
19 | "kotlin.RequiresOptIn",
20 | "kotlin.contracts.ExperimentalContracts",
21 | "kotlin.time.ExperimentalTime",
22 | "kotlinx.coroutines.ExperimentalCoroutinesApi",
23 | "kotlinx.serialization.ExperimentalSerializationApi",
24 | )
25 | )
26 | }
27 |
28 | sourceSets {
29 | matching { it.name.endsWith("Test") }.configureEach {
30 | compilerOptions {
31 | optIn.add("kotlinx.coroutines.FlowPreview")
32 | }
33 | }
34 |
35 | val commonMain by getting {
36 | kotlin {
37 | srcDirs("src/commonMain/kotlinX")
38 | }
39 | dependencies {
40 | implementation(libraries.kotlinx.datetime)
41 | implementation(libraries.kotlinx.coroutines.core)
42 | implementation(libraries.kotlinx.serialization.core)
43 | implementation(libraries.kotlinx.serialization.json)
44 | }
45 | }
46 |
47 | val commonTest by getting {
48 | dependencies {
49 | implementation(libraries.kotlin.test)
50 | implementation(libraries.kotlinx.coroutines.test)
51 | }
52 | }
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/samples/client-sample-nodejs/build.gradle.kts:
--------------------------------------------------------------------------------
1 | @file:OptIn(ExperimentalKotlinGradlePluginApi::class)
2 |
3 | import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi
4 | import org.jetbrains.kotlin.gradle.dsl.KotlinVersion
5 |
6 | plugins {
7 | kotlin("multiplatform")
8 |
9 | id("build-project")
10 | }
11 |
12 | description = "Ollama Client using Ktor Js engine"
13 |
14 | kotlin {
15 | js {
16 | nodejs()
17 |
18 | binaries.executable()
19 | }
20 |
21 | compilerOptions {
22 | apiVersion = providers.gradleProperty("kotlin.compilerOptions.apiVersion").map(KotlinVersion::fromVersion)
23 | languageVersion = providers.gradleProperty("kotlin.compilerOptions.languageVersion").map(KotlinVersion::fromVersion)
24 | progressiveMode = false
25 |
26 | freeCompilerArgs.add("-opt-in=kotlin.RequiresOptIn")
27 | }
28 |
29 | jvmToolchain {
30 | languageVersion = providers.gradleProperty("kotlin.javaToolchain.mainJvmCompiler").map(JavaLanguageVersion::of)
31 | }
32 |
33 | sourceSets {
34 | val commonMain by getting {
35 | dependencies {
36 | implementation(project(":modules:client-ktor"))
37 |
38 | implementation(libraries.ktor.client.js)
39 | }
40 | }
41 |
42 | val jsMain by getting {
43 | dependencies {
44 | implementation(libraries.kotlinx.coroutines.core)
45 | implementation(libraries.kotlinx.serialization.json)
46 | implementation(libraries.ktor.client.content.negotiation)
47 | implementation(libraries.ktor.client.logging)
48 | implementation(libraries.ktor.serialization.kotlinx.json)
49 | }
50 | }
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/modules/client/src/commonMain/kotlin/api/CreateBlobRequest.kt:
--------------------------------------------------------------------------------
1 | package org.nirmato.ollama.api
2 |
3 | import kotlin.contracts.InvocationKind
4 | import kotlin.contracts.contract
5 | import kotlinx.serialization.Required
6 | import kotlinx.serialization.SerialName
7 | import kotlinx.serialization.Serializable
8 | import org.nirmato.ollama.dsl.OllamaDslMarker
9 |
10 | @Serializable
11 | public data class CreateBlobRequest(
12 | /** The SHA256 digest of the blob */
13 | @SerialName(value = "digest")
14 | @Required
15 | val digest: String,
16 |
17 | @SerialName(value = "body")
18 | @Required
19 | val body: OctetByteArray,
20 | ) {
21 | public companion object {
22 | /** A request for creating a blob. */
23 | public fun createBlobRequest(block: CreateBlobRequestBuilder.() -> Unit): CreateBlobRequest {
24 | contract {
25 | callsInPlace(block, InvocationKind.EXACTLY_ONCE)
26 | }
27 |
28 | return CreateBlobRequestBuilder().apply(block).build()
29 | }
30 |
31 | public fun builder(): CreateBlobRequestBuilder = CreateBlobRequestBuilder()
32 | }
33 |
34 | /** Builder of [CreateBlobRequestBuilder] instances. */
35 | @OllamaDslMarker
36 | public class CreateBlobRequestBuilder {
37 | private var digest: String? = null
38 | private var body: OctetByteArray? = null
39 |
40 | public fun digest(digest: String): CreateBlobRequestBuilder = apply { this.digest = digest }
41 | public fun body(body: OctetByteArray): CreateBlobRequestBuilder = apply { this.body = body }
42 |
43 | /** Create [CreateBlobRequest] instance. */
44 | public fun build(): CreateBlobRequest = CreateBlobRequest(
45 | digest = requireNotNull(digest) { "digest is required" },
46 | body = requireNotNull(body) { "body is required" }
47 | )
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/.github/workflows/ci-codeql.yml:
--------------------------------------------------------------------------------
1 | name: CI CodeQL Analysis
2 |
3 | on:
4 | workflow_dispatch:
5 | schedule:
6 | - cron: '0 0 1 * *'
7 |
8 | concurrency:
9 | group: ci-codeql-${{ github.workflow }}-${{ github.ref }}
10 | cancel-in-progress: true
11 |
12 | jobs:
13 | analyze:
14 | name: Analyze {{ matrix.language }} code with CodeQL
15 | runs-on: 'ubuntu-latest'
16 |
17 | permissions:
18 | contents: read
19 | packages: read
20 | security-events: write
21 |
22 | strategy:
23 | fail-fast: true
24 | matrix:
25 | include:
26 | - language: java-kotlin
27 | build-mode: manual
28 |
29 | continue-on-error: false
30 |
31 | steps:
32 | - name: Checkout
33 | uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
34 | with:
35 | fetch-depth: 0
36 |
37 | - name: Setup environment
38 | uses: ./.github/actions/setup
39 | with:
40 | cache-read-only: ${{ github.ref != 'refs/heads/main' }}
41 |
42 | - name: Initialize CodeQL tools for scanning.
43 | uses: github/codeql-action/init@192325c86100d080feab897ff886c34abd4c83a3 #v3.30.3
44 | with:
45 | languages: ${{ matrix.language }}
46 | build-mode: ${{ matrix.build-mode }}
47 |
48 | - if: matrix.build-mode == 'manual'
49 | shell: bash
50 | run: |
51 | ./gradlew assemble
52 |
53 | - name: Perform CodeQL Analysis
54 | uses: github/codeql-action/analyze@192325c86100d080feab897ff886c34abd4c83a3 #v3.30.3
55 | with:
56 | category: "/language:${{matrix.language}}"
57 |
--------------------------------------------------------------------------------
/modules/client/src/commonMain/kotlin/api/DeleteModelRequest.kt:
--------------------------------------------------------------------------------
1 | package org.nirmato.ollama.api
2 |
3 | import kotlin.contracts.InvocationKind
4 | import kotlin.contracts.contract
5 | import kotlinx.serialization.Required
6 | import kotlinx.serialization.SerialName
7 | import kotlinx.serialization.Serializable
8 | import org.nirmato.ollama.dsl.OllamaDslMarker
9 |
10 | /**
11 | * Request class for deleting a model.
12 | */
13 | @Serializable
14 | public data class DeleteModelRequest(
15 |
16 | /**
17 | * The model name.
18 | * Model names follow a model:tag format, where model can have an optional namespace such as example/model.
19 | * Some examples are orca-mini:3b-q4_1 and llama3:70b. The tag is optional and, if not provided, will default to latest.
20 | * The tag is used to identify a specific version.
21 | */
22 | @SerialName(value = "model")
23 | @Required
24 | val model: String,
25 | ) {
26 | public companion object {
27 | /** A request for creating a model. */
28 | public fun deleteModelRequest(block: DeleteModelRequestBuilder.() -> Unit): DeleteModelRequest {
29 | contract {
30 | callsInPlace(block, InvocationKind.EXACTLY_ONCE)
31 | }
32 |
33 | return DeleteModelRequestBuilder().apply(block).build()
34 | }
35 |
36 | public fun builder(): DeleteModelRequestBuilder = DeleteModelRequestBuilder()
37 | }
38 |
39 | /** Builder of [DeleteModelRequest] instances. */
40 | @OllamaDslMarker
41 | public class DeleteModelRequestBuilder() {
42 | public var model: String? = null
43 |
44 | public fun model(model: String): DeleteModelRequestBuilder = apply { this.model = model }
45 |
46 | /** Create [DeleteModelRequest] instance. */
47 | public fun build(): DeleteModelRequest = DeleteModelRequest(
48 | model = requireNotNull(model) { "model is required" },
49 | )
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/modules/client/src/commonMain/kotlin/api/ShowModelRequest.kt:
--------------------------------------------------------------------------------
1 | package org.nirmato.ollama.api
2 |
3 | import kotlin.contracts.InvocationKind
4 | import kotlin.contracts.contract
5 | import kotlinx.serialization.Required
6 | import kotlinx.serialization.SerialName
7 | import kotlinx.serialization.Serializable
8 | import org.nirmato.ollama.dsl.OllamaDslMarker
9 |
10 | /**
11 | * Request class for the show model info.
12 | */
13 | @Serializable
14 | public data class ShowModelRequest(
15 | /**
16 | * The model name.
17 | * Model names follow a model:tag format, where model can have an optional namespace such as example/model.
18 | * Some examples are orca-mini:3b-q4_1 and llama3:70b. The tag is optional and, if not provided, will default to latest.
19 | * The tag is used to identify a specific version.
20 | */
21 | @SerialName(value = "name")
22 | @Required
23 | val name: String,
24 | ) {
25 | init {
26 | require(name.isNotBlank()) { "Model name cannot be blank" }
27 | }
28 |
29 | public companion object {
30 | /** A request to show the model info. */
31 | public fun showModelRequest(block: ShowModelRequestBuilder.() -> Unit): ShowModelRequest {
32 | contract {
33 | callsInPlace(block, InvocationKind.EXACTLY_ONCE)
34 | }
35 |
36 | return ShowModelRequestBuilder().apply(block).build()
37 | }
38 |
39 | public fun builder(): ShowModelRequestBuilder = ShowModelRequestBuilder()
40 | }
41 |
42 | /** Builder of [ShowModelRequest] instances. */
43 | @OllamaDslMarker
44 | public class ShowModelRequestBuilder() {
45 | private var name: String? = null
46 |
47 | public fun name(name: String?): ShowModelRequestBuilder = apply { this.name = name }
48 |
49 | /** Create [ShowModelRequest] instance. */
50 | public fun build(): ShowModelRequest = ShowModelRequest(
51 | name = requireNotNull(name) { "name is required" },
52 | )
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/modules/client/src/commonMain/kotlin/api/ChatResponse.kt:
--------------------------------------------------------------------------------
1 | package org.nirmato.ollama.api
2 |
3 | import kotlinx.datetime.Instant
4 | import kotlinx.serialization.SerialName
5 | import kotlinx.serialization.Serializable
6 |
7 | /**
8 | * The response class for the chat endpoint.
9 | */
10 | @Serializable
11 | public data class ChatResponse(
12 | /**
13 | * The model name.
14 | * Model names follow a model:tag format, where model can have an optional namespace such as example/model.
15 | * Some examples are orca-mini:3b-q4_1 and llama3:70b. The tag is optional and, if not provided, will default to latest.
16 | * The tag is used to identify a specific version.
17 | */
18 | @SerialName(value = "model")
19 | val model: String? = null,
20 |
21 | /** Date on which a model was created. */
22 | @SerialName(value = "created_at")
23 | val createdAt: Instant? = null,
24 |
25 | @SerialName(value = "message")
26 | val message: Message? = null,
27 |
28 | /** Whether the response has completed. */
29 | @SerialName(value = "done")
30 | val done: Boolean? = null,
31 |
32 | @SerialName(value = "done_reason")
33 | val doneReason: DoneReason? = null,
34 |
35 | /** Time spent generating the response. */
36 | @SerialName(value = "total_duration")
37 | val totalDuration: Long? = null,
38 |
39 | /** Time spent in nanoseconds loading the model. */
40 | @SerialName(value = "load_duration")
41 | val loadDuration: Long? = null,
42 |
43 | /** Number of tokens in the prompt. */
44 | @SerialName(value = "prompt_eval_count")
45 | val promptEvalCount: Int? = null,
46 |
47 | /** Time spent in nanoseconds evaluating the prompt. */
48 | @SerialName(value = "prompt_eval_duration")
49 | val promptEvalDuration: Long? = null,
50 |
51 | /** Number of tokens the response. */
52 | @SerialName(value = "eval_count")
53 | val evalCount: Int? = null,
54 |
55 | /** Time in nanoseconds spent generating the response. */
56 | @SerialName(value = "eval_duration")
57 | val evalDuration: Long? = null,
58 | )
59 |
--------------------------------------------------------------------------------
/modules/client-ktor/src/commonTest/kotlin/EmbedClientTest.kt:
--------------------------------------------------------------------------------
1 | package org.nirmato.ollama.client.ktor
2 |
3 | import kotlin.test.Test
4 | import kotlinx.coroutines.test.runTest
5 | import io.ktor.client.engine.mock.respond
6 | import io.ktor.http.HttpHeaders
7 | import io.ktor.http.HttpStatusCode
8 | import io.ktor.http.headers
9 | import org.nirmato.ollama.api.EmbedRequest.Companion.embedRequest
10 | import org.nirmato.ollama.api.EmbeddedInput.EmbeddedList
11 | import org.nirmato.ollama.api.EmbeddedInput.EmbeddedText
12 |
13 | internal class EmbedClientTest {
14 |
15 | @Test
16 | fun generateEmbed_validRequest_returnSuccess() = runTest {
17 | val ollamaClient = OllamaClient(MockHttpClientEngineFactory()) {
18 | httpClient {
19 | engine {
20 | addHandler {
21 | respond(
22 | content = """{
23 | "model": "all-minilm",
24 | "embeddings": [[
25 | 0.010071029, -0.0017594862, 0.05007221, 0.04692972, 0.054916814,
26 | 0.008599704, 0.105441414, -0.025878139, 0.12958129, 0.031952348
27 | ],[
28 | -0.0098027075, 0.06042469, 0.025257962, -0.006364387, 0.07272725,
29 | 0.017194884, 0.09032035, -0.051705178, 0.09951512, 0.09072481
30 | ]]
31 | }""",
32 | status = HttpStatusCode.OK,
33 | headers {
34 | append(HttpHeaders.ContentType, "application/json")
35 | }
36 | )
37 | }
38 | }
39 | }
40 | }
41 |
42 | val generateEmbedRequest = embedRequest {
43 | model("tinyllama")
44 | input(EmbeddedList(listOf(EmbeddedText("Why is the sky blue?"), EmbeddedText("Why is the grass green?"))))
45 | }
46 |
47 | val response = ollamaClient.generateEmbed(generateEmbedRequest)
48 |
49 | println(response.toString())
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/.github/COMPATIBILITY.md:
--------------------------------------------------------------------------------
1 | # Compatibility
2 |
3 | ## Compatibility of dependencies
4 |
5 | | nirmato-ollama | Java | Kotlin Language & API | Ollama API |
6 | |----------------|--------------|-----------------------|------------|
7 | | 0.3.0 | 11 or higher | 2.0 or higher | 0.11.10 |
8 | | 0.2.0 | 11 or higher | 1.8 or higher | 0.9.0 |
9 | | 0.1.1 | 11 or higher | 1.8 or higher | 0.5.7 |
10 | | 0.1.0 | 11 or higher | 1.8 or higher | 0.5.7 |
11 |
12 | ## Supported Platforms
13 |
14 | The following ktor platforms are supported https://ktor.io/docs/client-supported-platforms.html
15 |
16 | | Target Platform | Target Preset |
17 | |:---------------:|--------------------------------------------------------------------------------------------------------------------|
18 | | Kotlin/JVM | |
19 | | Kotlin/JS | |
20 | | Kotlin/WasmJS | |
21 | | iOS | - `iosX64`
- `iosArm64`
- `iosSimulatorArm64`
|
22 | | WatchOS | - `watchosX64`
- `watchosArm64`
- `watchosDeviceArm64`
- `watchosSimulatorArm64`
|
23 | | TvOS | - `tvosX64`
- `tvosArm64`
- `tvosSimulatorArm64`
|
24 | | MacOS | |
25 | | Linux | |
26 | | MingwX64 | |
27 |
--------------------------------------------------------------------------------
/modules/client/src/commonMain/kotlin/api/CopyModelRequest.kt:
--------------------------------------------------------------------------------
1 | package org.nirmato.ollama.api
2 |
3 | import kotlin.contracts.InvocationKind
4 | import kotlin.contracts.contract
5 | import kotlinx.serialization.Required
6 | import kotlinx.serialization.SerialName
7 | import kotlinx.serialization.Serializable
8 | import org.nirmato.ollama.dsl.OllamaDslMarker
9 |
10 | /**
11 | * Request class for copying a model.
12 | */
13 | @Serializable
14 | public data class CopyModelRequest(
15 |
16 | /** Name of the model to copy. */
17 | @SerialName(value = "source")
18 | @Required
19 | val source: String,
20 |
21 | /** Name of the new model. */
22 | @SerialName(value = "destination")
23 | @Required
24 | val destination: String,
25 | ) {
26 | init {
27 | require(source.isNotBlank()) { "Model source cannot be blank" }
28 | require(destination.isNotBlank()) { "Model destination cannot be blank" }
29 | }
30 |
31 | public companion object {
32 | /** A request for copying a model. */
33 | public fun copyModelRequest(block: CopyModelRequestBuilder.() -> Unit): CopyModelRequest {
34 | contract {
35 | callsInPlace(block, InvocationKind.EXACTLY_ONCE)
36 | }
37 |
38 | return CopyModelRequestBuilder().apply(block).build()
39 | }
40 |
41 | public fun builder(): CopyModelRequestBuilder = CopyModelRequestBuilder()
42 | }
43 |
44 | /** Builder of [CopyModelRequest] instances. */
45 | @OllamaDslMarker
46 | public class CopyModelRequestBuilder() {
47 | private var source: String? = null
48 | private var destination: String? = null
49 |
50 | public fun source(source: String): CopyModelRequestBuilder = apply { this.source = source }
51 | public fun destination(destination: String): CopyModelRequestBuilder = apply { this.destination = destination }
52 |
53 | /** Create [CopyModelRequest] instance. */
54 | public fun build(): CopyModelRequest = CopyModelRequest(
55 | source = requireNotNull(source) { "source is required" },
56 | destination = requireNotNull(destination) { "destination is required" },
57 | )
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/samples/client-sample-cio/build.gradle.kts:
--------------------------------------------------------------------------------
1 | @file:OptIn(ExperimentalKotlinGradlePluginApi::class)
2 |
3 | import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi
4 | import org.jetbrains.kotlin.gradle.dsl.KotlinVersion
5 |
6 | plugins {
7 | kotlin("multiplatform")
8 |
9 | id("build-project")
10 | }
11 |
12 | description = "Ollama Client using Ktor CIO engine"
13 |
14 | kotlin {
15 | compilerOptions {
16 | apiVersion = providers.gradleProperty("kotlin.compilerOptions.apiVersion").map(KotlinVersion::fromVersion)
17 | languageVersion = providers.gradleProperty("kotlin.compilerOptions.languageVersion").map(KotlinVersion::fromVersion)
18 | progressiveMode = false
19 |
20 | freeCompilerArgs.add("-opt-in=kotlin.RequiresOptIn")
21 | }
22 |
23 | jvm {
24 | mainRun {
25 | mainClass.set("org.nirmato.ollama.client.samples.ApplicationKt")
26 | }
27 | }
28 |
29 | jvmToolchain {
30 | languageVersion = providers.gradleProperty("kotlin.javaToolchain.mainJvmCompiler").map(JavaLanguageVersion::of)
31 | }
32 |
33 | sourceSets {
34 | val commonMain by getting {
35 | dependencies {
36 | implementation(project(":modules:client-ktor"))
37 |
38 | implementation(libraries.ktor.client.cio)
39 | }
40 | }
41 |
42 | val jvmMain by getting {
43 | dependencies {
44 | implementation(libraries.kotlinx.coroutines.slf4j)
45 | implementation(libraries.kotlinx.serialization.json)
46 | implementation(libraries.ktor.client.content.negotiation)
47 | implementation(libraries.ktor.client.logging)
48 | implementation(libraries.ktor.serialization.kotlinx.json)
49 | }
50 | }
51 |
52 | val jvmTest by getting {
53 | dependencies {
54 | implementation(libraries.kotlin.test.junit5)
55 | implementation(libraries.kotlinx.coroutines.test)
56 | }
57 |
58 | tasks {
59 | named("jvmTest") {
60 | useJUnitPlatform()
61 | }
62 | }
63 | }
64 | }
65 | }
66 |
--------------------------------------------------------------------------------
/.github/workflows/run-publish.yml:
--------------------------------------------------------------------------------
1 | name: Run publish
2 |
3 | on:
4 | workflow_call:
5 | inputs:
6 | version:
7 | description: 'Version to publish'
8 | required: true
9 | type: string
10 |
11 | defaults:
12 | run:
13 | shell: bash
14 |
15 | permissions:
16 | contents: read
17 |
18 | env:
19 | GRADLE_OPTS: "-Dorg.gradle.daemon=false -Dkotlin.incremental=false"
20 |
21 | jobs:
22 | publish:
23 | name: Publish
24 | if: github.ref == 'refs/heads/main' && github.event_name != 'pull_request' && github.repository == 'nirmato/nirmato-ollama'
25 |
26 | continue-on-error: false
27 | strategy:
28 | fail-fast: true
29 | matrix:
30 | os: [ 'ubuntu-latest' ]
31 |
32 | runs-on: ${{ matrix.os }}
33 | timeout-minutes: 15
34 |
35 | concurrency:
36 | group: run-publish-${{ github.workflow }}-${{ github.ref }}-${{ matrix.os }}
37 | cancel-in-progress: true
38 |
39 | steps:
40 | - name: Checkout
41 | uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
42 |
43 | - name: Setup environment
44 | uses: ./.github/actions/setup
45 | with:
46 | cache-read-only: ${{ github.ref != 'refs/heads/main' }}
47 |
48 | - name: Publish to Maven Central
49 | env:
50 | GPG_SIGNING_KEY_ID: ${{ secrets.SIGNING_KEY_ID }}
51 | GPG_SIGNING_KEY: ${{ secrets.SIGNING_KEY }}
52 | GPG_SIGNING_PASSPHRASE: ${{ secrets.SIGNING_KEY_PASSWORD }}
53 | ORG_GRADLE_PROJECT_mavenCentralUsername: ${{ secrets.MAVEN_CENTRAL_USERNAME }}
54 | ORG_GRADLE_PROJECT_mavenCentralPassword: ${{ secrets.MAVEN_CENTRAL_PASSWORD }}
55 | VERSION: ${{ github.event.inputs.version }}
56 | run: |
57 | VERSION=$(echo "${VERSION}" | cut -d "/" -f3 | sed 's/^v//')
58 | echo "Release version: ${VERSION}"
59 |
60 | ./gradlew --no-configuration-cache -Pversion=${VERSION} publishToMavenCentral
61 |
--------------------------------------------------------------------------------
/modules/client-ktor/build.gradle.kts:
--------------------------------------------------------------------------------
1 | plugins {
2 | alias(libraries.plugins.kotlinx.serialization)
3 | alias(libraries.plugins.kotlinx.kover)
4 |
5 | id("build-project")
6 | id("build-kotlin-multiplatform")
7 | id("build-maven-publish")
8 | }
9 |
10 | description = "Ollama client support for Ktor"
11 |
12 | kotlin {
13 | explicitApi()
14 |
15 | compilerOptions {
16 | optIn.addAll(
17 | listOf(
18 | "kotlin.ExperimentalStdlibApi",
19 | "kotlin.RequiresOptIn",
20 | "kotlin.contracts.ExperimentalContracts",
21 | "kotlin.time.ExperimentalTime",
22 | "kotlinx.coroutines.ExperimentalCoroutinesApi",
23 | "kotlinx.serialization.ExperimentalSerializationApi",
24 | )
25 | )
26 | }
27 |
28 | sourceSets {
29 | matching { it.name.endsWith("Test") }.configureEach {
30 | compilerOptions {
31 | optIn.add("kotlinx.coroutines.FlowPreview")
32 | }
33 | }
34 |
35 | val commonMain by getting {
36 | kotlin {
37 | srcDirs("src/commonMain/kotlinX")
38 | }
39 | dependencies {
40 | api(project(":modules:client"))
41 |
42 | implementation(libraries.kotlinx.datetime)
43 | implementation(libraries.kotlinx.coroutines.core)
44 | implementation(libraries.kotlinx.serialization.core)
45 | implementation(libraries.kotlinx.serialization.json)
46 | implementation(libraries.ktor.client.content.negotiation)
47 | implementation(libraries.ktor.client.core)
48 | implementation(libraries.ktor.client.logging)
49 | implementation("ch.qos.logback:logback-classic:1.5.16")
50 | implementation(libraries.ktor.serialization.kotlinx.json)
51 | }
52 | }
53 |
54 | val commonTest by getting {
55 | dependencies {
56 | implementation(libraries.kotlin.test)
57 | implementation(libraries.ktor.client.mock)
58 | implementation(libraries.kotlinx.coroutines.test)
59 | }
60 | }
61 | }
62 | }
63 |
--------------------------------------------------------------------------------
/.github/actions/commit-messages-validation/action.yml:
--------------------------------------------------------------------------------
1 | name: Commit Message Validation
2 |
3 | runs:
4 | using: 'composite'
5 | steps:
6 | - name: Checkout
7 | uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
8 | with:
9 | fetch-depth: 0
10 |
11 | - name: Validate commit messages
12 | shell: bash
13 | run: |
14 | # Regex for Conventional Commits specification
15 | COMMIT_REGEX="^(build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test)(!)?(\([^\)]*\))?:\s?.+$"
16 |
17 | # Get all commits in the pull request (from base to head)
18 | COMMITS=$(git log --format=%s --no-merges origin/main..${{ github.sha }})
19 |
20 | # Initialize counters and store invalid commits
21 | INVALID_COMMITS=()
22 | VALID_COMMITS=()
23 |
24 | echo "ℹ️ Checking if commit messages are following the Conventional Commits specification..."
25 |
26 | # Loop through each commit message
27 | IFS=$'\n'
28 | for COMMIT_MSG in $COMMITS; do
29 | # Check if commit message matches the regex
30 | if [[ ! "$COMMIT_MSG" =~ $COMMIT_REGEX ]]; then
31 | INVALID_COMMITS+=("$COMMIT_MSG")
32 | echo -e "❌ $COMMIT_MSG"
33 | else
34 | VALID_COMMITS+=("$COMMIT_MSG")
35 | echo -e "✅ $COMMIT_MSG"
36 | fi
37 | done
38 |
39 | # If there are invalid commits, print the summary
40 | if [ ${#INVALID_COMMITS[@]} -gt 0 ]; then
41 | echo ""
42 | echo "🛑 Some commit messages are not following the Conventional Commits specification."
43 | echo ""
44 | echo "Valid commit message format: (): "
45 | echo "Example: fix: Bug in insert"
46 | echo "Please check https://www.conventionalcommits.org/en/v1.0.0/#summary"
47 | exit 1
48 | fi
49 |
50 | echo "🎉 All commit messages are following the Conventional Commits specification."
51 |
--------------------------------------------------------------------------------
/modules/client/src/commonMain/kotlin/api/OctetByteArray.kt:
--------------------------------------------------------------------------------
1 | package org.nirmato.ollama.api
2 |
3 | import kotlinx.serialization.KSerializer
4 | import kotlinx.serialization.Serializable
5 | import kotlinx.serialization.descriptors.PrimitiveKind
6 | import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor
7 | import kotlinx.serialization.descriptors.SerialDescriptor
8 | import kotlinx.serialization.encoding.Decoder
9 | import kotlinx.serialization.encoding.Encoder
10 |
11 | @Serializable(OctetByteArray.Companion::class)
12 | public class OctetByteArray(public val value: ByteArray) {
13 |
14 | override fun equals(other: Any?): Boolean {
15 | if (this === other) {
16 | return true
17 | }
18 |
19 | if (other == null || this::class != other::class) {
20 | return false
21 | }
22 |
23 | other as OctetByteArray
24 |
25 | return value.contentEquals(other.value)
26 | }
27 |
28 | override fun hashCode(): Int = value.contentHashCode()
29 |
30 | override fun toString(): String = "OctetByteArray(${hex(value)})"
31 |
32 | public companion object : KSerializer {
33 | private val digits = "0123456789abcdef".toCharArray()
34 |
35 | override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("OctetByteArray", PrimitiveKind.STRING)
36 |
37 | override fun serialize(encoder: Encoder, value: OctetByteArray): Unit = encoder.encodeString(hex(value.value))
38 |
39 | override fun deserialize(decoder: Decoder): OctetByteArray = OctetByteArray(hex(decoder.decodeString()))
40 |
41 | private fun hex(bytes: ByteArray): String {
42 | val result = CharArray(bytes.size * 2)
43 | var resultIndex = 0
44 | val digits = digits
45 |
46 | for (element in bytes) {
47 | val b = element.toInt() and 0xff
48 | result[resultIndex++] = digits[b shr 4]
49 | result[resultIndex++] = digits[b and 0x0f]
50 | }
51 |
52 | return result.concatToString()
53 | }
54 |
55 | private fun hex(s: String): ByteArray {
56 | val result = ByteArray(s.length / 2)
57 | for (idx in result.indices) {
58 | val srcIdx = idx * 2
59 | val high = s[srcIdx].toString().toInt(16) shl 4
60 | val low = s[srcIdx + 1].toString().toInt(16)
61 | result[idx] = (high or low).toByte()
62 | }
63 |
64 | return result
65 | }
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/modules/client/src/commonMain/kotlin/api/ChatCompletionResponse.kt:
--------------------------------------------------------------------------------
1 | package org.nirmato.ollama.api
2 |
3 | import kotlinx.datetime.Instant
4 | import kotlinx.serialization.SerialName
5 | import kotlinx.serialization.Serializable
6 |
7 | /**
8 | * The response class for the generate endpoint.
9 | */
10 | @Serializable
11 | public data class ChatCompletionResponse(
12 | /**
13 | * The model name.
14 | * Model names follow a model:tag format, where model can have an optional namespace such as example/model.
15 | * Some examples are orca-mini:3b-q4_1 and llama3:70b. The tag is optional and, if not provided, will default to latest.
16 | * The tag is used to identify a specific version.
17 | */
18 | @SerialName(value = "model")
19 | val model: String? = null,
20 |
21 | /** Date on which a model was created. */
22 | @SerialName(value = "created_at")
23 | val createdAt: Instant? = null,
24 |
25 | /**
26 | * The response for a given prompt with a provided model.
27 | *
28 | * Empty if the response was streamed, if not streamed, this will contain the full response
29 | */
30 | @SerialName(value = "response")
31 | val response: String? = null,
32 |
33 | /** Whether the response has completed. */
34 | @SerialName(value = "done")
35 | val done: Boolean? = null,
36 |
37 | /** Whether the response has completed. */
38 | @SerialName(value = "done_reason")
39 | val doneReason: DoneReason? = null,
40 |
41 | /** Time spent generating the response. */
42 | @SerialName(value = "total_duration")
43 | val totalDuration: Long? = null,
44 |
45 | /** Time spent in nanoseconds loading the model. */
46 | @SerialName(value = "load_duration")
47 | val loadDuration: Long? = null,
48 |
49 | /** Number of tokens in the prompt. */
50 | @SerialName(value = "prompt_eval_count")
51 | val promptEvalCount: Int? = null,
52 |
53 | /** Time spent in nanoseconds evaluating the prompt. */
54 | @SerialName(value = "prompt_eval_duration")
55 | val promptEvalDuration: Long? = null,
56 |
57 | /** Number of tokens the response. */
58 | @SerialName(value = "eval_count")
59 | val evalCount: Int? = null,
60 |
61 | /** Time in nanoseconds spent generating the response. */
62 | @SerialName(value = "eval_duration")
63 | val evalDuration: Long? = null,
64 |
65 | /** An encoding of the conversation used in this response, this can be sent in the next request to keep a conversational memory. */
66 | @SerialName(value = "context")
67 | val context: List? = null,
68 | )
69 |
--------------------------------------------------------------------------------
/publishing/bom/README.md:
--------------------------------------------------------------------------------
1 | # Bill of Materials
2 |
3 | This library is composed of multiple modules where each module is published as a separate artifact. We also publish a
4 | single top-level artifact that automatically brings in all the artifacts of this library, so the simplest way to depend
5 | on the entire library is to add a single dependency to the top-level artifact as described in
6 | [dependency details](../../README.md#Installation).
7 |
8 | In addition to the above, we also publish a bill of materials, BOM, which references all the artifact versions in that
9 | release. The main use-case for the BOM is if you prefer to pick and choose individual components instead of
10 | depending on the entire library. In that case, you can reference the BOM at a particular version and add dependencies
11 | for the components that you want without specifying their individual versions.
12 |
13 | ## Gradle
14 |
15 | ```kotlin
16 | dependencies {
17 | // import the BOM at a particular version
18 | implementation(platform("org.nirmato.ollama:nirmato-ollama-bom:[version]"))
19 |
20 | // pick the artifacts that you want but don't specify their versions as that's controlled by the BOM
21 | implementation("org.nirmato.ollama:nirmato-ollama-client")
22 | implementation("org.nirmato.ollama:nirmato-ollama-client-ktor")
23 | }
24 | ```
25 |
26 | ## Maven
27 |
28 | ```xml
29 |
30 |
31 |
32 |
33 |
34 |
35 | org.nirmato.ollama
36 | nirmato-ollama-bom
37 | [version]
38 | pom
39 | import
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 | org.nirmato.ollama
48 | nirmato-ollama-client
49 |
50 |
51 | org.nirmato.ollama
52 | nirmato-ollama-client-ktor
53 |
54 |
55 |
56 | ```
57 |
58 | ## Artifacts
59 |
60 | The `bom` references the following artifacts:
61 |
62 | | GroupId | ArtifactId | Description |
63 | |:-------------------------|:-----------------------------|:-------------------------------|
64 | | `org.nirmato.ollama` | `nirmato-ollama-client` | Ollama client API |
65 | | `org.nirmato.ollama` | `nirmato-ollama-client-ktor` | Ollama client support for Ktor |
66 |
--------------------------------------------------------------------------------
/.github/actions/setup/action.yml:
--------------------------------------------------------------------------------
1 | name: 'Setup environment for the build'
2 |
3 | inputs:
4 | cache-disabled:
5 | description: 'gradle.cache-disabled'
6 | default: 'false'
7 | cache-read-only:
8 | description: 'gradle.cache-read-only'
9 | default: 'false'
10 | java-version:
11 | description: 'Java version'
12 | default: '21'
13 |
14 | runs:
15 | using: 'composite'
16 | steps:
17 | - name: Configure JDK
18 | uses: actions/setup-java@dded0888837ed1f317902acf8a20df0ad188d165 #v5.0.0
19 | with:
20 | distribution: 'zulu'
21 | java-version: ${{ inputs.java-version }}
22 |
23 | - uses: gradle/actions/setup-gradle@ed408507eac070d1f99cc633dbcf757c94c7933a #v4.4.3
24 | with:
25 | validate-wrappers: true
26 | cache-disabled: ${{ inputs.cache-disabled }}
27 | cache-read-only: ${{ inputs.cache-read-only }}
28 |
29 | - name: Cache Gradle Wrapper
30 | uses: actions/cache@0400d5f644dc74513175e3cd8d07132dd4860809 #v4.2.4
31 | id: gradle-wrapper-cache
32 | with:
33 | path: ~/.gradle/wrapper
34 | key: ${{ runner.os }}-gradle-wrapper-${{ hashFiles('gradle/wrapper/gradle-wrapper.properties') }}
35 | restore-keys: ${{ runner.os }}-gradle-wrapper-
36 | enableCrossOsArchive: true
37 |
38 | - name: Cache Node Modules
39 | uses: actions/cache@0400d5f644dc74513175e3cd8d07132dd4860809 #v4.2.4
40 | id: gradle-yarn-cache
41 | with:
42 | path: build/js/node_modules
43 | key: ${{ runner.os }}-yarn-${{ hashFiles('gradle/js/yarn.lock') }}
44 | restore-keys: ${{ runner.os }}-yarn-
45 | enableCrossOsArchive: true
46 |
47 | - name: Initialize Gradle wrapper
48 | if: matrix.os == 'ubuntu-latest' || matrix.os == 'macos-latest'
49 | run: chmod +x ./gradlew
50 | shell: "bash"
51 |
52 | - name: Cache Konan
53 | uses: actions/cache@0400d5f644dc74513175e3cd8d07132dd4860809 #v4.2.4
54 | with:
55 | path: |
56 | ~/.konan/cache
57 | ~/.konan/dependencies
58 | ~/.konan/kotlin-native-macos*
59 | ~/.konan/kotlin-native-mingw*
60 | ~/.konan/kotlin-native-windows*
61 | ~/.konan/kotlin-native-linux*
62 | ~/.konan/kotlin-native-prebuilt-macos*
63 | ~/.konan/kotlin-native-prebuilt-mingw*
64 | ~/.konan/kotlin-native-prebuilt-windows*
65 | ~/.konan/kotlin-native-prebuilt-linux*
66 | key: ${{ runner.os }}-konan-${{ hashFiles('**/*.gradle*') }}
67 | restore-keys: |
68 | ${{ runner.os }}-konan-
69 |
70 | - if: matrix.os == 'Windows'
71 | uses: msys2/setup-msys2@fb197b72ce45fb24f17bf3f807a388985654d1f2 #v2.29.0
72 | with:
73 | release: false
74 | install: mingw-w64-x86_64-openssl
75 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @rem
2 | @rem Copyright 2015 the original author or authors.
3 | @rem
4 | @rem Licensed under the Apache License, Version 2.0 (the "License");
5 | @rem you may not use this file except in compliance with the License.
6 | @rem You may obtain a copy of the License at
7 | @rem
8 | @rem https://www.apache.org/licenses/LICENSE-2.0
9 | @rem
10 | @rem Unless required by applicable law or agreed to in writing, software
11 | @rem distributed under the License is distributed on an "AS IS" BASIS,
12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | @rem See the License for the specific language governing permissions and
14 | @rem limitations under the License.
15 | @rem
16 | @rem SPDX-License-Identifier: Apache-2.0
17 | @rem
18 |
19 | @if "%DEBUG%"=="" @echo off
20 | @rem ##########################################################################
21 | @rem
22 | @rem Gradle startup script for Windows
23 | @rem
24 | @rem ##########################################################################
25 |
26 | @rem Set local scope for the variables with windows NT shell
27 | if "%OS%"=="Windows_NT" setlocal
28 |
29 | set DIRNAME=%~dp0
30 | if "%DIRNAME%"=="" set DIRNAME=.
31 | @rem This is normally unused
32 | set APP_BASE_NAME=%~n0
33 | set APP_HOME=%DIRNAME%
34 |
35 | @rem Resolve any "." and ".." in APP_HOME to make it shorter.
36 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
37 |
38 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
39 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
40 |
41 | @rem Find java.exe
42 | if defined JAVA_HOME goto findJavaFromJavaHome
43 |
44 | set JAVA_EXE=java.exe
45 | %JAVA_EXE% -version >NUL 2>&1
46 | if %ERRORLEVEL% equ 0 goto execute
47 |
48 | echo. 1>&2
49 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2
50 | echo. 1>&2
51 | echo Please set the JAVA_HOME variable in your environment to match the 1>&2
52 | echo location of your Java installation. 1>&2
53 |
54 | goto fail
55 |
56 | :findJavaFromJavaHome
57 | set JAVA_HOME=%JAVA_HOME:"=%
58 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
59 |
60 | if exist "%JAVA_EXE%" goto execute
61 |
62 | echo. 1>&2
63 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2
64 | echo. 1>&2
65 | echo Please set the JAVA_HOME variable in your environment to match the 1>&2
66 | echo location of your Java installation. 1>&2
67 |
68 | goto fail
69 |
70 | :execute
71 | @rem Setup the command line
72 |
73 |
74 |
75 | @rem Execute Gradle
76 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -jar "%APP_HOME%\gradle\wrapper\gradle-wrapper.jar" %*
77 |
78 | :end
79 | @rem End local scope for the variables with windows NT shell
80 | if %ERRORLEVEL% equ 0 goto mainEnd
81 |
82 | :fail
83 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
84 | rem the _cmd.exe /c_ return code!
85 | set EXIT_CODE=%ERRORLEVEL%
86 | if %EXIT_CODE% equ 0 set EXIT_CODE=1
87 | if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE%
88 | exit /b %EXIT_CODE%
89 |
90 | :mainEnd
91 | if "%OS%"=="Windows_NT" endlocal
92 |
93 | :omega
94 |
--------------------------------------------------------------------------------
/modules/client/src/commonMain/kotlin/api/PushModelRequest.kt:
--------------------------------------------------------------------------------
1 | package org.nirmato.ollama.api
2 |
3 | import kotlin.contracts.InvocationKind
4 | import kotlin.contracts.contract
5 | import kotlinx.serialization.Required
6 | import kotlinx.serialization.SerialName
7 | import kotlinx.serialization.Serializable
8 | import org.nirmato.ollama.dsl.OllamaDslMarker
9 |
10 | /**
11 | * Request for pushing a model.
12 | */
13 | @Serializable
14 | public data class PushModelRequest(
15 |
16 | /** The name of the model to push in the form of /:. */
17 | @SerialName(value = "model")
18 | @Required
19 | val model: String,
20 |
21 | /** Allow insecure connections to the library. Only use this if you are pushing to your library during development. */
22 | @SerialName(value = "insecure")
23 | val insecure: Boolean? = false,
24 |
25 | /** Ollama username. */
26 | @SerialName(value = "username")
27 | val username: String? = null,
28 |
29 | /** Ollama password. */
30 | @SerialName(value = "password")
31 | val password: String? = null,
32 |
33 | /** If `false` the response will be returned as a single response object, otherwise, the response will be streamed as a series of objects. */
34 | @SerialName(value = "stream")
35 | val stream: Boolean? = false,
36 | ) {
37 | init {
38 | require(model.isNotBlank()) { "Model name cannot be blank" }
39 | }
40 |
41 | public companion object {
42 | /** A request for pushing a model. */
43 | public fun pushModelRequest(block: PushModelRequestBuilder.() -> Unit): PushModelRequest {
44 | contract {
45 | callsInPlace(block, InvocationKind.EXACTLY_ONCE)
46 | }
47 |
48 | return PushModelRequestBuilder().apply(block).build()
49 | }
50 |
51 | public fun builder(): PushModelRequestBuilder = PushModelRequestBuilder()
52 | public fun builder(pushModelRequest: PushModelRequest): PushModelRequestBuilder = PushModelRequestBuilder(pushModelRequest)
53 | }
54 |
55 | /** Builder of [PushModelRequest] instances. */
56 | @OllamaDslMarker
57 | public class PushModelRequestBuilder() {
58 | private var model: String? = null
59 | private var insecure: Boolean? = false
60 | private var username: String? = null
61 | private var password: String? = null
62 | private var stream: Boolean? = null
63 |
64 | public constructor(pushModelRequest: PushModelRequest) : this() {
65 | model = pushModelRequest.model
66 | insecure = pushModelRequest.insecure
67 | username = pushModelRequest.username
68 | password = pushModelRequest.password
69 | stream = pushModelRequest.stream
70 | }
71 |
72 | public fun model(model: String): PushModelRequestBuilder = apply { this.model = model }
73 | public fun insecure(insecure: Boolean): PushModelRequestBuilder = apply { this.insecure = insecure }
74 | public fun username(username: String): PushModelRequestBuilder = apply { this.username = username }
75 | public fun password(password: String): PushModelRequestBuilder = apply { this.password = password }
76 | public fun stream(stream: Boolean): PushModelRequestBuilder = apply { this.stream = stream }
77 |
78 | /** Create [PushModelRequest] instance. */
79 | public fun build(): PushModelRequest = PushModelRequest(
80 | model = requireNotNull(model) { "model is required" },
81 | insecure = insecure,
82 | username = username,
83 | password = password,
84 | stream = stream,
85 | )
86 | }
87 | }
88 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Created by https://www.toptal.com/developers/gitignore/api/gradle,kotlin,intellij+all
2 | # Edit at https://www.toptal.com/developers/gitignore?templates=gradle,kotlin,intellij+all
3 |
4 | ### Intellij+all ###
5 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
6 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
7 |
8 | # User-specific stuff
9 | .idea/**/workspace.xml
10 | .idea/**/tasks.xml
11 | .idea/**/usage.statistics.xml
12 | .idea/**/shelf
13 |
14 | # AWS User-specific
15 | .idea/**/aws.xml
16 |
17 | # Generated files
18 | .idea/**/contentModel.xml
19 |
20 | # Sensitive or high-churn files
21 | .idea/**/dataSources/
22 | .idea/**/dataSources.ids
23 | .idea/**/dataSources.local.xml
24 | .idea/**/sqlDataSources.xml
25 | .idea/**/dynamic.xml
26 | .idea/**/uiDesigner.xml
27 | .idea/**/dbnavigator.xml
28 |
29 | # Gradle
30 | .idea/**/gradle.xml
31 | .idea/**/libraries
32 |
33 | # Gradle and Maven with auto-import
34 | # When using Gradle or Maven with auto-import, you should exclude module files,
35 | # since they will be recreated, and may cause churn. Uncomment if using
36 | # auto-import.
37 | .idea/artifacts
38 | .idea/compiler.xml
39 | .idea/jarRepositories.xml
40 | .idea/modules.xml
41 | .idea/*.iml
42 | .idea/modules
43 | *.iml
44 | *.ipr
45 |
46 | # CMake
47 | cmake-build-*/
48 |
49 | # Mongo Explorer plugin
50 | .idea/**/mongoSettings.xml
51 |
52 | # File-based project format
53 | *.iws
54 |
55 | # IntelliJ
56 | out/
57 |
58 | # mpeltonen/sbt-idea plugin
59 | .idea_modules/
60 |
61 | # JIRA plugin
62 | atlassian-ide-plugin.xml
63 |
64 | # Cursive Clojure plugin
65 | .idea/replstate.xml
66 |
67 | # SonarLint plugin
68 | .idea/sonarlint/
69 |
70 | # Crashlytics plugin (for Android Studio and IntelliJ)
71 | com_crashlytics_export_strings.xml
72 | crashlytics.properties
73 | crashlytics-build.properties
74 | fabric.properties
75 |
76 | # Editor-based Rest Client
77 | .idea/httpRequests
78 |
79 | # Android studio 3.1+ serialized cache file
80 | .idea/caches/build_file_checksums.ser
81 |
82 | ### Intellij+all Patch ###
83 | # Ignore everything but code style settings and run configurations
84 | # that are supposed to be shared within teams.
85 |
86 | .idea/*
87 |
88 | !.idea/codeStyles
89 | !.idea/inspectionProfiles
90 | .idea/runConfigurations
91 |
92 | ### Kotlin ###
93 | # Compiled class file
94 | *.class
95 |
96 | # Log file
97 | *.log
98 |
99 | # BlueJ files
100 | *.ctxt
101 |
102 | # Mobile Tools for Java (J2ME)
103 | .mtj.tmp/
104 |
105 | # Package Files #
106 | *.jar
107 | *.war
108 | *.nar
109 | *.ear
110 | *.zip
111 | *.tar.gz
112 | *.rar
113 |
114 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
115 | hs_err_pid*
116 | replay_pid*
117 |
118 | ### Gradle ###
119 | .gradle
120 | **/build/
121 | !src/**/build/
122 |
123 | # Ignore Gradle GUI config
124 | gradle-app.setting
125 |
126 | # Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored)
127 | !gradle-wrapper.jar
128 |
129 | # Avoid ignore Gradle wrappper properties
130 | !gradle-wrapper.properties
131 |
132 | # Cache of project
133 | .gradletasknamecache
134 |
135 | # Eclipse Gradle plugin generated files
136 | # Eclipse Core
137 | .project
138 | # JDT-specific (Eclipse Java Development Tools)
139 | .classpath
140 |
141 | ### Gradle Patch ###
142 | # Java heap dump
143 | *.hprof
144 |
145 | # End of https://www.toptal.com/developers/gitignore/api/gradle,kotlin,intellij+all
146 |
147 | !.idea/dictionaries
148 | .idea/dictionaries/*
149 | !.idea/dictionaries/ProjectDictionary.xml
150 |
151 | **/.kotlin
152 |
153 | TODO
154 |
--------------------------------------------------------------------------------
/modules/client/src/commonMain/kotlin/api/EmbedRequest.kt:
--------------------------------------------------------------------------------
1 | package org.nirmato.ollama.api
2 |
3 | import kotlin.contracts.InvocationKind
4 | import kotlin.contracts.contract
5 | import kotlinx.serialization.Required
6 | import kotlinx.serialization.SerialName
7 | import kotlinx.serialization.Serializable
8 | import org.nirmato.ollama.dsl.OllamaDslMarker
9 |
10 | /**
11 | * Generate embeddings from a model.
12 | */
13 | @Serializable
14 | public data class EmbedRequest(
15 |
16 | /**
17 | * The model name.
18 | * Model names follow a model:tag format, where model can have an optional namespace such as example/model.
19 | * Some examples are orca-mini:3b-q4_1 and llama3:70b. The tag is optional and, if not provided, will default to latest.
20 | * The tag is used to identify a specific version.
21 | */
22 | @SerialName(value = "model")
23 | @Required
24 | val model: String,
25 |
26 | /** Text or list of text to generate embeddings for. */
27 | @SerialName(value = "input")
28 | @Required
29 | val input: EmbeddedInput,
30 |
31 | /** Truncates the end of each input to fit within context length. Returns error if false and context length is exceeded. Defaults to true. */
32 | @SerialName(value = "truncate")
33 | val truncate: Boolean? = false,
34 |
35 | @SerialName(value = "options")
36 | val options: Options? = null,
37 |
38 | /**
39 | * How long (in minutes) to keep the model loaded in memory.
40 | * - If set to a positive duration (e.g. 20), the model will stay loaded for the provided duration.
41 | * - If set to a negative duration (e.g. -1), the model will stay loaded indefinitely.
42 | * - If set to 0, the model will be unloaded immediately once finished.
43 | * - If not set, the model will stay loaded for 5 minutes by default
44 | */
45 | @SerialName(value = "keep_alive")
46 | val keepAlive: Int? = null,
47 | ) {
48 | init {
49 | require(model.isNotBlank()) { "Model name cannot be blank" }
50 | }
51 |
52 | public companion object {
53 | /** A request to generate an embeddings from a model. */
54 | public fun embedRequest(block: EmbedRequestBuilder.() -> Unit): EmbedRequest {
55 | contract {
56 | callsInPlace(block, InvocationKind.EXACTLY_ONCE)
57 | }
58 |
59 | return EmbedRequestBuilder().apply(block).build()
60 | }
61 |
62 | public fun builder(): EmbedRequestBuilder = EmbedRequestBuilder()
63 | }
64 |
65 | /** Builder of [EmbedRequest] instances. */
66 | @OllamaDslMarker
67 | public class EmbedRequestBuilder() {
68 | private var model: String? = null
69 | private var input: EmbeddedInput? = null
70 | private var truncate: Boolean? = null
71 | private var options: Options? = null
72 | private var keepAlive: Int? = null
73 |
74 | public fun model(model: String): EmbedRequestBuilder = apply { this.model = model }
75 | public fun input(input: EmbeddedInput): EmbedRequestBuilder = apply { this.input = input }
76 | public fun truncate(truncate: Boolean): EmbedRequestBuilder = apply { this.truncate = truncate }
77 | public fun options(options: Options): EmbedRequestBuilder = apply { this.options = options }
78 | public fun keepAlive(keepAlive: Int): EmbedRequestBuilder = apply { this.keepAlive = keepAlive }
79 |
80 | /** Create [EmbedRequest] instance. */
81 | public fun build(): EmbedRequest = EmbedRequest(
82 | model = requireNotNull(model) { "model is required" },
83 | input = requireNotNull(input) { "input is required" },
84 | truncate = truncate,
85 | options = options,
86 | keepAlive = keepAlive,
87 | )
88 | }
89 | }
90 |
--------------------------------------------------------------------------------
/modules/client/src/commonMain/kotlin/api/PullModelRequest.kt:
--------------------------------------------------------------------------------
1 | package org.nirmato.ollama.api
2 |
3 | import kotlin.contracts.InvocationKind
4 | import kotlin.contracts.contract
5 | import kotlinx.serialization.Required
6 | import kotlinx.serialization.SerialName
7 | import kotlinx.serialization.Serializable
8 | import org.nirmato.ollama.dsl.OllamaDslMarker
9 |
10 | /**
11 | * Request for pulling a model.
12 | */
13 | @Serializable
14 | public data class PullModelRequest(
15 | /**
16 | * The model name.
17 | * Model names follow a model:tag format, where model can have an optional namespace such as example/model.
18 | * Some examples are orca-mini:3b-q4_1 and llama3:70b. The tag is optional and, if not provided, will default to latest.
19 | * The tag is used to identify a specific version.
20 | */
21 | @SerialName(value = "model")
22 | @Required
23 | val name: String,
24 |
25 | /** Allow insecure connections to the library. Only use this if you are pulling from your own library during development. */
26 | @SerialName(value = "insecure")
27 | val insecure: Boolean? = false,
28 |
29 | /** If `false` the response will be returned as a single response object, otherwise the response will be streamed as a series of objects. */
30 | @SerialName(value = "stream")
31 | val stream: Boolean? = false,
32 |
33 | /** Ollama username. */
34 | @SerialName(value = "username")
35 | val username: String? = null,
36 |
37 | /** Ollama password. */
38 | @SerialName(value = "password")
39 | val password: String? = null,
40 | ) {
41 | init {
42 | require(name.isNotBlank()) { "Model name cannot be blank" }
43 | }
44 |
45 | public companion object {
46 | /** A request for creating a model. */
47 | public fun pullModelRequest(block: PullModelRequestBuilder.() -> Unit): PullModelRequest {
48 | contract {
49 | callsInPlace(block, InvocationKind.EXACTLY_ONCE)
50 | }
51 |
52 | return PullModelRequestBuilder().apply(block).build()
53 | }
54 |
55 | public fun builder(): PullModelRequestBuilder = PullModelRequestBuilder()
56 | public fun builder(pullModelRequest: PullModelRequest): PullModelRequestBuilder = PullModelRequestBuilder(pullModelRequest)
57 | }
58 |
59 | /** Builder of [PullModelRequest] instances. */
60 | @OllamaDslMarker
61 | public class PullModelRequestBuilder() {
62 | private var name: String? = null
63 | private var insecure: Boolean? = false
64 | private var username: String? = null
65 | private var password: String? = null
66 | private var stream: Boolean? = false
67 |
68 | public constructor(pullModelRequest: PullModelRequest) : this() {
69 | name = pullModelRequest.name
70 | insecure = pullModelRequest.insecure
71 | username = pullModelRequest.username
72 | password = pullModelRequest.password
73 | stream = pullModelRequest.stream
74 | }
75 |
76 | public fun name(name: String): PullModelRequestBuilder = apply { this.name = name }
77 | public fun insecure(insecure: Boolean): PullModelRequestBuilder = apply { this.insecure = insecure }
78 | public fun username(username: String): PullModelRequestBuilder = apply { this.username = username }
79 | public fun password(password: String): PullModelRequestBuilder = apply { this.password = password }
80 | public fun stream(stream: Boolean): PullModelRequestBuilder = apply { this.stream = stream }
81 |
82 | /** Create [PullModelRequest] instance. */
83 | public fun build(): PullModelRequest = PullModelRequest(
84 | name = requireNotNull(name) { "model is required" },
85 | insecure = insecure,
86 | username = username,
87 | password = password,
88 | stream = stream,
89 | )
90 | }
91 | }
92 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # nirmato-ollama
2 |
3 | [](https://klibs.io/project/nirmato/nirmato-ollama)
4 | ](https://img.shields.io/badge/kotlin--multiplatform-2.2.20-blue.svg?logo=kotlin)
5 | ](https://img.shields.io/github/license/nirmato/nirmato-ollama)
6 |
7 | An Ollama client SDK that allows you to easily interact with the Ollama API.
8 |
9 | The supported API follows the OpenAPI definition [Ollama API](oas/ollama-openapi.yaml) described in [Ollama API Docs](https://github.com/ollama/ollama/blob/main/docs/api.md).
10 |
11 |
12 |
13 |
14 |
15 | ## Features
16 |
17 | - **Comprehensive API Coverage:** Full support for the Ollama API, including chat, streaming, model management, and embeddings.
18 | - **Kotlin Multiplatform:** Write once, run on any platform that Kotlin supports.
19 | - **Asynchronous Operations:** Built with Kotlin Coroutines for non-blocking, asynchronous API calls.
20 | - **Type-Safe DSL:** A clean and expressive DSL for building requests.
21 | - **Streaming Support:** Handle large responses efficiently with built-in streaming capabilities.
22 |
23 | ## Requirements
24 |
25 | - Kotlin 1.8 or higher
26 | - JVM 11 or higher
27 |
28 | ## Installation
29 |
30 | Add the dependency to your Gradle configuration:
31 |
32 | ```kotlin
33 | implementation("org.nirmato.ollama:nirmato-ollama-client-ktor:0.2.0")
34 |
35 | // example using ktor CIO engine
36 | implementation("io.ktor:ktor-client-cio:3.1.3")
37 | ```
38 |
39 | or to your Maven pom:
40 |
41 | ```xml
42 |
43 | org.nirmato.ollama
44 | nirmato-ollama-client-ktor
45 | 0.2.0
46 |
47 |
48 |
49 |
50 | io.ktor
51 | ktor-client-cio
52 | 3.1.3
53 |
54 | ```
55 |
56 | ## Usage
57 |
58 | The `OllamaClient` class contains all the methods needed to interact with the Ollama API. An example of calling Ollama:
59 |
60 | ```kotlin
61 | val ollamaClient = OllamaClient(CIO) {
62 | httpClient {
63 | // ktor HttpClient configurations
64 | defaultRequest {
65 | url("http://localhost:11434/api/")
66 | }
67 | }
68 | }
69 |
70 | val request = chatRequest {
71 | model("tinyllama")
72 | messages(listOf(Message(role = USER, content = "Why is the sky blue?")))
73 | }
74 |
75 | val response = ollamaClient.chat(request)
76 | ```
77 |
78 | See the [samples](samples) directory for complete examples.
79 |
80 | ## API Overview
81 |
82 | The `OllamaApi` interface provides the following methods:
83 |
84 | - `chat(chatRequest)`: Send a chat request and get a response.
85 | - `chatStream(chatRequest)`: Stream the chat response.
86 | - `generateEmbed(generateEmbedRequest)`: Generate embeddings for a given input.
87 | - `createModel(createModelRequest)`: Create a new model.
88 | - `pullModel(pullModelRequest)`: Pull a model from the registry.
89 | - `pushModel(pushModelRequest)`: Push a model to the registry.
90 | - `listModels()`: List all available models.
91 | - ...and many more.
92 |
93 | For a complete list of methods, please refer to the `OllamaApi` interface.
94 |
95 | ## Contributing
96 |
97 | Contributions are welcome! Please feel free to submit a Pull Request.
98 |
99 | ## Acknowledgements
100 |
101 | - [JetBrains](https://www.jetbrains.com/) for creating [Kotlin](https://kotlinlang.org/).
102 |
103 | ## License
104 |
105 | The source code is distributed under the [Apache License 2.0](LICENSE.md).
106 |
--------------------------------------------------------------------------------
/gradle/catalogs/libraries.versions.toml:
--------------------------------------------------------------------------------
1 | [versions]
2 | detekt = "1.23.8"
3 | foojay-resolver = "1.0.0"
4 | gradle-maven-publish-plugin = "0.32.0"
5 | kotlin = "2.2.21"
6 | kotlinx-bcv = "0.18.1"
7 | kotlinx-coroutines = "1.10.2"
8 | kotlinx-datetime = "0.6.2"
9 | kotlinx-io = "0.7.0"
10 | kotlinx-kover = "0.9.1"
11 | kotlinx-serialization = "1.9.0"
12 | ktor = "3.3.2"
13 |
14 | [libraries]
15 | detekt-gradle-plugin = { module= "io.gitlab.arturbosch.detekt:detekt-gradle-plugin", version.ref = "detekt" }
16 | foojay-resolver = { module = "org.gradle.toolchains:foojay-resolver", version.ref = "foojay-resolver" }
17 | gradle-maven-publish-plugin = { module = "com.vanniktech:gradle-maven-publish-plugin", version.ref = "gradle-maven-publish-plugin" }
18 | kotlin-compiler = { module = "org.jetbrains.kotlin:kotlin-compiler", version.ref = "kotlin" }
19 | kotlin-gradle-plugin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin" }
20 | kotlin-reflect = { module = "org.jetbrains.kotlin:kotlin-reflect", version.ref = "kotlin" }
21 | kotlin-stdlib = { module = "org.jetbrains.kotlin:kotlin-stdlib", version.ref = "kotlin" }
22 | kotlin-test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotlin" }
23 | kotlin-test-js = { module = "org.jetbrains.kotlin:kotlin-test-js", version.ref = "kotlin" }
24 | kotlin-test-junit5 = { module = "org.jetbrains.kotlin:kotlin-test-junit5", version.ref = "kotlin" }
25 | kotlinx-coroutines-bom = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-bom", version.ref = "kotlinx-coroutines" }
26 | kotlinx-coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "kotlinx-coroutines" }
27 | kotlinx-coroutines-slf4j = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-slf4j", version.ref = "kotlinx-coroutines" }
28 | kotlinx-coroutines-test = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-test", version.ref = "kotlinx-coroutines" }
29 | kotlinx-datetime = { module = "org.jetbrains.kotlinx:kotlinx-datetime", version.ref = "kotlinx-datetime" }
30 | kotlinx-io-core = { module = "org.jetbrains.kotlinx:kotlinx-io-core", version.ref = "kotlinx-io" }
31 | kotlinx-serialization-bom = { module = "org.jetbrains.kotlinx:kotlinx-serialization-bom", version.ref = "kotlinx-serialization" }
32 | kotlinx-serialization-core = { module = "org.jetbrains.kotlinx:kotlinx-serialization-core", version.ref = "kotlinx-serialization" }
33 | kotlinx-serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "kotlinx-serialization" }
34 | ktor-bom = { module = "io.ktor:ktor-bom", version.ref = "ktor" }
35 | ktor-client-cio = { module = "io.ktor:ktor-client-cio", version.ref = "ktor" }
36 | ktor-client-content-negotiation = { module = "io.ktor:ktor-client-content-negotiation", version.ref = "ktor" }
37 | ktor-client-core = { module = "io.ktor:ktor-client-core", version.ref = "ktor" }
38 | ktor-client-curl = { module = "io.ktor:ktor-client-curl", version.ref = "ktor" }
39 | ktor-client-java = { module = "io.ktor:ktor-client-java", version.ref = "ktor" }
40 | ktor-client-js = { module = "io.ktor:ktor-client-js", version.ref = "ktor" }
41 | ktor-client-json = { module = "io.ktor:ktor-client-json", version.ref = "ktor" }
42 | ktor-client-logging = { module = "io.ktor:ktor-client-logging", version.ref = "ktor" }
43 | ktor-client-mock = { module = "io.ktor:ktor-client-mock", version.ref = "ktor" }
44 | ktor-serialization-kotlinx-json = { module = "io.ktor:ktor-serialization-kotlinx-json", version.ref = "ktor" }
45 |
46 | [bundles]
47 |
48 | [plugins]
49 | detekt = { id = "io.gitlab.arturbosch.detekt", version.ref = "detekt" }
50 | kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
51 | kotlin-multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
52 | kotlinx-bcv = { id = "org.jetbrains.kotlinx.binary-compatibility-validator", version.ref = "kotlinx-bcv" }
53 | kotlinx-kover = { id = "org.jetbrains.kotlinx.kover", version.ref = "kotlinx-kover" }
54 | kotlinx-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin" }
55 |
--------------------------------------------------------------------------------
/modules/client/src/commonMain/kotlin/api/ChatRequest.kt:
--------------------------------------------------------------------------------
1 | package org.nirmato.ollama.api
2 |
3 | import kotlin.contracts.InvocationKind
4 | import kotlin.contracts.contract
5 | import kotlinx.serialization.Required
6 | import kotlinx.serialization.SerialName
7 | import kotlinx.serialization.Serializable
8 | import org.nirmato.ollama.dsl.OllamaDslMarker
9 |
10 | /**
11 | * Request to generate a predicted chat completion for a prompt.
12 | */
13 | @Serializable
14 | public data class ChatRequest(
15 | /**
16 | * The model name.
17 | * Model names follow a `model:tag` format, where `model` can have an optional namespace such as `example/model`.
18 | * Some examples are `orca-mini:3b-q8_0` and `llama3:70b`. The tag is optional and, if not provided, will default to `latest`.
19 | * The tag is used to identify a specific version.
20 | */
21 | @SerialName(value = "model")
22 | @Required
23 | public val model: String,
24 |
25 | /** The messages of the chat, this can be used to keep a chat memory */
26 | @SerialName(value = "messages")
27 | @Required
28 | public val messages: List,
29 |
30 | /** The list of tools in JSON for the model to use if supported. */
31 | @SerialName(value = "tools")
32 | public val tools: List? = null,
33 |
34 | /** (for thinking models) should the model think before responding? */
35 | @SerialName(value = "think")
36 | val think: Boolean? = null,
37 |
38 | /** The format to return a response in. Currently, the only accepted value is json. */
39 | @SerialName(value = "format")
40 | public val format: Format? = null,
41 |
42 | /** Additional model parameters listed in the documentation for the Modelfile such as `temperature`. */
43 | @SerialName(value = "options")
44 | public val options: Options? = null,
45 |
46 | /**
47 | * How long (in minutes) to keep the model loaded in memory.
48 | *
49 | * - If set to a positive duration (e.g. 20), the model will stay loaded for the provided duration.
50 | * - If set to a negative duration (e.g. -1), the model will stay loaded indefinitely.
51 | * - If set to 0, the model will be unloaded immediately once finished.
52 | * - If not set, the model will stay loaded for 5 minutes by default
53 | */
54 | @SerialName(value = "keep_alive")
55 | public val keepAlive: Int? = null,
56 |
57 | /** If `false` the response will be returned as a single response object, otherwise, the response will be streamed as a series of objects. */
58 | @SerialName(value = "stream")
59 | public val stream: Boolean? = false,
60 | ) {
61 | init {
62 | require(model.isNotBlank()) { "Model name cannot be blank" }
63 | require(messages.isEmpty() || messages.all { it.content.isNotBlank() || it.tools?.isNotEmpty() ?: false }) { "All messages must have a content" }
64 | }
65 |
66 | public companion object {
67 | /** A request to generate a predicted completion for a prompt. */
68 | public fun chatRequest(block: ChatRequestBuilder.() -> Unit): ChatRequest {
69 | contract {
70 | callsInPlace(block, InvocationKind.EXACTLY_ONCE)
71 | }
72 |
73 | return ChatRequestBuilder().apply(block).build()
74 | }
75 |
76 | public fun builder(): ChatRequestBuilder = ChatRequestBuilder()
77 | public fun builder(chatRequest: ChatRequest): ChatRequestBuilder = ChatRequestBuilder(chatRequest)
78 | }
79 |
80 | /** Builder of [ChatRequest] instances. */
81 | @OllamaDslMarker
82 | public class ChatRequestBuilder() {
83 | private var model: String? = null
84 | private var messages: List? = null
85 | private var tools: List? = null
86 | private var format: Format? = null
87 | private var options: Options? = null
88 | private var keepAlive: Int? = null
89 | private var stream: Boolean? = false
90 |
91 | public constructor(chatRequest: ChatRequest) : this() {
92 | model = chatRequest.model
93 | messages = chatRequest.messages
94 | tools = chatRequest.tools
95 | format = chatRequest.format
96 | options = chatRequest.options
97 | keepAlive = chatRequest.keepAlive
98 | stream = chatRequest.stream
99 | }
100 |
101 | public fun model(model: String): ChatRequestBuilder = apply { this.model = model }
102 | public fun messages(messages: List): ChatRequestBuilder = apply { this.messages = messages }
103 | public fun tools(tools: List): ChatRequestBuilder = apply { this.tools = tools }
104 | public fun format(format: Format): ChatRequestBuilder = apply { this.format = format }
105 | public fun options(options: Options): ChatRequestBuilder = apply { this.options = options }
106 | public fun keepAlive(keepAlive: Int): ChatRequestBuilder = apply { this.keepAlive = keepAlive }
107 | public fun stream(stream: Boolean): ChatRequestBuilder = apply { this.stream = stream }
108 |
109 | /** Create [ChatRequest] instance. */
110 | public fun build(): ChatRequest = ChatRequest(
111 | model = requireNotNull(model) { "model is required" },
112 | messages = requireNotNull(messages) { "messages is required" },
113 | tools = tools,
114 | format = format,
115 | options = options,
116 | keepAlive = keepAlive,
117 | stream = stream,
118 | )
119 | }
120 | }
121 |
--------------------------------------------------------------------------------
/modules/client-ktor/src/commonMain/kotlin/client/ktor/OllamaClient.kt:
--------------------------------------------------------------------------------
1 | package org.nirmato.ollama.client.ktor
2 |
3 | import kotlinx.serialization.json.ClassDiscriminatorMode
4 | import kotlinx.serialization.json.Json
5 | import io.ktor.client.HttpClient
6 | import io.ktor.client.HttpClientConfig
7 | import io.ktor.client.engine.HttpClientEngineConfig
8 | import io.ktor.client.engine.HttpClientEngineFactory
9 | import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
10 | import io.ktor.client.plugins.defaultRequest
11 | import io.ktor.http.ContentType
12 | import io.ktor.http.contentType
13 | import io.ktor.serialization.kotlinx.json.json
14 | import org.nirmato.ollama.api.OllamaApi
15 |
16 | /**
17 | * Client for interacting with the Ollama API.
18 | *
19 | * This client provides methods for all Ollama API endpoints.
20 | */
21 | public class OllamaClient(
22 | private val httpClient: HttpClient,
23 | private val clientConfig: OllamaClientConfig,
24 | ) : OllamaApi by OllamaService(
25 | httpTransport = HttpTransport(httpClient, clientConfig)
26 | ), AutoCloseable {
27 |
28 | /**
29 | * Builder for configuring and creating OllamaClient instances.
30 | *
31 | * @param httpClientEngineFactory The ktor HTTP client engine factory to use for the client.
32 | */
33 | public open class Builder internal constructor(
34 | private var httpClientEngineFactory: HttpClientEngineFactory,
35 | ) {
36 | /**
37 | * Base URL for the Ollama API.
38 | */
39 | protected var ollamaBaseUrl: String = "http://localhost:11434/api/"
40 |
41 | /**
42 | * JSON configuration for serialization and deserialization.
43 | */
44 | protected var jsonConfig: Json = Json {
45 | classDiscriminatorMode = ClassDiscriminatorMode.NONE
46 | encodeDefaults = true
47 | explicitNulls = false
48 | ignoreUnknownKeys = true
49 | isLenient = true
50 | prettyPrint = false
51 | }
52 |
53 | protected var client: HttpClient? = null
54 |
55 | protected var httpClientConfig: HttpClientConfig.() -> Unit = {}
56 |
57 | /**
58 | * Configures the JSON serialization and deserialization configuration.
59 | *
60 | * Example:
61 | * ```kotlin
62 | * val client = OllamaClient {
63 | * jsonConfig {
64 | * prettyPrint = false
65 | * ignoreUnknownKeys = true
66 | * }
67 | * }
68 | * ```
69 | *
70 | * @param block Configuration block for JSON configuration.
71 | * @return This builder for chaining
72 | */
73 | public fun jsonConfig(block: Json.() -> Unit) {
74 | jsonConfig = jsonConfig.apply(block)
75 | }
76 |
77 | public fun jsonConfig(json: Json) {
78 | jsonConfig = json
79 | }
80 |
81 | /**
82 | * Configures the underlying HTTP client with custom settings.
83 | *
84 | * Use this method for advanced customization of the HTTP client.
85 | *
86 | * Example:
87 | * ```kotlin
88 | * val client = OllamaClient(CIO) {
89 | * httpClient {
90 | * install(HttpTimeout) {
91 | * requestTimeoutMillis = 60000
92 | * }
93 | * }
94 | * }
95 | * ```
96 | *
97 | * @param httpClientConfig Configuration block for the HTTP client
98 | * @return This builder for chaining
99 | */
100 | public fun httpClient(httpClientConfig: HttpClientConfig.() -> Unit) {
101 | this.httpClientConfig = httpClientConfig
102 | }
103 |
104 | public fun httpClient(client: HttpClient) {
105 | this.client = client
106 | }
107 |
108 | /**
109 | * Builds and returns a new OllamaClient instance with the configured settings.
110 | *
111 | * @return A new OllamaClient instance
112 | */
113 | public fun build(): OllamaClient = OllamaClient(
114 | httpClient = client ?: HttpClient(httpClientEngineFactory) {
115 | defaultRequest {
116 | url(ollamaBaseUrl)
117 | contentType(ContentType.Application.Json)
118 | }
119 |
120 | install(ContentNegotiation) {
121 | json(jsonConfig)
122 | }
123 |
124 | expectSuccess = true
125 |
126 | httpClientConfig(this)
127 | },
128 |
129 | clientConfig = OllamaClientConfig(jsonConfig),
130 | )
131 | }
132 |
133 | /**
134 | * Closes the underlying HTTP client and releases resources.
135 | */
136 | override fun close() {
137 | httpClient.close()
138 | }
139 |
140 | public companion object {
141 | /**
142 | * Creates a new OllamaClient instance using the specified HTTP client engine factory.
143 | *
144 | * @param httpClientEngineFactory The HTTP client engine factory to use for the client.
145 | * @param block Optional configuration block for the client builder.
146 | * @return A new OllamaClient instance
147 | */
148 | public operator fun invoke(httpClientEngineFactory: HttpClientEngineFactory, block: Builder.() -> Unit = {}): OllamaClient =
149 | Builder(httpClientEngineFactory).apply(block).build()
150 | }
151 | }
152 |
--------------------------------------------------------------------------------
/.idea/codeStyles/Project.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
--------------------------------------------------------------------------------
/modules/client/src/commonMain/kotlin/api/Options.kt:
--------------------------------------------------------------------------------
1 | package org.nirmato.ollama.api
2 |
3 | import kotlinx.serialization.SerialName
4 | import kotlinx.serialization.Serializable
5 |
6 | /**
7 | * Additional model parameters listed in the documentation for the Modelfile, such as `temperature`.
8 | */
9 | @Serializable
10 | public data class Options(
11 | /** Sets the size of the context window used to generate the next token. (Default: 2048) */
12 | @SerialName(value = "num_ctx")
13 | val numCtx: Int? = null,
14 |
15 | /** Sets how far back for the model to look back to prevent repetition. (Default: 64, 0 = disabled, -1 = num_ctx) */
16 | @SerialName(value = "repeat_last_n")
17 | val repeatLastN: Int? = null,
18 |
19 | /**
20 | * Sets how strongly to penalize repetitions.
21 | * A higher value (e.g., 1.5) will penalize repetitions more strongly, while a lower value (e.g., 0.9) will be more lenient. (Default: 1.1)
22 | */
23 | @SerialName(value = "repeat_penalty")
24 | val repeatPenalty: Float? = null,
25 |
26 | /**
27 | * The temperature of the model.
28 | * Increasing the temperature will make the model answer more creatively. (Default: 0.8)
29 | */
30 | @SerialName(value = "temperature")
31 | val temperature: Float? = null,
32 |
33 | /**
34 | * Sets the random number seed to use for generation.
35 | * Setting this to a specific number will make the model generate the same text for the same prompt. (Default: 0)
36 | */
37 | @SerialName(value = "seed")
38 | val seed: Int? = null,
39 |
40 | /**
41 | * Sets the stop sequences to use. When this pattern is encountered the LLM will stop generating text and return.
42 | * Multiple stop patterns may be set by specifying multiple separate stop parameters in a modelfile.
43 | * The returned text will not contain the stop sequence.
44 | */
45 | @SerialName(value = "stop")
46 | val stop: List? = null,
47 |
48 | /**
49 | * Tail free sampling is used to reduce the impact of less probable tokens from the output.
50 | * A higher value (e.g., 2.0) will reduce the impact more, while a value of 1.0 disables this setting. (default: 1)
51 | */
52 | @SerialName(value = "tfs_z")
53 | val tfsZ: Float? = null,
54 |
55 | /** Maximum number of tokens to predict when generating text. (Default: 128, -1 = infinite generation, -2 = fill context) */
56 | @SerialName(value = "num_predict")
57 | val numPredict: Int? = null,
58 |
59 | /**
60 | * Reduces the probability of generating nonsense.
61 | * A higher value (e.g. 100) will give more diverse answers, while a lower value (e.g. 10) will be more conservative. (Default: 40)
62 | */
63 | @SerialName(value = "top_k")
64 | val topK: Int? = null,
65 |
66 | /**
67 | * Works together with top-k.
68 | * A higher value (e.g., 0.95) will lead to more diverse text, while a lower value (e.g., 0.5) will generate more focused and conservative text. (Default: 0.9)
69 | */
70 | @SerialName(value = "top_p")
71 | val topP: Float? = null,
72 |
73 | /**
74 | * Alternative to the top_p, and aims to ensure a balance of quality and variety.
75 | * The parameter p represents the minimum probability for a token to be considered, relative to the probability of the most likely token.
76 | * For example, with p=0.05 and the most likely token having a probability of 0.9, logits with a value less than 0.045 are filtered out. (Default: 0.0)
77 | */
78 | @SerialName(value = "min_p")
79 | val minP: Float? = null,
80 |
81 | /** Number of tokens to keep from the prompt. */
82 | @SerialName(value = "num_keep")
83 | val numKeep: Int? = null,
84 |
85 | /** Typical p is used to reduce the impact of less probable tokens from the output. */
86 | @SerialName(value = "typical_p")
87 | val typicalP: Float? = null,
88 |
89 | /** Positive values penalize new tokens based on whether they appear in the text so far, increasing the model's likelihood to talk about new topics. */
90 | @SerialName(value = "presence_penalty")
91 | val presencePenalty: Float? = null,
92 |
93 | /** Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model's likelihood to repeat the same line verbatim. */
94 | @SerialName(value = "frequency_penalty")
95 | val frequencyPenalty: Float? = null,
96 |
97 | /** Penalize newlines in the output. (Default: false) */
98 | @SerialName(value = "penalize_newline")
99 | val penalizeNewline: Boolean? = null,
100 |
101 | /** Enable NUMA support. (Default: false) */
102 | @SerialName(value = "numa")
103 | val numa: Boolean? = null,
104 |
105 | /** Sets the number of batches to use for generation. (Default: 1) */
106 | @SerialName(value = "num_batch")
107 | val numBatch: Int? = null,
108 |
109 | /** The number of layers to send to the GPU(s). On macOS, it defaults to 1 to enable metal support, 0 to disable. */
110 | @SerialName(value = "num_gpu")
111 | val numGpu: Int? = null,
112 |
113 | /** The GPU to use for the main model. Default is 0. */
114 | @SerialName(value = "main_gpu")
115 | val mainGpu: Int? = null,
116 |
117 | /** Enable f16 key/value. (Default: false) */
118 | @SerialName(value = "f16_kv")
119 | val f16Kv: Boolean? = null,
120 |
121 | /** Enable logits all. (Default: false) */
122 | @SerialName(value = "logits_all")
123 | val logitsAll: Boolean? = null,
124 |
125 | /** Enable mmap. (Default: false) */
126 | @SerialName(value = "use_mmap")
127 | val useMmap: Boolean? = null,
128 |
129 | /**
130 | * Sets the number of threads to use during computation.
131 | * By default, Ollama will detect this for optimal performance.
132 | * It is recommended to set this value to the number of physical CPU cores your system has (as opposed to the logical number of cores). */
133 | @SerialName(value = "num_thread")
134 | val numThread: Int? = null,
135 | )
136 |
--------------------------------------------------------------------------------
/modules/client/src/commonMain/kotlin/api/CreateModelRequest.kt:
--------------------------------------------------------------------------------
1 | package org.nirmato.ollama.api
2 |
3 | import kotlin.contracts.InvocationKind
4 | import kotlin.contracts.contract
5 | import kotlinx.serialization.Required
6 | import kotlinx.serialization.SerialName
7 | import kotlinx.serialization.Serializable
8 | import org.nirmato.ollama.dsl.OllamaDslMarker
9 |
10 | /**
11 | * Create model request object.
12 | */
13 | @Serializable
14 | public data class CreateModelRequest(
15 |
16 | /** The name of the model to create. */
17 | @SerialName(value = "model")
18 | @Required
19 | val model: String,
20 |
21 | /** The name of an existing model to create the new model from. */
22 | @SerialName(value = "from")
23 | val from: String? = null,
24 |
25 | /** A dictionary of file names to SHA256 digests of blobs to create the model from. */
26 | @SerialName(value = "files")
27 | val files: Map? = null,
28 |
29 | /** A dictionary of file names to SHA256 digests of blobs for LORA adapters. */
30 | @SerialName(value = "adapters")
31 | val adapters: Map? = null,
32 |
33 | /** The prompt template for the model. */
34 | @SerialName(value = "template")
35 | val template: String? = null,
36 |
37 | /** The license or licenses for the model. */
38 | @SerialName(value = "license")
39 | val license: LicenseModel? = null,
40 |
41 | /** The system prompt for the model. */
42 | @SerialName(value = "system")
43 | val system: String? = null,
44 |
45 | /** The parameters for the model. */
46 | @SerialName(value = "parameters")
47 | val parameters: Map? = null,
48 |
49 | /** The list of message objects used to create a conversation. */
50 | @SerialName(value = "messages")
51 | val messages: List? = null,
52 |
53 | /** Quantize a non-quantized (e.g. float16) model. */
54 | @SerialName(value = "quantize")
55 | val quantize: Quantize? = null,
56 |
57 | /** If `false` the response will be returned as a single response object, otherwise, the response will be streamed as a series of objects. */
58 | @SerialName(value = "stream")
59 | val stream: Boolean? = false,
60 | ) {
61 |
62 | public sealed interface LicenseModel {
63 | public data class SingleLicenseModel(val license: String) : LicenseModel
64 | public data class MultipleLicenseModel(val licenses: List) : LicenseModel
65 | }
66 |
67 | @Serializable
68 | public enum class Quantize {
69 | @SerialName(value = "q4_K_M")
70 | Q4_K_M,
71 |
72 | @SerialName(value = "q4_K_S")
73 | Q4_K_S,
74 |
75 | @SerialName(value = "q8_0")
76 | Q8_0,
77 | }
78 |
79 | public companion object {
80 | /** A request for creating a model. */
81 | public fun createModelRequest(block: CreateModelRequestBuilder.() -> Unit): CreateModelRequest {
82 | contract {
83 | callsInPlace(block, InvocationKind.EXACTLY_ONCE)
84 | }
85 |
86 | return CreateModelRequestBuilder().apply(block).build()
87 | }
88 |
89 | public fun builder(): CreateModelRequestBuilder = CreateModelRequestBuilder()
90 | public fun builder(createModelRequest: CreateModelRequest): CreateModelRequestBuilder = CreateModelRequestBuilder(createModelRequest)
91 | }
92 |
93 | /** Builder of [CreateModelRequest] instances. */
94 | @OllamaDslMarker
95 | public class CreateModelRequestBuilder() {
96 | private var model: String? = null
97 | private var from: String? = null
98 | private var files: Map? = null
99 | private var adapters: Map? = null
100 | private var template: String? = null
101 | private var license: LicenseModel? = null
102 | private var system: String? = null
103 | private var parameters: Map? = null
104 | private var messages: List? = null
105 | private var quantize: Quantize? = null
106 | private var stream: Boolean? = false
107 |
108 | public constructor(createModelRequest: CreateModelRequest) : this() {
109 | model = createModelRequest.model
110 | from = createModelRequest.from
111 | files = createModelRequest.files
112 | adapters = createModelRequest.adapters
113 | template = createModelRequest.template
114 | license = createModelRequest.license
115 | system = createModelRequest.system
116 | parameters = createModelRequest.parameters
117 | messages = createModelRequest.messages
118 | quantize = createModelRequest.quantize
119 | stream = createModelRequest.stream
120 | }
121 |
122 | public fun model(model: String): CreateModelRequestBuilder = apply { this.model = model }
123 | public fun from(from: String): CreateModelRequestBuilder = apply { this.from = from }
124 | public fun files(files: Map): CreateModelRequestBuilder = apply { this.files = files }
125 | public fun adapters(adapters: Map): CreateModelRequestBuilder = apply { this.adapters = adapters }
126 | public fun template(template: String): CreateModelRequestBuilder = apply { this.template = template }
127 | public fun license(license: LicenseModel): CreateModelRequestBuilder = apply { this.license = license }
128 | public fun system(system: String): CreateModelRequestBuilder = apply { this.system = system }
129 | public fun parameters(parameters: Map): CreateModelRequestBuilder = apply { this.parameters = parameters }
130 | public fun messages(messages: List): CreateModelRequestBuilder = apply { this.messages = messages }
131 | public fun quantize(quantize: Quantize): CreateModelRequestBuilder = apply { this.quantize = quantize }
132 | public fun stream(stream: Boolean): CreateModelRequestBuilder = apply { this.stream = stream }
133 |
134 | /** Create [CreateModelRequest] instance. */
135 | public fun build(): CreateModelRequest = CreateModelRequest(
136 | model = requireNotNull(model) { "model is required" },
137 | from = from,
138 | files = files,
139 | adapters = adapters,
140 | template = template,
141 | license = license,
142 | system = system,
143 | parameters = parameters,
144 | messages = messages,
145 | quantize = quantize,
146 | stream = stream,
147 | )
148 | }
149 | }
150 |
--------------------------------------------------------------------------------
/modules/client-ktor/api/client-ktor.api:
--------------------------------------------------------------------------------
1 | public abstract interface class org/nirmato/ollama/api/OllamaApi {
2 | public abstract fun chat (Lorg/nirmato/ollama/api/ChatRequest;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
3 | public abstract fun chatCompletion (Lorg/nirmato/ollama/api/ChatCompletionRequest;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
4 | public abstract fun chatCompletionStream (Lorg/nirmato/ollama/api/ChatCompletionRequest;)Lkotlinx/coroutines/flow/Flow;
5 | public abstract fun chatStream (Lorg/nirmato/ollama/api/ChatRequest;)Lkotlinx/coroutines/flow/Flow;
6 | public abstract fun checkBlob (Lorg/nirmato/ollama/api/CheckBlobRequest;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
7 | public abstract fun copyModel (Lorg/nirmato/ollama/api/CopyModelRequest;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
8 | public abstract fun createBlob (Lorg/nirmato/ollama/api/CreateBlobRequest;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
9 | public abstract fun createModel (Lorg/nirmato/ollama/api/CreateModelRequest;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
10 | public abstract fun createModelStream (Lorg/nirmato/ollama/api/CreateModelRequest;)Lkotlinx/coroutines/flow/Flow;
11 | public abstract fun deleteModel (Lorg/nirmato/ollama/api/DeleteModelRequest;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
12 | public abstract fun generateEmbed (Lorg/nirmato/ollama/api/EmbedRequest;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
13 | public abstract fun getMonitoring (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
14 | public abstract fun getVersion (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
15 | public abstract fun listModels (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
16 | public abstract fun listRunningModels (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
17 | public abstract fun pullModel (Lorg/nirmato/ollama/api/PullModelRequest;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
18 | public abstract fun pullModelStream (Lorg/nirmato/ollama/api/PullModelRequest;)Lkotlinx/coroutines/flow/Flow;
19 | public abstract fun pushModel (Lorg/nirmato/ollama/api/PushModelRequest;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
20 | public abstract fun pushModelStream (Lorg/nirmato/ollama/api/PushModelRequest;)Lkotlinx/coroutines/flow/Flow;
21 | public abstract fun showModel (Lorg/nirmato/ollama/api/ShowModelRequest;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
22 | }
23 |
24 | public final class org/nirmato/ollama/client/ktor/HttpTransportKt {
25 | public static final field MAX_READ_LINE I
26 | }
27 |
28 | public final class org/nirmato/ollama/client/ktor/OllamaClient : java/lang/AutoCloseable, org/nirmato/ollama/api/OllamaApi {
29 | public static final field Companion Lorg/nirmato/ollama/client/ktor/OllamaClient$Companion;
30 | public fun (Lio/ktor/client/HttpClient;Lorg/nirmato/ollama/client/ktor/OllamaClientConfig;)V
31 | public fun chat (Lorg/nirmato/ollama/api/ChatRequest;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
32 | public fun chatCompletion (Lorg/nirmato/ollama/api/ChatCompletionRequest;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
33 | public fun chatCompletionStream (Lorg/nirmato/ollama/api/ChatCompletionRequest;)Lkotlinx/coroutines/flow/Flow;
34 | public fun chatStream (Lorg/nirmato/ollama/api/ChatRequest;)Lkotlinx/coroutines/flow/Flow;
35 | public fun checkBlob (Lorg/nirmato/ollama/api/CheckBlobRequest;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
36 | public fun close ()V
37 | public fun copyModel (Lorg/nirmato/ollama/api/CopyModelRequest;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
38 | public fun createBlob (Lorg/nirmato/ollama/api/CreateBlobRequest;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
39 | public fun createModel (Lorg/nirmato/ollama/api/CreateModelRequest;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
40 | public fun createModelStream (Lorg/nirmato/ollama/api/CreateModelRequest;)Lkotlinx/coroutines/flow/Flow;
41 | public fun deleteModel (Lorg/nirmato/ollama/api/DeleteModelRequest;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
42 | public fun generateEmbed (Lorg/nirmato/ollama/api/EmbedRequest;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
43 | public fun getMonitoring (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
44 | public fun getVersion (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
45 | public fun listModels (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
46 | public fun listRunningModels (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
47 | public fun pullModel (Lorg/nirmato/ollama/api/PullModelRequest;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
48 | public fun pullModelStream (Lorg/nirmato/ollama/api/PullModelRequest;)Lkotlinx/coroutines/flow/Flow;
49 | public fun pushModel (Lorg/nirmato/ollama/api/PushModelRequest;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
50 | public fun pushModelStream (Lorg/nirmato/ollama/api/PushModelRequest;)Lkotlinx/coroutines/flow/Flow;
51 | public fun showModel (Lorg/nirmato/ollama/api/ShowModelRequest;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
52 | }
53 |
54 | public class org/nirmato/ollama/client/ktor/OllamaClient$Builder {
55 | public final fun build ()Lorg/nirmato/ollama/client/ktor/OllamaClient;
56 | protected final fun getClient ()Lio/ktor/client/HttpClient;
57 | protected final fun getHttpClientConfig ()Lkotlin/jvm/functions/Function1;
58 | protected final fun getJsonConfig ()Lkotlinx/serialization/json/Json;
59 | protected final fun getOllamaBaseUrl ()Ljava/lang/String;
60 | public final fun httpClient (Lio/ktor/client/HttpClient;)V
61 | public final fun httpClient (Lkotlin/jvm/functions/Function1;)V
62 | public final fun jsonConfig (Lkotlin/jvm/functions/Function1;)V
63 | public final fun jsonConfig (Lkotlinx/serialization/json/Json;)V
64 | protected final fun setClient (Lio/ktor/client/HttpClient;)V
65 | protected final fun setHttpClientConfig (Lkotlin/jvm/functions/Function1;)V
66 | protected final fun setJsonConfig (Lkotlinx/serialization/json/Json;)V
67 | protected final fun setOllamaBaseUrl (Ljava/lang/String;)V
68 | }
69 |
70 | public final class org/nirmato/ollama/client/ktor/OllamaClient$Companion {
71 | public final fun invoke (Lio/ktor/client/engine/HttpClientEngineFactory;Lkotlin/jvm/functions/Function1;)Lorg/nirmato/ollama/client/ktor/OllamaClient;
72 | public static synthetic fun invoke$default (Lorg/nirmato/ollama/client/ktor/OllamaClient$Companion;Lio/ktor/client/engine/HttpClientEngineFactory;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lorg/nirmato/ollama/client/ktor/OllamaClient;
73 | }
74 |
75 | public final class org/nirmato/ollama/client/ktor/OllamaClientConfig {
76 | public fun ()V
77 | public fun (Lkotlinx/serialization/json/Json;)V
78 | public synthetic fun (Lkotlinx/serialization/json/Json;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
79 | public final fun getJsonConfig ()Lkotlinx/serialization/json/Json;
80 | }
81 |
82 |
--------------------------------------------------------------------------------
/modules/client-ktor/src/commonTest/kotlin/ChatCompletionsClientTest.kt:
--------------------------------------------------------------------------------
1 | package org.nirmato.ollama.client.ktor
2 |
3 | import kotlin.test.Test
4 | import kotlinx.coroutines.test.runTest
5 | import io.ktor.client.engine.mock.respond
6 | import io.ktor.http.HttpHeaders
7 | import io.ktor.http.HttpStatusCode
8 | import io.ktor.http.headers
9 | import org.nirmato.ollama.api.ChatCompletionRequest.Companion.chatCompletionRequest
10 |
11 | internal class ChatCompletionsClientTest {
12 |
13 | @Test
14 | fun chatCompletion_validRequest_returnSuccess() = runTest {
15 | val ollamaClient = OllamaClient(MockHttpClientEngineFactory()) {
16 | httpClient {
17 | engine {
18 | addHandler {
19 | respond(
20 | content = """{
21 | "model": "tinyllama",
22 | "created_at": "2025-01-05T21:48:31.086207438Z",
23 | "response": "The sky blue color occurs due to the presence of tiny organic molecules called chlorophyll. These molecules absorb blue light, which has wavelengths between 400 nm (near-infrared) and 725 nm (ultraviolet), emitted by plants during daylight hours. The excess blue light is then reflected back into the earth's atmosphere, causing the sky to turn a vibrant blue color.\n\nThe exact chemical composition of chlorophyll is still not fully understood, but it is thought that there are several types of chlorophyll present in the leaves of plants, each with different absorption properties. The blue light absorbing property of these chlorophyll molecules is responsible for the vibrant and distinctive color of the sky during daylight hours.",
24 | "done": true,
25 | "done_reason": "stop",
26 | "context": [529, 29989, 5205, 29989, 29958, 13, 3492, 526, 263, 8444, 319, 29902, 20255, 29889, 2, 29871, 13, 29966, 29989, 1792, 29989, 29958, 13, 11008, 338, 278, 14744, 7254, 29973, 2, 29871, 13, 29966, 29989, 465, 22137, 29989, 29958, 13, 1576, 14744, 7254, 2927, 10008, 2861, 304, 278, 10122, 310, 21577, 2894, 293, 13206, 21337, 2000, 521, 5095, 3021, 15114, 29889, 4525, 13206, 21337, 17977, 29890, 7254, 3578, 29892, 607, 756, 10742, 2435, 4141, 9499, 1546, 29871, 29946, 29900, 29900, 302, 29885, 313, 28502, 29899, 7192, 25983, 29881, 29897, 322, 29871, 29955, 29906, 29945, 302, 29885, 313, 499, 5705, 601, 1026, 511, 20076, 9446, 491, 18577, 2645, 2462, 4366, 6199, 29889, 450, 19163, 7254, 3578, 338, 769, 25312, 1250, 964, 278, 8437, 29915, 29879, 25005, 29892, 10805, 278, 14744, 304, 2507, 263, 3516, 2634, 593, 7254, 2927, 29889, 13, 13, 1576, 2684, 22233, 15259, 310, 521, 5095, 3021, 15114, 338, 1603, 451, 8072, 23400, 517, 397, 29892, 541, 372, 338, 2714, 393, 727, 526, 3196, 4072, 310, 521, 5095, 3021, 15114, 2198, 297, 278, 11308, 310, 18577, 29892, 1269, 411, 1422, 17977, 683, 4426, 29889, 450, 7254, 3578, 17977, 10549, 2875, 310, 1438, 521, 5095, 3021, 15114, 13206, 21337, 338, 14040, 363, 278, 3516, 2634, 593, 322, 8359, 573, 2927, 310, 278, 14744, 2645, 2462, 4366, 6199, 29889],
27 | "total_duration": 3824372967,
28 | "load_duration": 1023083163,
29 | "prompt_eval_count": 40,
30 | "prompt_eval_duration": 251000000,
31 | "eval_count": 177,
32 | "eval_duration": 2542000000
33 | }""",
34 | status = HttpStatusCode.OK,
35 | headers {
36 | append(HttpHeaders.ContentType, "application/json")
37 | }
38 | )
39 | }
40 | }
41 | }
42 | }
43 |
44 | val completionRequest = chatCompletionRequest {
45 | model("tinyllama")
46 | prompt("Why is the sky blue?")
47 | }
48 | val response = ollamaClient.chatCompletion(completionRequest)
49 |
50 | println(response.toString())
51 | }
52 |
53 | @Test
54 | fun chatGenerateCompletion_validStreamRequest_returnSuccess() = runTest {
55 | val ollamaClient = OllamaClient(MockHttpClientEngineFactory()) {
56 | httpClient {
57 | engine {
58 | addHandler {
59 | respond(
60 | content = """{"model":"tinyllama","created_at":"2025-01-05T21:48:31.086207438Z","response":"The sky blue color occurs due to the presence of tiny organic molecules called chlorophyll. These molecules absorb blue light, which has wavelengths between 400 nm (near-infrared) and 725 nm (ultraviolet), emitted by plants during daylight hours. The excess blue light is then reflected back into the earth's atmosphere, causing the sky to turn a vibrant blue color.\n\nThe exact chemical composition of chlorophyll is still not fully understood, but it is thought that there are several types of chlorophyll present in the leaves of plants, each with different absorption properties. The blue light absorbing property of these chlorophyll molecules is responsible for the vibrant and distinctive color of the sky during daylight hours.","done":true,"done_reason":"stop","context":[529,29989,5205,29989,29958,13,3492,526,263,8444,319,29902,20255,29889,2,29871,13,29966,29989,1792,29989,29958,13,11008,338,278,14744,7254,29973,2,29871,13,29966,29989,465,22137,29989,29958,13,1576,14744,7254,2927,10008,2861,304,278,10122,310,21577,2894,293,13206,21337,2000,521,5095,3021,15114,29889,4525,13206,21337,17977,29890,7254,3578,29892,607,756,10742,2435,4141,9499,1546,29871,29946,29900,29900,302,29885,313,28502,29899,7192,25983,29881,29897,322,29871,29955,29906,29945,302,29885,313,499,5705,601,1026,511,20076,9446,491,18577,2645,2462,4366,6199,29889,450,19163,7254,3578,338,769,25312,1250,964,278,8437,29915,29879,25005,29892,10805,278,14744,304,2507,263,3516,2634,593,7254,2927,29889,13,13,1576,2684,22233,15259,310,521,5095,3021,15114,338,1603,451,8072,23400,517,397,29892,541,372,338,2714,393,727,526,3196,4072,310,521,5095,3021,15114,2198,297,278,11308,310,18577,29892,1269,411,1422,17977,683,4426,29889,450,7254,3578,17977,10549,2875,310,1438,521,5095,3021,15114,13206,21337,338,14040,363,278,3516,2634,593,322,8359,573,2927,310,278,14744,2645,2462,4366,6199,29889],"total_duration":3824372967,"load_duration":1023083163,"prompt_eval_count":40,"prompt_eval_duration":251000000,"eval_count":177,"eval_duration":2542000000}""",
61 | status = HttpStatusCode.OK,
62 | headers {
63 | append(HttpHeaders.ContentType, "application/json")
64 | }
65 | )
66 | }
67 | }
68 | }
69 | }
70 |
71 | val completionRequest = chatCompletionRequest {
72 | model("tinyllama")
73 | prompt("Why is the sky blue?")
74 | }
75 |
76 | val response = ollamaClient.chatCompletionStream(completionRequest)
77 |
78 | response.collect { println(it) }
79 | }
80 | }
81 |
--------------------------------------------------------------------------------
/modules/client-ktor/src/commonMain/kotlin/client/ktor/HttpTransport.kt:
--------------------------------------------------------------------------------
1 | package org.nirmato.ollama.client.ktor
2 |
3 | import kotlin.coroutines.coroutineContext
4 | import kotlinx.coroutines.cancel
5 | import kotlinx.coroutines.currentCoroutineContext
6 | import kotlinx.coroutines.flow.Flow
7 | import kotlinx.coroutines.flow.FlowCollector
8 | import kotlinx.coroutines.flow.flow
9 | import kotlinx.coroutines.isActive
10 | import kotlinx.io.IOException
11 | import io.ktor.client.HttpClient
12 | import io.ktor.client.call.body
13 | import io.ktor.client.network.sockets.ConnectTimeoutException
14 | import io.ktor.client.network.sockets.SocketTimeoutException
15 | import io.ktor.client.plugins.ClientRequestException
16 | import io.ktor.client.plugins.HttpRequestTimeoutException
17 | import io.ktor.client.plugins.ServerResponseException
18 | import io.ktor.client.request.HttpRequestBuilder
19 | import io.ktor.client.request.request
20 | import io.ktor.client.statement.HttpResponse
21 | import io.ktor.client.statement.HttpStatement
22 | import io.ktor.http.HttpStatusCode.Companion.BadRequest
23 | import io.ktor.http.HttpStatusCode.Companion.Conflict
24 | import io.ktor.http.HttpStatusCode.Companion.Forbidden
25 | import io.ktor.http.HttpStatusCode.Companion.NotFound
26 | import io.ktor.http.HttpStatusCode.Companion.OK
27 | import io.ktor.http.HttpStatusCode.Companion.TooManyRequests
28 | import io.ktor.http.HttpStatusCode.Companion.Unauthorized
29 | import io.ktor.http.HttpStatusCode.Companion.UnsupportedMediaType
30 | import io.ktor.util.reflect.TypeInfo
31 | import io.ktor.util.reflect.typeInfo
32 | import io.ktor.utils.io.ByteReadChannel
33 | import io.ktor.utils.io.CancellationException
34 | import io.ktor.utils.io.cancel
35 | import io.ktor.utils.io.readUTF8Line
36 | import org.nirmato.ollama.api.AuthenticationException
37 | import org.nirmato.ollama.api.FailureResponse
38 | import org.nirmato.ollama.api.InvalidRequestException
39 | import org.nirmato.ollama.api.OllamaClientException
40 | import org.nirmato.ollama.api.OllamaException
41 | import org.nirmato.ollama.api.OllamaGenericIOException
42 | import org.nirmato.ollama.api.OllamaServerException
43 | import org.nirmato.ollama.api.OllamaTimeoutException
44 | import org.nirmato.ollama.api.PermissionException
45 | import org.nirmato.ollama.api.RateLimitException
46 | import org.nirmato.ollama.api.UnknownAPIException
47 |
48 | public const val MAX_READ_LINE: Int = 128_000
49 |
50 | /**
51 | * A transport layer for handling HTTP requests and responses in the Ollama client.
52 | * This class provides methods to perform requests, handle exceptions, and stream events from responses.
53 | *
54 | * @property httpClient The HTTP client used to make requests.
55 | * @property clientConfig Configuration settings for the Ollama client.
56 | */
57 | internal class HttpTransport(
58 | private val httpClient: HttpClient,
59 | private val clientConfig: OllamaClientConfig,
60 | ) {
61 | @Suppress("TooGenericExceptionCaught")
62 | suspend fun handleRequest(info: TypeInfo, builder: HttpRequestBuilder.() -> Unit): T = try {
63 | val response = httpClient.request(builder)
64 |
65 | when (response.status) {
66 | OK -> response.body(info)
67 | else -> throw handleClientException(ClientRequestException(response, ""))
68 | }
69 |
70 | } catch (e: Exception) {
71 | throw handleException(e)
72 | }
73 |
74 | /**
75 | * Perform an HTTP request.
76 | */
77 | suspend inline fun handleRequest(noinline builder: HttpRequestBuilder.() -> Unit): T {
78 | return handleRequest(typeInfo(), builder)
79 | }
80 |
81 | @Suppress("TooGenericExceptionCaught")
82 | suspend fun handleRequest(builder: HttpRequestBuilder.() -> Unit, block: suspend (response: HttpResponse) -> T) {
83 | try {
84 | HttpStatement(builder = HttpRequestBuilder().apply(builder), client = httpClient).execute {
85 | when (it.status) {
86 | OK -> block(it)
87 | else -> throw handleClientException(ClientRequestException(it, ""))
88 | }
89 | }
90 | } catch (e: Exception) {
91 | throw handleException(e)
92 | }
93 | }
94 |
95 | /**
96 | * Perform an HTTP request and transform the result.
97 | */
98 | inline fun handleFlow(noinline builder: HttpRequestBuilder.() -> Unit): Flow {
99 | return cancellableFlow {
100 | handleRequest(builder) { response ->
101 | streamEventsFrom(response)
102 | }
103 | }
104 | }
105 |
106 | @Suppress("LoopWithTooManyJumpStatements")
107 | suspend inline fun FlowCollector.streamEventsFrom(response: HttpResponse) {
108 | val channel = response.body()
109 | try {
110 | while (currentCoroutineContext().isActive && !channel.isClosedForRead) {
111 | val line = channel.readUTF8Line(MAX_READ_LINE)?.takeUnless { it.isEmpty() } ?: continue
112 |
113 | val value: T = this@HttpTransport.clientConfig.jsonConfig.decodeFromString(line)
114 |
115 | emit(value)
116 | }
117 | } finally {
118 | channel.cancel()
119 | }
120 | }
121 |
122 | /**
123 | * Creates a cancellable flow from a regular flow.
124 | * This is useful for streaming responses that need to be cancelled when no longer needed.
125 | *
126 | * @param T The type of data in the flow
127 | * @param block The suspend lambda that produces values for the flow
128 | * @return A new Flow that can be cancelled
129 | */
130 | fun cancellableFlow(block: suspend FlowCollector.() -> Unit): Flow = flow {
131 | try {
132 | block()
133 | } catch (e: Exception) {
134 | if (e is CancellationException) {
135 | coroutineContext.cancel()
136 | } else {
137 | throw e
138 | }
139 | }
140 | }
141 |
142 | /**
143 | * Handles various exceptions that can occur during an API request and converts them into appropriate [OllamaException] instances.
144 | */
145 | private suspend fun handleException(cause: Throwable): RuntimeException = when (cause) {
146 | is CancellationException -> cause
147 | is ClientRequestException -> handleClientException(cause)
148 | is ServerResponseException -> OllamaServerException(cause)
149 |
150 | is HttpRequestTimeoutException,
151 | is SocketTimeoutException,
152 | is ConnectTimeoutException,
153 | -> OllamaTimeoutException(cause)
154 |
155 | is IOException -> OllamaGenericIOException(cause)
156 | else -> OllamaClientException(cause)
157 | }
158 |
159 | /**
160 | * Converts a [ClientRequestException] into a corresponding [OllamaException] based on the HTTP status code.
161 | * This function helps in handling specific API errors and categorizing them into appropriate exception classes.
162 | */
163 | private suspend fun handleClientException(exception: ClientRequestException): OllamaException {
164 | val response = exception.response
165 | val status = response.status
166 | val error = response.body()
167 |
168 | return when (status) {
169 | TooManyRequests -> RateLimitException(status.value, error, exception)
170 |
171 | BadRequest,
172 | NotFound,
173 | Conflict,
174 | UnsupportedMediaType,
175 | -> InvalidRequestException(status.value, error, exception)
176 |
177 | Unauthorized -> AuthenticationException(status.value, error, exception)
178 | Forbidden -> PermissionException(status.value, error, exception)
179 | else -> UnknownAPIException(status.value, error, exception)
180 | }
181 | }
182 | }
183 |
--------------------------------------------------------------------------------
/modules/client/src/commonMain/kotlin/api/ChatCompletionRequest.kt:
--------------------------------------------------------------------------------
1 | package org.nirmato.ollama.api
2 |
3 | import kotlin.contracts.InvocationKind
4 | import kotlin.contracts.contract
5 | import kotlinx.serialization.Required
6 | import kotlinx.serialization.SerialName
7 | import kotlinx.serialization.Serializable
8 | import org.nirmato.ollama.dsl.OllamaDslMarker
9 |
10 | /**
11 | * Request to generate a predicted completion for a prompt.
12 | */
13 | @Serializable
14 | public data class ChatCompletionRequest(
15 | /**
16 | * The model name.
17 | * Model names follow a model:tag format, where model can have an optional namespace such as example/model.
18 | * Some examples are orca-mini:3b-q4_1 and llama3:70b. The tag is optional and, if not provided, will default to latest.
19 | * The tag is used to identify a specific version.
20 | */
21 | @SerialName(value = "model")
22 | @Required
23 | val model: String,
24 |
25 | /** The prompt to generate a response. */
26 | @SerialName(value = "prompt")
27 | @Required
28 | val prompt: String,
29 |
30 | /** The text after the model response. */
31 | @SerialName(value = "suffix")
32 | val suffix: String? = null,
33 |
34 | /** A list of Base64-encoded images to include in the message (for multimodal models such as llava) */
35 | @SerialName(value = "images")
36 | val images: List? = null,
37 |
38 | /** (for thinking models) should the model think before responding? */
39 | @SerialName(value = "think")
40 | val think: Boolean? = null,
41 |
42 | /** The format to return a response in. Format can be json or a JSON schema */
43 | @SerialName(value = "format")
44 | val format: Format? = null,
45 |
46 | /** Additional model parameters listed in the documentation for the Modelfile such as `temperature`. */
47 | @SerialName(value = "options")
48 | val options: Options? = null,
49 |
50 | /** The system prompt to (overrides what is defined in the Modelfile). */
51 | @SerialName(value = "system")
52 | val system: String? = null,
53 |
54 | /** The full prompt or prompt template to use (overrides what is defined in the Modelfile). */
55 | @SerialName(value = "template")
56 | val template: String? = null,
57 |
58 | /**
59 | * If `true` no formatting will be applied to the prompt and no context will be returned.
60 | * You may choose to use the `raw` parameter if you are specifying a full templated prompt in your request to the API, and are managing history yourself.
61 | */
62 | @SerialName(value = "raw")
63 | val raw: Boolean? = null,
64 |
65 | /**
66 | * How long (in minutes) to keep the model loaded into memory following the request (default: 5m)
67 | *
68 | * - If set to a positive duration (e.g. 20), the model will stay loaded for the provided duration.
69 | * - If set to a negative duration (e.g. -1), the model will stay loaded indefinitely.
70 | * - If set to 0, the model will be unloaded immediately once finished.
71 | * - If not set, the model will stay loaded for 5 minutes by default
72 | */
73 | @SerialName(value = "keep_alive")
74 | val keepAlive: Int? = null,
75 |
76 | /** If `false` the response will be returned as a single response object, otherwise, the response will be streamed as a series of objects. */
77 | @SerialName(value = "stream")
78 | val stream: Boolean? = false,
79 |
80 | /** The context parameter returned from a previous request to [CompletionsApi#completion], this can be used to keep a short conversational memory. */
81 | @SerialName(value = "context")
82 | val context: List? = null,
83 | ) {
84 | init {
85 | require(model.isNotBlank()) { "Model name cannot be blank" }
86 | }
87 |
88 | public companion object {
89 | /** A request to generate a predicted completion for a prompt. */
90 | public fun chatCompletionRequest(block: ChatCompletionRequestBuilder.() -> Unit): ChatCompletionRequest {
91 | contract {
92 | callsInPlace(block, InvocationKind.EXACTLY_ONCE)
93 | }
94 |
95 | return ChatCompletionRequestBuilder().apply(block).build()
96 | }
97 |
98 | public fun builder(): ChatCompletionRequestBuilder = ChatCompletionRequestBuilder()
99 | public fun builder(chatCompletionRequest: ChatCompletionRequest): ChatCompletionRequestBuilder = ChatCompletionRequestBuilder(chatCompletionRequest)
100 | }
101 |
102 | /** Builder of [ChatCompletionRequest] instances. */
103 | @OllamaDslMarker
104 | public class ChatCompletionRequestBuilder() {
105 | private var model: String? = null
106 | private var prompt: String? = null
107 | private var suffix: String? = null
108 | private var images: List? = null
109 | private var format: Format? = null
110 | private var options: Options? = null
111 | private var system: String? = null
112 | private var template: String? = null
113 | private var context: List? = null
114 | private var raw: Boolean? = null
115 | private var keepAlive: Int? = null
116 | private var stream: Boolean? = null
117 |
118 | public constructor(chatCompletionRequest: ChatCompletionRequest) : this() {
119 | model = chatCompletionRequest.model
120 | prompt = chatCompletionRequest.prompt
121 | suffix = chatCompletionRequest.suffix
122 | images = chatCompletionRequest.images
123 | format = chatCompletionRequest.format
124 | options = chatCompletionRequest.options
125 | system = chatCompletionRequest.system
126 | template = chatCompletionRequest.template
127 | context = chatCompletionRequest.context
128 | raw = chatCompletionRequest.raw
129 | keepAlive = chatCompletionRequest.keepAlive
130 | stream = chatCompletionRequest.stream
131 | }
132 |
133 | public fun model(model: String): ChatCompletionRequestBuilder = apply { this.model = model }
134 | public fun prompt(prompt: String): ChatCompletionRequestBuilder = apply { this.prompt = prompt }
135 | public fun suffix(suffix: String): ChatCompletionRequestBuilder = apply { this.suffix = suffix }
136 | public fun images(images: List): ChatCompletionRequestBuilder = apply { this.images = images }
137 | public fun format(format: Format): ChatCompletionRequestBuilder = apply { this.format = format }
138 | public fun options(options: Options): ChatCompletionRequestBuilder = apply { this.options = options }
139 | public fun system(system: String): ChatCompletionRequestBuilder = apply { this.system = system }
140 | public fun template(template: String): ChatCompletionRequestBuilder = apply { this.template = template }
141 | public fun context(context: List): ChatCompletionRequestBuilder = apply { this.context = context }
142 | public fun raw(raw: Boolean): ChatCompletionRequestBuilder = apply { this.raw = raw }
143 | public fun keepAlive(keepAlive: Int): ChatCompletionRequestBuilder = apply { this.keepAlive = keepAlive }
144 | public fun stream(stream: Boolean): ChatCompletionRequestBuilder = apply { this.stream = stream }
145 |
146 | /** Create [ChatCompletionRequest] instance. */
147 | public fun build(): ChatCompletionRequest = ChatCompletionRequest(
148 | model = requireNotNull(model) { "model is required" },
149 | prompt = requireNotNull(prompt) { "prompt is required" },
150 | suffix = suffix,
151 | images = images,
152 | format = format,
153 | options = options,
154 | system = system,
155 | template = template,
156 | context = context,
157 | raw = raw,
158 | keepAlive = keepAlive,
159 | stream = stream,
160 | )
161 | }
162 | }
163 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 |
3 | #
4 | # Copyright © 2015 the original authors.
5 | #
6 | # Licensed under the Apache License, Version 2.0 (the "License");
7 | # you may not use this file except in compliance with the License.
8 | # You may obtain a copy of the License at
9 | #
10 | # https://www.apache.org/licenses/LICENSE-2.0
11 | #
12 | # Unless required by applicable law or agreed to in writing, software
13 | # distributed under the License is distributed on an "AS IS" BASIS,
14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | # See the License for the specific language governing permissions and
16 | # limitations under the License.
17 | #
18 | # SPDX-License-Identifier: Apache-2.0
19 | #
20 |
21 | ##############################################################################
22 | #
23 | # Gradle start up script for POSIX generated by Gradle.
24 | #
25 | # Important for running:
26 | #
27 | # (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is
28 | # noncompliant, but you have some other compliant shell such as ksh or
29 | # bash, then to run this script, type that shell name before the whole
30 | # command line, like:
31 | #
32 | # ksh Gradle
33 | #
34 | # Busybox and similar reduced shells will NOT work, because this script
35 | # requires all of these POSIX shell features:
36 | # * functions;
37 | # * expansions «$var», «${var}», «${var:-default}», «${var+SET}»,
38 | # «${var#prefix}», «${var%suffix}», and «$( cmd )»;
39 | # * compound commands having a testable exit status, especially «case»;
40 | # * various built-in commands including «command», «set», and «ulimit».
41 | #
42 | # Important for patching:
43 | #
44 | # (2) This script targets any POSIX shell, so it avoids extensions provided
45 | # by Bash, Ksh, etc; in particular arrays are avoided.
46 | #
47 | # The "traditional" practice of packing multiple parameters into a
48 | # space-separated string is a well documented source of bugs and security
49 | # problems, so this is (mostly) avoided, by progressively accumulating
50 | # options in "$@", and eventually passing that to Java.
51 | #
52 | # Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS,
53 | # and GRADLE_OPTS) rely on word-splitting, this is performed explicitly;
54 | # see the in-line comments for details.
55 | #
56 | # There are tweaks for specific operating systems such as AIX, CygWin,
57 | # Darwin, MinGW, and NonStop.
58 | #
59 | # (3) This script is generated from the Groovy template
60 | # https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
61 | # within the Gradle project.
62 | #
63 | # You can find Gradle at https://github.com/gradle/gradle/.
64 | #
65 | ##############################################################################
66 |
67 | # Attempt to set APP_HOME
68 |
69 | # Resolve links: $0 may be a link
70 | app_path=$0
71 |
72 | # Need this for daisy-chained symlinks.
73 | while
74 | APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path
75 | [ -h "$app_path" ]
76 | do
77 | ls=$( ls -ld "$app_path" )
78 | link=${ls#*' -> '}
79 | case $link in #(
80 | /*) app_path=$link ;; #(
81 | *) app_path=$APP_HOME$link ;;
82 | esac
83 | done
84 |
85 | # This is normally unused
86 | # shellcheck disable=SC2034
87 | APP_BASE_NAME=${0##*/}
88 | # Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036)
89 | APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s\n' "$PWD" ) || exit
90 |
91 | # Use the maximum available, or set MAX_FD != -1 to use that value.
92 | MAX_FD=maximum
93 |
94 | warn () {
95 | echo "$*"
96 | } >&2
97 |
98 | die () {
99 | echo
100 | echo "$*"
101 | echo
102 | exit 1
103 | } >&2
104 |
105 | # OS specific support (must be 'true' or 'false').
106 | cygwin=false
107 | msys=false
108 | darwin=false
109 | nonstop=false
110 | case "$( uname )" in #(
111 | CYGWIN* ) cygwin=true ;; #(
112 | Darwin* ) darwin=true ;; #(
113 | MSYS* | MINGW* ) msys=true ;; #(
114 | NONSTOP* ) nonstop=true ;;
115 | esac
116 |
117 |
118 |
119 | # Determine the Java command to use to start the JVM.
120 | if [ -n "$JAVA_HOME" ] ; then
121 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
122 | # IBM's JDK on AIX uses strange locations for the executables
123 | JAVACMD=$JAVA_HOME/jre/sh/java
124 | else
125 | JAVACMD=$JAVA_HOME/bin/java
126 | fi
127 | if [ ! -x "$JAVACMD" ] ; then
128 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
129 |
130 | Please set the JAVA_HOME variable in your environment to match the
131 | location of your Java installation."
132 | fi
133 | else
134 | JAVACMD=java
135 | if ! command -v java >/dev/null 2>&1
136 | then
137 | die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
138 |
139 | Please set the JAVA_HOME variable in your environment to match the
140 | location of your Java installation."
141 | fi
142 | fi
143 |
144 | # Increase the maximum file descriptors if we can.
145 | if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
146 | case $MAX_FD in #(
147 | max*)
148 | # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked.
149 | # shellcheck disable=SC2039,SC3045
150 | MAX_FD=$( ulimit -H -n ) ||
151 | warn "Could not query maximum file descriptor limit"
152 | esac
153 | case $MAX_FD in #(
154 | '' | soft) :;; #(
155 | *)
156 | # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked.
157 | # shellcheck disable=SC2039,SC3045
158 | ulimit -n "$MAX_FD" ||
159 | warn "Could not set maximum file descriptor limit to $MAX_FD"
160 | esac
161 | fi
162 |
163 | # Collect all arguments for the java command, stacking in reverse order:
164 | # * args from the command line
165 | # * the main class name
166 | # * -classpath
167 | # * -D...appname settings
168 | # * --module-path (only if needed)
169 | # * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables.
170 |
171 | # For Cygwin or MSYS, switch paths to Windows format before running java
172 | if "$cygwin" || "$msys" ; then
173 | APP_HOME=$( cygpath --path --mixed "$APP_HOME" )
174 |
175 | JAVACMD=$( cygpath --unix "$JAVACMD" )
176 |
177 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
178 | for arg do
179 | if
180 | case $arg in #(
181 | -*) false ;; # don't mess with options #(
182 | /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath
183 | [ -e "$t" ] ;; #(
184 | *) false ;;
185 | esac
186 | then
187 | arg=$( cygpath --path --ignore --mixed "$arg" )
188 | fi
189 | # Roll the args list around exactly as many times as the number of
190 | # args, so each arg winds up back in the position where it started, but
191 | # possibly modified.
192 | #
193 | # NB: a `for` loop captures its iteration list before it begins, so
194 | # changing the positional parameters here affects neither the number of
195 | # iterations, nor the values presented in `arg`.
196 | shift # remove old arg
197 | set -- "$@" "$arg" # push replacement arg
198 | done
199 | fi
200 |
201 |
202 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
203 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
204 |
205 | # Collect all arguments for the java command:
206 | # * DEFAULT_JVM_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments,
207 | # and any embedded shellness will be escaped.
208 | # * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be
209 | # treated as '${Hostname}' itself on the command line.
210 |
211 | set -- \
212 | "-Dorg.gradle.appname=$APP_BASE_NAME" \
213 | -jar "$APP_HOME/gradle/wrapper/gradle-wrapper.jar" \
214 | "$@"
215 |
216 | # Stop when "xargs" is not available.
217 | if ! command -v xargs >/dev/null 2>&1
218 | then
219 | die "xargs is not available"
220 | fi
221 |
222 | # Use "xargs" to parse quoted args.
223 | #
224 | # With -n1 it outputs one arg per line, with the quotes and backslashes removed.
225 | #
226 | # In Bash we could simply go:
227 | #
228 | # readarray ARGS < <( xargs -n1 <<<"$var" ) &&
229 | # set -- "${ARGS[@]}" "$@"
230 | #
231 | # but POSIX shell has neither arrays nor command substitution, so instead we
232 | # post-process each arg (as a line of input to sed) to backslash-escape any
233 | # character that might be a shell metacharacter, then use eval to reverse
234 | # that process (while maintaining the separation between arguments), and wrap
235 | # the whole thing up as a single "set" statement.
236 | #
237 | # This will of course break if any of these variables contains a newline or
238 | # an unmatched quote.
239 | #
240 |
241 | eval "set -- $(
242 | printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" |
243 | xargs -n1 |
244 | sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' |
245 | tr '\n' ' '
246 | )" '"$@"'
247 |
248 | exec "$JAVACMD" "$@"
249 |
--------------------------------------------------------------------------------
/modules/client-ktor/src/commonMain/kotlin/client/ktor/OllamaService.kt:
--------------------------------------------------------------------------------
1 | package org.nirmato.ollama.client.ktor
2 |
3 | import kotlinx.coroutines.flow.Flow
4 | import io.ktor.client.request.accept
5 | import io.ktor.client.request.setBody
6 | import io.ktor.client.request.url
7 | import io.ktor.http.ContentType
8 | import io.ktor.http.HttpHeaders
9 | import io.ktor.http.HttpMethod
10 | import io.ktor.http.content.ByteArrayContent
11 | import io.ktor.http.contentType
12 | import io.ktor.http.headers
13 | import org.nirmato.ollama.api.ChatCompletionRequest
14 | import org.nirmato.ollama.api.ChatCompletionResponse
15 | import org.nirmato.ollama.api.ChatRequest
16 | import org.nirmato.ollama.api.ChatResponse
17 | import org.nirmato.ollama.api.CheckBlobRequest
18 | import org.nirmato.ollama.api.CopyModelRequest
19 | import org.nirmato.ollama.api.CreateBlobRequest
20 | import org.nirmato.ollama.api.CreateModelRequest
21 | import org.nirmato.ollama.api.DeleteModelRequest
22 | import org.nirmato.ollama.api.EmbedRequest
23 | import org.nirmato.ollama.api.EmbedResponse
24 | import org.nirmato.ollama.api.ListModelsResponse
25 | import org.nirmato.ollama.api.MonitoringResponse
26 | import org.nirmato.ollama.api.OllamaApi
27 | import org.nirmato.ollama.api.ProgressResponse
28 | import org.nirmato.ollama.api.PullModelRequest
29 | import org.nirmato.ollama.api.PushModelRequest
30 | import org.nirmato.ollama.api.ShowModelRequest
31 | import org.nirmato.ollama.api.ShowModelResponse
32 | import org.nirmato.ollama.api.VersionResponse
33 |
34 | /**
35 | * OllamaService is a client for interacting with the Ollama API.
36 | *
37 | * It provides methods to perform various operations such as chat, chat completion, embedding generation,
38 | * model management (create, copy, delete, pull, push), and more.
39 | *
40 | * @property httpTransport The transport layer used for making HTTP requests.
41 | */
42 | internal class OllamaService(
43 | private val httpTransport: HttpTransport,
44 | ) : OllamaApi {
45 |
46 | override suspend fun chat(chatRequest: ChatRequest): ChatResponse {
47 | val request = if (chatRequest.stream == true) chatRequest.copy(stream = false) else chatRequest
48 |
49 | return httpTransport.handleRequest {
50 | method = HttpMethod.Post
51 | url(path = "chat")
52 | setBody(request)
53 | contentType(ContentType.Application.Json)
54 | accept(ContentType.Application.Json)
55 | }
56 | }
57 |
58 | override fun chatStream(chatRequest: ChatRequest): Flow {
59 | val request = if (chatRequest.stream == false) chatRequest.copy(stream = true) else chatRequest
60 |
61 | return httpTransport.handleFlow {
62 | method = HttpMethod.Post
63 | url(path = "chat")
64 | setBody(request)
65 | contentType(ContentType.Application.Json)
66 | accept(ContentType.Text.EventStream)
67 | headers {
68 | append(HttpHeaders.CacheControl, "no-cache")
69 | append(HttpHeaders.Connection, "keep-alive")
70 | }
71 | }
72 | }
73 |
74 | override suspend fun chatCompletion(chatCompletionRequest: ChatCompletionRequest): ChatCompletionResponse {
75 | val request = if (chatCompletionRequest.stream == true) chatCompletionRequest.copy(stream = false) else chatCompletionRequest
76 |
77 | return httpTransport.handleRequest {
78 | method = HttpMethod.Post
79 | url(path = "generate")
80 | setBody(request)
81 | contentType(ContentType.Application.Json)
82 | accept(ContentType.Application.Json)
83 | }
84 | }
85 |
86 | override fun chatCompletionStream(chatCompletionRequest: ChatCompletionRequest): Flow {
87 | val request = if (chatCompletionRequest.stream == false) chatCompletionRequest.copy(stream = true) else chatCompletionRequest
88 |
89 | return httpTransport.handleFlow {
90 | method = HttpMethod.Post
91 | url(path = "generate")
92 | setBody(request)
93 | contentType(ContentType.Application.Json)
94 | accept(ContentType.Text.EventStream)
95 | headers {
96 | append(HttpHeaders.CacheControl, "no-cache")
97 | append(HttpHeaders.Connection, "keep-alive")
98 | }
99 | }
100 | }
101 |
102 | override suspend fun generateEmbed(generateEmbedRequest: EmbedRequest): EmbedResponse {
103 | return httpTransport.handleRequest {
104 | method = HttpMethod.Post
105 | url(path = "embed")
106 | setBody(generateEmbedRequest)
107 | contentType(ContentType.Application.Json)
108 | accept(ContentType.Application.Json)
109 | }
110 | }
111 |
112 | override suspend fun checkBlob(checkBlobRequest: CheckBlobRequest) {
113 | return httpTransport.handleRequest {
114 | method = HttpMethod.Head
115 | url(path = "blobs/${checkBlobRequest.digest}")
116 | contentType(ContentType.Application.Json)
117 | }
118 | }
119 |
120 | override suspend fun copyModel(copyModelRequest: CopyModelRequest) {
121 | return httpTransport.handleRequest {
122 | method = HttpMethod.Post
123 | url(path = "copy")
124 | setBody(copyModelRequest)
125 | contentType(ContentType.Application.Json)
126 | }
127 | }
128 |
129 | override suspend fun createBlob(createBlobRequest: CreateBlobRequest) {
130 | return httpTransport.handleRequest {
131 | method = HttpMethod.Post
132 | url(path = "blobs/${createBlobRequest.digest}")
133 | setBody(ByteArrayContent(createBlobRequest.body.value, ContentType.Application.OctetStream))
134 | contentType(ContentType.Application.Json)
135 | }
136 | }
137 |
138 | override suspend fun createModel(createModelRequest: CreateModelRequest): ProgressResponse {
139 | return httpTransport.handleRequest {
140 | method = HttpMethod.Post
141 | url(path = "create")
142 | setBody(createModelRequest)
143 | contentType(ContentType.Application.Json)
144 | accept(ContentType.Application.Json)
145 | }
146 | }
147 |
148 | override fun createModelStream(createModelRequest: CreateModelRequest): Flow {
149 | val request = if (createModelRequest.stream == false) createModelRequest.copy(stream = true) else createModelRequest
150 |
151 | return httpTransport.handleFlow {
152 | method = HttpMethod.Post
153 | url(path = "create")
154 | setBody(request)
155 | contentType(ContentType.Application.Json)
156 | accept(ContentType.Application.Json)
157 | }
158 | }
159 |
160 | override suspend fun deleteModel(deleteModelRequest: DeleteModelRequest) {
161 | return httpTransport.handleRequest {
162 | method = HttpMethod.Delete
163 | url(path = "delete")
164 | setBody(deleteModelRequest)
165 | contentType(ContentType.Application.Json)
166 | accept(ContentType.Application.Json)
167 | }
168 | }
169 |
170 | override suspend fun listModels(): ListModelsResponse {
171 | return httpTransport.handleRequest {
172 | method = HttpMethod.Get
173 | url(path = "tags")
174 | contentType(ContentType.Application.Json)
175 | accept(ContentType.Application.Json)
176 | }
177 | }
178 |
179 | override suspend fun listRunningModels(): ListModelsResponse {
180 | return httpTransport.handleRequest {
181 | method = HttpMethod.Get
182 | url(path = "ps")
183 | contentType(ContentType.Application.Json)
184 | accept(ContentType.Application.Json)
185 | }
186 | }
187 |
188 | override suspend fun pullModel(pullModelRequest: PullModelRequest): ProgressResponse {
189 | val request = if (pullModelRequest.stream == true) pullModelRequest.copy(stream = false) else pullModelRequest
190 |
191 | return httpTransport.handleRequest {
192 | method = HttpMethod.Post
193 | url(path = "pull")
194 | setBody(request)
195 | contentType(ContentType.Application.Json)
196 | accept(ContentType.Application.Json)
197 | }
198 | }
199 |
200 | override fun pullModelStream(pullModelRequest: PullModelRequest): Flow {
201 | val request = if (pullModelRequest.stream == false) pullModelRequest.copy(stream = true) else pullModelRequest
202 |
203 | return httpTransport.handleFlow {
204 | method = HttpMethod.Post
205 | url(path = "pull")
206 | setBody(request)
207 | contentType(ContentType.Application.Json)
208 | accept(ContentType.Application.Json)
209 | }
210 | }
211 |
212 | override suspend fun pushModel(pushModelRequest: PushModelRequest): ProgressResponse {
213 | val request = if (pushModelRequest.stream == true) pushModelRequest.copy(stream = false) else pushModelRequest
214 |
215 | return httpTransport.handleRequest {
216 | method = HttpMethod.Post
217 | url(path = "push")
218 | setBody(request)
219 | contentType(ContentType.Application.Json)
220 | accept(ContentType.Application.Json)
221 | }
222 | }
223 |
224 | override fun pushModelStream(pushModelRequest: PushModelRequest): Flow {
225 | val request = if (pushModelRequest.stream == false) pushModelRequest.copy(stream = true) else pushModelRequest
226 |
227 | return httpTransport.handleFlow {
228 | method = HttpMethod.Post
229 | url(path = "push")
230 | setBody(request)
231 | contentType(ContentType.Application.Json)
232 | accept(ContentType.Application.Json)
233 | }
234 | }
235 |
236 | override suspend fun showModel(showModelRequest: ShowModelRequest): ShowModelResponse {
237 | return httpTransport.handleRequest {
238 | method = HttpMethod.Post
239 | url(path = "show")
240 | setBody(showModelRequest)
241 | contentType(ContentType.Application.Json)
242 | accept(ContentType.Application.Json)
243 | }
244 | }
245 |
246 | override suspend fun getMonitoring(): MonitoringResponse {
247 | return httpTransport.handleRequest {
248 | method = HttpMethod.Get
249 | url(path = "version")
250 | accept(ContentType.Application.Json)
251 | }
252 | }
253 |
254 | override suspend fun getVersion(): VersionResponse {
255 | return httpTransport.handleRequest {
256 | method = HttpMethod.Get
257 | url(path = "version")
258 | accept(ContentType.Application.Json)
259 | }
260 | }
261 | }
262 |
--------------------------------------------------------------------------------