mirror of
https://github.com/CJackHwang/ds2api.git
synced 2026-05-12 04:07:42 +08:00
PR #460 introduced fullwidth pipe characters (|) in DSML tool call formatting to improve parsing robustness, but models exposed to these fullwidth pipes in system prompts exhibit significantly higher rates of tool output hallucinations. Reverting to halfwidth pipes (|) drastically reduces tokenizer/perplexity-driven hallucinations while retaining the existing confusable-hardening in the parser. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
38 lines
1.8 KiB
Go
38 lines
1.8 KiB
Go
package promptcompat
|
|
|
|
import (
|
|
"ds2api/internal/prompt"
|
|
)
|
|
|
|
func buildOpenAIFinalPrompt(messagesRaw []any, toolsRaw any, traceID string, thinkingEnabled bool) (string, []string) {
|
|
return BuildOpenAIPrompt(messagesRaw, toolsRaw, traceID, DefaultToolChoicePolicy(), thinkingEnabled)
|
|
}
|
|
|
|
func BuildOpenAIPrompt(messagesRaw []any, toolsRaw any, traceID string, toolPolicy ToolChoicePolicy, thinkingEnabled bool) (string, []string) {
|
|
return buildOpenAIPrompt(messagesRaw, toolsRaw, traceID, toolPolicy, thinkingEnabled, true)
|
|
}
|
|
|
|
func BuildOpenAIPromptWithToolInstructionsOnly(messagesRaw []any, toolsRaw any, traceID string, toolPolicy ToolChoicePolicy, thinkingEnabled bool) (string, []string) {
|
|
return buildOpenAIPrompt(messagesRaw, toolsRaw, traceID, toolPolicy, thinkingEnabled, false)
|
|
}
|
|
|
|
func buildOpenAIPrompt(messagesRaw []any, toolsRaw any, traceID string, toolPolicy ToolChoicePolicy, thinkingEnabled bool, includeToolDescriptions bool) (string, []string) {
|
|
messages := NormalizeOpenAIMessagesForPrompt(messagesRaw, traceID)
|
|
toolNames := []string{}
|
|
if tools, ok := toolsRaw.([]any); ok && len(tools) > 0 {
|
|
if includeToolDescriptions {
|
|
messages, toolNames = injectToolPrompt(messages, tools, toolPolicy)
|
|
} else {
|
|
messages, toolNames = injectToolPromptInstructionsOnly(messages, tools, toolPolicy)
|
|
}
|
|
}
|
|
return prompt.MessagesPrepareWithThinking(messages, thinkingEnabled), toolNames
|
|
}
|
|
|
|
// BuildOpenAIPromptForAdapter exposes the OpenAI-compatible prompt building flow so
|
|
// other protocol adapters (for example Gemini) can reuse the same tool/history
|
|
// normalization logic and remain behavior-compatible with chat/completions.
|
|
func BuildOpenAIPromptForAdapter(messagesRaw []any, toolsRaw any, traceID string, thinkingEnabled bool) (string, []string) {
|
|
return buildOpenAIFinalPrompt(messagesRaw, toolsRaw, traceID, thinkingEnabled)
|
|
}
|