Code: Select all
; SIMPLEST WAY TO FIND WHY PUREBASIC IS FANTASTIC (WINDOWS)
;
; install Ollama
; https://ollama.com/download
;if everything is fine you'll get a prompt
;-----------------------------------------
; Welcome to Ollama! !
; !
; Run your first model: !
; !
; ollama run llama3 !
;-----------------------------------------
;(folow directions)
;(leave open)
; run this purebasic code:
EnableExplicit
Procedure.s GetResult(sInput$)
If sInput$ = ""
ProcedureReturn
EndIf
Protected stoken$ = ~"\"response\":"
Repeat
Protected p = FindString( sInput$, stoken$, p+1)
If Not p
Break
EndIf
p = p + Len(stoken$) + 1
Protected q = FindString(sInput$, #DOUBLEQUOTE$, p)
Protected t$ = t$ + Mid(sInput$, p, q-p)
ForEver
ProcedureReturn ReplaceString(t$, "\n", #CRLF$)
EndProcedure
Enumeration #PB_Event_FirstCustomValue
#Event_Update
EndEnumeration
Global old_progress, progress
Procedure Ollama(*dummy)
Protected sPrompt$ = "Why Purebasic is fantastic?"
Protected url$ = "http://localhost:11434/api/generate"
Protected data2$ = ReplaceString(ReplaceString("{´´model´´:´´llama3´´, ´´prompt´´:´´$PROMPT$´´}", "´´", #DOUBLEQUOTE$), "$PROMPT$", sPrompt$)
Protected HttpRequest = HTTPRequest(#PB_HTTP_Post, url$, Data2$, #PB_HTTP_Asynchronous);, Header$())
If HttpRequest
Repeat
progress = HTTPProgress(HttpRequest)
Protected.s result = HTTPInfo(HttpRequest,#PB_HTTP_Response)
result = GetResult(result)
If IsGadget(0)
ClearGadgetItems(0)
SendMessage_(GadgetID(0),#EM_REPLACESEL,0, @result)
EndIf
Select Progress
Case #PB_HTTP_Success
FinishHTTP(HTTPRequest) ; Always call FinishHTTP() when request is finished
Break
Case #PB_HTTP_Failed
Debug "Download failed"
FinishHTTP(HTTPRequest) ; Always call FinishHTTP() when request failed
Break
Case #PB_HTTP_Aborted
Debug "Aborted"
FinishHTTP(HTTPRequest) ; Always call FinishHTTP() when request is aborted
Default
If progress > old_progress
PostEvent(#Event_Update, 0, 0)
old_progress = progress
EndIf
Delay(100)
EndSelect
ForEver
Else
Debug "Request creation failed"
EndIf
EndProcedure
Procedure Main()
Protected th = CreateThread(@Ollama(),0)
ExamineDesktops()
If OpenWindow(0, 0, 0, DesktopWidth(0)/3, DesktopHeight(0)/3, "EditorGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
EditorGadget(0, 0, 0, WindowWidth(0), WindowHeight(0), #PB_Editor_ReadOnly | #PB_Editor_WordWrap)
Repeat
Select WaitWindowEvent()
Case #PB_Event_CloseWindow
Break
Case #Event_Update
SetWindowTitle(0, "Progress: " + progress)
EndSelect
ForEver
EndIf
EndProcedure
Main()