Skip to content

Commit 13bc93f

Browse files
authored
Merge pull request #862 from trheyi/main
Improve tool call parsing and formatting
2 parents 12e15e6 + 617893a commit 13bc93f

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

neo/assistant/api.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ func (ast *Assistant) streamChat(
379379
// for native tool_calls response
380380
if msg.Type == "tool_calls_native" {
381381
if isFirstTool {
382-
msg.Text = "<tool>\n" + msg.Text // add the tool_calls begin tag
382+
msg.Text = "\n<tool>\n" + msg.Text // add the tool_calls begin tag
383383
isFirstTool = false
384384
isTool = true
385385
}

neo/assistant/hooks.go

+18-4
Original file line numberDiff line numberDiff line change
@@ -154,14 +154,28 @@ func (ast *Assistant) HookDone(c *gin.Context, context chatctx.Context, input []
154154
props := map[string]interface{}{}
155155
if text, ok := data.Props["text"].(string); ok {
156156

157-
// Remove <tool> and </tool> tags
158-
text = strings.ReplaceAll(text, "<tool>", "")
159-
text = strings.ReplaceAll(text, "</tool>", "")
157+
// Format the text keep only the <tool> and </tool> inner text
158+
parts := strings.Split(text, "<tool>")
159+
if len(parts) > 1 {
160+
text = parts[1]
161+
}
162+
163+
// Format the text keep only the <tool> and </tool> inner text
164+
parts = strings.Split(text, "</tool>")
165+
if len(parts) > 1 {
166+
text = parts[0]
167+
}
168+
169+
// Escape %7B and %7b to {, %7D and %7d to }
170+
text = strings.ReplaceAll(text, "%7B", "{")
171+
text = strings.ReplaceAll(text, "%7b", "{")
172+
text = strings.ReplaceAll(text, "%7D", "}")
173+
text = strings.ReplaceAll(text, "%7d", "}")
160174

161175
// Parse the text into props
162176
err := jsoniter.UnmarshalFromString(text, &props)
163177
if err != nil {
164-
props["error"] = err.Error()
178+
props["error"] = fmt.Sprintf("Can not parse the tool call: %s\n--original--\n%s", err.Error(), text)
165179
}
166180
}
167181

neo/message/message.go

+1
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ func NewOpenAI(data []byte, isThinking bool) *Message {
228228
text := arguments
229229
if id != "" {
230230
text = fmt.Sprintf(`{"id": "%s", "function": "%s", "arguments": %s`, id, function, arguments)
231+
msg.IsNew = true // mark as a new message
231232
}
232233

233234
msg.Text = text

0 commit comments

Comments
 (0)