Using Ollama + Microsoft Phi4 AI from [x]Harbour+FWH
Posted: Wed Feb 12, 2025 9:29 am

Code: Select all | Expand
// from cmd run: ollama serve
#include "FiveWin.ch"
static oChat, oOutput
static nStartTime, nTokenCount
function Main()
local oDlg, cOutput := "", oBtn, oFont
local oPrompt, cPrompt := Space( 100 )
oChat = TOLlama():New( "phi4" )
FW_SetUnicode( .T. )
DEFINE FONT oFont NAME "system-ui" SIZE 0, -16 BOLD
DEFINE DIALOG oDlg TITLE "Ollama Phi4" SIZE 1200, 600
@ 5.5, 0.7 GET oOutput VAR cOutput MULTILINE SIZE 590, 200 READONLY FONT oFont
@ 18.7, 1 SAY "Prompt:"
@ 21.5, 4 GET oPrompt VAR cPrompt SIZE 510, 15
@ 15.5, 92 BUTTON oBtn PROMPT "Send" SIZE 40, 15 ACTION SendPrompt( cPrompt, oOutput, oBtn, oPrompt ) DEFAULT
ACTIVATE DIALOG oDlg CENTERED
oChat:End()
oFont:End()
return nil
function SendPrompt( cPrompt, oOutput, oBtn, oPrompt )
local cToken
nStartTime = Seconds()
nTokenCount = 0
oBtn:Disable()
oChat:SendStream( AllTrim( cPrompt ), { | cBuffer | ShowTokens( cBuffer ) } )
ShowTokenStats()
oBtn:Enable()
oPrompt:SetFocus()
return nil
function ShowTokens( cBuffer )
local hResponse
hb_jsonDecode( cBuffer, @hResponse )
oOutput:Append( hResponse[ "message" ][ "content" ] )
nTokenCount++
SysRefresh()
return nil
function ShowTokenStats()
local nElapsedTime := Seconds() - nStartTime
local nTokensPerSecond := iif(nElapsedTime > 0, nTokenCount / nElapsedTime, 0)
oOutput:Append( StrTran( "Tokens por segundo: " + Str( nTokensPerSecond, 10, 2 ), ".", "," ) )
SysRefresh()
return nil