Skip to content

Commit

Permalink
Updated responce handling
Browse files Browse the repository at this point in the history
  • Loading branch information
RiabushenkoA committed Feb 5, 2025
1 parent 4b35d34 commit 9a8684d
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
26 changes: 25 additions & 1 deletion Apps.GoogleVertexAI/Actions/GeminiActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<string[]>(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.");
Expand Down Expand Up @@ -626,4 +637,17 @@ protected async Task<XliffDocument> DownloadXliffDocumentAsync(FileReference fil

return xliffDocument;
}

private bool IsValidJson(string json)
{
try
{
JToken.Parse(json);
return true;
}
catch (JsonReaderException)
{
return false;
}
}
}
2 changes: 1 addition & 1 deletion Apps.GoogleVertexAI/Apps.GoogleVertexAI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<Nullable>enable</Nullable>
<Product>Google Vertex AI</Product>
<Description>Powerful and unified machine learning platform offered by Google Cloud</Description>
<Version>1.2.1</Version>
<Version>1.2.2</Version>
<AssemblyName>Apps.GoogleVertexAI</AssemblyName>
</PropertyGroup>

Expand Down
22 changes: 22 additions & 0 deletions Tests.GoogleVertexAI/TestFiles/Output/test.xliff
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<xliff version="1.2" xmlns:sc="SmartcatXliff" xmlns="urn:oasis:names:tc:xliff:document:1.2">
<file datatype="plaintext" date="2025-01-07T00:09:28.3793965Z" original="c7df6de0b41f733940250863_10" source-language="en" target-language="es">
<header>
<tool tool-name="Smartcat.ai" tool-id="40c7d5b2-da26-4b36-84f1-8305b3aadb03" tool-version="9.6391.0.0" />
</header>
<body>
<trans-unit id="120" approved="yes" sc:locked="false" sc:last-modified-date="2025-01-06T18:07:28.928Z" sc:last-modified-user="C**** N****" extradata="10">
<source xml:space="preserve" xml:lang="en">Dogs are loyal companions who bring joy and love into our lives.</source>
<target state="final" xml:space="preserve" xml:lang="es">Los perros son compañeros leales que traen alegría y amor a nuestras vidas.</target>
</trans-unit>
<trans-unit id="121" approved="no" sc:locked="false" sc:last-modified-date="2025-01-06T17:56:49.941Z" extradata="10">
<source xml:space="preserve" xml:lang="en">Walking with your dog in the park is a wonderful way to bond and stay active.</source>
<target state="translated" xml:space="preserve" xml:lang="es">Caminar con tu perro en el parque es una manera maravillosa de crear un vínculo y mantenerse activo.</target>
</trans-unit>
<trans-unit id="122" approved="no" sc:locked="false" sc:last-modified-date="2025-01-06T17:56:49.956Z" extradata="10">
<source xml:space="preserve" xml:lang="en">Training and socialization are essential for ensuring a well-behaved and happy dog.</source>
<target state="translated" xml:space="preserve" xml:lang="es">El entrenamiento y la socialización son esenciales para garantizar un perro bien educado y feliz.</target>
</trans-unit>
</body>
</file>
</xliff>

0 comments on commit 9a8684d

Please sign in to comment.