-
Notifications
You must be signed in to change notification settings - Fork 347
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'dev' into fix-subscriber
- Loading branch information
Showing
21 changed files
with
277 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
Easydict/App/Assets.xcassets/service-icon/Ollama.imageset/Contents.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"images" : [ | ||
{ | ||
"idiom" : "universal", | ||
"scale" : "1x" | ||
}, | ||
{ | ||
"filename" : "Ollama.png", | ||
"idiom" : "universal", | ||
"scale" : "2x" | ||
}, | ||
{ | ||
"idiom" : "universal", | ||
"scale" : "3x" | ||
} | ||
], | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// | ||
// OllamaModel.swift | ||
// Easydict | ||
// | ||
// Created by tisfeng on 2024/7/7. | ||
// Copyright © 2024 izual. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
// MARK: - OllamaModels | ||
|
||
// Ollama docs https://github.com/ollama/ollama/blob/main/docs/api.md#list-local-models | ||
struct OllamaModels: Codable { | ||
let models: [OllamaModel] | ||
} | ||
|
||
// MARK: - OllamaModel | ||
|
||
struct OllamaModel: Codable { | ||
enum CodingKeys: String, CodingKey { | ||
case name, model | ||
case modifiedAt = "modified_at" | ||
case size, digest, details | ||
} | ||
|
||
let name, model, modifiedAt: String | ||
let size: Int | ||
let digest: String | ||
let details: OllamaModelDetails | ||
} | ||
|
||
// MARK: - OllamaModelDetails | ||
|
||
struct OllamaModelDetails: Codable { | ||
enum CodingKeys: String, CodingKey { | ||
case parentModel = "parent_model" | ||
case format, family, families | ||
case parameterSize = "parameter_size" | ||
case quantizationLevel = "quantization_level" | ||
} | ||
|
||
let parentModel, format, family: String | ||
let families: [String]? | ||
let parameterSize, quantizationLevel: String | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// | ||
// OllamaService.swift | ||
// Easydict | ||
// | ||
// Created by tisfeng on 2024/7/5. | ||
// Copyright © 2024 izual. All rights reserved. | ||
// | ||
|
||
import Alamofire | ||
import Defaults | ||
import Foundation | ||
|
||
// MARK: - OllamaService | ||
|
||
@objc(EZOllamaService) | ||
class OllamaService: BaseOpenAIService { | ||
// MARK: Lifecycle | ||
|
||
override init() { | ||
super.init() | ||
|
||
Task { | ||
let models = try await localModels() | ||
self.ollamaModels = models.models.map(\.name) | ||
logInfo("ollama models: \(ollamaModels)") | ||
} | ||
} | ||
|
||
// MARK: Public | ||
|
||
public override func name() -> String { | ||
NSLocalizedString("ollama_translate", comment: "") | ||
} | ||
|
||
public override func serviceType() -> ServiceType { | ||
.ollama | ||
} | ||
|
||
// MARK: Internal | ||
|
||
override var defaultEndpoint: String { | ||
"http://localhost:11434/v1/chat/completions" | ||
} | ||
|
||
override var defaultModels: [String] { | ||
ollamaModels | ||
} | ||
|
||
override var observeKeys: [Defaults.Key<String>] { | ||
[supportedModelsKey] | ||
} | ||
|
||
override var isSentenceEnabledByDefault: Bool { | ||
false | ||
} | ||
|
||
override var isDictionaryEnabledByDefault: Bool { | ||
false | ||
} | ||
|
||
override func configurationListItems() -> Any { | ||
StreamConfigurationView( | ||
service: self, | ||
showAPIKeySection: false | ||
) | ||
} | ||
|
||
// MARK: Private | ||
|
||
private var ollamaModels = [""] | ||
|
||
/// Get Ollama modles https://github.com/ollama/ollama/blob/main/docs/api.md#list-local-models | ||
private func localModels() async throws -> OllamaModels { | ||
// endpoint is http://localhost:11434/v1/chat/completions, we need url http://localhost:11434/api/tags | ||
let endpointURL = URL(string: endpoint) | ||
guard let endpointURL, let trueBaseURL = endpointURL.rootURL else { | ||
throw EZError(type: .param, description: "`\(serviceType().rawValue)` endpoint is invalid") | ||
} | ||
|
||
let modelsURL = trueBaseURL.appendingPathComponent("api/tags") | ||
let dataTask = AF.request(modelsURL).serializingDecodable(OllamaModels.self) | ||
return try await dataTask.value | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.