Why Purebasic is fantastic

Share your advanced PureBasic knowledge/code with the community.
acreis
Enthusiast
Enthusiast
Posts: 204
Joined: Fri Jun 01, 2012 12:20 am

Why Purebasic is fantastic

Post by acreis »

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()
User avatar
Caronte3D
Addict
Addict
Posts: 1361
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: Why Purebasic is fantastic

Post by Caronte3D »

Nice!
Thanks for sharing it :wink:
User avatar
Kiffi
Addict
Addict
Posts: 1497
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

Re: Why Purebasic is fantastic

Post by Kiffi »

llama3 wrote:9. **Free and open-source**: PureBasic is completely free and open-source, which means you can use it without worrying about licensing fees or restrictions.
oh, I didn't even know that. Where can I get the purchase price refunded? :wink:

Nevertheless, thanks for your code!
Hygge
User avatar
Kiffi
Addict
Addict
Posts: 1497
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

Re: Why Purebasic is fantastic

Post by Kiffi »

Kiffi wrote:Please give me a short summary of the PDF, which can be found at https://www.purebasic.com/documentation ... cSmall.pdf
llama3 wrote:The provided PDF document is an introduction to PureBasic, a programming language developed by Thomas Soenig. [...]
Who is Thomas Soenig? :lol:
Hygge
User avatar
Caronte3D
Addict
Addict
Posts: 1361
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: Why Purebasic is fantastic

Post by Caronte3D »

Note that you can load other larger models (if you have enough RAM) and you will get better responses.
User avatar
Piero
Addict
Addict
Posts: 914
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: Why Purebasic is fantastic

Post by Piero »

42
Guessing better response…
Loading Universe.......
Operation Failed: not enough RAM
Please update to windows 11
hoangdiemtinh
User
User
Posts: 97
Joined: Wed Nov 16, 2022 1:51 pm

Re: Why Purebasic is fantastic

Post by hoangdiemtinh »

Thanks for sharing it [love]
PC: Windows 10 x64, 8GB RAM. PB ver: 6.x
--
I love PB5 vs PB6 :)
dige
Addict
Addict
Posts: 1404
Joined: Wed Apr 30, 2003 8:15 am
Location: Germany
Contact:

Re: Why Purebasic is fantastic

Post by dige »

Wow, how cool is that? :D I had tested gpt4all before, but unfortunately it was very slow.
With Ollama it's really fast. I immediately extended your code to interact with the model.
Thanks for your solution!

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$ = GetGadgetText(1)
  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
          SetGadgetText(1, "")
          SetActiveGadget(1)
          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

#Shortcut = 3

Procedure Main()
  
  Protected th
  
  ExamineDesktops()
  If OpenWindow(0, 0, 0, DesktopWidth(0)-300, DesktopHeight(0)-300, "EditorGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    EditorGadget(0, 0, 0, WindowWidth(0), WindowHeight(0)-30, #PB_Editor_ReadOnly | #PB_Editor_WordWrap)
    StringGadget(1, 0, WindowHeight(0)-30, WindowWidth(0)-100, 30, "")
    ButtonGadget(2, WindowWidth(0)-100, WindowHeight(0)-30, 100, 30, "Ask LLAMA")
    SetActiveGadget(1)
    
    AddKeyboardShortcut(0, #PB_Shortcut_Return, #Shortcut)
    
    Repeat 
      Select WaitWindowEvent() 
        Case #PB_Event_CloseWindow
          Break
        Case #PB_Event_Gadget
          Select EventGadget()
              Case 2 : th = CreateThread(@Ollama(),0)
          EndSelect
        
        Case #Event_Update
          SetWindowTitle(0, "Progress: " + progress)
          
        Case #PB_Event_Menu
    			If EventMenu() = #Shortcut And GetActiveGadget() = 1
    				th = CreateThread(@Ollama(),0)
    			EndIf
          
      EndSelect  

      
    ForEver 
  EndIf
  
EndProcedure

Main()

"Daddy, I'll run faster, then it is not so far..."
Fred
Administrator
Administrator
Posts: 18199
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Why Purebasic is fantastic

Post by Fred »

Is it fast for you ? Here it's like 1 word every 5 secs :D
User avatar
Caronte3D
Addict
Addict
Posts: 1361
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: Why Purebasic is fantastic

Post by Caronte3D »

For me, it's very fast with the llama3 model (almost the same speed than ChatGPT).
I think you need a good GPU with a good amount of VRAM (and RAM).
BarryG
Addict
Addict
Posts: 4168
Joined: Thu Apr 18, 2019 8:17 am

Re: Why Purebasic is fantastic

Post by BarryG »

What's all this about? I don't want to install a 227 MB download to find out.
dige
Addict
Addict
Posts: 1404
Joined: Wed Apr 30, 2003 8:15 am
Location: Germany
Contact:

Re: Why Purebasic is fantastic

Post by dige »

You can work with a generative Ki, like Chat GPT, but locally on your computer.

Edit: and you need 4.3 GB in total to find out 😉
"Daddy, I'll run faster, then it is not so far..."
User avatar
Caronte3D
Addict
Addict
Posts: 1361
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: Why Purebasic is fantastic

Post by Caronte3D »

BarryG wrote: Sat Jun 22, 2024 2:05 am What's all this about? I don't want to install a 227 MB download to find out.
The point is: You can use AI unlimitedly (in all aspects) from PB without paying anything and preserving your privacy.
Post Reply