Skip to content

Commit

Permalink
feat: add Polishing and Summary Features (#609)
Browse files Browse the repository at this point in the history
* perf: add icons for polishing and grammar

* fix: wrong icon placement

* perf: setup base for grammar and polishing

* fix: missing files

* fix: wrong service name for summary

* perf: add LLMDerivService as the base of LLMDerivs

* feat: implement polishing and summary features

* refactor: make AIToolService override method chatMessageDicts()

* perf: rename LLM Derivatives to AI Tools

* perf: remove unused code

* perf: improve prompts for text summary

* fix: extraneous prompt

* fix: use first preferred langauge to summarize text

* feat: set AI tool service usage status to alway off by default

* fix: add missing en localization

* fix: icon size for polishing and grammar service

* fix: format fewShots to conform to original prompt

* perf(UI): add new icon for summary service

---------

Co-authored-by: tisfeng <tisfeng@gmail.com>
  • Loading branch information
Jerry23011 and tisfeng authored Jul 16, 2024
1 parent aaf6200 commit dafba90
Show file tree
Hide file tree
Showing 16 changed files with 314 additions and 13 deletions.
20 changes: 20 additions & 0 deletions Easydict.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,10 @@
C415C0AD2B450D4800A9D231 /* GeminiService.swift in Sources */ = {isa = PBXBuildFile; fileRef = C415C0AC2B450D4800A9D231 /* GeminiService.swift */; };
C477BF912C0E2C61006A3F27 /* InfoPlist.xcstrings in Resources */ = {isa = PBXBuildFile; fileRef = C477BF902C0E2C61006A3F27 /* InfoPlist.xcstrings */; };
C490BF722BE910B70021E40A /* AdvancedTabItemView.swift in Sources */ = {isa = PBXBuildFile; fileRef = C490BF712BE910B70021E40A /* AdvancedTabItemView.swift */; };
C4A512BE2C414A2400F00F33 /* AIToolService.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4A512BD2C414A2400F00F33 /* AIToolService.swift */; };
C4BFDD7A2BE61F550094026B /* Vortex in Frameworks */ = {isa = PBXBuildFile; productRef = C4BFDD792BE61F550094026B /* Vortex */; };
C4CCDAA32C407A0D00AC88BA /* SummaryService.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4CCDAA22C407A0D00AC88BA /* SummaryService.swift */; };
C4CCDAA52C407A4100AC88BA /* PolishingService.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4CCDAA42C407A4100AC88BA /* PolishingService.swift */; };
C4DD01E92B12B3C80025EE8E /* TencentService.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4DD01E82B12B3C80025EE8E /* TencentService.swift */; };
C4DD01EB2B12BA250025EE8E /* TencentResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4DD01EA2B12BA250025EE8E /* TencentResponse.swift */; };
C4DD01ED2B12BE9B0025EE8E /* TencentTranslateType.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4DD01EC2B12BE9B0025EE8E /* TencentTranslateType.swift */; };
Expand Down Expand Up @@ -792,6 +795,9 @@
C415C0AC2B450D4800A9D231 /* GeminiService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GeminiService.swift; sourceTree = "<group>"; };
C477BF902C0E2C61006A3F27 /* InfoPlist.xcstrings */ = {isa = PBXFileReference; lastKnownFileType = text.json.xcstrings; path = InfoPlist.xcstrings; sourceTree = "<group>"; };
C490BF712BE910B70021E40A /* AdvancedTabItemView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = AdvancedTabItemView.swift; path = Easydict/Swift/View/AdvancedTabItemView.swift; sourceTree = SOURCE_ROOT; };
C4A512BD2C414A2400F00F33 /* AIToolService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AIToolService.swift; sourceTree = "<group>"; };
C4CCDAA22C407A0D00AC88BA /* SummaryService.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SummaryService.swift; sourceTree = "<group>"; };
C4CCDAA42C407A4100AC88BA /* PolishingService.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PolishingService.swift; sourceTree = "<group>"; };
C4DD01E82B12B3C80025EE8E /* TencentService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TencentService.swift; sourceTree = "<group>"; };
C4DD01EA2B12BA250025EE8E /* TencentResponse.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TencentResponse.swift; sourceTree = "<group>"; };
C4DD01EC2B12BE9B0025EE8E /* TencentTranslateType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TencentTranslateType.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1204,6 +1210,7 @@
0A9AFBA92B7F8D6A0064C9A8 /* CustomOpenAI */,
C415C0AB2B450C4500A9D231 /* Gemini */,
03792EA02C3830E60074A145 /* Ollama */,
C4D1ADE12C3F340500A19D02 /* AITool */,
C4DD01E72B12B3B00025EE8E /* Tencent */,
2746AEBF2AF95040005FE0A1 /* Caiyun */,
62E2BF462B4082BA00E42D38 /* Ali */,
Expand Down Expand Up @@ -2295,6 +2302,16 @@
name = "Recovered References";
sourceTree = "<group>";
};
C4D1ADE12C3F340500A19D02 /* AITool */ = {
isa = PBXGroup;
children = (
C4A512BD2C414A2400F00F33 /* AIToolService.swift */,
C4CCDAA42C407A4100AC88BA /* PolishingService.swift */,
C4CCDAA22C407A0D00AC88BA /* SummaryService.swift */,
);
path = AITool;
sourceTree = "<group>";
};
C4DD01E72B12B3B00025EE8E /* Tencent */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -2777,6 +2794,7 @@
962B9ACE2BFA03270080F4D4 /* EZAppModel.m in Sources */,
03BDA7C42A26DA280079D04F /* NSDictionary+RubyDescription.m in Sources */,
62ED29A22B15F1F500901F51 /* EZWrapView.m in Sources */,
C4CCDAA32C407A0D00AC88BA /* SummaryService.swift in Sources */,
C4DD01EB2B12BA250025EE8E /* TencentResponse.swift in Sources */,
036A0DB82AD8403A006E6D4F /* NSString+EZHandleInputText.m in Sources */,
03BDA7C12A26DA280079D04F /* XPMArgumentParser.m in Sources */,
Expand All @@ -2791,6 +2809,7 @@
03D0435A2928C4C800E7559E /* EZWindowManager.m in Sources */,
6295DE342A84EF76006145F4 /* EZBingLookupModel.m in Sources */,
03FD68BE2B1E151A00FD388E /* String+EncryptAES.swift in Sources */,
C4A512BE2C414A2400F00F33 /* AIToolService.swift in Sources */,
03B0230729231FA6001C7E63 /* EZCommonView.m in Sources */,
03B0233329231FA6001C7E63 /* MMLog.m in Sources */,
0AC8A8352B6641A7006DA5CC /* TencentService+ConfigurableService.swift in Sources */,
Expand Down Expand Up @@ -2853,6 +2872,7 @@
03E02A2629250D1D00A10260 /* EZEventMonitor.m in Sources */,
03B0233429231FA6001C7E63 /* MMConsoleLogFormatter.m in Sources */,
037852B9295D49F900D0E2CF /* EZTableRowView.m in Sources */,
C4CCDAA52C407A4100AC88BA /* PolishingService.swift in Sources */,
03779F0E2BB256A7008D3C42 /* OpenAIService.swift in Sources */,
033363A6293C4AFA00FED9C8 /* PrintBeautifulLog.m in Sources */,
039CC914292FB3180037B91E /* EZPopUpButton.m in Sources */,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"images" : [
{
"filename" : "Polishing.png",
"idiom" : "universal",
"scale" : "1x"
},
{
"filename" : "Polishing@2x.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.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"images" : [
{
"filename" : "Summary.png",
"idiom" : "universal",
"scale" : "1x"
},
{
"filename" : "Summary@2x.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.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 32 additions & 0 deletions Easydict/App/Localizable.xcstrings
Original file line number Diff line number Diff line change
Expand Up @@ -2002,6 +2002,22 @@
}
}
},
"polishing_service" : {
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "Polishing"
}
},
"zh-Hans" : {
"stringUnit" : {
"state" : "translated",
"value" : "润色"
}
}
}
},
"present_participle" : {
"localizations" : {
"en" : {
Expand Down Expand Up @@ -3850,6 +3866,22 @@
}
}
},
"summary_service" : {
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "Summary"
}
},
"zh-Hans" : {
"stringUnit" : {
"state" : "translated",
"value" : "总结"
}
}
}
},
"superlative" : {
"localizations" : {
"en" : {
Expand Down
36 changes: 36 additions & 0 deletions Easydict/Swift/Service/AITool/AIToolService.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//
// AIToolService.swift
// Easydict
//
// Created by Jerry on 2024-07-12.
// Copyright © 2024 izual. All rights reserved.
//

import Defaults
import Foundation

/// A class used for AI Tools such as summary and polishing
class AIToolService: BuiltInAIService {
// MARK: Public

public override func configurationListItems() -> Any {
StreamConfigurationView(
service: self,
showNameSection: false,
showAPIKeySection: false,
showEndpointSection: false,
showSupportedModelsSection: false,
showUsedModelSection: false,
showTranslationToggle: false,
showSentenceToggle: false,
showDictionaryToggle: false,
showUsageStatusPicker: true
)
}

// MARK: Internal

override var serviceUsageStatusKey: Defaults.Key<ServiceUsageStatus> {
serviceDefaultsKey(.serviceUsageStatus, defaultValue: .alwaysOff)
}
}
29 changes: 29 additions & 0 deletions Easydict/Swift/Service/AITool/PolishingService.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//
// PolishingService.swift
// Easydict
//
// Created by Jerry on 2024-07-11.
// Copyright © 2024 izual. All rights reserved.
//

import Foundation
import OpenAI

@objc(EZPolishingService)
class PolishingService: AIToolService {
// MARK: Public

public override func name() -> String {
NSLocalizedString("polishing_service", comment: "")
}

public override func serviceType() -> ServiceType {
.polishing
}

// MARK: Internal

override func chatMessageDicts(_ chatQuery: ChatQueryParam) -> [[String: String]] {
polishingMessages(chatQuery)
}
}
29 changes: 29 additions & 0 deletions Easydict/Swift/Service/AITool/SummaryService.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//
// SummaryService.swift
// Easydict
//
// Created by Jerry on 2024-07-11.
// Copyright © 2024 izual. All rights reserved.
//

import Foundation
import OpenAI

@objc(EZSummaryService)
class SummaryService: AIToolService {
// MARK: Public

public override func name() -> String {
NSLocalizedString("summary_service", comment: "")
}

public override func serviceType() -> ServiceType {
.summary
}

// MARK: Internal

override func chatMessageDicts(_ chatQuery: ChatQueryParam) -> [[String: String]] {
summaryMessages(chatQuery)
}
}
12 changes: 12 additions & 0 deletions Easydict/Swift/Service/OpenAI/LLMStreamService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,18 @@ public class LLMStreamService: QueryService {
fatalError(mustOverride)
}

func chatMessageDicts(_ chatQuery: ChatQueryParam)
-> [[String: String]] {
switch chatQuery.queryType {
case .dictionary:
dictMessages(chatQuery)
case .sentence:
sentenceMessages(chatQuery)
default:
translationMessages(chatQuery)
}
}

func getFinalResultText(_ text: String) -> String {
var resultText = text.trim()

Expand Down
Loading

0 comments on commit dafba90

Please sign in to comment.