ai.ollama
Module ai.ollama

ballerinax/ai.ollama Ballerina library
Overview
This module offers APIs for connecting with Ollama Large Language Models (LLM).
Prerequisites
Ensure that your Ollama server is running locally before using this module in your Ballerina application.
Quickstart
To use the ai.ollama
module in your Ballerina application, update the .bal
file as follows:
Step 1: Import the module
Import the ai.ollama
module.
import ballerinax/ai.ollama;
Step 2: Intialize the Model Provider
Here's how to initialize the Model Provider:
import ballerina/ai; import ballerinax/ai.ollama; final ai:ModelProvider ollamaModel = check new ollama:ModelProvider("ollamaModelName");
Step 4: Invoke chat completion
ai:ChatMessage[] chatMessages = [{role: "user", content: "hi"}]; ai:ChatAssistantMessage response = check ollamaModel->chat(chatMessages, tools = []); chatMessages.push(response);
Clients
ai.ollama: ModelProvider
Provider represents a client for interacting with an Ollama language models.
Constructor
Initializes the client with the given connection configuration and model configuration.
init (string modelType, string serviceUrl, *OllamaModelParameters modleParameters, *ConnectionConfig connectionConfig)
- modelType string - The Ollama model name
- serviceUrl string DEFAULT_OLLAMA_SERVICE_URL - The base URL for the Ollama API endpoint
- modleParameters *OllamaModelParameters - Additional model parameters
- connectionConfig *ConnectionConfig - Additional connection configuration
chat
function chat(ChatMessage[]|ChatUserMessage messages, ChatCompletionFunctions[] tools, string? stop) returns ChatAssistantMessage|Error
Sends a chat request to the Ollama model with the given messages and tools.
Parameters
- messages ChatMessage[]|ChatUserMessage - List of chat messages or user message
- tools ChatCompletionFunctions[] (default []) - Tool definitions to be used for the tool call
- stop string? (default ()) - Stop sequence to stop the completion
Return Type
- ChatAssistantMessage|Error - Function to be called, chat response or an error in-case of failures
generate
function generate(Prompt prompt, typedesc<anydata> td) returns td|Error
Sends a chat request to the model and generates a value that belongs to the type corresponding to the type descriptor argument.
Parameters
- prompt Prompt - The prompt to use in the chat messages
- td typedesc<anydata> (default <>) - Type descriptor specifying the expected return type format
Return Type
- td|Error - Generates a value that belongs to the type, or an error if generation fails
Annotations
ai.ollama: JsonSchema
Records
ai.ollama: ConnectionConfig
Configurations for controlling the behaviours when communicating with a remote HTTP endpoint.
Fields
- httpVersion HttpVersion(default http:HTTP_2_0) - The HTTP version understood by the client
- http1Settings? ClientHttp1Settings - Configurations related to HTTP/1.x protocol
- http2Settings? ClientHttp2Settings - Configurations related to HTTP/2 protocol
- timeout decimal(default 60) - The maximum time to wait (in seconds) for a response before closing the connection
- forwarded string(default "disable") - The choice of setting
forwarded
/x-forwarded
header
- poolConfig? PoolConfiguration - Configurations associated with request pooling
- cache? CacheConfig - HTTP caching related configurations
- compression Compression(default http:COMPRESSION_AUTO) - Specifies the way of handling compression (
accept-encoding
) header
- circuitBreaker? CircuitBreakerConfig - Configurations associated with the behaviour of the Circuit Breaker
- retryConfig? RetryConfig - Configurations associated with retrying
- responseLimits? ResponseLimitConfigs - Configurations associated with inbound response size limits
- secureSocket? ClientSecureSocket - SSL/TLS-related options
- proxy? ProxyConfig - Proxy server related options
- validation boolean(default true) - Enables the inbound payload validation functionality which provided by the constraint package. Enabled by default
ai.ollama: OllamaModelParameters
Represents the model parameters for Ollama text generation. These parameters control the behavior and output of the model.
Fields
- mirostat 0|1|2 (default 0) - Enable Mirostat sampling for controlling perplexity.
0
= disabled1
= Mirostat2
= Mirostat 2.0
- mirostatEta float(default 0.1) - Influences how quickly the algorithm responds to feedback from the generated text.
A lower value results in slower adjustments, while a higher value makes the model more responsive.
- mirostatTau float(default 5.0) - Controls the balance between coherence and diversity of the output.
A lower value results in more focused and coherent text.
- numCtx int(default 2048) - Sets the size of the context window used to generate the next token.
- repeatLastN int(default 64) - Sets how far back the model should look to prevent repetition.
0
= disabled-1
= num_ctx
- repeatPenalty float(default 1.1) - Sets how strongly to penalize repetitions.
A higher value (e.g.,1.5
) will penalize repetitions more strongly,
while a lower value (e.g.,0.9
) will be more lenient.
- temperature float(default 0.8) - Controls the creativity of the model's responses.
A higher value makes the output more diverse, while a lower value makes it more focused.
- seed int(default 0) - Sets the random number seed for deterministic text generation.
A specific value ensures the same output for identical prompts.
- numPredict int(default -1) - Maximum number of tokens to generate.
-1
allows infinite generation.
- topK int(default 40) - Controls randomness by selecting the top-k most likely next words.
A higher value (e.g.,100
) increases diversity,
while a lower value (e.g.,10
) makes responses more conservative.
- topP float(default 0.9) - Controls randomness by considering the cumulative probability of choices.
A higher value (e.g.,0.95
) increases diversity,
while a lower value (e.g.,0.5
) makes responses more conservative.
- minP float(default 0.0) - Ensures a balance between quality and variety.
Filters out low-probability tokens relative to the highest probability token.
Import
import ballerinax/ai.ollama;
Metadata
Released date: about 14 hours ago
Version: 1.1.0
License: Apache-2.0
Compatibility
Platform: java21
Ballerina version: 2201.12.0
GraalVM compatible: Yes
Pull count
Total: 233
Current verison: 0
Weekly downloads
Keywords
AI
Agent
Ollama
Model
Provider
Contributors