Skip to content

Add a Package.swift for SwiftPM support #393

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 3 commits into from
Mar 28, 2023
Merged

Add a Package.swift for SwiftPM support #393

merged 3 commits into from
Mar 28, 2023

Conversation

j-f1
Copy link
Collaborator

@j-f1 j-f1 commented Mar 22, 2023

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)

@gjmulder gjmulder added enhancement New feature or request build Compilation issues labels Mar 22, 2023
@j-f1
Copy link
Collaborator Author

j-f1 commented Mar 23, 2023

Tested and it works locally!

@j-f1 j-f1 marked this pull request as ready for review March 23, 2023 17:55
@j-f1 j-f1 changed the title [WIP] Add a Package.swift for SwiftPM support Add a Package.swift for SwiftPM support Mar 23, 2023
@j-f1
Copy link
Collaborator Author

j-f1 commented Mar 26, 2023

Is anyone interested in reviewing this?

@ggerganov
Copy link
Member

This should go together with Flake and Docker scripts into a separate repository: #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 ggerganov merged commit 20e1e84 into master Mar 28, 2023
@ggerganov ggerganov deleted the jed/spm branch March 28, 2023 16:39
AAbushady pushed a commit to AAbushady/llama.cpp that referenced this pull request Jan 27, 2024
Fixing C2065 compiler error. 
Missed '3' on 3 separate identifiers (kB > kB3, MB > MB3)
Copy link

@Brianm93 Brianm93 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey

@mofosyne mofosyne added the Review Complexity : Low Trivial changes to code that most beginner devs (or those who want a break) can tackle. e.g. UI fix label May 11, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
build Compilation issues enhancement New feature or request Review Complexity : Low Trivial changes to code that most beginner devs (or those who want a break) can tackle. e.g. UI fix
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants