1- using OpenAI . ChatGpt . Models . ChatCompletion ;
1+ using System . Text . Json ;
2+ using System . Text . Json . Serialization ;
3+ using OpenAI . ChatGpt . Models . ChatCompletion ;
4+ using OpenAI . ChatGpt . Models . ChatCompletion . Messaging ;
5+ using OpenAI . ChatGpt . Modules . StructuredResponse ;
26
37namespace OpenAI . ChatGpt . Modules . Translator ;
48
9+ [ Fody . ConfigureAwait ( false ) ]
10+ // ReSharper disable once InconsistentNaming
511public class ChatGPTTranslatorService : IDisposable
612{
713 private readonly IOpenAiClient _client ;
@@ -45,14 +51,19 @@ public void Dispose()
4551 }
4652 }
4753
48- public async Task < string > Translate (
54+ public async Task < string > TranslateText (
4955 string text ,
5056 string ? sourceLanguage = null ,
5157 string ? targetLanguage = null ,
58+ int ? maxTokens = null ,
59+ string ? model = null ,
60+ float temperature = ChatCompletionTemperatures . Default ,
61+ string ? user = null ,
5262 Action < ChatCompletionRequest > ? requestModifier = null ,
63+ Action < ChatCompletionResponse > ? rawResponseGetter = null ,
5364 CancellationToken cancellationToken = default )
5465 {
55- if ( text == null ) throw new ArgumentNullException ( nameof ( text ) ) ;
66+ ArgumentNullException . ThrowIfNull ( text ) ;
5667 var sourceLanguageOrDefault = sourceLanguage ?? _defaultSourceLanguage ;
5768 var targetLanguageOrDefault = targetLanguage ?? _defaultTargetLanguage ;
5869 if ( sourceLanguageOrDefault is null )
@@ -63,21 +74,84 @@ public async Task<string> Translate(
6374 {
6475 throw new ArgumentNullException ( nameof ( targetLanguage ) , "Target language is not specified" ) ;
6576 }
66- var prompt = GetPrompt ( sourceLanguageOrDefault , targetLanguageOrDefault ) ;
77+
78+ var prompt = CreateTextTranslationPrompt ( sourceLanguageOrDefault , targetLanguageOrDefault ) ;
79+ var messages = Dialog . StartAsSystem ( prompt ) . ThenUser ( text ) . GetMessages ( ) . ToArray ( ) ;
80+ ( model , maxTokens ) = ChatCompletionMessage . FindOptimalModelAndMaxToken ( messages , model , maxTokens ) ;
6781 var response = await _client . GetChatCompletions (
68- Dialog . StartAsSystem ( prompt ) . ThenUser ( text ) ,
69- user : null ,
70- requestModifier : requestModifier ,
71- cancellationToken : cancellationToken
72- ) ;
82+ messages ,
83+ maxTokens . Value ,
84+ model ,
85+ temperature ,
86+ user ,
87+ requestModifier ,
88+ rawResponseGetter ,
89+ cancellationToken ) ;
7390 return response ;
91+
92+ string CreateTextTranslationPrompt ( string sourceLanguage , string targetLanguage )
93+ {
94+ ArgumentNullException . ThrowIfNull ( sourceLanguage ) ;
95+ ArgumentNullException . ThrowIfNull ( targetLanguage ) ;
96+ return $ "I want you to act as a translator from { sourceLanguage } to { targetLanguage } . " +
97+ "The user provides with a sentence and you translate it. " +
98+ "In the response write ONLY translated text." +
99+ ( _extraPrompt is not null ? "\n " + _extraPrompt : "" ) ;
100+ }
74101 }
75-
76- private string GetPrompt ( string sourceLanguage , string targetLanguage )
102+
103+ public async Task < TObject > TranslateObject < TObject > (
104+ TObject objectToTranslate ,
105+ string ? sourceLanguage = null ,
106+ string ? targetLanguage = null ,
107+ int ? maxTokens = null ,
108+ string ? model = null ,
109+ float temperature = ChatCompletionTemperatures . Default ,
110+ string ? user = null ,
111+ Action < ChatCompletionRequest > ? requestModifier = null ,
112+ Action < ChatCompletionResponse > ? rawResponseGetter = null ,
113+ JsonSerializerOptions ? jsonSerializerOptions = null ,
114+ JsonSerializerOptions ? jsonDeserializerOptions = null ,
115+ CancellationToken cancellationToken = default ) where TObject : class
77116 {
78- return $ "I want you to act as a translator from { sourceLanguage } to { targetLanguage } . " +
79- "I will provide you with an English sentence and you will translate it into Russian. " +
80- "In the response write ONLY translated text."
81- + ( _extraPrompt is not null ? "\n " + _extraPrompt : "" ) ;
117+ ArgumentNullException . ThrowIfNull ( objectToTranslate ) ;
118+ var sourceLanguageOrDefault = sourceLanguage ?? _defaultSourceLanguage ;
119+ var targetLanguageOrDefault = targetLanguage ?? _defaultTargetLanguage ;
120+ if ( sourceLanguageOrDefault is null )
121+ {
122+ throw new ArgumentNullException ( nameof ( sourceLanguage ) , "Source language is not specified" ) ;
123+ }
124+ if ( targetLanguageOrDefault is null )
125+ {
126+ throw new ArgumentNullException ( nameof ( targetLanguage ) , "Target language is not specified" ) ;
127+ }
128+
129+ var prompt = CreateObjectTranslationPrompt ( sourceLanguageOrDefault , targetLanguageOrDefault ) ;
130+ jsonSerializerOptions ??= new JsonSerializerOptions ( ) { DefaultIgnoreCondition = JsonIgnoreCondition . Never } ;
131+ var objectJson = JsonSerializer . Serialize ( objectToTranslate , jsonSerializerOptions ) ;
132+ var dialog = Dialog . StartAsSystem ( prompt ) . ThenUser ( objectJson ) ;
133+ var messages = dialog . GetMessages ( ) . ToArray ( ) ;
134+ ( model , maxTokens ) = ChatCompletionMessage . FindOptimalModelAndMaxToken ( messages , model , maxTokens ) ;
135+ var response = await _client . GetStructuredResponse < TObject > (
136+ dialog ,
137+ maxTokens . Value ,
138+ model ,
139+ temperature ,
140+ user ,
141+ requestModifier ,
142+ rawResponseGetter ,
143+ jsonDeserializerOptions ,
144+ cancellationToken
145+ ) ;
146+ return response ;
147+
148+ string CreateObjectTranslationPrompt ( string sourceLanguage , string targetLanguage )
149+ {
150+ ArgumentNullException . ThrowIfNull ( sourceLanguage ) ;
151+ ArgumentNullException . ThrowIfNull ( targetLanguage ) ;
152+ return $ "I want you to act as a translator from { sourceLanguage } to { targetLanguage } . " +
153+ "The user provides you with an object in json. You translate only the text fields that need to be translated. " +
154+ ( _extraPrompt is not null ? "\n " + _extraPrompt : "" ) ;
155+ }
82156 }
83157}
0 commit comments