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

refactor: rewrite openai with Swift #308

Closed
wants to merge 30 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
7998646
refactor: add OpenAIService in Swift
tisfeng Jan 1, 2024
766e77c
chore: add libray OpenAI
tisfeng Jan 1, 2024
7bd226f
feat: able to translate text in chat completions
tisfeng Jan 2, 2024
cabd944
feat: change to use chat stream
tisfeng Jan 2, 2024
619bd59
chore: use forked OpenAI package, change Choice index to optional, fi…
tisfeng Jan 2, 2024
788d6f4
add ali translate support (#294)
choykarl Jan 1, 2024
42263e3
docs: update README
tisfeng Jan 2, 2024
59de4ef
docs: update README
tisfeng Jan 2, 2024
f5b1152
perf: improve prompt
tisfeng Jan 3, 2024
756af91
perf: improve prompt
tisfeng Jan 3, 2024
5cc15b1
perf: set gpt-3.5-turbo-1106 as default model
tisfeng Jan 3, 2024
792ea08
bugfix: menu item alignment issue (#299)
phlpsong Jan 3, 2024
85b6069
bugfix: #302 placeholder text not fully display in mini window (#303)
phlpsong Jan 4, 2024
12b5a7a
docs: update README
tisfeng Jan 4, 2024
5ff5803
perf: increase Niutrans free quota to 800w
tisfeng Jan 4, 2024
6263bb0
chore: update app version to 2.5.0
tisfeng Jan 4, 2024
8a0df85
chore: update appcast.xml
tisfeng Jan 4, 2024
1fc1e32
bugfix: app language not follow language updates in system settings …
phlpsong Jan 5, 2024
e0ce1f5
fix: resolve warning in release mode
tisfeng Jan 5, 2024
c0a7e0d
Create swift.yml
tisfeng Jan 5, 2024
21faf88
Create codeql.yml
tisfeng Jan 5, 2024
bd37333
Revert "Create codeql.yml"
tisfeng Jan 5, 2024
fb00a45
Create codeql.yml
tisfeng Jan 5, 2024
2f075f8
chore: set xcode version to 15.1
tisfeng Jan 5, 2024
db85086
chore: fix indentation error
tisfeng Jan 5, 2024
b22ef5d
chore: set image to macos-13
tisfeng Jan 5, 2024
2e05309
chore: only set xcode if language is swfit
tisfeng Jan 5, 2024
4a4ca34
refactor: add OpenAIService in Swift
tisfeng Jan 1, 2024
29357db
Merge branch 'dev' into refactor-openai
tisfeng Jan 6, 2024
2fb666c
test ci
tisfeng Jan 6, 2024
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
37 changes: 31 additions & 6 deletions Easydict.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
033B7134293CE2430096E2DF /* EZWebViewTranslator.m in Sources */ = {isa = PBXBuildFile; fileRef = 033B7133293CE2430096E2DF /* EZWebViewTranslator.m */; };
033C30FC2A7409C40095926A /* TTTDictionary.m in Sources */ = {isa = PBXBuildFile; fileRef = 033C30FB2A7409C40095926A /* TTTDictionary.m */; };
033C31002A74CECE0095926A /* EZAppleDictionary.m in Sources */ = {isa = PBXBuildFile; fileRef = 033C30FF2A74CECE0095926A /* EZAppleDictionary.m */; };
0342801D2B41A01F002AF60D /* OpenAIService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0342801C2B41A01F002AF60D /* OpenAIService.swift */; };
034280222B4308D2002AF60D /* Prompt.swift in Sources */ = {isa = PBXBuildFile; fileRef = 034280212B4308D2002AF60D /* Prompt.swift */; };
034280252B441E9E002AF60D /* OpenAI in Frameworks */ = {isa = PBXBuildFile; productRef = 034280242B441E9E002AF60D /* OpenAI */; };
0342A9812AD64924002A9F5F /* NSString+EZSplit.m in Sources */ = {isa = PBXBuildFile; fileRef = 0342A9802AD64924002A9F5F /* NSString+EZSplit.m */; };
034749772B37279200FF679C /* String+EncryptAES.swift in Sources */ = {isa = PBXBuildFile; fileRef = 03FD68BD2B1E151A00FD388E /* String+EncryptAES.swift */; };
03542A30293645DF00C34C33 /* EZAppleService.m in Sources */ = {isa = PBXBuildFile; fileRef = 03542A2F293645DF00C34C33 /* EZAppleService.m */; };
Expand Down Expand Up @@ -91,7 +94,7 @@
0399116A292AA2EF00E1B06D /* EZLayoutManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 03991169292AA2EF00E1B06D /* EZLayoutManager.m */; };
0399C6A529A747E600B4AFCC /* EZDeepLTranslateResponse.m in Sources */ = {isa = PBXBuildFile; fileRef = 0399C6A429A747E600B4AFCC /* EZDeepLTranslateResponse.m */; };
0399C6A829A74E0F00B4AFCC /* EZQueryResult+EZDeepLTranslateResponse.m in Sources */ = {isa = PBXBuildFile; fileRef = 0399C6A729A74E0F00B4AFCC /* EZQueryResult+EZDeepLTranslateResponse.m */; };
0399C6AC29A860AA00B4AFCC /* EZOpenAIService.m in Sources */ = {isa = PBXBuildFile; fileRef = 0399C6AB29A860AA00B4AFCC /* EZOpenAIService.m */; };
0399C6AC29A860AA00B4AFCC /* EZObjcOpenAIService.m in Sources */ = {isa = PBXBuildFile; fileRef = 0399C6AB29A860AA00B4AFCC /* EZObjcOpenAIService.m */; };
0399C6B829A9F4B800B4AFCC /* EZSchemeParser.m in Sources */ = {isa = PBXBuildFile; fileRef = 0399C6B729A9F4B800B4AFCC /* EZSchemeParser.m */; };
039B694F2A9D9F370063709D /* EZWebViewManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 039B694E2A9D9F370063709D /* EZWebViewManager.m */; };
039CC90D292F664E0037B91E /* NSObject+EZWindowType.m in Sources */ = {isa = PBXBuildFile; fileRef = 039CC90C292F664E0037B91E /* NSObject+EZWindowType.m */; };
Expand Down Expand Up @@ -344,6 +347,8 @@
033C30FB2A7409C40095926A /* TTTDictionary.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TTTDictionary.m; sourceTree = "<group>"; };
033C30FE2A74CECE0095926A /* EZAppleDictionary.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = EZAppleDictionary.h; sourceTree = "<group>"; };
033C30FF2A74CECE0095926A /* EZAppleDictionary.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = EZAppleDictionary.m; sourceTree = "<group>"; };
0342801C2B41A01F002AF60D /* OpenAIService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OpenAIService.swift; sourceTree = "<group>"; };
034280212B4308D2002AF60D /* Prompt.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Prompt.swift; sourceTree = "<group>"; };
0342A97F2AD64924002A9F5F /* NSString+EZSplit.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "NSString+EZSplit.h"; sourceTree = "<group>"; };
0342A9802AD64924002A9F5F /* NSString+EZSplit.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "NSString+EZSplit.m"; sourceTree = "<group>"; };
03542A2E293645DF00C34C33 /* EZAppleService.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = EZAppleService.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -438,8 +443,8 @@
0399C6A429A747E600B4AFCC /* EZDeepLTranslateResponse.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = EZDeepLTranslateResponse.m; sourceTree = "<group>"; };
0399C6A629A74E0F00B4AFCC /* EZQueryResult+EZDeepLTranslateResponse.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "EZQueryResult+EZDeepLTranslateResponse.h"; sourceTree = "<group>"; };
0399C6A729A74E0F00B4AFCC /* EZQueryResult+EZDeepLTranslateResponse.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "EZQueryResult+EZDeepLTranslateResponse.m"; sourceTree = "<group>"; };
0399C6AA29A860AA00B4AFCC /* EZOpenAIService.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = EZOpenAIService.h; sourceTree = "<group>"; };
0399C6AB29A860AA00B4AFCC /* EZOpenAIService.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = EZOpenAIService.m; sourceTree = "<group>"; };
0399C6AA29A860AA00B4AFCC /* EZObjcOpenAIService.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = EZObjcOpenAIService.h; sourceTree = "<group>"; };
0399C6AB29A860AA00B4AFCC /* EZObjcOpenAIService.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = EZObjcOpenAIService.m; sourceTree = "<group>"; };
0399C6B629A9F4B800B4AFCC /* EZSchemeParser.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = EZSchemeParser.h; sourceTree = "<group>"; };
0399C6B729A9F4B800B4AFCC /* EZSchemeParser.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = EZSchemeParser.m; sourceTree = "<group>"; };
039B694D2A9D9F370063709D /* EZWebViewManager.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = EZWebViewManager.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -782,6 +787,7 @@
03A830902B4073E700112834 /* AppCenterAnalytics in Frameworks */,
03B63ABF2A86967800E155ED /* CoreServices.framework in Frameworks */,
038030972B4106800009230C /* CocoaLumberjackSwift in Frameworks */,
034280252B441E9E002AF60D /* OpenAI in Frameworks */,
038EA1AA2B41169C008A6DD1 /* ZipArchive in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -1181,8 +1187,10 @@
0399C6A929A8608000B4AFCC /* OpenAI */ = {
isa = PBXGroup;
children = (
0399C6AA29A860AA00B4AFCC /* EZOpenAIService.h */,
0399C6AB29A860AA00B4AFCC /* EZOpenAIService.m */,
0342801C2B41A01F002AF60D /* OpenAIService.swift */,
034280212B4308D2002AF60D /* Prompt.swift */,
0399C6AA29A860AA00B4AFCC /* EZObjcOpenAIService.h */,
0399C6AB29A860AA00B4AFCC /* EZObjcOpenAIService.m */,
03FC69962B399EF00035D2DA /* EZOpenAIChatResponse.h */,
03FC69992B39D13A0035D2DA /* EZOpenAIChatResponse.m */,
03CF27F92B3A787900E19B57 /* EZOpenAIService+EZPromptMessages.h */,
Expand Down Expand Up @@ -2234,6 +2242,7 @@
038030962B4106800009230C /* CocoaLumberjackSwift */,
038EA1A92B41169C008A6DD1 /* ZipArchive */,
038EA1AC2B41282F008A6DD1 /* MJExtension */,
034280242B441E9E002AF60D /* OpenAI */,
);
productName = Bob;
productReference = C99EEB182385796700FEE666 /* Easydict-debug.app */;
Expand Down Expand Up @@ -2292,6 +2301,7 @@
038030932B4106800009230C /* XCRemoteSwiftPackageReference "CocoaLumberjack" */,
038EA1A82B41169C008A6DD1 /* XCRemoteSwiftPackageReference "ZipArchive" */,
038EA1AB2B41282F008A6DD1 /* XCRemoteSwiftPackageReference "MJExtension" */,
034280232B441E9E002AF60D /* XCRemoteSwiftPackageReference "OpenAI" */,
);
productRefGroup = C99EEB192385796700FEE666 /* Products */;
projectDirPath = "";
Expand Down Expand Up @@ -2556,7 +2566,7 @@
03882F8F29D95044005B5A52 /* CTScreen.m in Sources */,
27FE980B2B3DD5D1000AD654 /* MenuItemView.swift in Sources */,
03DC7C6A2A3CA852000BF7C9 /* EZAppCell.m in Sources */,
0399C6AC29A860AA00B4AFCC /* EZOpenAIService.m in Sources */,
0399C6AC29A860AA00B4AFCC /* EZObjcOpenAIService.m in Sources */,
03542A432937B45E00C34C33 /* EZBaiduTranslate.m in Sources */,
03BB2DEB29F57DC000447EDD /* NSImage+EZSymbolmage.m in Sources */,
03B0230629231FA6001C7E63 /* EZLabel.m in Sources */,
Expand All @@ -2582,6 +2592,7 @@
03B0230529231FA6001C7E63 /* EZButton.m in Sources */,
03B0232329231FA6001C7E63 /* NSString+MM.m in Sources */,
036196772A000F5900806370 /* NSData+CommonCrypto.m in Sources */,
034280222B4308D2002AF60D /* Prompt.swift in Sources */,
03882F8D29D95044005B5A52 /* CTView.m in Sources */,
278322602B0FB0EA0026644C /* CaiyunResponse.swift in Sources */,
03B3B8B52925DD3D00168E8D /* EZPopButtonViewController.m in Sources */,
Expand Down Expand Up @@ -2691,6 +2702,7 @@
039F5504294B6E29004AB940 /* EZPreferencesWindowController.m in Sources */,
03008B3F29444B0A0062B821 /* NSView+EZAnimatedHidden.m in Sources */,
03B022FD29231FA6001C7E63 /* EZFixedQueryWindow.m in Sources */,
0342801D2B41A01F002AF60D /* OpenAIService.swift in Sources */,
03B0232C29231FA6001C7E63 /* NSView+MM.m in Sources */,
033C31002A74CECE0095926A /* EZAppleDictionary.m in Sources */,
03E2BF752A298F2B00E010F3 /* NSString+EZUtils.m in Sources */,
Expand Down Expand Up @@ -3179,6 +3191,14 @@
minimumVersion = 5.6.0;
};
};
034280232B441E9E002AF60D /* XCRemoteSwiftPackageReference "OpenAI" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/tisfeng/OpenAI";
requirement = {
branch = dev;
kind = branch;
};
};
038030932B4106800009230C /* XCRemoteSwiftPackageReference "CocoaLumberjack" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/CocoaLumberjack/CocoaLumberjack.git";
Expand Down Expand Up @@ -3269,6 +3289,11 @@
package = 03022F202B36D1A300B63209 /* XCRemoteSwiftPackageReference "SnapKit" */;
productName = SnapKit;
};
034280242B441E9E002AF60D /* OpenAI */ = {
isa = XCSwiftPackageProductDependency;
package = 034280232B441E9E002AF60D /* XCRemoteSwiftPackageReference "OpenAI" */;
productName = OpenAI;
};
038030942B4106800009230C /* CocoaLumberjack */ = {
isa = XCSwiftPackageProductDependency;
package = 038030932B4106800009230C /* XCRemoteSwiftPackageReference "CocoaLumberjack" */;
Expand Down
9 changes: 9 additions & 0 deletions Easydict.xcworkspace/xcshareddata/swiftpm/Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,15 @@
"version" : "2.30909.0"
}
},
{
"identity" : "openai",
"kind" : "remoteSourceControl",
"location" : "https://github.com/tisfeng/OpenAI",
"state" : {
"branch" : "dev",
"revision" : "71009401f8e469eb0770c5c8d83f747a1aa020a3"
}
},
{
"identity" : "plcrashreporter",
"kind" : "remoteSourceControl",
Expand Down
1 change: 1 addition & 0 deletions Easydict/App/Easydict-Bridging-Header.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@
#import "entry.h"
#import "AppDelegate.h"
#import "EZConfiguration.h"
#import "EZEnumTypes.h"

#import "NSString+EZConvenience.h"
2 changes: 1 addition & 1 deletion Easydict/Feature/Service/Model/EZServiceTypes.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#import "EZDeepLTranslate.h"
#import "EZVolcanoTranslate.h"
#import "EZAppleService.h"
#import "EZOpenAIService.h"
//#import "EZOpenAIService2.h"
#import "EZBingService.h"
#import "EZConfiguration.h"
#import "EZAppleDictionary.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

NS_ASSUME_NONNULL_BEGIN

@interface EZOpenAIService : EZQueryService
@interface EZObjcOpenAIService : EZQueryService

@end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// Copyright © 2023 izual. All rights reserved.
//

#import "EZOpenAIService.h"
#import "EZObjcOpenAIService.h"
#import "NSString+EZUtils.h"
#import "EZConfiguration.h"
#import "EZOpenAIChatResponse.h"
Expand All @@ -15,7 +15,7 @@

static NSString *const kEZLanguageWenYanWen = @"文言文";

@interface EZOpenAIService ()
@interface EZObjcOpenAIService ()

@property (nonatomic, copy) NSString *apiKey;
@property (nonatomic, copy) NSString *endPoint;
Expand All @@ -28,7 +28,7 @@ @interface EZOpenAIService ()

@end

@implementation EZOpenAIService
@implementation EZObjcOpenAIService

- (instancetype)init {
if (self = [super init]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
//

#import <Foundation/Foundation.h>
#import "EZOpenAIService.h"
#import "EZObjcOpenAIService.h"

NS_ASSUME_NONNULL_BEGIN

@interface EZOpenAIService (EZPromptMessages)
@interface EZObjcOpenAIService (EZPromptMessages)

/// Translation messages.
- (NSArray *)translatioMessages:(NSString *)text from:(EZLanguage)sourceLanguage to:(EZLanguage)targetLanguage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

static NSString *kTranslationSystemPrompt = @"You are a translation expert proficient in various languages that can only translate text and cannot interpret it. You are able to accurately understand the meaning of proper nouns, idioms, metaphors, allusions or other obscure words in sentences and translate them into appropriate words by combining the context and language environment. The result of the translation should be natural and fluent, you can only return the translated text, do not show additional information and notes.";

@implementation EZOpenAIService (EZPromptMessages)
@implementation EZObjcOpenAIService (EZPromptMessages)


#pragma mark - Chat messages
Expand Down
Loading
Loading