Reason 执行推理
(ctx context.Context, query string, systemPrompt string)
| 49 | |
| 50 | // Reason 执行推理 |
| 51 | func (e *Engine) Reason(ctx context.Context, query string, systemPrompt string) (*Chain, error) { |
| 52 | chain := NewChain(ChainConfig{ |
| 53 | MinSteps: e.config.MinSteps, |
| 54 | MaxSteps: e.config.MaxSteps, |
| 55 | MinConfidence: e.config.MinConfidence, |
| 56 | }) |
| 57 | |
| 58 | // 构建推理提示词 |
| 59 | reasoningPrompt := e.buildReasoningPrompt(query, systemPrompt) |
| 60 | |
| 61 | // 执行推理循环 |
| 62 | for chain.ShouldContinue() { |
| 63 | step, err := e.executeReasoningStep(ctx, chain, reasoningPrompt) |
| 64 | if err != nil { |
| 65 | return chain, fmt.Errorf("execute reasoning step: %w", err) |
| 66 | } |
| 67 | |
| 68 | if err := chain.AddStep(*step); err != nil { |
| 69 | return chain, fmt.Errorf("add reasoning step: %w", err) |
| 70 | } |
| 71 | |
| 72 | // 如果步骤指示完成,则退出 |
| 73 | if step.NextAction == NextActionComplete { |
| 74 | break |
| 75 | } |
| 76 | |
| 77 | // 如果置信度过低且已达到最小步数,则退出 |
| 78 | if step.Confidence < e.config.MinConfidence && len(chain.Steps) >= e.config.MinSteps { |
| 79 | break |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | chain.Complete() |
| 84 | return chain, nil |
| 85 | } |
| 86 | |
| 87 | // executeReasoningStep 执行单个推理步骤 |
| 88 | func (e *Engine) executeReasoningStep(ctx context.Context, chain *Chain, basePrompt string) (*Step, error) { |
nothing calls this directly
no test coverage detected