mirror of
https://github.com/mudler/LocalAI.git
synced 2026-06-07 08:16:53 -04:00
* fix(openresponses): populate Content and accept bare {role,content} items (#10039)
Fixes mudler/LocalAI#10039 — `/v1/responses` silently returned empty
output on any model whose YAML doesn't include a Go-side
`template.chat_message` block.
Three cooperating bugs:
* `convertORInputToMessages` populated only `StringContent` for string
input and for the `input.Instructions` system message, leaving the
`Content` (any) field nil.
* `TemplateMessages` gated all fallback content-rendering branches on
`Content != nil && StringContent != ""` — but every branch in that
function consumes `StringContent`, not `Content`. The `&&` silently
dropped messages that had StringContent set and Content nil, producing
an empty prompt that the 5× empty-retry guard then turned into a
200 OK with `output: []`.
* The array-input branch of `convertORInputToMessages` dispatched on
`itemMap["type"]` with no default, dropping bare `{role, content}`
items emitted by the OpenAI Python SDK helper
`client.responses.create(input=[{...}])`.
Fix:
* Set both `Content` and `StringContent` in the two openresponses
message-construction sites that only set one.
* Treat a bare `{role, content}` item (no `type`) as
`type: "message"` for OpenAI-SDK compatibility.
* Gate `TemplateMessages` fallback rendering on `StringContent != ""`,
which is what every downstream branch in that function actually
reads.
Regression test added to `evaluator_test.go` covering the fallback
path (no `ChatMessage` template) with a StringContent-only message,
both with and without a role mapping.
* test(openresponses): guard Content population and ToProto path (#10039)
Add regression tests for the two seams the original fix touched but
left uncovered:
* convertORInputToMessages must populate both Content and StringContent
for plain string input and for bare {role, content} array items (the
OpenAI SDK shape that omits the type discriminator). Both are
functional reds against the pre-fix code.
* Messages.ToProto reads Content, not StringContent — this is the path
UseTokenizerTemplate backends (imported GGUFs) take. The cases pin
that contract so a future regression on the producer side is caught.
Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
Assisted-by: Claude:claude-opus-4-7 [Claude Code]
---------
Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
Co-authored-by: Ettore Di Giacinto <mudler@localai.io>
259 lines
8.0 KiB
Go
259 lines
8.0 KiB
Go
package templates_test
|
|
|
|
import (
|
|
"github.com/mudler/LocalAI/core/config"
|
|
"github.com/mudler/LocalAI/core/schema"
|
|
. "github.com/mudler/LocalAI/core/templates"
|
|
"github.com/mudler/LocalAI/pkg/functions"
|
|
|
|
. "github.com/onsi/ginkgo/v2"
|
|
. "github.com/onsi/gomega"
|
|
)
|
|
|
|
const toolCallJinja = `{{ '<|begin_of_text|>' }}{% if messages[0]['role'] == 'system' %}{% set system_message = messages[0]['content'] %}{% endif %}{% if system_message is defined %}{{ '<|start_header_id|>system<|end_header_id|>
|
|
|
|
' + system_message + '<|eot_id|>' }}{% endif %}{% for message in messages %}{% set content = message['content'] %}{% if message['role'] == 'user' %}{{ '<|start_header_id|>user<|end_header_id|>
|
|
|
|
' + content + '<|eot_id|><|start_header_id|>assistant<|end_header_id|>
|
|
|
|
' }}{% elif message['role'] == 'assistant' %}{{ content + '<|eot_id|>' }}{% endif %}{% endfor %}`
|
|
|
|
const chatML = `<|im_start|>{{if eq .RoleName "assistant"}}assistant{{else if eq .RoleName "system"}}system{{else if eq .RoleName "tool"}}tool{{else if eq .RoleName "user"}}user{{end}}
|
|
{{- if .FunctionCall }}
|
|
<tool_call>
|
|
{{- else if eq .RoleName "tool" }}
|
|
<tool_response>
|
|
{{- end }}
|
|
{{- if .Content}}
|
|
{{.Content }}
|
|
{{- end }}
|
|
{{- if .FunctionCall}}
|
|
{{toJson .FunctionCall}}
|
|
{{- end }}
|
|
{{- if .FunctionCall }}
|
|
</tool_call>
|
|
{{- else if eq .RoleName "tool" }}
|
|
</tool_response>
|
|
{{- end }}<|im_end|>`
|
|
|
|
const llama3 = `<|start_header_id|>{{if eq .RoleName "assistant"}}assistant{{else if eq .RoleName "system"}}system{{else if eq .RoleName "tool"}}tool{{else if eq .RoleName "user"}}user{{end}}<|end_header_id|>
|
|
|
|
{{ if .FunctionCall -}}
|
|
Function call:
|
|
{{ else if eq .RoleName "tool" -}}
|
|
Function response:
|
|
{{ end -}}
|
|
{{ if .Content -}}
|
|
{{.Content -}}
|
|
{{ else if .FunctionCall -}}
|
|
{{ toJson .FunctionCall -}}
|
|
{{ end -}}
|
|
<|eot_id|>`
|
|
|
|
var llama3TestMatch map[string]map[string]any = map[string]map[string]any{
|
|
"user": {
|
|
"expected": "<|start_header_id|>user<|end_header_id|>\n\nA long time ago in a galaxy far, far away...<|eot_id|>",
|
|
"config": &config.ModelConfig{
|
|
TemplateConfig: config.TemplateConfig{
|
|
ChatMessage: llama3,
|
|
},
|
|
},
|
|
"functions": []functions.Function{},
|
|
"shouldUseFn": false,
|
|
"messages": []schema.Message{
|
|
{
|
|
Role: "user",
|
|
StringContent: "A long time ago in a galaxy far, far away...",
|
|
},
|
|
},
|
|
},
|
|
"assistant": {
|
|
"expected": "<|start_header_id|>assistant<|end_header_id|>\n\nA long time ago in a galaxy far, far away...<|eot_id|>",
|
|
"config": &config.ModelConfig{
|
|
TemplateConfig: config.TemplateConfig{
|
|
ChatMessage: llama3,
|
|
},
|
|
},
|
|
"functions": []functions.Function{},
|
|
"messages": []schema.Message{
|
|
{
|
|
Role: "assistant",
|
|
StringContent: "A long time ago in a galaxy far, far away...",
|
|
},
|
|
},
|
|
"shouldUseFn": false,
|
|
},
|
|
"function_call": {
|
|
|
|
"expected": "<|start_header_id|>assistant<|end_header_id|>\n\nFunction call:\n{\"function\":\"test\"}<|eot_id|>",
|
|
"config": &config.ModelConfig{
|
|
TemplateConfig: config.TemplateConfig{
|
|
ChatMessage: llama3,
|
|
},
|
|
},
|
|
"functions": []functions.Function{},
|
|
"messages": []schema.Message{
|
|
{
|
|
Role: "assistant",
|
|
FunctionCall: map[string]string{"function": "test"},
|
|
},
|
|
},
|
|
"shouldUseFn": false,
|
|
},
|
|
"function_response": {
|
|
"expected": "<|start_header_id|>tool<|end_header_id|>\n\nFunction response:\nResponse from tool<|eot_id|>",
|
|
"config": &config.ModelConfig{
|
|
TemplateConfig: config.TemplateConfig{
|
|
ChatMessage: llama3,
|
|
},
|
|
},
|
|
"functions": []functions.Function{},
|
|
"messages": []schema.Message{
|
|
{
|
|
Role: "tool",
|
|
StringContent: "Response from tool",
|
|
},
|
|
},
|
|
"shouldUseFn": false,
|
|
},
|
|
}
|
|
|
|
var chatMLTestMatch map[string]map[string]any = map[string]map[string]any{
|
|
"user": {
|
|
"expected": "<|im_start|>user\nA long time ago in a galaxy far, far away...<|im_end|>",
|
|
"config": &config.ModelConfig{
|
|
TemplateConfig: config.TemplateConfig{
|
|
ChatMessage: chatML,
|
|
},
|
|
},
|
|
"functions": []functions.Function{},
|
|
"shouldUseFn": false,
|
|
"messages": []schema.Message{
|
|
{
|
|
Role: "user",
|
|
StringContent: "A long time ago in a galaxy far, far away...",
|
|
},
|
|
},
|
|
},
|
|
"assistant": {
|
|
"expected": "<|im_start|>assistant\nA long time ago in a galaxy far, far away...<|im_end|>",
|
|
"config": &config.ModelConfig{
|
|
TemplateConfig: config.TemplateConfig{
|
|
ChatMessage: chatML,
|
|
},
|
|
},
|
|
"functions": []functions.Function{},
|
|
"messages": []schema.Message{
|
|
{
|
|
Role: "assistant",
|
|
StringContent: "A long time ago in a galaxy far, far away...",
|
|
},
|
|
},
|
|
"shouldUseFn": false,
|
|
},
|
|
"function_call": {
|
|
"expected": "<|im_start|>assistant\n<tool_call>\n{\"function\":\"test\"}\n</tool_call><|im_end|>",
|
|
"config": &config.ModelConfig{
|
|
TemplateConfig: config.TemplateConfig{
|
|
ChatMessage: chatML,
|
|
},
|
|
},
|
|
"functions": []functions.Function{
|
|
{
|
|
Name: "test",
|
|
Description: "test",
|
|
Parameters: nil,
|
|
},
|
|
},
|
|
"shouldUseFn": true,
|
|
"messages": []schema.Message{
|
|
{
|
|
Role: "assistant",
|
|
FunctionCall: map[string]string{"function": "test"},
|
|
},
|
|
},
|
|
},
|
|
"function_response": {
|
|
"expected": "<|im_start|>tool\n<tool_response>\nResponse from tool\n</tool_response><|im_end|>",
|
|
"config": &config.ModelConfig{
|
|
TemplateConfig: config.TemplateConfig{
|
|
ChatMessage: chatML,
|
|
},
|
|
},
|
|
"functions": []functions.Function{},
|
|
"shouldUseFn": false,
|
|
"messages": []schema.Message{
|
|
{
|
|
Role: "tool",
|
|
StringContent: "Response from tool",
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
var _ = Describe("Templates", func() {
|
|
Context("chat message ChatML", func() {
|
|
var evaluator *Evaluator
|
|
BeforeEach(func() {
|
|
evaluator = NewEvaluator("")
|
|
})
|
|
for key := range chatMLTestMatch {
|
|
foo := chatMLTestMatch[key]
|
|
It("renders correctly `"+key+"`", func() {
|
|
templated := evaluator.TemplateMessages(schema.OpenAIRequest{}, foo["messages"].([]schema.Message), foo["config"].(*config.ModelConfig), foo["functions"].([]functions.Function), foo["shouldUseFn"].(bool))
|
|
Expect(templated).To(Equal(foo["expected"]), templated)
|
|
})
|
|
}
|
|
})
|
|
Context("chat message llama3", func() {
|
|
var evaluator *Evaluator
|
|
BeforeEach(func() {
|
|
evaluator = NewEvaluator("")
|
|
})
|
|
for key := range llama3TestMatch {
|
|
foo := llama3TestMatch[key]
|
|
It("renders correctly `"+key+"`", func() {
|
|
templated := evaluator.TemplateMessages(schema.OpenAIRequest{}, foo["messages"].([]schema.Message), foo["config"].(*config.ModelConfig), foo["functions"].([]functions.Function), foo["shouldUseFn"].(bool))
|
|
Expect(templated).To(Equal(foo["expected"]), templated)
|
|
})
|
|
}
|
|
})
|
|
// Regression test for mudler/LocalAI#10039: when a model has no Go-side
|
|
// TemplateConfig.ChatMessage block (e.g. backends that rely on the GGUF's
|
|
// jinja template), TemplateMessages falls through to the role-prefix path.
|
|
// That path must still render messages whose StringContent is populated but
|
|
// Content (any) is nil — which is the shape /v1/responses produced before
|
|
// the fix to convertORInputToMessages.
|
|
Context("fallback path with StringContent-only message (no ChatMessage template)", func() {
|
|
var evaluator *Evaluator
|
|
BeforeEach(func() {
|
|
evaluator = NewEvaluator("")
|
|
})
|
|
It("renders the role prefix and content when only StringContent is set", func() {
|
|
cfg := &config.ModelConfig{
|
|
TemplateConfig: config.TemplateConfig{},
|
|
Roles: map[string]string{"user": "USER: "},
|
|
}
|
|
messages := []schema.Message{
|
|
{
|
|
Role: "user",
|
|
StringContent: "hello",
|
|
// Content intentionally left nil — reproduces /v1/responses string-input.
|
|
},
|
|
}
|
|
templated := evaluator.TemplateMessages(schema.OpenAIRequest{}, messages, cfg, []functions.Function{}, false)
|
|
Expect(templated).To(Equal("USER: hello"), templated)
|
|
})
|
|
It("renders content even with no role mapping", func() {
|
|
cfg := &config.ModelConfig{
|
|
TemplateConfig: config.TemplateConfig{},
|
|
}
|
|
messages := []schema.Message{
|
|
{Role: "user", StringContent: "hello"},
|
|
}
|
|
templated := evaluator.TemplateMessages(schema.OpenAIRequest{}, messages, cfg, []functions.Function{}, false)
|
|
Expect(templated).To(Equal("hello"), templated)
|
|
})
|
|
})
|
|
})
|