TOLlama (Ollama / Local LLM)
Fonte: 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
HTTPS] end subgraph "Local Ollama Server" C["localhost:11434/api/chat"] end A --> B B --> C
Key DATA Members
| DATA | Type | Default | Description |
|---|---|---|---|
cModel | Character | "deepseek-r1:14b" | Ollama model to use |
cPrompt | Character | Last prompt sent to the API | |
cResponse | Character | Raw JSON response from the API | |
cUrl | Character | "http://localhost:11434/api/chat" | Ollama chat API endpoint |
hCurl | Handle | libcurl easy handle | |
nError | Numeric | 0 | curl error code (0 = HB_CURLE_OK) |
nHttpCode | Numeric | 0 | HTTP response status code |
aAgents | Array | Array of TAgent objects |
Methods
| Method | Description |
|---|---|
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
| DATA | Type | Description |
|---|---|---|
cCategory | Character | Agent category or identifier |
aTools | Array | Array 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.
| Method | Description |
|---|---|
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
- TOLlama connects to a locally-running Ollama server. No API key or internet access is required.
- The default model is
"deepseek-r1:14b"(set in the class), but you should pass the model name you have pulled locally (e.g.,"llama3.2","llava","mistral"). - For image analysis, use a vision-capable model such as
"llava"or"bakllava". GetValue()extracts themessage.contentfield from the Ollama JSON response. On error it returns theerror.messagefield.- The
Send()method setsstream: falseandtemperature: 0.5in the request.SendStream()setsstream: true. - Requires the
hb_curllibrary. Include#include "hbcurl.ch"for curl constants.