-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
92 additions
and
2 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
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,70 @@ | ||
import Alamofire | ||
import Foundation | ||
|
||
struct OllamaResult: Decodable { | ||
let role: String | ||
let content: String | ||
} | ||
|
||
struct OllamaResponse: BaseResponse { | ||
let model: String | ||
let created_at: String | ||
let message: OllamaResult | ||
let done: Bool | ||
|
||
var target: String? { | ||
return message.content | ||
} | ||
|
||
var errorMessage: String? { | ||
return nil | ||
} | ||
} | ||
|
||
let OllamaProviderName = "Ollama" | ||
|
||
class OllamaProvider: BaseProvider { | ||
static let shared = OllamaProvider() | ||
let delay: DispatchTimeInterval = .seconds(1) | ||
let name = OllamaProviderName | ||
|
||
var apiUrl: String { | ||
return Defaults.shared.OllamaAPI.isEmpty ? "http://localhost:11434/api/chat" : Defaults.shared.OllamaAPI | ||
} | ||
|
||
var model: String { | ||
return Defaults.shared.OllamaModel.isEmpty ? "qwen2" : Defaults.shared.OllamaModel | ||
} | ||
|
||
func translate(source: String, from: Language, to: Language, cb: @escaping (_ target: String, _ _sourceLanguage: Language?, _ _targetLanguage: Language?) -> Void) { | ||
let prompt = "Assuming you are a seasoned translator, please translate the following source text to \(to.name) as target language, ensuring accuracy while trying to retain emotion and natural flow. Your response should ONLY contain the translated result.\n The source text is: ```\(source)```" | ||
let parameters: [String: Any] = [ | ||
"model": model, | ||
"stream": false, | ||
"messages": [ | ||
[ | ||
"role": "user", | ||
"content": prompt | ||
] | ||
], | ||
] | ||
|
||
debugPrint("[OllamaProvider] parameters:", parameters) | ||
|
||
AF.request(apiUrl, method: .post, parameters: parameters, encoding: JSONEncoding.default) | ||
.cacheResponse(using: .cache) | ||
.cURLDescription(calling: { curl in | ||
print(curl) | ||
}) | ||
.responseDecodable(of: OllamaResponse.self) { response in | ||
if response.error != nil { | ||
cb(response.error!.errorDescription!, nil, nil) | ||
} else if response.value?.errorMessage != nil { | ||
cb(response.value!.errorMessage!, nil, nil) | ||
} else { | ||
cb(response.value!.target!, from, to) | ||
} | ||
} | ||
} | ||
|
||
} |
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