forked from tmc/langchaingo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstructured_test.go
71 lines (63 loc) · 2.57 KB
/
structured_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
package outputparser
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestStructured(t *testing.T) {
t.Parallel()
type test struct {
name string
responseSchema []ResponseSchema
llmOutput string
formatInstruction string
parsed map[string]string
expectError bool
}
testCases := []test{
{
name: "Single",
responseSchema: []ResponseSchema{
{Name: "url", Description: "A link to the resource"},
},
llmOutput: "```json\n{\n\t\"url\": \"https://google.com\" \n}\n```",
formatInstruction: "The output should be a markdown code snippet formatted in the following schema: \n```json\n{\n\t\"url\": string // A link to the resource\n}\n```", //nolint
parsed: map[string]string{"url": "https://google.com"},
expectError: false,
},
{
name: "Double",
responseSchema: []ResponseSchema{
{Name: "answer", Description: "The answer to the question"},
{Name: "source", Description: "A link to the source"},
},
llmOutput: " ``` foo```json \n{\n\t\"answer\": \"Paris\",\n\t\"source\": \"https://google.com\" \n}\n``` ``` bar zoo", //nolint
formatInstruction: "The output should be a markdown code snippet formatted in the following schema: \n```json\n{\n\t\"answer\": string // The answer to the question\n\t\"source\": string // A link to the source\n}\n```", //nolint
parsed: map[string]string{"answer": "Paris", "source": "https://google.com"},
expectError: false,
},
{
name: "MissingKey",
responseSchema: []ResponseSchema{
{Name: "answer", Description: "The answer to the question"},
{Name: "source", Description: "A link to the source"},
},
llmOutput: "```json \n{\n\t\"source\": \"https://google.com\" \n}\n```",
formatInstruction: "The output should be a markdown code snippet formatted in the following schema: \n```json\n{\n\t\"answer\": string // The answer to the question\n\t\"source\": string // A link to the source\n}\n```", //nolint
parsed: nil,
expectError: true,
},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
parser := NewStructured(tc.responseSchema)
assert.Equal(t, tc.formatInstruction, parser.GetFormatInstructions())
parsed, err := parser.Parse(tc.llmOutput)
if (err != nil) != tc.expectError {
t.Fatalf("expected error: %v, got: %v", tc.expectError, err)
}
assert.Equal(t, parsed, tc.parsed)
})
}
}