This commit is contained in:
1 parent
c6088aeb4b
commit
f12128a5c3
4 files changed
+136
-33
No files matched your search
@@ -8,6 +8,8 @@ import (
|
||||
"regexp"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -22,8 +24,12 @@ type Translator interface {
|
||||
// each field.
|
||||
type Options struct {
|
||||
// BatchSize is the number of strings sent to the LLM per call.
|
||||
// Default 40.
|
||||
// Default 20.
|
||||
BatchSize int
|
||||
// BatchConcurrency is how many LLM batches may run in parallel.
|
||||
// Default 4. Providers rate-limit; higher values queue on the chat
|
||||
// client's own concurrency gate.
|
||||
BatchConcurrency int
|
||||
// MaxUnitRunes caps a single translatable text unit. Longer units are
|
||||
// left untranslated and reported, so one oversized cell cannot blow up
|
||||
// an LLM call. Default 6000.
|
||||
@@ -40,7 +46,14 @@ func (o *Options) batchSize() int {
|
||||
if o != nil && o.BatchSize > 0 {
|
||||
return o.BatchSize
|
||||
}
|
||||
return 40
|
||||
return 20
|
||||
}
|
||||
|
||||
func (o *Options) batchConcurrency() int {
|
||||
if o != nil && o.BatchConcurrency > 0 {
|
||||
return o.BatchConcurrency
|
||||
}
|
||||
return 4
|
||||
}
|
||||
|
||||
func (o *Options) maxUnitRunes() int {
|
||||
@@ -266,36 +279,70 @@ func Translate(ctx context.Context, data []byte, opt *Options, tr Translator) ([
|
||||
todo = append(todo, src)
|
||||
}
|
||||
}
|
||||
for lo := 0; lo < len(todo); lo += opt.batchSize() {
|
||||
hi := lo + opt.batchSize()
|
||||
|
||||
// Batches run with bounded parallelism: a 63 MB workbook translates
|
||||
// several hundred units, and serial batches would spend far longer on
|
||||
// LLM latency than on everything else combined.
|
||||
batchSize := opt.batchSize()
|
||||
var batches [][]string
|
||||
for lo := 0; lo < len(todo); lo += batchSize {
|
||||
hi := lo + batchSize
|
||||
if hi > len(todo) {
|
||||
hi = len(todo)
|
||||
}
|
||||
batch := todo[lo:hi]
|
||||
masked := make([]string, len(batch))
|
||||
tokens := make([][]string, len(batch))
|
||||
for i, src := range batch {
|
||||
masked[i], tokens[i] = mask(src)
|
||||
batches = append(batches, todo[lo:hi])
|
||||
}
|
||||
batchOut := make([][]string, len(batches))
|
||||
batchTokens := make([][][]string, len(batches))
|
||||
batchErr := make([]error, len(batches))
|
||||
report.LLMCalls = len(batches)
|
||||
if concurrency := opt.batchConcurrency(); concurrency <= 1 || len(batches) <= 1 {
|
||||
for i, batch := range batches {
|
||||
batchOut[i], batchTokens[i], batchErr[i] = runBatch(ctx, tr, batch)
|
||||
}
|
||||
out, err := tr.TranslateBatch(ctx, masked)
|
||||
if err != nil {
|
||||
return nil, report, fmt.Errorf("LLM batch %d failed: %w", lo/opt.batchSize()+1, err)
|
||||
} else {
|
||||
sem := make(chan struct{}, concurrency)
|
||||
var failed atomic.Bool
|
||||
runCtx, cancel := context.WithCancel(ctx)
|
||||
defer cancel()
|
||||
var wg sync.WaitGroup
|
||||
for i := range batches {
|
||||
wg.Add(1)
|
||||
go func(i int) {
|
||||
defer wg.Done()
|
||||
if failed.Load() {
|
||||
return // a sibling already failed; stop burning provider calls
|
||||
}
|
||||
sem <- struct{}{}
|
||||
defer func() { <-sem }()
|
||||
out, tokens, err := runBatch(runCtx, tr, batches[i])
|
||||
batchOut[i], batchTokens[i], batchErr[i] = out, tokens, err
|
||||
if err != nil {
|
||||
failed.Store(true)
|
||||
cancel()
|
||||
}
|
||||
}(i)
|
||||
}
|
||||
if len(out) != len(batch) {
|
||||
wg.Wait()
|
||||
}
|
||||
for i, batch := range batches {
|
||||
if batchErr[i] != nil {
|
||||
return nil, report, fmt.Errorf("LLM batch %d failed: %w", i+1, batchErr[i])
|
||||
}
|
||||
if len(batchOut[i]) != len(batch) {
|
||||
return nil, report, fmt.Errorf("LLM batch %d returned %d results for %d inputs",
|
||||
lo/opt.batchSize()+1, len(out), len(batch))
|
||||
i+1, len(batchOut[i]), len(batch))
|
||||
}
|
||||
report.LLMCalls++
|
||||
for i, dst := range out {
|
||||
restored, miss := unmask(dst, tokens[i])
|
||||
for j, dst := range batchOut[i] {
|
||||
restored, miss := unmask(dst, batchTokens[i][j])
|
||||
if len(miss) > 0 {
|
||||
report.Warnings = append(report.Warnings, fmt.Sprintf(
|
||||
"译文存在未能回填的占位符,请人工复核:%s", truncate(batch[i], 40)))
|
||||
"译文存在未能回填的占位符,请人工复核:%s", truncate(batch[j], 40)))
|
||||
}
|
||||
if opt.normalizePunctuation() {
|
||||
restored = normalizePunctuation(restored)
|
||||
}
|
||||
translations[batch[i]] = restored
|
||||
translations[batch[j]] = restored
|
||||
}
|
||||
}
|
||||
report.AppliedTranslations = len(translations)
|
||||
@@ -497,6 +544,24 @@ func readZip(f *zip.File) ([]byte, error) {
|
||||
return buf.Bytes(), nil
|
||||
}
|
||||
|
||||
// runBatch masks one batch and hands it to the translator, returning the
|
||||
// translations together with the per-string placeholder tokens for unmasking.
|
||||
func runBatch(ctx context.Context, tr Translator, batch []string) ([]string, [][]string, error) {
|
||||
masked := make([]string, len(batch))
|
||||
tokens := make([][]string, len(batch))
|
||||
for i, src := range batch {
|
||||
masked[i], tokens[i] = mask(src)
|
||||
}
|
||||
out, err := tr.TranslateBatch(ctx, masked)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
if len(out) != len(batch) {
|
||||
return nil, nil, fmt.Errorf("translator returned %d results for %d inputs", len(out), len(batch))
|
||||
}
|
||||
return out, tokens, nil
|
||||
}
|
||||
|
||||
func isSheetXML(name string) bool {
|
||||
return strings.HasPrefix(name, "xl/worksheets/sheet") && strings.HasSuffix(name, ".xml")
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user