llama.cpp
llama.cpp copied to clipboard
Add a Package.swift for SwiftPM support
This allows llama.cpp to be called directly from Swift! First add https://github.com/ggerganov/llama.cpp to your Package.swift or Xcode project, selecting either this branch or master (once the PR is merged).
Here’s a basic example of calling from Swift:
import llama
let ctx = llama_init_from_file(url.path(percentEncoded: false), llama_context_default_params())
let promptTokens = Array<llama_token>(unsafeUninitializedCapacity: prompt.utf8.count) { buffer, initializedCount in
initializedCount = Int(llama_tokenize(ctx, prompt, buffer.baseAddress, Int32(buffer.count), true))
}
for var token in promptTokens {
llama_eval(ctx, &token, 1, Int32(tokens.count), nThreads)
}
while true { // should stop after reaching context limit!
var token = llama_sample_top_p_top_k(ctx, nil, 0, topK, topP, temperature, 1)
if token == llama_token_eos() {
print("[end of text]")
break
}
print(String(cString: llama_token_to_str(ctx, token)), terminator: "")
llama_eval(ctx, &token, 1, Int32(tokens.count), nThreads)
}
llama_free(ctx)
Tested and it works locally!
Is anyone interested in reviewing this?
This should go together with Flake and Docker scripts into a separate repository: https://github.com/ggerganov/llama.cpp/issues/506
I will merge for now, but we have to clean the root folder - it's too distracting to have so many unrelated files there