llama.cpp icon indicating copy to clipboard operation
llama.cpp copied to clipboard

Add a Package.swift for SwiftPM support

Open j-f1 opened this issue 2 years ago • 2 comments

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)

j-f1 avatar Mar 22 '23 14:03 j-f1

Tested and it works locally!

j-f1 avatar Mar 23 '23 17:03 j-f1

Is anyone interested in reviewing this?

j-f1 avatar Mar 26 '23 14:03 j-f1

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

ggerganov avatar Mar 28 '23 16:03 ggerganov