Simplify token scanning and streaming logic in chat processing
- Removed unnecessary breakpoint and token tracking variables - Streamlined token scanning and message processing in assistant API - Simplified text handling and token parsing in contents scanning - Reduced complexity of stream chat message generation
This commit is contained in:
parent
b19a6b953a
commit
991c884d78
2 changed files with 0 additions and 16 deletions
|
|
@ -321,7 +321,6 @@ func (ast *Assistant) streamChat(
|
||||||
msg.AppendTo(contents) // Append content and send message
|
msg.AppendTo(contents) // Append content and send message
|
||||||
|
|
||||||
// Scan the tokens
|
// Scan the tokens
|
||||||
breakpoint := false
|
|
||||||
contents.ScanTokens(func(token string, begin bool, text string, tails string) {
|
contents.ScanTokens(func(token string, begin bool, text string, tails string) {
|
||||||
msg.Type = token
|
msg.Type = token
|
||||||
msg.Text = "" // clear the text
|
msg.Text = "" // clear the text
|
||||||
|
|
@ -332,12 +331,6 @@ func (ast *Assistant) streamChat(
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ignore the end of the token
|
|
||||||
if !begin && tails == "" {
|
|
||||||
breakpoint = true
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// New message with the tails
|
// New message with the tails
|
||||||
newMsg, err := chatMessage.NewString(tails)
|
newMsg, err := chatMessage.NewString(tails)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -346,11 +339,6 @@ func (ast *Assistant) streamChat(
|
||||||
messages = append(messages, *newMsg)
|
messages = append(messages, *newMsg)
|
||||||
})
|
})
|
||||||
|
|
||||||
// If the breakpoint is true, continue the stream
|
|
||||||
if breakpoint {
|
|
||||||
return 1 // continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle stream
|
// Handle stream
|
||||||
res, err := ast.HookStream(c, ctx, messages, msg, contents)
|
res, err := ast.HookStream(c, ctx, messages, msg, contents)
|
||||||
if err == nil && res != nil {
|
if err == nil && res != nil {
|
||||||
|
|
|
||||||
|
|
@ -61,9 +61,6 @@ func (c *Contents) ScanTokens(cb func(token string, begin bool, text string, tai
|
||||||
if index > 0 {
|
if index > 0 {
|
||||||
tails = text[index+len(token[1]):]
|
tails = text[index+len(token[1]):]
|
||||||
}
|
}
|
||||||
|
|
||||||
text = strings.TrimLeft(text[len(token[0]):index], "\n")
|
|
||||||
c.Data[c.Current].Bytes = []byte(text)
|
|
||||||
c.UpdateType(c.token, map[string]interface{}{"text": text})
|
c.UpdateType(c.token, map[string]interface{}{"text": text})
|
||||||
c.NewText([]byte(tails)) // Create new text with the tails
|
c.NewText([]byte(tails)) // Create new text with the tails
|
||||||
cb(c.token, false, text, tails)
|
cb(c.token, false, text, tails)
|
||||||
|
|
@ -80,7 +77,6 @@ func (c *Contents) ScanTokens(cb func(token string, begin bool, text string, tai
|
||||||
for name, token := range tokens {
|
for name, token := range tokens {
|
||||||
if index := strings.Index(text, token[0]); index >= 0 {
|
if index := strings.Index(text, token[0]); index >= 0 {
|
||||||
c.token = name
|
c.token = name
|
||||||
text = strings.TrimSpace(text[index+len(token[0]):])
|
|
||||||
cb(name, true, text, "") // call the callback
|
cb(name, true, text, "") // call the callback
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue