Enha: filter out thinking blocks from chat history, removed {role}:
This commit is contained in:
@@ -48,3 +48,4 @@ EnableMouse = false # Enable mouse support in the UI
|
|||||||
CharSpecificContextEnabled = true
|
CharSpecificContextEnabled = true
|
||||||
CharSpecificContextTag = "@"
|
CharSpecificContextTag = "@"
|
||||||
AutoTurn = true
|
AutoTurn = true
|
||||||
|
StripThinkingFromAPI = true # Strip <think> blocks from messages before sending to LLM (keeps them in chat history)
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ type Config struct {
|
|||||||
ToolRole string `toml:"ToolRole"`
|
ToolRole string `toml:"ToolRole"`
|
||||||
ToolUse bool `toml:"ToolUse"`
|
ToolUse bool `toml:"ToolUse"`
|
||||||
ThinkUse bool `toml:"ThinkUse"`
|
ThinkUse bool `toml:"ThinkUse"`
|
||||||
|
StripThinkingFromAPI bool `toml:"StripThinkingFromAPI"`
|
||||||
AssistantRole string `toml:"AssistantRole"`
|
AssistantRole string `toml:"AssistantRole"`
|
||||||
SysDir string `toml:"SysDir"`
|
SysDir string `toml:"SysDir"`
|
||||||
ChunkLimit uint32 `toml:"ChunkLimit"`
|
ChunkLimit uint32 `toml:"ChunkLimit"`
|
||||||
|
|||||||
19
helpfuncs.go
19
helpfuncs.go
@@ -23,6 +23,25 @@ func isASCII(s string) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// stripThinkingFromMsg removes thinking blocks from assistant messages.
|
||||||
|
// Skips user, tool, and system messages as they may contain thinking examples.
|
||||||
|
func stripThinkingFromMsg(msg models.RoleMsg) *models.RoleMsg {
|
||||||
|
if !cfg.StripThinkingFromAPI {
|
||||||
|
return &msg
|
||||||
|
}
|
||||||
|
// Skip user, tool, and system messages - they might contain thinking examples
|
||||||
|
if msg.Role == cfg.UserRole || msg.Role == cfg.ToolRole || msg.Role == "system" {
|
||||||
|
return &msg
|
||||||
|
}
|
||||||
|
// Strip thinking from assistant messages
|
||||||
|
if thinkRE.MatchString(msg.Content) {
|
||||||
|
msg.Content = thinkRE.ReplaceAllString(msg.Content, "")
|
||||||
|
// Clean up any double newlines that might result
|
||||||
|
msg.Content = strings.TrimSpace(msg.Content)
|
||||||
|
}
|
||||||
|
return &msg
|
||||||
|
}
|
||||||
|
|
||||||
// refreshChatDisplay updates the chat display based on current character view
|
// refreshChatDisplay updates the chat display based on current character view
|
||||||
// It filters messages for the character the user is currently "writing as"
|
// It filters messages for the character the user is currently "writing as"
|
||||||
// and updates the textView with the filtered conversation
|
// and updates the textView with the filtered conversation
|
||||||
|
|||||||
62
llm.go
62
llm.go
@@ -13,28 +13,6 @@ var imageAttachmentPath string // Global variable to track image attachment for
|
|||||||
var lastImg string // for ctrl+j
|
var lastImg string // for ctrl+j
|
||||||
var RAGMsg = "Retrieved context for user's query:\n"
|
var RAGMsg = "Retrieved context for user's query:\n"
|
||||||
|
|
||||||
// addPersonaSuffixToLastUserMessage adds the persona suffix to the last user message
|
|
||||||
// to indicate to the assistant who it should reply as
|
|
||||||
func addPersonaSuffixToLastUserMessage(messages []models.RoleMsg, persona string) []models.RoleMsg {
|
|
||||||
if len(messages) == 0 {
|
|
||||||
return messages
|
|
||||||
}
|
|
||||||
// // Find the last user message to modify
|
|
||||||
// for i := len(messages) - 1; i >= 0; i-- {
|
|
||||||
// if messages[i].Role == cfg.UserRole || messages[i].Role == "user" {
|
|
||||||
// // Create a copy of the message to avoid modifying the original
|
|
||||||
// modifiedMsg := messages[i]
|
|
||||||
// modifiedMsg.Content = modifiedMsg.Content + "\n" + persona + ":"
|
|
||||||
// messages[i] = modifiedMsg
|
|
||||||
// break
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
modifiedMsg := messages[len(messages)-1]
|
|
||||||
modifiedMsg.Content = modifiedMsg.Content + "\n" + persona + ":\n"
|
|
||||||
messages[len(messages)-1] = modifiedMsg
|
|
||||||
return messages
|
|
||||||
}
|
|
||||||
|
|
||||||
// containsToolSysMsg checks if the toolSysMsg already exists in the chat body
|
// containsToolSysMsg checks if the toolSysMsg already exists in the chat body
|
||||||
func containsToolSysMsg() bool {
|
func containsToolSysMsg() bool {
|
||||||
for _, msg := range chatBody.Messages {
|
for _, msg := range chatBody.Messages {
|
||||||
@@ -187,7 +165,7 @@ func (lcp LCPCompletion) FormMsg(msg, role string, resume bool) (io.Reader, erro
|
|||||||
filteredMessages, botPersona := filterMessagesForCurrentCharacter(chatBody.Messages)
|
filteredMessages, botPersona := filterMessagesForCurrentCharacter(chatBody.Messages)
|
||||||
messages := make([]string, len(filteredMessages))
|
messages := make([]string, len(filteredMessages))
|
||||||
for i, m := range filteredMessages {
|
for i, m := range filteredMessages {
|
||||||
messages[i] = m.ToPrompt()
|
messages[i] = stripThinkingFromMsg(m).ToPrompt()
|
||||||
}
|
}
|
||||||
prompt := strings.Join(messages, "\n")
|
prompt := strings.Join(messages, "\n")
|
||||||
// Add multimodal media markers to the prompt text when multimodal data is present
|
// Add multimodal media markers to the prompt text when multimodal data is present
|
||||||
@@ -341,23 +319,21 @@ func (op LCPChat) FormMsg(msg, role string, resume bool) (io.Reader, error) {
|
|||||||
logger.Debug("LCPChat: RAG message added to chat body", "role", ragMsg.Role,
|
logger.Debug("LCPChat: RAG message added to chat body", "role", ragMsg.Role,
|
||||||
"rag_content_len", len(ragMsg.Content), "message_count_after_rag", len(chatBody.Messages))
|
"rag_content_len", len(ragMsg.Content), "message_count_after_rag", len(chatBody.Messages))
|
||||||
}
|
}
|
||||||
filteredMessages, botPersona := filterMessagesForCurrentCharacter(chatBody.Messages)
|
filteredMessages, _ := filterMessagesForCurrentCharacter(chatBody.Messages)
|
||||||
// openai /v1/chat does not support custom roles; needs to be user, assistant, system
|
// openai /v1/chat does not support custom roles; needs to be user, assistant, system
|
||||||
// Add persona suffix to the last user message to indicate who the assistant should reply as
|
// Add persona suffix to the last user message to indicate who the assistant should reply as
|
||||||
if cfg.AutoTurn && !resume {
|
|
||||||
filteredMessages = addPersonaSuffixToLastUserMessage(filteredMessages, botPersona)
|
|
||||||
}
|
|
||||||
bodyCopy := &models.ChatBody{
|
bodyCopy := &models.ChatBody{
|
||||||
Messages: make([]models.RoleMsg, len(filteredMessages)),
|
Messages: make([]models.RoleMsg, len(filteredMessages)),
|
||||||
Model: chatBody.Model,
|
Model: chatBody.Model,
|
||||||
Stream: chatBody.Stream,
|
Stream: chatBody.Stream,
|
||||||
}
|
}
|
||||||
for i, msg := range filteredMessages {
|
for i, msg := range filteredMessages {
|
||||||
if msg.Role == cfg.UserRole {
|
strippedMsg := *stripThinkingFromMsg(msg)
|
||||||
bodyCopy.Messages[i] = msg
|
if strippedMsg.Role == cfg.UserRole {
|
||||||
|
bodyCopy.Messages[i] = strippedMsg
|
||||||
bodyCopy.Messages[i].Role = "user"
|
bodyCopy.Messages[i].Role = "user"
|
||||||
} else {
|
} else {
|
||||||
bodyCopy.Messages[i] = msg
|
bodyCopy.Messages[i] = strippedMsg
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Clean null/empty messages to prevent API issues
|
// Clean null/empty messages to prevent API issues
|
||||||
@@ -437,7 +413,7 @@ func (ds DeepSeekerCompletion) FormMsg(msg, role string, resume bool) (io.Reader
|
|||||||
filteredMessages, botPersona := filterMessagesForCurrentCharacter(chatBody.Messages)
|
filteredMessages, botPersona := filterMessagesForCurrentCharacter(chatBody.Messages)
|
||||||
messages := make([]string, len(filteredMessages))
|
messages := make([]string, len(filteredMessages))
|
||||||
for i, m := range filteredMessages {
|
for i, m := range filteredMessages {
|
||||||
messages[i] = m.ToPrompt()
|
messages[i] = stripThinkingFromMsg(m).ToPrompt()
|
||||||
}
|
}
|
||||||
prompt := strings.Join(messages, "\n")
|
prompt := strings.Join(messages, "\n")
|
||||||
// strings builder?
|
// strings builder?
|
||||||
@@ -519,22 +495,20 @@ func (ds DeepSeekerChat) FormMsg(msg, role string, resume bool) (io.Reader, erro
|
|||||||
logger.Debug("RAG message added to chat body", "message_count", len(chatBody.Messages))
|
logger.Debug("RAG message added to chat body", "message_count", len(chatBody.Messages))
|
||||||
}
|
}
|
||||||
// Create copy of chat body with standardized user role
|
// Create copy of chat body with standardized user role
|
||||||
filteredMessages, botPersona := filterMessagesForCurrentCharacter(chatBody.Messages)
|
filteredMessages, _ := filterMessagesForCurrentCharacter(chatBody.Messages)
|
||||||
// Add persona suffix to the last user message to indicate who the assistant should reply as
|
// Add persona suffix to the last user message to indicate who the assistant should reply as
|
||||||
if cfg.AutoTurn && !resume {
|
|
||||||
filteredMessages = addPersonaSuffixToLastUserMessage(filteredMessages, botPersona)
|
|
||||||
}
|
|
||||||
bodyCopy := &models.ChatBody{
|
bodyCopy := &models.ChatBody{
|
||||||
Messages: make([]models.RoleMsg, len(filteredMessages)),
|
Messages: make([]models.RoleMsg, len(filteredMessages)),
|
||||||
Model: chatBody.Model,
|
Model: chatBody.Model,
|
||||||
Stream: chatBody.Stream,
|
Stream: chatBody.Stream,
|
||||||
}
|
}
|
||||||
for i, msg := range filteredMessages {
|
for i, msg := range filteredMessages {
|
||||||
if msg.Role == cfg.UserRole || i == 1 {
|
strippedMsg := *stripThinkingFromMsg(msg)
|
||||||
bodyCopy.Messages[i] = msg
|
if strippedMsg.Role == cfg.UserRole || i == 1 {
|
||||||
|
bodyCopy.Messages[i] = strippedMsg
|
||||||
bodyCopy.Messages[i].Role = "user"
|
bodyCopy.Messages[i].Role = "user"
|
||||||
} else {
|
} else {
|
||||||
bodyCopy.Messages[i] = msg
|
bodyCopy.Messages[i] = strippedMsg
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Clean null/empty messages to prevent API issues
|
// Clean null/empty messages to prevent API issues
|
||||||
@@ -605,7 +579,7 @@ func (or OpenRouterCompletion) FormMsg(msg, role string, resume bool) (io.Reader
|
|||||||
filteredMessages, botPersona := filterMessagesForCurrentCharacter(chatBody.Messages)
|
filteredMessages, botPersona := filterMessagesForCurrentCharacter(chatBody.Messages)
|
||||||
messages := make([]string, len(filteredMessages))
|
messages := make([]string, len(filteredMessages))
|
||||||
for i, m := range filteredMessages {
|
for i, m := range filteredMessages {
|
||||||
messages[i] = m.ToPrompt()
|
messages[i] = stripThinkingFromMsg(m).ToPrompt()
|
||||||
}
|
}
|
||||||
prompt := strings.Join(messages, "\n")
|
prompt := strings.Join(messages, "\n")
|
||||||
// strings builder?
|
// strings builder?
|
||||||
@@ -718,21 +692,19 @@ func (or OpenRouterChat) FormMsg(msg, role string, resume bool) (io.Reader, erro
|
|||||||
logger.Debug("RAG message added to chat body", "message_count", len(chatBody.Messages))
|
logger.Debug("RAG message added to chat body", "message_count", len(chatBody.Messages))
|
||||||
}
|
}
|
||||||
// Create copy of chat body with standardized user role
|
// Create copy of chat body with standardized user role
|
||||||
filteredMessages, botPersona := filterMessagesForCurrentCharacter(chatBody.Messages)
|
filteredMessages, _ := filterMessagesForCurrentCharacter(chatBody.Messages)
|
||||||
// Add persona suffix to the last user message to indicate who the assistant should reply as
|
// Add persona suffix to the last user message to indicate who the assistant should reply as
|
||||||
if cfg.AutoTurn && !resume {
|
|
||||||
filteredMessages = addPersonaSuffixToLastUserMessage(filteredMessages, botPersona)
|
|
||||||
}
|
|
||||||
bodyCopy := &models.ChatBody{
|
bodyCopy := &models.ChatBody{
|
||||||
Messages: make([]models.RoleMsg, len(filteredMessages)),
|
Messages: make([]models.RoleMsg, len(filteredMessages)),
|
||||||
Model: chatBody.Model,
|
Model: chatBody.Model,
|
||||||
Stream: chatBody.Stream,
|
Stream: chatBody.Stream,
|
||||||
}
|
}
|
||||||
for i, msg := range filteredMessages {
|
for i, msg := range filteredMessages {
|
||||||
bodyCopy.Messages[i] = msg
|
strippedMsg := *stripThinkingFromMsg(msg)
|
||||||
|
bodyCopy.Messages[i] = strippedMsg
|
||||||
// Standardize role if it's a user role
|
// Standardize role if it's a user role
|
||||||
if bodyCopy.Messages[i].Role == cfg.UserRole {
|
if bodyCopy.Messages[i].Role == cfg.UserRole {
|
||||||
bodyCopy.Messages[i] = msg
|
bodyCopy.Messages[i] = strippedMsg
|
||||||
bodyCopy.Messages[i].Role = "user"
|
bodyCopy.Messages[i].Role = "user"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user