Skip to content

Commit

Permalink
feat: add new models for gemini translation (#571)
Browse files Browse the repository at this point in the history
* perf: update gemini package

* feat: add new models for gemini translation

* fix: type in openai service

* fix: type in ezconstkey annotation

* fix: type in a few files

* perf: update chatgpt link

* fix: build error

* perf: remove gemini keys in ezschemeparser

* format: add back empty line

* perf: rename opanaiusagestatus to serviceusagestatus

* perf: remove queryTextType since gemini supports all type

* perf: update xcstring state

* perf: enable LLMStreamService to change models quickly

* fix: typo in gemini service

* perf: support dictionary and sentence query

* perf: implement results handle for gemini

* docs: update sponsor list

* perf: remove unused annotation

Co-Authored-By: Tisfeng <[email protected]>

* perf: remove unused code

* perf: implement systemInstruction and role/model prompt

* perf: remove redundant code

* fix: gemini stream ui

* perf: move promptContent to extension GeminiService

* perf: optimize usage of handleResult

* perf: add bool argument to opt out LLM systemPrompt

* fix: use dict system prompt when querying a word

* perf: add dynamic variable for gemini available models

* refactor: replace all AI const stored keys with dynamic variables

* fix: gemini-1.0-pro model cannot use system instruction

* perf: change system prompt to user prompt for gemini-1.0-pro model

---------

Co-authored-by: tisfeng <[email protected]>
Co-authored-by: Tisfeng <[email protected]>
  • Loading branch information
3 people authored Jun 5, 2024
1 parent 927d837 commit 8420e58
Show file tree
Hide file tree
Showing 16 changed files with 522 additions and 174 deletions.
2 changes: 1 addition & 1 deletion Easydict.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -3555,7 +3555,7 @@
repositoryURL = "https://github.com/google/generative-ai-swift";
requirement = {
kind = upToNextMajorVersion;
minimumVersion = 0.4.4;
minimumVersion = 0.5.3;
};
};
03022F1A2B35DEBA00B63209 /* XCRemoteSwiftPackageReference "Hue" */ = {
Expand Down
4 changes: 2 additions & 2 deletions Easydict.xcworkspace/xcshareddata/swiftpm/Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/google/generative-ai-swift",
"state" : {
"revision" : "dcbdb5e591e1aa2bb68851dc7515f6b0a59026cd",
"version" : "0.4.7"
"revision" : "5d750b80651da9721c37c5eb1fc0b6750d1884d3",
"version" : "0.5.3"
}
},
{
Expand Down
6 changes: 3 additions & 3 deletions Easydict/App/Localizable.xcstrings
Original file line number Diff line number Diff line change
Expand Up @@ -2410,18 +2410,18 @@
}
}
},
"service.configuration.gemini.api_key.title" : {
"service.configuration.gemini.api_key.placeholder" : {
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "API Key"
"value" : "xxxxxxxxxxxxx"
}
},
"zh-Hans" : {
"stringUnit" : {
"state" : "translated",
"value" : "API Key"
"value" : "xxxxxxxxxxxxx"
}
}
}
Expand Down
76 changes: 58 additions & 18 deletions Easydict/Swift/Feature/Configuration/Configuration+Defaults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ class ShortcutWrapper<T: KeyCombo> {
// Service Configuration
extension Defaults.Keys {
// OpenAI
static let openAIAPIKey = Key<String?>(EZOpenAIAPIKey)
static let openAIAPIKey = Key<String?>(apiStoredKey(.openAI)) // EZOpenAIAPIKey
static let openAITranslation = Key<String>(
translationStoredKey(.openAI),
default: "1"
Expand All @@ -221,27 +221,30 @@ extension Defaults.Keys {
sentenceStoredKey(.openAI),
default: "1"
)
static let openAIServiceUsageStatus = Key<OpenAIUsageStatus>(
static let openAIServiceUsageStatus = Key<ServiceUsageStatus>(
serviceUsageStatusStoredKey(.openAI),
default: .default
)
static let openAIEndPoint = Key<String?>(EZOpenAIEndPointKey)
static let openAIModel = Key<String>(EZOpenAIModelKey, default: OpenAIModel.gpt3_5_turbo.rawValue)
static let openAIEndPoint = Key<String?>(endpointStoredKey(.openAI))
static let openAIModel = Key<String>(
modelStoredKey(.openAI),
default: OpenAIModel.gpt3_5_turbo.rawValue
)
static let openAIAvailableModels = Key<String?>(
EZOpenAIAvailableModelsKey,
availableModelsStoredKey(.openAI),
default: OpenAIModel.allCases.map { $0.rawValue }.joined(separator: ",")
)
static let openAIVaildModels = Key<Array>(
EZOpenAIValidModelsKey,
validModelsStoredKey(.openAI),
default: OpenAIModel.allCases.map { $0.rawValue }
)

// Custom OpenAI
static let customOpenAINameKey = Key<String?>(
EZCustomOpenAINameKey,
nameStoredKey(.customOpenAI),
default: NSLocalizedString("custom_openai", comment: "")
)
static let customOpenAIAPIKey = Key<String?>(EZCustomOpenAIAPIKey, default: "")
static let customOpenAIAPIKey = Key<String?>(apiStoredKey(.customOpenAI))
static let customOpenAITranslation = Key<String>(
translationStoredKey(.customOpenAI),
default: "1"
Expand All @@ -254,20 +257,29 @@ extension Defaults.Keys {
sentenceStoredKey(.customOpenAI),
default: "0"
)
static let customOpenAIServiceUsageStatus = Key<OpenAIUsageStatus>(
static let customOpenAIServiceUsageStatus = Key<ServiceUsageStatus>(
serviceUsageStatusStoredKey(.builtInAI),
default: .default
)
static let customOpenAIEndPoint = Key<String?>(EZCustomOpenAIEndPointKey, default: "")
static let customOpenAIModel = Key<String>(EZCustomOpenAIModelKey, default: "")
static let customOpenAIAvailableModels = Key<String?>(EZCustomOpenAIAvailableModelsKey, default: "")
static let customOpenAIEndPoint = Key<String?>(endpointStoredKey(.customOpenAI))
static let customOpenAIModel = Key<String>(
modelStoredKey(.customOpenAI),
default: ""
)
static let customOpenAIAvailableModels = Key<String?>(
availableModelsStoredKey(.customOpenAI),
default: ""
)
static let customOpenAIVaildModels = Key<Array>(
EZCustomOpenAIValidModelsKey,
validModelsStoredKey(.customOpenAI),
default: [""]
)

// Built-in AI
static let builtInAIModel = Key<String>(EZBuiltInAIModelKey, default: "")
static let builtInAIModel = Key<String>(
modelStoredKey(.builtInAI),
default: ""
) // EZBuiltInAIModelKey
static let builtInAITranslation = Key<String>(
translationStoredKey(.builtInAI),
default: "1"
Expand All @@ -280,11 +292,42 @@ extension Defaults.Keys {
sentenceStoredKey(.builtInAI),
default: "0"
)
static let builtInAIServiceUsageStatus = Key<OpenAIUsageStatus>(
static let builtInAIServiceUsageStatus = Key<ServiceUsageStatus>(
serviceUsageStatusStoredKey(.builtInAI),
default: .default
)

// Gemni
static let geminiAPIKey = Key<String?>(apiStoredKey(.gemini)) // EZGeminiAPIKey
static let geminiTranslation = Key<String>(
translationStoredKey(.gemini),
default: "1"
)
static let geminiDictionary = Key<String>(
dictionaryStoredKey(.gemini),
default: "1"
)
static let geminiSentence = Key<String>(
sentenceStoredKey(.gemini),
default: "1"
)
static let geminiServiceUsageStatus = Key<ServiceUsageStatus>(
serviceUsageStatusStoredKey(.gemini),
default: .default
)
static let geminiModel = Key<String>(
modelStoredKey(.gemini),
default: GeminiModel.gemini1_5_flash.rawValue
)
static let geminiAvailableModels = Key<String?>(
availableModelsStoredKey(.gemini),
default: GeminiModel.allCases.map { $0.rawValue }.joined(separator: ",")
)
static let geminiValidModels = Key<Array>(
validModelsStoredKey(.gemini),
default: GeminiModel.allCases.map { $0.rawValue }
)

// DeepL
static let deepLAuth = Key<String?>(EZDeepLAuthKey)
static let deepLTranslation = Key<DeepLAPIUsagePriority>(
Expand All @@ -309,9 +352,6 @@ extension Defaults.Keys {
// Ali
static let aliAccessKeyId = Key<String?>(EZAliAccessKeyId)
static let aliAccessKeySecret = Key<String?>(EZAliAccessKeySecret)

// Gemni
static let geminiAPIKey = Key<String?>(EZGeminiAPIKey)
}

/// shortcut
Expand Down
25 changes: 25 additions & 0 deletions Easydict/Swift/Feature/Configuration/DefaultsStoredKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import Foundation

// TODO: refactor key with enum key type.
func storedKey(_ key: String, serviceType: ServiceType) -> String {
// This key should be compatible with existing OpenAI config keys
// EZOpenAIServiceUsageStatusKey
Expand All @@ -31,6 +32,30 @@ func dictionaryStoredKey(_ serviceType: ServiceType) -> String {
storedKey(EZDictionaryKey, serviceType: serviceType)
}

func availableModelsStoredKey(_ serviceType: ServiceType) -> String {
storedKey(EZAvailableModelsKey, serviceType: serviceType)
}

func validModelsStoredKey(_ serviceType: ServiceType) -> String {
storedKey(EZValidModelsKey, serviceType: serviceType)
}

func modelStoredKey(_ serviceType: ServiceType) -> String {
storedKey(EZModelKey, serviceType: serviceType)
}

func apiStoredKey(_ serviceType: ServiceType) -> String {
storedKey(EZAPIKey, serviceType: serviceType)
}

func endpointStoredKey(_ serviceType: ServiceType) -> String {
storedKey(EZEndpointKey, serviceType: serviceType)
}

func nameStoredKey(_ serviceType: ServiceType) -> String {
storedKey(EZNameKey, serviceType: serviceType)
}

extension UserDefaults {
static func bool(forKey key: String, serviceType: ServiceType) -> Bool {
let key = storedKey(key, serviceType: serviceType)
Expand Down
Loading

0 comments on commit 8420e58

Please sign in to comment.