-
Notifications
You must be signed in to change notification settings - Fork 7
/
mrkl.go
163 lines (129 loc) · 4.28 KB
/
mrkl.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package agent
import (
"context"
"fmt"
"regexp"
"strings"
"github.com/hupe1980/golc"
"github.com/hupe1980/golc/chain"
"github.com/hupe1980/golc/prompt"
"github.com/hupe1980/golc/schema"
)
// Compile time check to ensure ZeroShotReactDescription satisfies the agent interface.
var _ schema.Agent = (*ZeroShotReactDescription)(nil)
const (
defaultMRKLPrefix = `Answer the following questions as best you can. You have access to the following tools:
{{.toolDescriptions}}`
defaultMRKLInstructions = `Use the following format:
Question: the input question you must answer
Thought: you should always think about what to do
Action: the action to take, should be one of [{{.toolNames}}]
Action Input: the input to the action
Observation: the result of the action
... (this Thought/Action/Action Input/Observation can repeat N times)
Thought: I now know the final answer
Final Answer: the final answer to the original input question`
defaultMRKLSuffix = `Begin!
Question: {{.input}}
Thought: {{.agentScratchpad}}`
finalAnswerAction = "Final Answer:"
)
type ZeroShotReactDescriptionOptions struct {
Prefix string
Instructions string
Suffix string
OutputKey string
}
type ZeroShotReactDescription struct {
chain schema.Chain
tools []schema.Tool
opts ZeroShotReactDescriptionOptions
}
func NewZeroShotReactDescription(llm schema.LLM, tools []schema.Tool) (*ZeroShotReactDescription, error) {
opts := ZeroShotReactDescriptionOptions{
Prefix: defaultMRKLPrefix,
Instructions: defaultMRKLInstructions,
Suffix: defaultMRKLSuffix,
OutputKey: "output",
}
prompt, err := createMRKLPrompt(tools, opts.Prefix, opts.Instructions, opts.Suffix)
if err != nil {
return nil, err
}
llmChain, err := chain.NewLLM(llm, prompt)
if err != nil {
return nil, err
}
return &ZeroShotReactDescription{
chain: llmChain,
tools: tools,
opts: opts,
}, nil
}
func (a *ZeroShotReactDescription) Plan(ctx context.Context, intermediateSteps []schema.AgentStep, inputs map[string]string) ([]schema.AgentAction, *schema.AgentFinish, error) {
fullInputes := make(schema.ChainValues, len(inputs))
for key, value := range inputs {
fullInputes[key] = value
}
fullInputes["agentScratchpad"] = a.constructScratchPad(intermediateSteps)
resp, err := golc.Call(ctx, a.chain, fullInputes)
if err != nil {
return nil, nil, err
}
output, ok := resp[a.chain.OutputKeys()[0]].(string)
if !ok {
return nil, nil, ErrInvalidChainReturnType
}
return a.parseOutput(output)
}
func (a *ZeroShotReactDescription) InputKeys() []string {
chainInputs := a.chain.InputKeys()
agentInput := make([]string, 0, len(chainInputs))
for _, v := range chainInputs {
if v == "agentScratchpad" {
continue
}
agentInput = append(agentInput, v)
}
return agentInput
}
func (a *ZeroShotReactDescription) OutputKeys() []string {
return []string{a.opts.OutputKey}
}
// constructScratchPad constructs the scratchpad that lets the agent
// continue its thought process.
func (a *ZeroShotReactDescription) constructScratchPad(steps []schema.AgentStep) string {
scratchPad := ""
for _, step := range steps {
scratchPad += step.Action.Log
scratchPad += fmt.Sprintf("\nObservation: %s\nThought:", step.Observation)
}
return scratchPad
}
func (a *ZeroShotReactDescription) 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.opts.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
}
func createMRKLPrompt(tools []schema.Tool, prefix, instructions, suffix string) (*prompt.Template, error) {
return prompt.NewTemplate(strings.Join([]string{prefix, instructions, suffix}, "\n\n"), func(o *prompt.TemplateOptions) {
o.PartialValues = prompt.PartialValues{
"toolNames": toolNames(tools),
"toolDescriptions": toolDescriptions(tools),
}
})
}