Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions pkg/settings/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ type Config struct {
// 相似度匹配数量
VectorLimit int `envconfig:"Vector_Limit" default:"5"`

// LLM调用循环次数限制,防止无限循环
MaxLoopIterations int `envconfig:"MAX_LOOP_ITERATIONS" default:"5"`

Embedding Provider
Interact Provider
Summarize Provider
Expand Down
14 changes: 12 additions & 2 deletions pkg/web/api/handle_convo.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,12 +283,22 @@ func (a *api) chatStreamResponseLoop(ccr *chatRequest, w http.ResponseWriter, r
w.Header().Add("Conversation-ID", ccr.cs.GetID())

var iter int
maxLoopIterations := settings.Current.MaxLoopIterations
if maxLoopIterations <= 0 {
maxLoopIterations = 5
}
for {
iter++
// 达到迭代次数限制,跳出循环
if iter > maxLoopIterations {
logger().Infow("chat loop iteration limit reached", "maxIter", maxLoopIterations)
break
}

// 调用流式响应处理
streamRes := a.doChatStream(ccr, w, r)
logger().Infow("stream round done", "iter", iter, "answer_len", len(streamRes.answer),
"toolCalls_len", len(streamRes.toolCalls))
logger().Infow("stream round done", "iter", iter, "maxIter", maxLoopIterations,
"answer_len", len(streamRes.answer), "toolCalls_len", len(streamRes.toolCalls))

// 累积答案
res.answer += streamRes.answer
Expand Down
Loading