@@ -11,7 +11,7 @@ def __init__(self, promptsFolder=None):
11
11
promptsFolder = os .path .join (os .path .dirname (__file__ ), '../prompts' )
12
12
self ._LLM = ChatOpenAI (model = "gpt-3.5-turbo" )
13
13
14
- self ._translateShallow = LLMChain (
14
+ self ._translateShallowQuery = LLMChain (
15
15
llm = self ._LLM ,
16
16
prompt = ChatPromptTemplate .from_messages ([
17
17
HumanMessagePromptTemplate (
@@ -22,7 +22,7 @@ def __init__(self, promptsFolder=None):
22
22
),
23
23
]),
24
24
)
25
- self ._translateDeep = LLMChain (
25
+ self ._translateDeepQuery = LLMChain (
26
26
llm = self ._LLM ,
27
27
prompt = ChatPromptTemplate .from_messages ([
28
28
HumanMessagePromptTemplate (
@@ -60,37 +60,53 @@ def _executePrompt(self, prompt, variables):
60
60
res ['Flags' ] = flags
61
61
return res
62
62
63
- def translate (self , text , fastTranslation , language ):
64
- # run shallow translation
63
+ def _translateShallow (self , text , translation , language ):
65
64
res = self ._executePrompt (
66
- self ._translateShallow ,
65
+ self ._translateShallowQuery ,
67
66
{
68
67
'UserInput' : text ,
69
- 'FastTranslation' : fastTranslation ,
68
+ 'FastTranslation' : translation ,
70
69
'Language' : language
71
70
}
72
71
)
72
+ translation = res ['Translation' ]
73
73
flags = res ['Flags' ]
74
74
totalIssues = sum ([int (v ) for v in flags .values ()])
75
75
if totalIssues < 2 :
76
- yield res ['Translation' ]
77
- return # all ok, no need to run deep translation
78
- yield res ['Translation' ], res .get ('Notification' , '' )
79
-
80
- # run deep translation
81
- inputLanguage = res .get ('Input language' , 'unknown' )
76
+ return translation # all ok, no need to run deep translation
77
+
78
+ return res , translation , res .get ('Notification' , '' )
79
+
80
+ def _translateDeep (self , text , translation , language , inputLanguage , flags ):
82
81
# extract first word from input language, can be separated by space, comma, etc.,
83
82
inputLanguage = re .split (r'[\s,]+' , inputLanguage )[0 ]
84
83
inputLanguage = inputLanguage .strip ().capitalize ()
84
+
85
85
res = self ._executePrompt (
86
- self ._translateDeep ,
86
+ self ._translateDeepQuery ,
87
87
{
88
88
'UserInput' : text ,
89
- 'FastTranslation' : res [ 'Translation' ], # use shallow translation as reference
89
+ 'FastTranslation' : translation ,
90
90
'Language' : language ,
91
91
'InputLanguage' : inputLanguage ,
92
92
'Flags' : ', ' .join ([k for k , v in flags .items () if v ])
93
93
}
94
94
)
95
- yield res ['Translation' ]
95
+ return res ['Translation' ]
96
+
97
+ def translate (self , text , fastTranslation , language ):
98
+ # run shallow translation
99
+ res = self ._translateShallow (text = text , translation = fastTranslation , language = language )
100
+ if isinstance (res , str ):
101
+ yield res
102
+ return # all ok, no need to run deep translation
103
+
104
+ raw , translation , notification = res
105
+ yield translation , notification # yield shallow translation with notification
106
+ # run deep translation
107
+ yield self ._translateDeep (
108
+ text = text , translation = translation , language = language ,
109
+ inputLanguage = raw .get ('Input language' , 'unknown' ),
110
+ flags = raw ['Flags' ]
111
+ )
96
112
return
0 commit comments