slight improvements

This commit is contained in:
Daniel Goc
2026-03-13 14:28:21 +01:00
parent d8a2e26896
commit 6ff47ce1c4
3 changed files with 33 additions and 85 deletions

View File

@@ -113,6 +113,7 @@ func (s *ProductDescriptionService) TranslateProductDescription(userID uint, pro
if err != nil {
return nil, fmt.Errorf("database error: %w", err)
}
ProductDescription.LangID = productToLangID
// we translate all changeable fields, and we keep the exact same HTML structure in relevant fields.
lang, err := langsService.LangSrv.GetLanguageById(productToLangID)
@@ -120,42 +121,34 @@ func (s *ProductDescriptionService) TranslateProductDescription(userID uint, pro
return nil, err
}
request := "Translate to " + lang.ISOCode + " without changing the html structure."
request += "\n\n<translation_of_product_description>"
request += ProductDescription.Description
request += "</translation_of_product_description>\n\n"
request += "Remember: translate to " + lang.ISOCode + " without changing the html structure."
request += "\n\n<translation_of_product_short_description>"
request += ProductDescription.DescriptionShort
request += "</translation_of_product_short_description>\n\n"
request += "Remember: translate to " + lang.ISOCode + " without changing the html structure."
request += "\n\n<translation_of_product_meta_description>"
request += ProductDescription.MetaDescription
request += "</translation_of_product_meta_description>\n\n"
request += "Remember: translate to " + lang.ISOCode + " without changing the html structure."
request += "\n\n<translation_of_product_meta_title>"
request += ProductDescription.MetaTitle
request += "</translation_of_product_meta_title>\n\n"
request += "Remember: translate to " + lang.ISOCode + " without changing the html structure."
request += "\n\n<translation_of_product_name>"
request += ProductDescription.Name
request += "</translation_of_product_name>\n\n"
request += "Remember: translate to " + lang.ISOCode + " without changing the html structure."
request += "\n\n<translation_of_display_text_available_now>"
request += ProductDescription.AvailableNow
request += "</translation_of_display_text_available_now>\n\n"
request += "Remember: translate to " + lang.ISOCode + " without changing the html structure."
request += "\n\n<translation_of_display_text_available_later>"
request += ProductDescription.AvailableLater
request += "</translation_of_display_text_available_later>\n\n"
request += "Remember: translate to " + lang.ISOCode + " without changing the html structure."
request += "\n\n<translation_of_product_usage>"
request += ProductDescription.Usage
request += "</translation_of_product_usage>"
fields := []*string{&ProductDescription.Description,
&ProductDescription.DescriptionShort,
&ProductDescription.MetaDescription,
&ProductDescription.MetaTitle,
&ProductDescription.Name,
&ProductDescription.AvailableNow,
&ProductDescription.AvailableLater,
&ProductDescription.Usage,
}
keys := []string{"translation_of_product_description",
"translation_of_product_short_description",
"translation_of_product_meta_description",
"translation_of_product_meta_title",
"translation_of_product_name",
"translation_of_product_available_now",
"translation_of_product_available_later",
"translation_of_product_usage",
}
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)
openai_response, err := s.client.Responses.New(context.Background(), responses.ResponseNewParams{
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,
@@ -163,59 +156,14 @@ func (s *ProductDescriptionService) TranslateProductDescription(userID uint, pro
if openai_response.Status != "completed" {
return nil, responseErrors.ErrOpenAIResponseFail
}
output := openai_response.OutputText()
// for testing purposes
// fi, err := os.ReadFile("/home/daniel/coding/work/b2b/app/service/productDescriptionService/test_out.txt") // just pass the file name
// output := string(fi)
success, resolution := resolveResponse(ProductDescription.Description, output, "translation_of_product_description")
if !success {
return nil, responseErrors.ErrOpenAIBadOutput
for i := 0; i < len(keys); i++ {
success, resolution := resolveResponse(*fields[i], openai_response.OutputText(), keys[i])
if !success {
return nil, responseErrors.ErrOpenAIBadOutput
}
*fields[i] = resolution
}
ProductDescription.Description = resolution
success, resolution = resolveResponse(ProductDescription.DescriptionShort, output, "translation_of_product_short_description")
if !success {
return nil, responseErrors.ErrOpenAIBadOutput
}
ProductDescription.DescriptionShort = resolution
success, resolution = resolveResponse(ProductDescription.MetaDescription, output, "translation_of_product_meta_description")
if !success {
return nil, responseErrors.ErrOpenAIBadOutput
}
ProductDescription.MetaDescription = resolution
success, resolution = resolveResponse(ProductDescription.MetaTitle, output, "translation_of_product_meta_title")
if !success {
return nil, responseErrors.ErrOpenAIBadOutput
}
ProductDescription.MetaTitle = resolution
success, resolution = resolveResponse(ProductDescription.Name, output, "translation_of_product_name")
if !success {
return nil, responseErrors.ErrOpenAIBadOutput
}
ProductDescription.Name = resolution
success, resolution = resolveResponse(ProductDescription.AvailableNow, output, "translation_of_display_text_available_now")
if !success {
return nil, responseErrors.ErrOpenAIBadOutput
}
ProductDescription.AvailableNow = resolution
success, resolution = resolveResponse(ProductDescription.AvailableLater, output, "translation_of_display_text_available_later")
if !success {
return nil, responseErrors.ErrOpenAIBadOutput
}
ProductDescription.AvailableLater = resolution
success, resolution = resolveResponse(ProductDescription.Usage, output, "translation_of_product_usage")
if !success {
return nil, responseErrors.ErrOpenAIBadOutput
}
ProductDescription.Usage = resolution
return &ProductDescription, nil
}