TOLlama (Ollama / Local LLM)

Source: source/classes/tollama.prg

TOLlama is FiveWin's client for the Ollama API, enabling interaction with locally-hosted LLMs via Ollama's REST API (default: http://localhost:11434). It supports text chat, streaming responses, and image analysis for vision-capable models. The class uses Harbour's hb_curl library and connects to your local Ollama server, requiring no API keys or internet connectivity. The companion TAgent class provides a data container for tool/agent definitions.

Architecture

flowchart LR subgraph "FiveWin Application" A[TOLlama] end subgraph "libcurl" B[hb_curl
HTTPS] end subgraph "Local Ollama Server" C["localhost:11434/api/chat"] end A --> B B --> C

Key DATA Members

DATATypeDefaultDescription
cModelCharacter"deepseek-r1:14b"Ollama model to use
cPromptCharacterLast prompt sent to the API
cResponseCharacterRaw JSON response from the API
cUrlCharacter"http://localhost:11434/api/chat"Ollama chat API endpoint
hCurlHandlelibcurl easy handle
nErrorNumeric0curl error code (0 = HB_CURLE_OK)
nHttpCodeNumeric0HTTP response status code
aAgentsArrayArray of TAgent objects

Methods

MethodDescription
New( cModel )Create a TOLlama instance. Sets the model name and initializes the libcurl handle. Default endpoint is http://localhost:11434/api/chat.
Send( cPrompt )Send a text prompt to the Ollama chat API. Stores the raw JSON response in cResponse and returns it.
SendStream( cPrompt, bWriteFunction )Send a prompt with streaming enabled. The bWriteFunction codeblock receives each JSON chunk as it arrives from the server.
SendImage( cImageFileName, cPrompt )Send a local image file with a text prompt for vision analysis. Encodes the image as base64. Supported by vision models like LLaVA.
GetValue()Extract the response text from the JSON in cResponse. Returns message.content or the error message.
End()Clean up the libcurl handle. Automatically called by the destructor.

TAgent Class

DATATypeDescription
cCategoryCharacterAgent category or identifier
aToolsArrayArray of tool definitions associated with this agent

TAgent is a data container for defining agents and their associated tool sets. It works alongside TOLlama's aAgents array to organize multiple agent configurations.

MethodDescription
New( cCategory, aTools )Create a TAgent instance with a category name and an array of tool definitions.

Example: Local LLM Chat

#include "FiveWin.ch"

function Main()

   local oAI := TOLlama():New( "llama3.2" )

   oAI:Send( "What is the capital of France?" )

   if oAI:nError == 0 .and. oAI:nHttpCode == 200
      MsgInfo( oAI:GetValue() )
   else
      MsgStop( "Error: " + oAI:cResponse )
   endif

   oAI:End()

return nil

Example: Streaming Response

#include "FiveWin.ch"

function Main()

   local oAI := TOLlama():New( "llama3.2" )
   local cBuffer := ""

   oAI:SendStream( "Tell me a joke.", ;
      { |cChunk| cBuffer += cChunk, QOut( cChunk ) } )

   if oAI:nError == 0
      MsgInfo( "Complete response:" + CRLF + cBuffer )
   endif

   oAI:End()

return nil

Example: Image Analysis

#include "FiveWin.ch"

function Main()

   local oAI := TOLlama():New( "llava" )  // vision-capable model

   oAI:SendImage( "C:\photos\photo.jpg", "What objects do you see?" )

   if oAI:nError == 0
      MsgInfo( oAI:GetValue() )
   endif

   oAI:End()

return nil

Notes

See Also