B4J Library Ollama4j library - Pnd_Ollama4j - Your local offline LLM like ChatGPT

What is Ollama?
Ollama is a free and open-source project that lets you run various open source LLMs locally.
For more info check this link.

Ollama for Windows and system requirements

Download Ollama server

Top LLMs for Coding All Developers Should Know About

Short video (3 minutes) about Ollama on You Tube

When you finish Ollama server installation, you can run example app to get model of your choice.
You can chat from application, download and delete models, for now.
When you choose model and tag, check DOWNLOAD SIZE, right bottom on image below, some models are over 200GB.
You can start with MODEL: mistral and TAG: 7b, download size is 4.1GB

Hardware requirements:
RAM: 8GB for running 3B models, 16GB for running 7B models, 32GB for running 13B models
Disk Space: 5GB for installing Ollama, additional space required for storing model data, depending on the models you use.
CPU: Any modern CPU with at least 4 cores is recommended, for running 13B models a CPU with at least 8 cores is recommended.
GPU(Optional): A GPU is not required for running Ollama, but it can improve performance, especially for running larger models. If you have a GPU, you can use it to accelerate training of custom models.


1736375081121.png


Chat example:
1736375286831.png



Pnd_Ollama4j

Author:
Author: Ollama4j - B4j Wrapper: Pendrush
Version: 0.30
  • Pnd_LibraryModel
    • Properties:
      • Description As String [read only]
        Returns: the description of the model. This is a brief summary of what the model does.
        It can be used to understand the purpose and capabilities of the model.
      • LastUpdated As String [read only]
        Returns: the last updated date of the model.
        The format of the timestamp may vary depending on the source of the information.
      • Name As String [read only]
        Returns: the name of the model. This is a unique identifier for the model.
      • Object As io.github.ollama4j.models.response.LibraryModel [read only]
      • PopularTags As List [read only]
        Returns: a list of popular tags associated with the model.
        These tags can help categorize and identify the model's purpose or features.
      • PullCount As String [read only]
        Returns: the number of times the model has been pulled or downloaded.
        This can be an indicator of its popularity and usage.
        The format may vary depending on the source.
      • ToString As String [read only]
        Returns a string representation of the Pnd_LibraryModel object.
      • TotalTags As Int [read only]
  • Pnd_LibraryModelDetail
    • Properties:
      • Tags As List [read only]
        Returns: List of Pnd_LibraryModelTag objects. The tags associated with the model.
  • Pnd_LibraryModelTag
    • Properties:
      • LastUpdated As String [read only]
        Returns: the last updated date of the model.
      • Name As String [read only]
        Returns: the name of the model.
      • Size As String [read only]
        Returns: the size of the model.
      • Tag As String [read only]
        Returns: the tag of the model.
      • ToString As String [read only]
        Returns: the ToString representation of the Pnd_LibraryModelTag object.
  • Pnd_Model
    • Properties:
      • Model As String [read only]
        Returns: The model identifier. It is the combination of the model name and version, separated by a slash.
        For example, "gpt-4/0314". This string can be used to uniquely identify the model in systems that support multiple versions of models.
      • ModelName As String [read only]
        Returns: the model name without its version.
      • ModelVersion As String [read only]
        Returns: The version of the model. For example, "0314". This string can be used to identify different versions of the same model.
        It is often a date or a sequence number indicating when the model was trained or updated.
      • Name As String [read only]
        Returns: A string representing the name of the model.
      • Size As Long [read only]
        Returns: The size of the model in kilobytes (KB). This value represents the amount of memory or storage required to store the model.
        It can be useful for understanding the resource requirements of running the model, especially in environments with limited resources.
  • Pnd_Ollama4j
    • Events:
      • PullModelComplete
      • SyncStreamed (Text As String)
    • Functions:
      • Chat (Pnd_OllamaChatRequest As Pnd_OllamaChatRequest) As Pnd_OllamaChatResult
        This method is used to generate a chat response based on the provided request.
        It takes an instance of the Pnd_OllamaChatRequest class as a parameter.
        The method returns an instance of the Pnd_OllamaChatResult class containing the chat response.
      • DeleteModel (ModelName As String)
        Delete a model from Ollama server.
        ModelName – the name of the model to be deleted
      • Embed (ModelName As String, Inputs As List) As Pnd_OllamaEmbedResponseModel
        This method fetches the embeddings for a List of input texts using a specific model from the Ollama library.
        ModelName – the name of the model to be used for generating embeddings.
        Inputs – a List of Strings representing the input texts for which embeddings need to be generated.
        Returns: an Pnd_OllamaEmbedResponseModel object containing the embeddings and other relevant information.
      • GenerateAsync (Model As String, Prompt As String, Raw As Boolean) As Pnd_OllamaAsyncResult
        Model – The name or identifier of the AI model to use for generating the response.
        Prompt – The input text or prompt to provide to the AI model.
        Raw – In some cases, you may wish to bypass the templating system and provide a full prompt. In this case, you can use the raw parameter to disable templating. Also note that raw mode will not return a context.
      • GenerateSync (Model As String, Prompt As String, Raw As Boolean) As Pnd_OllamaResult
        Model – The name or identifier of the AI model to use for generating the response.
        Prompt – The input text or prompt to provide to the AI model.
        Raw – In some cases, you may wish to bypass the templating system and provide a full prompt. In this case, you can use the raw parameter to disable templating. Also note that raw mode will not return a context.
      • GenerateSyncStreamed (Model As String, Prompt As String, Raw As Boolean) As Pnd_OllamaResult
        Model – The name or identifier of the AI model to use for generating the response.
        Prompt – The input text or prompt to provide to the AI model.
        Raw – In some cases, you may wish to bypass the templating system and provide a full prompt. In this case, you can use the raw parameter to disable templating. Also note that raw mode will not return a context.
        Returns Text in event _SyncStreamed (Text As String)
      • GenerateWithImageFiles (Model As String, Prompt As String, ListOfFiles As List) As Pnd_OllamaResult
        Model – The name or identifier of the AI model to use for generating the response.
        Prompt – The input text or prompt to provide to the AI model.
        ListOfFiles - A List containing paths to image files.
      • GenerateWithImageURLs (Model As String, Prompt As String, ListOfURLs As List) As Pnd_OllamaResult
        Model – The name or identifier of the AI model to use for generating the response.
        Prompt – The input text or prompt to provide to the AI model.
        ListOfURLs - A List containing URLs to image files.
      • Initialize (EventName As String, Host As String)
        Initializes the Ollama4J with a given host URL.
        EventName - Event name
        Host - The URL of the Ollama server.
        Ollama.Initialize("Ollama", "http://localhost:11434/")
      • IsInitialized As Boolean
      • LibraryModelDetails (LibraryModel As Pnd_LibraryModel) As Pnd_LibraryModelDetail
        Fetches the tags associated with a specific model from Ollama library.
        This method fetches the available model tags directly from Ollama library model page, including model tag name, size and time when model was last updated into a list of LibraryModelTag objects.
        LibraryModel – the LibraryModel object which contains the name of the library model for which the tags need to be fetched.
        Returns: a List of Pnd_LibraryModelDetail objects containing the extracted tags and their associated metadata.
      • ListModels As List
        Lists available models from the Ollama server.
        Returns: a List of models available on the server
      • ListModelsFromLibrary As List
        Retrieves a list of models from the Ollama library.
        This method fetches the available models directly from Ollama library page, including model details such as the name, pull count, popular tags, tag count, and the time when model was updated.
        Returns: a List of LibraryModel
      • Ping As Boolean
        API to check the reachability of Ollama server.
        Returns: True if the server is reachable, False otherwise.
      • PullModel (ModelName As String)
        Pull a model on the Ollama server from the list of available models.
        ModelName – the name of the model.
    • Properties:
      • RequestTimeoutSeconds As Long [write only]
        Sets the request timeout in seconds for API calls.
        RequestTimeoutSeconds - The timeout duration in seconds.
      • Verbose As Boolean [write only]
        Enables or disables verbose logging for API requests.
  • Pnd_OllamaAsyncResult
    • Properties:
      • CompleteResponse As String [read only]
        Returns a string representation of the complete response associated with this `Pnd_OllamaAsyncResult` object.
        It's important to note that this method should be called after the streaming process has completed to ensure that all parts of the response are included in the returned string.
      • HttpStatusCode As Int [read only]
        Returns the HTTP status code of the response associated with this `Pnd_OllamaAsyncResult` object.
        This method can be useful for determining the success or failure of the API request that generated the response. Common status codes include:
      • IsAlive As Boolean [read only]
        Returns: True if the thread is alive. A thread is alive if it has been started and has not yet died.
        In a more practical sense, this method can be used to check if the asynchronous operation represented by the
        `Pnd_OllamaAsyncResult` object is still ongoing or has completed.
      • IsDaemon As Boolean [read only]
        Returns: True if the thread is a daemon.
        A daemon thread is a low-priority thread that runs in the background.
      • IsInterrupted As Boolean [read only]
        Returns: True if the thread has been interrupted. A thread is interrupted when another thread invokes its interrupt method.
        This method can be used to check if the asynchronous operation has been interrupted or canceled.
      • IsSucceeded As Boolean [read only]
        Returns: True if the thread has finished execution. A thread is considered to have finished execution when its run method exits.
        This method can be used to check if the asynchronous operation represented by the `Pnd_OllamaAsyncResult` object has completed successfully.
      • StreamPool As String [read only]
        Returns: String associated with this `Pnd_OllamaAsyncResult` object.
        This stream contains the parts of the response that have been received so far.
      • ToString As String [read only]
        Returns the complete response as a string, including all parts of the streamed response.
        This method is useful when you need to access the entire content of the response after the streaming process has completed.
  • Pnd_OllamaChatMessage
    • Properties:
      • Content As String [read only]
        Get content of the message.
      • Images As List [read only]
        Returns: a List of images associated with the message. Each image is represented as a byte array.
      • Role As Pnd_OllamaChatMessageRole [read only]
        Returns: the role of the message as an Pnd_OllamaChatMessageRole object.
      • ToolCalls As List [read only]
        Returns: a List of OllamaChatToolCalls associated with the message.
      • ToString As String [read only]
        Returns: object as string.
  • Pnd_OllamaChatMessageRole
    • Functions:
      • ASSISTANT As io.github.ollama4j.models.chat.OllamaChatMessageRole
        A role that represents a assistant message.
      • getRole (roleName As String) As io.github.ollama4j.models.chat.OllamaChatMessageRole
      • getRoles (roleName As String) As java.util.List
      • Initialize
      • IsInitialized As Boolean
      • newCustomRole (roleName As String) As io.github.ollama4j.models.chat.OllamaChatMessageRole
      • SYSTEM As io.github.ollama4j.models.chat.OllamaChatMessageRole
        A role that represents a system message.
      • TOOL As io.github.ollama4j.models.chat.OllamaChatMessageRole
        A role that represents a tool message.
      • USER As io.github.ollama4j.models.chat.OllamaChatMessageRole
        A role that represents a user message.
  • Pnd_OllamaChatRequest
    • Functions:
      • Initialize (Pnd_OllamaChatRequest As Pnd_OllamaChatRequest)
        Initialize the wrapper with an existing Pnd_OllamaChatRequest object.
      • IsInitialized As Boolean
      • isStream As Boolean
    • Properties:
      • KeepAlive As String [read only]
      • Model As String [read only]
      • ReturnFormatJson As Boolean [read only]
      • Template As String [read only]
      • ToString As String [read only]
        Returns: Object as string
  • Pnd_OllamaChatRequestBuilder
    • Functions:
      • Build As Pnd_OllamaChatRequest
        Build chat request.
      • Initialize (ModelName As String) As io.github.ollama4j.models.chat.OllamaChatRequestBuilder
        Initialize the builder with a model name. This is required to create a valid chat request.
      • IsInitialized As Boolean
      • Reset
        Reset the builder to its initial state.
      • WithGetJsonResponse As io.github.ollama4j.models.chat.OllamaChatRequestBuilder
      • WithKeepAlive (KeepAlive As String) As io.github.ollama4j.models.chat.OllamaChatRequestBuilder
      • WithMessage (role As Pnd_OllamaChatMessageRole, Content As String) As io.github.ollama4j.models.chat.OllamaChatRequestBuilder
        Add a message to the chat request.
      • WithMessages (Messages As java.util.List) As io.github.ollama4j.models.chat.OllamaChatRequestBuilder
        Add a list of messages to the chat request.
      • WithStreaming As io.github.ollama4j.models.chat.OllamaChatRequestBuilder
      • WithTemplate (Template As String) As io.github.ollama4j.models.chat.OllamaChatRequestBuilder
  • Pnd_OllamaChatResponseModel
    • Properties:
      • Context As List [read only]
        Returns: a List that contains the context values integers.
      • CreatedAt As String [read only]
      • DoneReason As String [read only]
        Returns: A string that indicates the reason why the response is done.
      • Error As String [read only]
        Returns: A string that indicates the error message if there is an error in the response.
      • EvalCount As Int [read only]
        Returns: an integer that contains the evaluation count.
      • EvalDuration As Long [read only]
        Returns: a Long that contains the evaluation duration.
      • IsDone As Boolean [read only]
        Returns: A boolean indicating whether the response is done or not.
      • Message As Pnd_OllamaChatMessage [read only]
        Returns: a Pns_OllamaChatMessage that contains the message object.
      • Model As String [read only]
        Returns: The model used for the response.
      • PromptEvalCount As Int [read only]
        Returns: an integer that contains the prompt evaluation count.
      • PromptEvalDuration As Long [read only]
        Returns: a Long that contains the duration of the prompt evaluation.
      • ToString As String [read only]
        Returns: object as string
      • TotalDuration As Long [read only]
        Returns: a Long that contains the total duration of the response.
  • Pnd_OllamaChatResult
    • Functions:
      • IsInitialized As Boolean
    • Properties:
      • ChatHistory As java.util.List [read only]
        Get Java list of the chat history from the chat result.
        This is used when you want to continue a conversation.
        It is a list of OllamaChatMessage objects.
      • ChatHistoryList As List [read only]
        ChatHistoryList is B4X List and have same values as ChatHistory.
        Returns: a B4X List of the chat history from the chat result.
      • ResponseModel As Pnd_OllamaChatResponseModel [read only]
        Get the response model from the chat result.
        This method returns a object that provides access to the underlying Pnd_OllamaChatResponse.
      • ToString As String [read only]
        Get the string representation of the chat result.
  • Pnd_OllamaChatToolCalls
    • Functions:
      • toString As String
        Returns: the string representation of the Pnd_OllamaChatToolCalls.
    • Properties:
      • Function As Pnd_OllamaToolCallsFunction [read only]
        Return: an instance of the Pnd_OllamaToolCallsFunction.
  • Pnd_OllamaEmbedResponseModel
    • Properties:
      • Embeddings As List [read only]
        Returns a List of embeddings generated by the model. Each embedding is represented as a List of double values.
        The number of embeddings returned depends on the number of prompts/inputs provided to the model during evaluation.
        This method will return a List containing an List of Double values representing an embedding for each prompt.
      • LoadDuration As Long [read only]
        Returns the duration of loading the model in milliseconds.
        This value is set when the model is loaded and can be used to measure the performance of the model loading process.
      • Model As String [read only]
        Returns the name of the model used for generating the embeddings.
      • PromptEvalCount As Int [read only]
        Returns the number of times the prompt was evaluated.
        This value is typically set to 1 for a single prompt evaluation, but may be higher if the prompt is evaluated multiple times.
      • ToString As String [read only]
        Returns the embeddings generated by the model as string.
      • TotalDuration As Long [read only]
        Returns the total duration of the prompt evaluation in milliseconds.
        This includes the time taken to load the model and execute the prompt.
        Note that this value may vary depending on the hardware and software environment where the code is executed.
  • Pnd_OllamaModelType
    • Fields:
      • ALFRED As String
      • ALL_MINILM As String
      • BAKLLAVA As String
      • CODEBOOGA As String
      • CODELLAMA As String
      • CODESTRAL As String
      • CODEUP As String
      • DEEPSEEK_CODER As String
      • DEEPSEEK_LLM As String
      • DOLPHIN_MISTRAL As String
      • DOLPHIN_MIXTRAL As String
      • DOLPHIN_PHI As String
      • DUCKDB_NSQL As String
      • EVERYTHINGLM As String
      • FALCON As String
      • GEMMA As String
      • GEMMA2 As String
      • GOLIATH As String
      • LLAMA2 As String
      • LLAMA2_CHINESE As String
      • LLAMA2_UNCENSORED As String
      • LLAMA3 As String
      • LLAMA3_1 As String
      • LLAMA_PRO As String
      • LLAVA As String
      • LLAVA_PHI3 As String
      • MAGICODER As String
      • MEDITRON As String
      • MEDLLAMA2 As String
      • MEGADOLPHIN As String
      • MISTRAL As String
      • MISTRAL_OPENORCA As String
      • MISTRALLITE As String
      • MIXTRAL As String
      • NEURAL_CHAT As String
      • NEXUSRAVEN As String
      • NOMIC_EMBED_TEXT As String
      • NOTUS As String
      • NOTUX As String
      • NOUS_HERMES As String
      • NOUS_HERMES2 As String
      • NOUS_HERMES2_MIXTRAL As String
      • OPEN_ORCA_PLATYPUS2 As String
      • OPENCHAT As String
      • OPENHERMES As String
      • ORCA2 As String
      • ORCA_MINI As String
      • PHI As String
      • PHI3 As String
      • PHIND_CODELLAMA As String
      • QWEN As String
      • QWEN2 As String
      • SAMANTHA_MISTRAL As String
      • SOLAR As String
      • SQLCODER As String
      • STABLE_BELUGA As String
      • STABLE_CODE As String
      • STABLELM2 As String
      • STABLELM_ZEPHYR As String
      • STARCODER As String
      • STARLING_LM As String
      • TINYDOLPHIN As String
      • TINYLLAMA As String
      • VICUNA As String
      • WIZARD_MATH As String
      • WIZARD_VICUNA As String
      • WIZARD_VICUNA_UNCENSORED As String
      • WIZARDCODER As String
      • WIZARDLM As String
      • WIZARDLM_UNCENSORED As String
      • XWINLM As String
      • YARN_LLAMA2 As String
      • YARN_MISTRAL As String
      • YI As String
      • ZEPHYR As String
  • Pnd_OllamaResult
    • Properties:
      • HttpStatusCode As Int [read only]
        Returns: An integer representing the HTTP status code.
      • Response As String [read only]
        Returns: A string representing the response from the API.
      • ResponseTime As Long [read only]
        Returns: A long representing the response time in milliseconds.
      • ToString As String [read only]
        Returns: A string that represents the object.
  • Pnd_OllamaToolCallsFunction
    • Properties:
      • Arguments As Map [read only]
        Returns: the arguments of the tool call as a Map.
        The keys are the argument names as String and the values are the argument values as Objects of their respective types.
      • Name As String [read only]
        Returns: the name
      • ToString As String [read only]
        Returns: the string representation of the Pnd_OllamaToolCallsFunction.
  • Pnd_PromptBuilder
    • Functions:
      • Add (Text As String) As io.github.ollama4j.utils.PromptBuilder
        Appends the specified text to the prompt.
        Text – the text to be added to the prompt.
      • AddLine (Text As String) As io.github.ollama4j.utils.PromptBuilder
        Appends the specified text followed by a newline character to the prompt.
        Text – the text to be added as a line to the prompt
      • AddSeparator As io.github.ollama4j.utils.PromptBuilder
        Appends a separator line to the prompt.
        The separator is a newline followed by a line of dashes.
      • Build As String
        Builds and returns the final prompt as a string.
      • Initialize
        Initializes a the Pnd_PromptBuilder class.
      • IsInitialized As Boolean

Versions:

v0.17
  • New methods: GenerateWithImageFiles and GenerateWithImageURLs
  • New example apps

v0.18
  • New class: Pnd_OllamaEmbedResponseModel
  • New method: Embed
  • New example app

v0.30
  • Lots of new classes to support chat feature
  • New method: Chat
  • Original library update to v1.0.91
  • New example apps


You can also check B4X AI Assistant based on this library (including B4J source code).

Wrapper is based on Ollama4j v1.0.91 from HERE.
This wrapper is in a very early stage of development, but you can try it.
Download library from: https://www.dropbox.com/scl/fi/e2wx...ey=fneim7i6rct2kzfsq8cvyp919&st=9ly5ihxr&dl=0
 

Attachments

  • Ollama4jExamples.zip
    117.6 KB · Views: 147
Last edited:

jmon

Well-Known Member
Licensed User
Longtime User
Last edited:

jmon

Well-Known Member
Licensed User
Longtime User
To compile as a standalone package you need to add this :
B4X:
#PackagerProperty: IncludedModules = java.sql

edit: hum.. I can't get it to work after compilation with the packager. I wonder if It need org.jsoup or com.sun or something else. I get this error:
java.lang.NoClassDefFoundError: com/sun/management/OperatingSystemMXBean
 
Last edited:

Pendrush

Well-Known Member
Licensed User
Longtime User
Can you try with:

B4X:
    #PackagerProperty: AdditionalModuleInfoString = exports org.slf4j;
    #PackagerProperty: AdditionalModuleInfoString = exports org.slf4j.spi;
    #PackagerProperty: AdditionalModuleInfoString = exports org.slf4j.event;
    #PackagerProperty: AdditionalModuleInfoString = exports org.slf4j.helpers;
    #PackagerProperty: AdditionalModuleInfoString = uses org.slf4j.spi.SLF4JServiceProvider;

    '#PackagerProperty: AdditionalModuleInfoString = requires transitive java.xml;
    #PackagerProperty: AdditionalModuleInfoString = exports com.fasterxml.jackson.annotation;
    #PackagerProperty: AdditionalModuleInfoString = opens com.fasterxml.jackson.annotation;
    '#PackagerProperty: AdditionalModuleInfoString = requires static org.jspecify;
    #PackagerProperty: AdditionalModuleInfoString = exports org.jsoup;
    #PackagerProperty: AdditionalModuleInfoString = exports org.jsoup.helper;
    #PackagerProperty: AdditionalModuleInfoString = exports org.jsoup.nodes;
    #PackagerProperty: AdditionalModuleInfoString = exports org.jsoup.parser;
    #PackagerProperty: AdditionalModuleInfoString = exports org.jsoup.safety;
    #PackagerProperty: AdditionalModuleInfoString = exports org.jsoup.select;
    #PackagerProperty: AdditionalModuleInfoString = requires java.logging;
    #PackagerProperty: AdditionalModuleInfoString = exports com.fasterxml.jackson.core;
    '#PackagerProperty: AdditionalModuleInfoString = requires transitive com.fasterxml.jackson.annotation;
    '#PackagerProperty: AdditionalModuleInfoString = requires transitive com.fasterxml.jackson.core;
    #PackagerProperty: AdditionalModuleInfoString = requires static java.sql;
    '#PackagerProperty: AdditionalModuleInfoString = requires static java.xml;
    #PackagerProperty: AdditionalModuleInfoString = requires static java.desktop;
    #PackagerProperty: AdditionalModuleInfoString = exports com.fasterxml.jackson.databind;
    #PackagerProperty: AdditionalModuleInfoString = exports com.fasterxml.jackson.databind.cfg;
    #PackagerProperty: AdditionalModuleInfoString = exports com.fasterxml.jackson.databind.annotation;
    #PackagerProperty: AdditionalModuleInfoString = exports com.fasterxml.jackson.databind.deser;
    #PackagerProperty: AdditionalModuleInfoString = exports com.fasterxml.jackson.databind.deser.impl;
    #PackagerProperty: AdditionalModuleInfoString = exports com.fasterxml.jackson.databind.deser.std;
    #PackagerProperty: AdditionalModuleInfoString = exports com.fasterxml.jackson.databind.exc;
    #PackagerProperty: AdditionalModuleInfoString = exports com.fasterxml.jackson.databind.ext;
    #PackagerProperty: AdditionalModuleInfoString = exports com.fasterxml.jackson.databind.introspect;
    #PackagerProperty: AdditionalModuleInfoString = exports com.fasterxml.jackson.databind.json;
    #PackagerProperty: AdditionalModuleInfoString = exports com.fasterxml.jackson.databind.jsonFormatVisitors;
    #PackagerProperty: AdditionalModuleInfoString = exports com.fasterxml.jackson.databind.jsonschema;
    #PackagerProperty: AdditionalModuleInfoString = exports com.fasterxml.jackson.databind.jsontype;
    #PackagerProperty: AdditionalModuleInfoString = exports com.fasterxml.jackson.databind.jsontype.impl;
    #PackagerProperty: AdditionalModuleInfoString = exports com.fasterxml.jackson.databind.module;
    #PackagerProperty: AdditionalModuleInfoString = exports com.fasterxml.jackson.databind.node;
    #PackagerProperty: AdditionalModuleInfoString = exports com.fasterxml.jackson.databind.ser;
    #PackagerProperty: AdditionalModuleInfoString = exports com.fasterxml.jackson.databind.ser.impl;
    #PackagerProperty: AdditionalModuleInfoString = exports com.fasterxml.jackson.databind.ser.std;
    #PackagerProperty: AdditionalModuleInfoString = exports com.fasterxml.jackson.databind.type;
    #PackagerProperty: AdditionalModuleInfoString = exports com.fasterxml.jackson.databind.util;
    '#PackagerProperty: AdditionalModuleInfoString = uses com.fasterxml.jackson.databind.Module;
    #PackagerProperty: AdditionalModuleInfoString = provides com.fasterxml.jackson.core.ObjectCodec with com.fasterxml.jackson.databind.ObjectMapper;

For some reason I also need to add on top:
B4X:
    #PackagerProperty: IncludedModules = javafx.swing
    #PackagerProperty: IncludedModules = java.net.http
    #PackagerProperty: IncludedModules = javafx.controls

Example app is updated.
 
Last edited:

Pendrush

Well-Known Member
Licensed User
Longtime User
Test model with question to optimize B4X code.

Model: qwen2.5-coder:14b


Question:
Your task is to improve the code's efficiency, readability, and adherence to best practices without changing its core functionality.
Analyze the code and suggest optimizations that could improve its performance, readability, or maintainability. Focus on:
Reducing time complexity
Improving space efficiency
Enhancing code readability
Applying relevant design patterns or coding best practices
Provide your optimized version of the code, along with brief comments explaining the key changes and their benefits.
Keep your response concise and focused on the most impactful optimizations.
Here's the code to optimize writen with B4X language:
B4X:
Dim Result As Pnd_OllamaAsyncResult = Main.Ollama.GenerateAsync(cBox.Value, txtInput.Text, False)
    Do While True
        Dim Token As String = Result.StreamPool
        txtOutput.Text = txtOutput.Text & Token.Replace("\n", CRLF)
        txtOutput.SetSelection(txtOutput.Text.Length, txtOutput.Text.Length)
        If Result.IsAlive = False Then
            txtOutput.Text = txtOutput.Text & CRLF & CRLF
            txtOutput.SetSelection(txtOutput.Text.Length, txtOutput.Text.Length)
            Exit
        End If
        Sleep(100)
    Loop


Answer:
To optimize the provided code, we can focus on reducing unnecessary operations, improving readability, and ensuring better performance. Here are the key changes:

1. **Avoid Concatenating Strings in a Loop**: Instead of appending strings in each iteration, use a `StringBuilder` to reduce memory allocation overhead.
2. **Use Constants for Magic Numbers**: Replace magic numbers like `100` with named constants for better readability and maintainability.
3. **Optimize the Exit Condition**: Ensure that the loop exits as soon as possible by checking the exit condition more frequently.
4. **Improve Readability with Clear Naming**: Use clear and descriptive variable names to enhance code readability.

Here is the optimized version of the code:

B4X:
' Constants for better readability and maintainability
Dim POLL_INTERVAL As Int = 100 ' milliseconds

' Use StringBuilder for efficient string concatenation
Dim outputBuilder As StringBuilder
outputBuilder.Initialize

' Start the asynchronous generation process
Dim result As Pnd_OllamaAsyncResult = Main.Ollama.GenerateAsync(cBox.Value, txtInput.Text, False)

Do While True
    ' Fetch the next token from the stream pool
    Dim token As String = Result.StreamPool

    ' Append the token to the output builder, replacing newlines with CRLF
    outputBuilder.Append(token.Replace("\n", CRLF))

    ' Update the text box with the current content of the StringBuilder
    txtOutput.Text = outputBuilder.ToString()
    txtOutput.SetSelection(txtOutput.Text.Length, txtOutput.Text.Length)

    ' Check if the asynchronous process is complete
    If Result.IsAlive = False Then

        ' Append two newlines and update the selection position
        outputBuilder.Append(CRLF & CRLF)
        txtOutput.Text = outputBuilder.ToString()
        txtOutput.SetSelection(txtOutput.Text.Length, txtOutput.Text.Length)
     
        ' Exit the loop as the process is complete
        Exit
    End If
 
    ' Sleep for a short period before checking again
    Sleep(POLL_INTERVAL)
Loop


### Key Changes and Benefits:

1. **StringBuilder**: By using `StringBuilder`, we avoid creating multiple intermediate string objects, which reduces memory usage and improves performance.
2. **Constants**: The use of named constants (`POLL_INTERVAL`) makes the code more readable and easier to maintain.
3. **Efficient Updates**: Updating the text box only once per loop iteration (after appending all tokens) minimizes unnecessary UI updates, improving responsiveness.
4. **Clear Naming**: Descriptive variable names like `outputBuilder` make the code easier to understand.

These changes should enhance the performance, readability, and maintainability of the code without altering its core functionality.
-- END OF ANSWER --


1736550667426.png
 
Last edited:

jmon

Well-Known Member
Licensed User
Longtime User
Can you try with:

B4X:
    #PackagerProperty: AdditionalModuleInfoString = exports org.slf4j;
    #PackagerProperty: AdditionalModuleInfoString = exports org.slf4j.spi;
    #PackagerProperty: AdditionalModuleInfoString = exports org.slf4j.event;
    #PackagerProperty: AdditionalModuleInfoString = exports org.slf4j.helpers;
    #PackagerProperty: AdditionalModuleInfoString = uses org.slf4j.spi.SLF4JServiceProvider;

    '#PackagerProperty: AdditionalModuleInfoString = requires transitive java.xml;
    #PackagerProperty: AdditionalModuleInfoString = exports com.fasterxml.jackson.annotation;
    #PackagerProperty: AdditionalModuleInfoString = opens com.fasterxml.jackson.annotation;
    '#PackagerProperty: AdditionalModuleInfoString = requires static org.jspecify;
    #PackagerProperty: AdditionalModuleInfoString = exports org.jsoup;
    #PackagerProperty: AdditionalModuleInfoString = exports org.jsoup.helper;
    #PackagerProperty: AdditionalModuleInfoString = exports org.jsoup.nodes;
    #PackagerProperty: AdditionalModuleInfoString = exports org.jsoup.parser;
    #PackagerProperty: AdditionalModuleInfoString = exports org.jsoup.safety;
    #PackagerProperty: AdditionalModuleInfoString = exports org.jsoup.select;
    #PackagerProperty: AdditionalModuleInfoString = requires java.logging;
    #PackagerProperty: AdditionalModuleInfoString = exports com.fasterxml.jackson.core;
    '#PackagerProperty: AdditionalModuleInfoString = requires transitive com.fasterxml.jackson.annotation;
    '#PackagerProperty: AdditionalModuleInfoString = requires transitive com.fasterxml.jackson.core;
    #PackagerProperty: AdditionalModuleInfoString = requires static java.sql;
    '#PackagerProperty: AdditionalModuleInfoString = requires static java.xml;
    #PackagerProperty: AdditionalModuleInfoString = requires static java.desktop;
    #PackagerProperty: AdditionalModuleInfoString = exports com.fasterxml.jackson.databind;
    #PackagerProperty: AdditionalModuleInfoString = exports com.fasterxml.jackson.databind.cfg;
    #PackagerProperty: AdditionalModuleInfoString = exports com.fasterxml.jackson.databind.annotation;
    #PackagerProperty: AdditionalModuleInfoString = exports com.fasterxml.jackson.databind.deser;
    #PackagerProperty: AdditionalModuleInfoString = exports com.fasterxml.jackson.databind.deser.impl;
    #PackagerProperty: AdditionalModuleInfoString = exports com.fasterxml.jackson.databind.deser.std;
    #PackagerProperty: AdditionalModuleInfoString = exports com.fasterxml.jackson.databind.exc;
    #PackagerProperty: AdditionalModuleInfoString = exports com.fasterxml.jackson.databind.ext;
    #PackagerProperty: AdditionalModuleInfoString = exports com.fasterxml.jackson.databind.introspect;
    #PackagerProperty: AdditionalModuleInfoString = exports com.fasterxml.jackson.databind.json;
    #PackagerProperty: AdditionalModuleInfoString = exports com.fasterxml.jackson.databind.jsonFormatVisitors;
    #PackagerProperty: AdditionalModuleInfoString = exports com.fasterxml.jackson.databind.jsonschema;
    #PackagerProperty: AdditionalModuleInfoString = exports com.fasterxml.jackson.databind.jsontype;
    #PackagerProperty: AdditionalModuleInfoString = exports com.fasterxml.jackson.databind.jsontype.impl;
    #PackagerProperty: AdditionalModuleInfoString = exports com.fasterxml.jackson.databind.module;
    #PackagerProperty: AdditionalModuleInfoString = exports com.fasterxml.jackson.databind.node;
    #PackagerProperty: AdditionalModuleInfoString = exports com.fasterxml.jackson.databind.ser;
    #PackagerProperty: AdditionalModuleInfoString = exports com.fasterxml.jackson.databind.ser.impl;
    #PackagerProperty: AdditionalModuleInfoString = exports com.fasterxml.jackson.databind.ser.std;
    #PackagerProperty: AdditionalModuleInfoString = exports com.fasterxml.jackson.databind.type;
    #PackagerProperty: AdditionalModuleInfoString = exports com.fasterxml.jackson.databind.util;
    '#PackagerProperty: AdditionalModuleInfoString = uses com.fasterxml.jackson.databind.Module;
    #PackagerProperty: AdditionalModuleInfoString = provides com.fasterxml.jackson.core.ObjectCodec with com.fasterxml.jackson.databind.ObjectMapper;

For some reason I also need to add on top:
B4X:
    #PackagerProperty: IncludedModules = javafx.swing
    #PackagerProperty: IncludedModules = java.net.http
    #PackagerProperty: IncludedModules = javafx.controls

Example app is updated.
It compiles, but after installation it doesn't run and gives this error, I wonder if there is a conflit with AWTRobot or something:

java.lang.NoClassDefFoundError: com/sun/management/OperatingSystemMXBean
at b4j/butt.droid.awtRobot.AWTRobot.<init>(Unknown Source)
at b4j/com.xxx.xxxx.main$ResumableSub_AppStart.resume(Unknown Source)
at b4j/com.xxx.xxxx.main._appstart(Unknown Source)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(Unknown Source)
at java.base/java.lang.reflect.Method.invoke(Unknown Source)
at b4j/anywheresoftware.b4a.BA.raiseEvent2(Unknown Source)
at b4j/anywheresoftware.b4a.BA.raiseEvent(Unknown Source)
at b4j/com.jmontserrat.postchatclient.main.start(Unknown Source)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(Unknown Source)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(Unknown Source)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(Unknown Source)
at java.base/java.security.AccessController.doPrivileged(Unknown Source)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(Unknown Source)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(Unknown Source)
at java.base/java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: com.sun.management.OperatingSystemMXBean
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(Unknown Source)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(Unknown Source)
at java.base/java.lang.ClassLoader.loadClass(Unknown Source)
... 17 more
 

Pendrush

Well-Known Member
Licensed User
Longtime User
Any possibility to implement the other functions:
"OllamaEmbedResponseModel" ?

I could help on the source if you need help
I will implement OllamaEmbedResponseModel but I need to find some free time to do it.
 

jmon

Well-Known Member
Licensed User
Longtime User
It compiles, but after installation it doesn't run and gives this error, I wonder if there is a conflit with AWTRobot or something:
Hi,

This is the list of imports that works for me. Somehow the one you listed created problems with AWT Robot and HikariCP.

B4X:
    #PackagerProperty: IncludedModules = javafx.swing
    #PackagerProperty: IncludedModules = java.net.http
    #PackagerProperty: IncludedModules = javafx.controls
'    #PackagerProperty: IncludedModules = javax.management
'    #PackagerProperty: IncludedModules = java.lang.management
'    #PackagerProperty: IncludedModules = java.sql
    #PackagerProperty: AdditionalModuleInfoString = exports org.slf4j;
    #PackagerProperty: AdditionalModuleInfoString = exports org.slf4j.spi;
    #PackagerProperty: AdditionalModuleInfoString = exports org.slf4j.event;
    #PackagerProperty: AdditionalModuleInfoString = exports org.slf4j.helpers;
    #PackagerProperty: AdditionalModuleInfoString = uses org.slf4j.spi.SLF4JServiceProvider;
    '#PackagerProperty: AdditionalModuleInfoString = requires transitive java.xml;
    #PackagerProperty: AdditionalModuleInfoString = exports com.fasterxml.jackson.annotation;
    #PackagerProperty: AdditionalModuleInfoString = opens com.fasterxml.jackson.annotation;
    '#PackagerProperty: AdditionalModuleInfoString = requires static org.jspecify;
    #PackagerProperty: AdditionalModuleInfoString = exports org.jsoup;
    #PackagerProperty: AdditionalModuleInfoString = exports org.jsoup.helper;
    #PackagerProperty: AdditionalModuleInfoString = exports org.jsoup.nodes;
    #PackagerProperty: AdditionalModuleInfoString = exports org.jsoup.parser;
    #PackagerProperty: AdditionalModuleInfoString = exports org.jsoup.safety;
    #PackagerProperty: AdditionalModuleInfoString = exports org.jsoup.select;
    #PackagerProperty: AdditionalModuleInfoString = requires java.logging;
    #PackagerProperty: AdditionalModuleInfoString = exports com.fasterxml.jackson.core;
    '#PackagerProperty: AdditionalModuleInfoString = requires transitive com.fasterxml.jackson.annotation;
    '#PackagerProperty: AdditionalModuleInfoString = requires transitive com.fasterxml.jackson.core;
    #PackagerProperty: AdditionalModuleInfoString = requires java.naming; 'Added
    #PackagerProperty: AdditionalModuleInfoString = requires jdk.management; 'Added
    #PackagerProperty: AdditionalModuleInfoString = requires java.sql; 'Changed
    #PackagerProperty: AdditionalModuleInfoString = requires javafx.fxml; requires javafx.graphics; 'Added
'    #PackagerProperty: AdditionalModuleInfoString = requires static java.sql;
    '#PackagerProperty: AdditionalModuleInfoString = requires static java.xml;
    #PackagerProperty: AdditionalModuleInfoString = requires static java.desktop;
    #PackagerProperty: AdditionalModuleInfoString = exports com.fasterxml.jackson.databind;
    #PackagerProperty: AdditionalModuleInfoString = exports com.fasterxml.jackson.databind.cfg;
    #PackagerProperty: AdditionalModuleInfoString = exports com.fasterxml.jackson.databind.annotation;
    #PackagerProperty: AdditionalModuleInfoString = exports com.fasterxml.jackson.databind.deser;
    #PackagerProperty: AdditionalModuleInfoString = exports com.fasterxml.jackson.databind.deser.impl;
    #PackagerProperty: AdditionalModuleInfoString = exports com.fasterxml.jackson.databind.deser.std;
    #PackagerProperty: AdditionalModuleInfoString = exports com.fasterxml.jackson.databind.exc;
    #PackagerProperty: AdditionalModuleInfoString = exports com.fasterxml.jackson.databind.ext;
    #PackagerProperty: AdditionalModuleInfoString = exports com.fasterxml.jackson.databind.introspect;
    #PackagerProperty: AdditionalModuleInfoString = exports com.fasterxml.jackson.databind.json;
    #PackagerProperty: AdditionalModuleInfoString = exports com.fasterxml.jackson.databind.jsonFormatVisitors;
    #PackagerProperty: AdditionalModuleInfoString = exports com.fasterxml.jackson.databind.jsonschema;
    #PackagerProperty: AdditionalModuleInfoString = exports com.fasterxml.jackson.databind.jsontype;
    #PackagerProperty: AdditionalModuleInfoString = exports com.fasterxml.jackson.databind.jsontype.impl;
    #PackagerProperty: AdditionalModuleInfoString = exports com.fasterxml.jackson.databind.module;
    #PackagerProperty: AdditionalModuleInfoString = exports com.fasterxml.jackson.databind.node;
    #PackagerProperty: AdditionalModuleInfoString = exports com.fasterxml.jackson.databind.ser;
    #PackagerProperty: AdditionalModuleInfoString = exports com.fasterxml.jackson.databind.ser.impl;
    #PackagerProperty: AdditionalModuleInfoString = exports com.fasterxml.jackson.databind.ser.std;
    #PackagerProperty: AdditionalModuleInfoString = exports com.fasterxml.jackson.databind.type;
    #PackagerProperty: AdditionalModuleInfoString = exports com.fasterxml.jackson.databind.util;
    '#PackagerProperty: AdditionalModuleInfoString = uses com.fasterxml.jackson.databind.Module;
    #PackagerProperty: AdditionalModuleInfoString = provides com.fasterxml.jackson.core.ObjectCodec with com.fasterxml.jackson.databind.ObjectMapper;
I'm not good with this packager imports, but the changes are:

1: Removed the "static" from requires static java.sql
2: Added requires java.naming;
3: Added requires jdk.management;
4: Added requires javafx.fxml; requires javafx.graphics;


What do you think?
 
Last edited:
Top