Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

Sanitize LLM control tokens from OpenAI-compatible responses

rcourtmane5eb15918ec3
diff --git a/internal/ai/providers/openai.go b/internal/ai/providers/openai.go
index 80b760da4b..7035afe239 100644
--- a/internal/ai/providers/openai.go
+++ b/internal/ai/providers/openai.go
@@ -8,12 +8,23 @@ import (
"io"
"net/http"
"net/url"
+ "regexp"
"strings"
"time"
"github.com/rs/zerolog/log"
)
+// controlTokenPattern matches common LLM control tokens that shouldn't appear in output
+// These are internal tokens used by some models (llama.cpp, etc.) for chat templating
+var controlTokenPattern = regexp.MustCompile(`<\|[a-zA-Z_]+\|>`)
+
+// sanitizeContent removes LLM control tokens from content that shouldn't be shown to users
+func sanitizeContent(content string) string {
+ // Remove control tokens like <|channel|>, <|constrain|>, <|message|>, etc.
+ return controlTokenPattern.ReplaceAllString(content, "")
+}
+
const (
openaiAPIURL = "https://api.openai.com/v1/chat/completions"
openaiMaxRetries = 3
@@ -425,6 +436,9 @@ func (c *OpenAIClient) Chat(ctx context.Context, req ChatRequest) (*ChatResponse
contentToUse = choice.Message.ReasoningContent
}
+ // Sanitize content to remove any leaked control tokens from local models
+ contentToUse = sanitizeContent(contentToUse)
+
result := &ChatResponse{
Content: contentToUse,
ReasoningContent: choice.Message.ReasoningContent, // DeepSeek thinking mode
@@ -774,12 +788,15 @@ func (c *OpenAIClient) ChatStream(ctx context.Context, req ChatRequest, callback
delta := choice.Delta
- // Regular content
+ // Regular content - sanitize to remove any leaked control tokens
if delta.Content != "" {
- callback(StreamEvent{
- Type: "content",
- Data: ContentEvent{Text: delta.Content},
- })
+ sanitized := sanitizeContent(delta.Content)
+ if sanitized != "" {
+ callback(StreamEvent{
+ Type: "content",
+ Data: ContentEvent{Text: sanitized},
+ })
+ }
}
// Reasoning content (DeepSeek)