Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Additional options native embeddings #23

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,40 @@
import Foundation
import NaturalLanguage

public enum NativeEmbeddingType {
case wordEmbedding
case sentenceEmbedding
}

@available(macOS 11.0, iOS 15.0, *)
public class NativeEmbeddings: EmbeddingsProtocol {
public let model: ModelActor
public let tokenizer: any TokenizerProtocol

public init(language: NLLanguage = .english) {
public init(language: NLLanguage = .english, type: NativeEmbeddingType = .sentenceEmbedding) {
self.tokenizer = NativeTokenizer()
self.model = ModelActor(language: language)
self.model = ModelActor(language: language, type: type)
}

// MARK: - Dense Embeddings

public actor ModelActor {
private let model: NLEmbedding

init(language: NLLanguage) {
guard let nativeModel = NLEmbedding.sentenceEmbedding(for: language) else {
fatalError("Failed to load the Core ML model.")
init(language: NLLanguage, type:NativeEmbeddingType = .sentenceEmbedding) {
BernhardEisvogel marked this conversation as resolved.
Show resolved Hide resolved
switch type {
case .sentenceEmbedding:
guard let nativeModel = NLEmbedding.sentenceEmbedding(for: language) else {
fatalError("Failed to load the Core ML model.")
}
model = nativeModel
case .wordEmbedding:
guard let nativeModel = NLEmbedding.wordEmbedding(for: language) else {
fatalError("Failed to load the Core ML model.")
}
model = nativeModel
}
model = nativeModel

}

func vector(for sentence: String) -> [Float]? {
Expand Down