forked from tmc/langchaingo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregex_dict_test.go
74 lines (63 loc) · 1.97 KB
/
regex_dict_test.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
package outputparser_test
import (
"reflect"
"testing"
"github.com/tmc/langchaingo/outputparser"
)
func TestRegexDict(t *testing.T) {
t.Parallel()
testText := `We have just received a new result from the LLM, and our next step is
to filter and read its format using regular expressions to identify specific fields,
such as:
- Action: Search
- Action Input: How to use this class?
- Additional Fields: "N/A"
To assist us in this task, we use the regex_dict class. This class allows us to send a
dictionary containing an output key and the expected format, which in turn enables us to
retrieve the result of the matching formats and extract specific information from it.
To exclude irrelevant information from our return dictionary, we can instruct the LLM to
use a specific command that notifies us when it doesn't know the answer. We call this
variable the "no_update_value", and for our current case, we set it to "N/A". Therefore,
we expect the result to only contain the following fields:
{
{key = action, value = search}
{key = action_input, value = "How to use this class?"}.
}`
testCases := []struct {
noUpdateValue string
outputKeys map[string]string
expected map[string]string
}{
{
noUpdateValue: "",
outputKeys: map[string]string{
"action": "Action",
"action_input": "Action Input",
},
expected: map[string]string{
"action": "Search",
"action_input": "How to use this class?",
},
},
{
noUpdateValue: "Search",
outputKeys: map[string]string{
"action": "Action",
"action_input": "Action Input",
},
expected: map[string]string{
"action_input": "How to use this class?",
},
},
}
for _, tc := range testCases {
parser := outputparser.NewRegexDict(tc.outputKeys, tc.noUpdateValue)
actual, err := parser.Parse(testText)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if !reflect.DeepEqual(actual, tc.expected) {
t.Errorf("Expected %v, got %v", tc.expected, actual)
}
}
}