From 9a8684df3605e65961364fa49afedfe43ddee85f Mon Sep 17 00:00:00 2001 From: RiabushenkoA Date: Wed, 5 Feb 2025 20:27:41 +0200 Subject: [PATCH] Updated responce handling --- Apps.GoogleVertexAI/Actions/GeminiActions.cs | 26 ++++++++++++++++++- .../Apps.GoogleVertexAI.csproj | 2 +- .../TestFiles/Output/test.xliff | 22 ++++++++++++++++ 3 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 Tests.GoogleVertexAI/TestFiles/Output/test.xliff diff --git a/Apps.GoogleVertexAI/Actions/GeminiActions.cs b/Apps.GoogleVertexAI/Actions/GeminiActions.cs index 2260bfd..c4eaca4 100644 --- a/Apps.GoogleVertexAI/Actions/GeminiActions.cs +++ b/Apps.GoogleVertexAI/Actions/GeminiActions.cs @@ -24,6 +24,7 @@ using Newtonsoft.Json; using Apps.GoogleVertexAI.Utils.Xliff; using Blackbird.Applications.Sdk.Common.Exceptions; +using Newtonsoft.Json.Linq; namespace Apps.GoogleVertexAI.Actions; @@ -503,12 +504,22 @@ private string GetSystemPrompt(bool translator) try { + var startIndex = translatedText.IndexOf("["); + if (startIndex == -1) + throw new PluginApplicationException("Invalid JSON format: missing opening bracket."); + + var extractedJson = translatedText.Substring(startIndex); + + if (!IsValidJson(extractedJson)) + { + throw new PluginApplicationException($"Invalid JSON received from Vertex AI:\n{extractedJson}"); + } var result = JsonConvert.DeserializeObject(translatedText.Substring(translatedText.IndexOf("["))); if (result.Length != batch.Count()) { throw new PluginApplicationException( - "OpenAI returned inappropriate response. " + + "Server returned inappropriate response. " + "The number of translated texts does not match the number of source texts. " + "Probably there is a duplication or a missing text in translation unit. " + "Try change model or bucket size (to lower values) or add retries to this action."); @@ -626,4 +637,17 @@ protected async Task DownloadXliffDocumentAsync(FileReference fil return xliffDocument; } + + private bool IsValidJson(string json) + { + try + { + JToken.Parse(json); + return true; + } + catch (JsonReaderException) + { + return false; + } + } } \ No newline at end of file diff --git a/Apps.GoogleVertexAI/Apps.GoogleVertexAI.csproj b/Apps.GoogleVertexAI/Apps.GoogleVertexAI.csproj index fbbded9..dd89cf5 100644 --- a/Apps.GoogleVertexAI/Apps.GoogleVertexAI.csproj +++ b/Apps.GoogleVertexAI/Apps.GoogleVertexAI.csproj @@ -6,7 +6,7 @@ enable Google Vertex AI Powerful and unified machine learning platform offered by Google Cloud - 1.2.1 + 1.2.2 Apps.GoogleVertexAI diff --git a/Tests.GoogleVertexAI/TestFiles/Output/test.xliff b/Tests.GoogleVertexAI/TestFiles/Output/test.xliff new file mode 100644 index 0000000..09a81e1 --- /dev/null +++ b/Tests.GoogleVertexAI/TestFiles/Output/test.xliff @@ -0,0 +1,22 @@ + + + +
+ +
+ + + Dogs are loyal companions who bring joy and love into our lives. + Los perros son compañeros leales que traen alegría y amor a nuestras vidas. + + + Walking with your dog in the park is a wonderful way to bond and stay active. + Caminar con tu perro en el parque es una manera maravillosa de crear un vínculo y mantenerse activo. + + + Training and socialization are essential for ensuring a well-behaved and happy dog. + El entrenamiento y la socialización son esenciales para garantizar un perro bien educado y feliz. + + +
+
\ No newline at end of file