set two options for translating

This commit is contained in:
Daniel Goc
2026-03-16 11:12:17 +01:00
parent 56a1495a0b
commit 7aa16c644f
19 changed files with 115 additions and 6788 deletions

View File

@@ -6,15 +6,19 @@ import (
"fmt"
"io"
"log"
"net/http"
"os"
"slices"
"strings"
"time"
"git.ma-al.com/goc_daniel/b2b/app/config"
"git.ma-al.com/goc_daniel/b2b/app/db"
"git.ma-al.com/goc_daniel/b2b/app/model"
"git.ma-al.com/goc_daniel/b2b/app/service/langsService"
"git.ma-al.com/goc_daniel/b2b/app/utils/responseErrors"
"github.com/openai/openai-go/responses"
"github.com/openai/openai-go/v3"
"google.golang.org/api/option"
"gorm.io/gorm"
@@ -29,6 +33,7 @@ type ProductDescriptionService struct {
db *gorm.DB
ctx context.Context
googleCli translate.TranslationClient
client openai.Client
// projectID is the Google Cloud project ID used as the "parent" in API calls,
// e.g. "projects/my-project-123/locations/global"
projectID string
@@ -70,9 +75,13 @@ func New() *ProductDescriptionService {
log.Fatalf("productDescriptionService: cannot create Translation client: %v", err)
}
client := openai.NewClient(option.WithAPIKey("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"),
option.WithHTTPClient(&http.Client{Timeout: 300 * time.Second})) // five minutes timeout
return &ProductDescriptionService{
db: db.Get(),
ctx: ctx,
client: client,
googleCli: *googleCli,
projectID: cfg.GoogleTranslate.ProjectID,
}
@@ -152,7 +161,7 @@ func (s *ProductDescriptionService) SaveProductDescription(userID uint, productI
//
// The Google Cloud project must have the Cloud Translation API enabled and the
// service account must hold the "Cloud Translation API User" role.
func (s *ProductDescriptionService) TranslateProductDescription(userID uint, productID uint, productShopID uint, productFromLangID uint, productToLangID uint) (*model.ProductDescription, error) {
func (s *ProductDescriptionService) TranslateProductDescription(userID uint, productID uint, productShopID uint, productFromLangID uint, productToLangID uint, model string) (*model.ProductDescription, error) {
var ProductDescription model.ProductDescription
err := s.db.
@@ -189,69 +198,69 @@ func (s *ProductDescriptionService) TranslateProductDescription(userID uint, pro
"translation_of_product_usage",
}
// request := "Translate to " + lang.ISOCode + " without changing the html structure.\n"
request := ""
if model == "OpenAI" {
request = "Translate to " + lang.ISOCode + " without changing the html structure.\n"
}
for i := 0; i < len(keys); i++ {
request += "\n<" + keys[i] + ">"
request += *fields[i]
request += "</" + keys[i] + ">\n"
}
request = cleanForPrompt(request)
// TranslateText is the standard Cloud Translation v3 endpoint.
// "parent" must be "projects/<PROJECT_ID>/locations/global" (or a specific region).
// MimeType "text/plain" is used because cleanForPrompt strips HTML attributes;
// switch to "text/html" if you want Google to preserve HTML tags automatically.
req := &translatepb.TranslateTextRequest{
Parent: fmt.Sprintf("projects/%s/locations/global", s.projectID),
TargetLanguageCode: lang.ISOCode,
MimeType: "text/html",
Contents: []string{request},
}
responseGoogle, err := s.googleCli.TranslateText(s.ctx, req)
if err != nil {
fmt.Println(err)
return nil, err
if model == "OpenAI" {
request = cleanForPrompt(request)
}
// TranslateText returns one Translation per input string.
if len(responseGoogle.GetTranslations()) == 0 {
return nil, responseErrors.ErrAIBadOutput
}
response := responseGoogle.GetTranslations()[0].GetTranslatedText()
if model == "OpenAI" {
openai_response, _ := s.client.Responses.New(context.Background(), responses.ResponseNewParams{
Input: responses.ResponseNewParamsInputUnion{OfString: openai.String(request)},
Model: openai.ChatModelGPT4_1Mini,
// Model: openai.ChatModelGPT4_1Nano,
})
if openai_response.Status != "completed" {
return nil, responseErrors.ErrAIResponseFail
}
response := openai_response.OutputText()
for i := 0; i < len(keys); i++ {
success, resolution := resolveResponse(*fields[i], response, keys[i])
if !success {
for i := 0; i < len(keys); i++ {
success, resolution := resolveResponse(*fields[i], response, keys[i])
if !success {
return nil, responseErrors.ErrAIBadOutput
}
*fields[i] = resolution
}
} else if model == "Google" {
// TranslateText is the standard Cloud Translation v3 endpoint.
req := &translatepb.TranslateTextRequest{
Parent: fmt.Sprintf("projects/%s/locations/global", s.projectID),
TargetLanguageCode: lang.ISOCode,
MimeType: "text/html",
Contents: []string{request},
}
responseGoogle, err := s.googleCli.TranslateText(s.ctx, req)
if err != nil {
return nil, err
}
// TranslateText returns one Translation per input string.
if len(responseGoogle.GetTranslations()) == 0 {
return nil, responseErrors.ErrAIBadOutput
}
*fields[i] = resolution
response := responseGoogle.GetTranslations()[0].GetTranslatedText()
for i := 0; i < len(keys); i++ {
success, match := GetStringInBetween(response, "<"+keys[i]+">", "</"+keys[i]+">")
if !success || !isValidXHTML(match) {
return nil, responseErrors.ErrAIBadOutput
}
*fields[i] = match
}
}
return &ProductDescription, nil
}
// isValidXHTML checks if the string obeys the XHTML format
func isValidXHTML(s string) bool {
r := strings.NewReader(s)
d := xml.NewDecoder(r)
// Configure the decoder for HTML; leave off strict and autoclose for XHTML
d.Strict = true
d.AutoClose = xml.HTMLAutoClose
d.Entity = xml.HTMLEntity
for {
_, err := d.Token()
switch err {
case io.EOF:
return true // We're done, it's valid!
case nil:
default:
return false // Oops, something wasn't right
}
}
}
func cleanForPrompt(s string) string {
r := strings.NewReader(s)
d := xml.NewDecoder(r)
@@ -321,6 +330,27 @@ func GetStringInBetween(str string, start string, end string) (success bool, res
return true, str[s : s+e]
}
// isValidXHTML checks if the string obeys the XHTML format
func isValidXHTML(s string) bool {
r := strings.NewReader(s)
d := xml.NewDecoder(r)
// Configure the decoder for HTML; leave off strict and autoclose for XHTML
d.Strict = true
d.AutoClose = xml.HTMLAutoClose
d.Entity = xml.HTMLEntity
for {
_, err := d.Token()
switch err {
case io.EOF:
return true // We're done, it's valid!
case nil:
default:
return false // Oops, something wasn't right
}
}
}
// Rebuilds HTML using the original HTML as a template and the response as a source
// Assumes that both original and response have the exact same XML structure
func RebuildFromResponse(s_original string, s_response string) (bool, string) {