Merge pull request #862 from trheyi/main
Improve tool call parsing and formatting
This commit is contained in:
commit
13bc93fe0b
3 changed files with 20 additions and 5 deletions
|
|
@ -379,7 +379,7 @@ func (ast *Assistant) streamChat(
|
||||||
// for native tool_calls response
|
// for native tool_calls response
|
||||||
if msg.Type == "tool_calls_native" {
|
if msg.Type == "tool_calls_native" {
|
||||||
if isFirstTool {
|
if isFirstTool {
|
||||||
msg.Text = "<tool>\n" + msg.Text // add the tool_calls begin tag
|
msg.Text = "\n<tool>\n" + msg.Text // add the tool_calls begin tag
|
||||||
isFirstTool = false
|
isFirstTool = false
|
||||||
isTool = true
|
isTool = true
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -154,14 +154,28 @@ func (ast *Assistant) HookDone(c *gin.Context, context chatctx.Context, input []
|
||||||
props := map[string]interface{}{}
|
props := map[string]interface{}{}
|
||||||
if text, ok := data.Props["text"].(string); ok {
|
if text, ok := data.Props["text"].(string); ok {
|
||||||
|
|
||||||
// Remove <tool> and </tool> tags
|
// Format the text keep only the <tool> and </tool> inner text
|
||||||
text = strings.ReplaceAll(text, "<tool>", "")
|
parts := strings.Split(text, "<tool>")
|
||||||
text = strings.ReplaceAll(text, "</tool>", "")
|
if len(parts) > 1 {
|
||||||
|
text = parts[1]
|
||||||
|
}
|
||||||
|
|
||||||
|
// Format the text keep only the <tool> and </tool> inner text
|
||||||
|
parts = strings.Split(text, "</tool>")
|
||||||
|
if len(parts) > 1 {
|
||||||
|
text = parts[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
// Escape %7B and %7b to {, %7D and %7d to }
|
||||||
|
text = strings.ReplaceAll(text, "%7B", "{")
|
||||||
|
text = strings.ReplaceAll(text, "%7b", "{")
|
||||||
|
text = strings.ReplaceAll(text, "%7D", "}")
|
||||||
|
text = strings.ReplaceAll(text, "%7d", "}")
|
||||||
|
|
||||||
// Parse the text into props
|
// Parse the text into props
|
||||||
err := jsoniter.UnmarshalFromString(text, &props)
|
err := jsoniter.UnmarshalFromString(text, &props)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
props["error"] = err.Error()
|
props["error"] = fmt.Sprintf("Can not parse the tool call: %s\n--original--\n%s", err.Error(), text)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -228,6 +228,7 @@ func NewOpenAI(data []byte, isThinking bool) *Message {
|
||||||
text := arguments
|
text := arguments
|
||||||
if id != "" {
|
if id != "" {
|
||||||
text = fmt.Sprintf(`{"id": "%s", "function": "%s", "arguments": %s`, id, function, arguments)
|
text = fmt.Sprintf(`{"id": "%s", "function": "%s", "arguments": %s`, id, function, arguments)
|
||||||
|
msg.IsNew = true // mark as a new message
|
||||||
}
|
}
|
||||||
|
|
||||||
msg.Text = text
|
msg.Text = text
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue