Enha: force stop string on client side
This commit is contained in:
8
bot.go
8
bot.go
@@ -540,6 +540,8 @@ func extractDetailedErrorFromBytes(body []byte, statusCode int) string {
|
|||||||
// sendMsgToLLM expects streaming resp
|
// sendMsgToLLM expects streaming resp
|
||||||
func sendMsgToLLM(body io.Reader) {
|
func sendMsgToLLM(body io.Reader) {
|
||||||
choseChunkParser()
|
choseChunkParser()
|
||||||
|
// openrouter does not respect stop strings, so we have to cut the message ourselves
|
||||||
|
stopStrings := chatBody.MakeStopSliceExcluding("", listChatRoles())
|
||||||
req, err := http.NewRequest("POST", cfg.CurrentAPI, body)
|
req, err := http.NewRequest("POST", cfg.CurrentAPI, body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error("newreq error", "error", err)
|
logger.Error("newreq error", "error", err)
|
||||||
@@ -678,6 +680,12 @@ func sendMsgToLLM(body io.Reader) {
|
|||||||
}
|
}
|
||||||
// bot sends way too many \n
|
// bot sends way too many \n
|
||||||
answerText = strings.ReplaceAll(chunk.Chunk, "\n\n", "\n")
|
answerText = strings.ReplaceAll(chunk.Chunk, "\n\n", "\n")
|
||||||
|
// Accumulate text to check for stop strings that might span across chunks
|
||||||
|
// check if chunk is in stopstrings => stop
|
||||||
|
if slices.Contains(stopStrings, answerText) {
|
||||||
|
logger.Debug("Stop string detected and handled", "stop_string", answerText)
|
||||||
|
streamDone <- true
|
||||||
|
}
|
||||||
chunkChan <- answerText
|
chunkChan <- answerText
|
||||||
openAIToolChan <- chunk.ToolChunk
|
openAIToolChan <- chunk.ToolChunk
|
||||||
if chunk.FuncName != "" {
|
if chunk.FuncName != "" {
|
||||||
|
|||||||
Reference in New Issue
Block a user