Skip to content

Commit

Permalink
perf: improve prompt, add classical Chinese few shot
Browse files Browse the repository at this point in the history
  • Loading branch information
tisfeng committed Apr 30, 2024
1 parent 8f9c876 commit af67e11
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 19 deletions.
23 changes: 9 additions & 14 deletions Easydict/Swift/Service/OpenAI/BaseOpenAIService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,15 @@ public class BaseOpenAIService: QueryService {
}

override public func supportLanguagesDictionary() -> MMOrderedDictionary<AnyObject, AnyObject> {
let orderedDict = MMOrderedDictionary<AnyObject, AnyObject>()
for language in EZLanguageManager.shared().allLanguages {
var value = language.rawValue
if language == .classicalChinese {
value = Language.wenyanwen
}

if language != .burmese {
orderedDict.setObject(value as NSString, forKey: language.rawValue as NSString)
}
let allLangauges = EZLanguageManager.shared().allLanguages
let supportedLanguages = allLangauges.filter { language in
!unsupportedLanguages.contains(language)
}

let orderedDict = MMOrderedDictionary<AnyObject, AnyObject>()
for language in supportedLanguages {
orderedDict.setObject(language.rawValue as NSString, forKey: language.rawValue as NSString)
}
return orderedDict
}

Expand Down Expand Up @@ -150,6 +147,8 @@ public class BaseOpenAIService: QueryService {

var model = ""

var unsupportedLanguages: [Language] = []

var availableModels: [String] {
[""]
}
Expand Down Expand Up @@ -240,7 +239,3 @@ public class BaseOpenAIService: QueryService {
return resultText
}
}

extension Language {
static var wenyanwen = "文言文"
}
103 changes: 98 additions & 5 deletions Easydict/Swift/Service/OpenAI/Prompt.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@ extension QueryService {
"""

func translationPrompt(text: String, from sourceLanguage: Language, to targetLanguage: Language) -> String {
let prompt =
"Translate the following \(sourceLanguage) text into \(targetLanguage) text:\n\n\"\"\"\n\(text)\n\"\"\" "
return prompt
"Translate the following \(sourceLanguage.queryLangaugeName) text into \(targetLanguage.queryLangaugeName) text:\"\"\"\(text)\"\"\""
}

func translatioMessages(text: String, from: Language, to: Language) -> [[String: String]] {
// Use """ %@ """ to wrap user input, Ref: https://help.openai.com/en/articles/6654000-best-practices-for-prompt-engineering-with-openai-api#h_21d4f4dc3d
let prompt = "Translate the following \(from.rawValue) text into \(to.rawValue) text: \"\"\"\(text)\"\"\""
// let prompt = "Translate the following \(from.rawValue) text into \(to.rawValue) text: \"\"\"\(text)\"\"\""

let prompt = translationPrompt(text: text, from: from, to: to)

let chineseFewShot = [
// en --> zh
[
"role": "user",
"content": "Translate the following English text into Simplified-Chinese: \"\"\"The stock market has now reached a plateau.\"\"\"",
"content": "Translate the following English text into Simplified-Chinese text: \"\"\"The stock market has now reached a plateau.\"\"\"",
],
[
"role": "assistant",
Expand Down Expand Up @@ -131,6 +131,79 @@ extension QueryService {
],
]

let classicalChinseseFewShot = [
// wyw --> zh
[
"role": "user",
"content": """
Translate the following 中文文言文 text into 简体中文白话文 text:
\"\"\"曾经沧海难为水,除却巫山不是云。\"\"\"
""",
],
[
"role": "assistant",
"content": """
经历过波澜壮阔的大海,别处的水再也不值得一观。陶醉过巫山的云雨的梦幻,别处的风景就不称之为云雨了。
""",
],
[
"role": "user",
"content": "露从今夜白,月是故乡明。",
],
[
"role": "assistant",
"content": "从今夜就进入了白露节气,月亮还是故乡的最明亮。",
],
[
"role": "user",
"content": """
苏溪亭上草漫漫,谁倚东风十二阑。
燕子不归春事晚,一汀烟雨杏花寒。
""",
],
[
"role": "assistant",
"content": """
苏溪亭外野草青青,无边无际;是谁随着东风唱着阑干十二曲呢?
燕子还没有回到旧窝,而美好的春光已快要完了;迷蒙的烟雨笼罩着一片沙洲,杏花在料峭春风中只感凄寒。
""",
],
[
"role": "user",
"content": """
《不第后赋菊》
待到秋来九月八,我花开后百花杀。
冲天香阵透长安,满城尽带黄金甲。
""",
],
[
"role": "assistant",
"content": """
《不第后赋菊》
等到秋天九月重阳节来临的时候,菊花盛开以后别的花就凋零了。
盛开的菊花香气弥漫整个长安,遍地都是金黄如铠甲般的菊花。
""",
],

[
"role": "user",
"content": """
Translate the following 中文文言文 text into 繁体中文白话文 text:
《题菊花》
飒飒西风满院栽,蕊寒香冷蝶难来。
他年我若为青帝, 报与桃花一处开。
""",
],
[
"role": "assistant",
"content": """
《題菊花》
秋風颯颯搖動滿院菊花,花蕊花香充滿寒意,再難有蝴蝶飛來採蜜。
若是有朝一日我成為了司春之神,一定要讓菊花和桃花同在春天盛開。
""",
],
]

let systemMessages = [
[
"role": "system",
Expand All @@ -141,6 +214,10 @@ extension QueryService {
var messages = systemMessages
messages.append(contentsOf: chineseFewShot)

if from == .classicalChinese {
messages.append(contentsOf: classicalChinseseFewShot)
}

let userMessages = [
[
"role": "user",
Expand Down Expand Up @@ -732,4 +809,20 @@ extension QueryService {
}
}

extension Language {
var queryLangaugeName: String {
let languageName = switch self {
case .classicalChinese:
"中文文言文"
case .simplifiedChinese:
"简体中文白话文"
case .traditionalChinese:
"繁体中文白话文"
default:
rawValue
}
return languageName
}
}

// swiftlint:enable all

0 comments on commit af67e11

Please sign in to comment.