llamafile

Logo

Use llamafile from 4D

View My GitHub Profile

version platform license downloads

Use llamafile from 4D

Abstract

llamafile is an open-source project created by Mozilla that lets you distribute and run Large Language Models (LLMs) as a single, runnable file.

Usage

Instantiate cs.llamafile.llamafile in your On Startup database method:

var $llama : cs.llamafile.llamafile

If (False)
    $llama:=cs.llamafile.llamafile.new()  //default
Else 
    var $homeFolder : 4D.Folder
    $homeFolder:=Folder(fk home folder).folder(".llamafile")
    var $URL : Text
    var $file : 4D.File
    var $port : Integer
    
    var $event : cs.event.event
    $event:=cs.event.event.new()
    /*
        Function onError($params : Object; $error : cs.event.error)
        Function onSuccess($params : Object; $models : cs.event.models)
        Function onData($request : 4D.HTTPRequest; $event : Object)
        Function onResponse($request : 4D.HTTPRequest; $event : Object)
        Function onTerminate($worker : 4D.SystemWorker; $params : Object)
    */
    
    $event.onError:=Formula(ALERT($2.message))
    $event.onSuccess:=Formula(ALERT($2.models.extract("name").join(",")+" loaded!"))
    $event.onData:=Formula(LOG EVENT(Into 4D debug message; "download:"+String((This.range.end/This.range.length)*100; "###.00%")))
    $event.onResponse:=Formula(LOG EVENT(Into 4D debug message; "download complete"))
    $event.onTerminate:=Formula(LOG EVENT(Into 4D debug message; (["process"; $1.pid; "terminated!"].join(" "))))
    
    /*
        embeddings
    */
    
    $file:=$homeFolder.file("nomic-embed-text-v1.Q8_0.gguf")
    $URL:="https://huggingface.co/nomic-ai/nomic-embed-text-v1-GGUF/resolve/main/nomic-embed-text-v1.Q8_0.gguf"
    $port:=8086
    $llamafile:=cs.llamafile.llamafile.new($port; $file; $URL; $event)
    
    /*
        chat completion (with images)
    */
    
    $file:=$homeFolder.file("Llama-3.1-8B-Instruct-Q4_K_M.gguf")
    $URL:="https://huggingface.co/bartowski/Meta-Llama-3.1-8B-Instruct-GGUF/resolve/main/Meta-Llama-3.1-8B-Instruct-Q4_K_M.gguf"
    $port:=8087
    $llamafile:=cs.llamafile.llamafile.new($port; $file; $URL; $event)
    
End if  

Unless the server is already running (in which case the costructor does nothing), the following procedure runs in the background:

  1. The specified model is downloaded via HTTP
  2. The llamafiler program is started (the process name is .ape-1.10)

Now you can test the server:

curl -X POST http://127.0.0.1:8080/v1/embeddings \
     -H "Content-Type: application/json" \
     -d '{"input":"The quick brown fox jumps over the lazy dog."}'

Or, use AI Kit:

var $AIClient : cs.AIKit.OpenAI
$AIClient:=cs.AIKit.OpenAI.new()
$AIClient.baseURL:="http://127.0.0.1:8080/v1"

var $text : Text
$text:="The quick brown fox jumps over the lazy dog."

var $responseEmbeddings : cs.AIKit.OpenAIEmbeddingsResult
$responseEmbeddings:=$AIClient.embeddings.create($text)

Finally to terminate the server:

var $llama : cs.llamafile.llamafile
$llama:=cs.llamafile.llamafile.new()
$llama.terminate()

AI Kit compatibility

The API is compatibile with Open AI.

Class API Availability
Models /v1/models
Chat /v1/chat/completions
Images /v1/images/generations  
Moderations /v1/moderations  
Embeddings /v1/embeddings
Files /v1/files