This commit is contained in:
1 parent
c6088aeb4b
commit
f12128a5c3
4 files changed
+136
-33
No files matched your search
@@ -132,9 +132,7 @@ func (t *XlsxTranslateTool) Execute(ctx context.Context, args json.RawMessage) (
|
||||
// ---- Translate ----
|
||||
runCtx, cancel := context.WithTimeout(ctx, t.timeout)
|
||||
defer cancel()
|
||||
translated, report, err := xlsxtranslate.Translate(runCtx, data, &xlsxtranslate.Options{
|
||||
BatchSize: 40,
|
||||
}, &chatBatchTranslator{model: t.model})
|
||||
translated, report, err := xlsxtranslate.Translate(runCtx, data, &xlsxtranslate.Options{}, &chatBatchTranslator{model: t.model})
|
||||
if err != nil {
|
||||
logger.Warnf(ctx, "[Tool][XlsxTranslate] session=%s file=%s failed: %v", sessionID, source, err)
|
||||
return &types.ToolResult{Success: false,
|
||||
@@ -300,15 +298,12 @@ func (c *chatBatchTranslator) TranslateBatch(ctx context.Context, texts []string
|
||||
}
|
||||
}
|
||||
user := fmt.Sprintf("Translate the following JSON array of strings to English. Return a JSON array with exactly %d items in the same order.\n\n%s", len(texts), payload)
|
||||
resp, err := c.model.Chat(ctx, []chat.Message{
|
||||
{Role: "system", Content: translateSystemPrompt},
|
||||
{Role: "user", Content: user},
|
||||
}, &chat.ChatOptions{Temperature: 0.2, MaxCompletionTokens: 16384})
|
||||
content, err := c.translateOnce(ctx, user)
|
||||
if err != nil {
|
||||
lastErr = err
|
||||
continue
|
||||
}
|
||||
out, err := parseTranslationArray(resp.Content, len(texts))
|
||||
out, err := parseTranslationArray(content, len(texts))
|
||||
if err != nil {
|
||||
lastErr = err
|
||||
continue
|
||||
@@ -318,6 +313,29 @@ func (c *chatBatchTranslator) TranslateBatch(ctx context.Context, texts []string
|
||||
return nil, fmt.Errorf("translation call failed after retries: %w", lastErr)
|
||||
}
|
||||
|
||||
// translateOnce issues one streaming batch call and returns the concatenated
|
||||
// answer. Streaming is deliberate: long batches then fall under the chat
|
||||
// client's stall-based timeout instead of a single non-stream HTTP timeout.
|
||||
func (c *chatBatchTranslator) translateOnce(ctx context.Context, user string) (string, error) {
|
||||
ch, err := c.model.ChatStream(ctx, []chat.Message{
|
||||
{Role: "system", Content: translateSystemPrompt},
|
||||
{Role: "user", Content: user},
|
||||
}, &chat.ChatOptions{Temperature: 0.2, MaxCompletionTokens: 16384})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
var content strings.Builder
|
||||
for chunk := range ch {
|
||||
switch chunk.ResponseType {
|
||||
case types.ResponseTypeError:
|
||||
return "", fmt.Errorf("%s", chunk.Content)
|
||||
default:
|
||||
content.WriteString(chunk.Content)
|
||||
}
|
||||
}
|
||||
return content.String(), nil
|
||||
}
|
||||
|
||||
// parseTranslationArray extracts the JSON array from a model response that
|
||||
// may wrap it in markdown fences or prose.
|
||||
func parseTranslationArray(content string, want int) ([]string, error) {
|
||||
|
||||
@@ -90,7 +90,19 @@ type fakeTranslateChat struct {
|
||||
prompts []string
|
||||
}
|
||||
|
||||
func (f *fakeTranslateChat) Chat(_ context.Context, messages []chat.Message, _ *chat.ChatOptions) (*types.ChatResponse, error) {
|
||||
func (f *fakeTranslateChat) Chat(ctx context.Context, messages []chat.Message, opts *chat.ChatOptions) (*types.ChatResponse, error) {
|
||||
resp, err := f.ChatStream(ctx, messages, opts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var content strings.Builder
|
||||
for chunk := range resp {
|
||||
content.WriteString(chunk.Content)
|
||||
}
|
||||
return &types.ChatResponse{Content: content.String()}, nil
|
||||
}
|
||||
|
||||
func (f *fakeTranslateChat) ChatStream(_ context.Context, messages []chat.Message, _ *chat.ChatOptions) (<-chan types.StreamResponse, error) {
|
||||
f.prompts = append(f.prompts, messages[len(messages)-1].Content)
|
||||
content := messages[len(messages)-1].Content
|
||||
start := strings.IndexByte(content, '[')
|
||||
@@ -104,11 +116,11 @@ func (f *fakeTranslateChat) Chat(_ context.Context, messages []chat.Message, _ *
|
||||
out = append(out, replaceCJKRuns(t))
|
||||
}
|
||||
b, _ := json.Marshal(out)
|
||||
return &types.ChatResponse{Content: string(b)}, nil
|
||||
}
|
||||
|
||||
func (f *fakeTranslateChat) ChatStream(_ context.Context, _ []chat.Message, _ *chat.ChatOptions) (<-chan types.StreamResponse, error) {
|
||||
return nil, fmt.Errorf("not implemented")
|
||||
ch := make(chan types.StreamResponse, 2)
|
||||
ch <- types.StreamResponse{ResponseType: types.ResponseTypeAnswer, Content: string(b)}
|
||||
ch <- types.StreamResponse{ResponseType: types.ResponseTypeAnswer, Done: true}
|
||||
close(ch)
|
||||
return ch, nil
|
||||
}
|
||||
|
||||
func (f *fakeTranslateChat) GetModelName() string { return "fake" }
|
||||
|
||||
Reference in new issue
Block a user