1 Star 0 Fork 0

Survivor_zzc / langchaingo

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
mrkl.go 3.27 KB
一键复制 编辑 原始数据 按行查看 历史
Survivor_zzc 提交于 2023-10-24 17:21 . package name
package agents
import (
"context"
"fmt"
"regexp"
"strings"
"time"
"gitee.com/zzcadmin/langchaingo/chains"
"gitee.com/zzcadmin/langchaingo/llms"
"gitee.com/zzcadmin/langchaingo/schema"
"gitee.com/zzcadmin/langchaingo/tools"
)
const (
_finalAnswerAction = "Final Answer:"
_defaultOutputKey = "output"
)
// OneShotZeroAgent is a struct that represents an agent responsible for deciding
// what to do or give the final output if the task is finished given a set of inputs
// and previous steps taken.
//
// This agent is optimized to be used with LLMs.
type OneShotZeroAgent struct {
// Chain is the chain used to call with the values. The chain should have an
// input called "agent_scratchpad" for the agent to put it's thoughts in.
Chain chains.Chain
// Tools is a list of the tools the agent can use.
Tools []tools.Tool
// Output key is the key where the final output is placed.
OutputKey string
}
var _ Agent = (*OneShotZeroAgent)(nil)
// NewOneShotAgent creates a new OneShotZeroAgent with the given LLM model, tools,
// and options. It returns a pointer to the created agent. The opts parameter
// represents the options for the agent.
func NewOneShotAgent(llm llms.LanguageModel, tools []tools.Tool, opts ...CreationOption) *OneShotZeroAgent {
options := mrklDefaultOptions()
for _, opt := range opts {
opt(&options)
}
return &OneShotZeroAgent{
Chain: chains.NewLLMChain(llm, options.getMrklPrompt(tools)),
Tools: tools,
OutputKey: options.outputKey,
}
}
// Plan decides what action to take or returns the final result of the input.
func (a *OneShotZeroAgent) Plan(
ctx context.Context,
intermediateSteps []schema.AgentStep,
inputs map[string]string,
) ([]schema.AgentAction, *schema.AgentFinish, error) {
fullInputs := make(map[string]any, len(inputs))
for key, value := range inputs {
fullInputs[key] = value
}
fullInputs["agent_scratchpad"] = constructScratchPad(intermediateSteps)
fullInputs["today"] = time.Now().Format("January 02, 2006")
output, err := chains.Predict(
ctx,
a.Chain,
fullInputs,
chains.WithStopWords([]string{"\nObservation:", "\n\tObservation:"}),
)
if err != nil {
return nil, nil, err
}
return a.parseOutput(output)
}
func (a *OneShotZeroAgent) GetInputKeys() []string {
chainInputs := a.Chain.GetInputKeys()
// Remove inputs given in plan.
agentInput := make([]string, 0, len(chainInputs))
for _, v := range chainInputs {
if v == "agent_scratchpad" || v == "today" {
continue
}
agentInput = append(agentInput, v)
}
return agentInput
}
func (a *OneShotZeroAgent) GetOutputKeys() []string {
return []string{a.OutputKey}
}
func (a *OneShotZeroAgent) parseOutput(output string) ([]schema.AgentAction, *schema.AgentFinish, error) {
if strings.Contains(output, _finalAnswerAction) {
splits := strings.Split(output, _finalAnswerAction)
return nil, &schema.AgentFinish{
ReturnValues: map[string]any{
a.OutputKey: splits[len(splits)-1],
},
Log: output,
}, nil
}
r := regexp.MustCompile(`Action:\s*(.+)\s*Action Input:\s*(.+)`)
matches := r.FindStringSubmatch(output)
if len(matches) == 0 {
return nil, nil, fmt.Errorf("%w: %s", ErrUnableToParseOutput, output)
}
return []schema.AgentAction{
{Tool: strings.TrimSpace(matches[1]), ToolInput: strings.TrimSpace(matches[2]), Log: output},
}, nil, nil
}
Go
1
https://gitee.com/zzcadmin/langchaingo.git
git@gitee.com:zzcadmin/langchaingo.git
zzcadmin
langchaingo
langchaingo
v0.4.7

搜索帮助