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.

Detect incompatible models that don't support function calling

rcourtman11a72ee2633e
diff --git a/internal/ai/providers/openai.go b/internal/ai/providers/openai.go
index 80b760da4b..b1e79d1b45 100644
--- a/internal/ai/providers/openai.go
+++ b/internal/ai/providers/openai.go
@@ -695,6 +695,7 @@ func (c *OpenAIClient) ChatStream(ctx context.Context, req ChatRequest, callback
})
var inputTokens, outputTokens int
var finishReason string
+ var contentBuffer strings.Builder // Accumulate content to detect incompatible model output
for {
n, err := reader.Read(buf)
@@ -776,6 +777,20 @@ func (c *OpenAIClient) ChatStream(ctx context.Context, req ChatRequest, callback
// Regular content
if delta.Content != "" {
+ // Accumulate content to detect control tokens from incompatible models
+ contentBuffer.WriteString(delta.Content)
+
+ // Check for LLM control tokens that indicate the model doesn't support function calling
+ // These tokens appear when local models (LM Studio, llama.cpp) receive tool definitions
+ // but don't know how to handle them properly
+ accumulated := contentBuffer.String()
+ if containsControlTokens(accumulated) {
+ return fmt.Errorf("model_incompatible: This model appears to not support function calling. " +
+ "It's outputting internal control tokens instead of proper responses. " +
+ "Please use a model that supports OpenAI-compatible function calling " +
+ "(e.g., Llama 3.1+, Mistral, Qwen 2.5, or newer instruction-tuned models)")
+ }
+
callback(StreamEvent{
Type: "content",
Data: ContentEvent{Text: delta.Content},
@@ -884,3 +899,29 @@ func (c *OpenAIClient) ListModels(ctx context.Context) ([]ModelInfo, error) {
return models, nil
}
+
+// containsControlTokens checks if the content contains LLM control tokens
+// that indicate the model doesn't support function calling properly.
+// These tokens are internal to models and should never appear in output.
+func containsControlTokens(content string) bool {
+ // Common control tokens from various LLM architectures
+ controlTokens := []string{
+ "<|channel|>", // Tool/function calling control
+ "<|constrain|>", // Output constraint control
+ "<|message|>", // Message boundary control
+ "<|im_start|>", // ChatML start token
+ "<|im_end|>", // ChatML end token
+ "<|eot_id|>", // Llama end of turn
+ "<|start_header_id|>", // Llama header
+ "<|end_header_id|>", // Llama header end
+ "<tool_call>", // Some models' tool call marker
+ "[TOOL_CALLS]", // Alternative tool call marker
+ }
+
+ for _, token := range controlTokens {
+ if strings.Contains(content, token) {
+ return true
+ }
+ }
+ return false
+}
diff --git a/internal/ai/providers/openai_test.go b/internal/ai/providers/openai_test.go
index 0f7581cbdf..d0192097c0 100644
--- a/internal/ai/providers/openai_test.go
+++ b/internal/ai/providers/openai_test.go
@@ -299,3 +299,117 @@ func TestOpenAIClient_TestConnection(t *testing.T) {
err := client.TestConnection(context.Background())
require.NoError(t, err)
}
+
+func TestContainsControlTokens(t *testing.T) {
+ tests := []struct {
+ name string
+ content string
+ expected bool
+ }{
+ {
+ name: "normal content",
+ content: "Hello, how can I help you today?",
+ expected: false,
+ },
+ {
+ name: "channel control token",
+ content: "Sure, I'll help. <|channel|> tool_call",
+ expected: true,
+ },
+ {
+ name: "constrain control token",
+ content: "<|constrain|>json",
+ expected: true,
+ },
+ {
+ name: "message control token",
+ content: "<|message|>assistant",
+ expected: true,
+ },
+ {
+ name: "im_start token",
+ content: "<|im_start|>assistant\nHello",
+ expected: true,
+ },
+ {
+ name: "im_end token",
+ content: "Hello<|im_end|>",
+ expected: true,
+ },
+ {
+ name: "llama eot token",
+ content: "Response complete<|eot_id|>",
+ expected: true,
+ },
+ {
+ name: "llama header tokens",
+ content: "<|start_header_id|>assistant<|end_header_id|>",
+ expected: true,
+ },
+ {
+ name: "tool_call xml marker",
+ content: "<tool_call>get_weather</tool_call>",
+ expected: true,
+ },
+ {
+ name: "TOOL_CALLS marker",
+ content: "[TOOL_CALLS] get_weather",
+ expected: true,
+ },
+ {
+ name: "code with angle brackets is fine",
+ content: "Here's some code: if (a < b && c > d) { return true; }",
+ expected: false,
+ },
+ {
+ name: "html-like content is fine",
+ content: "<div>Hello</div>",
+ expected: false,
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ result := containsControlTokens(tt.content)
+ assert.Equal(t, tt.expected, result, "containsControlTokens(%q)", tt.content)
+ })
+ }
+}
+
+func TestOpenAIClient_ChatStream_ControlTokenDetection(t *testing.T) {
+ // Mock a server that returns control tokens (simulating an incompatible model)
+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ w.Header().Set("Content-Type", "text/event-stream")
+ w.WriteHeader(http.StatusOK)
+
+ // Simulate a model that outputs control tokens instead of proper tool calls
+ events := []string{
+ `{"id":"chatcmpl-1","choices":[{"delta":{"content":"I'll help you. "}}]}`,
+ `{"id":"chatcmpl-1","choices":[{"delta":{"content":"<|channel|>"}}]}`,
+ `{"id":"chatcmpl-1","choices":[{"delta":{"content":" tool_use"}}]}`,
+ `[DONE]`,
+ }
+
+ for _, event := range events {
+ fmt.Fprintf(w, "data: %s\n\n", event)
+ w.(http.Flusher).Flush()
+ time.Sleep(10 * time.Millisecond)
+ }
+ }))
+ defer server.Close()
+
+ client := NewOpenAIClient("sk-test", "local-model", server.URL, 0)
+
+ callback := func(event StreamEvent) {
+ // We shouldn't get many events before the error
+ }
+
+ req := ChatRequest{
+ Messages: []Message{{Role: "user", Content: "What's the weather?"}},
+ }
+
+ err := client.ChatStream(context.Background(), req, callback)
+ require.Error(t, err)
+ assert.Contains(t, err.Error(), "model_incompatible")
+ assert.Contains(t, err.Error(), "function calling")
+}