File tree Expand file tree Collapse file tree 3 files changed +62
-2
lines changed Expand file tree Collapse file tree 3 files changed +62
-2
lines changed Original file line number Diff line number Diff line change 4
4
"fmt"
5
5
"golangchain/pkg/lib"
6
6
"golangchain/pkg/openai"
7
+ "golangchain/pkg/parser"
7
8
"golangchain/pkg/prompt"
8
9
)
9
10
@@ -13,9 +14,10 @@ func main() {
13
14
fmt .Println (err )
14
15
}
15
16
prompt , err := prompt .NewPromptTemplate ("{{.Word}}の意味を教えて。" )
17
+ parser := parser .NewStrOutputParser ()
16
18
17
19
pipeline := lib .NewPipeline ()
18
- pipeline .Pipe (prompt ).Pipe (llm )
20
+ pipeline .Pipe (prompt ).Pipe (llm ). Pipe ( parser )
19
21
m := map [string ]string {
20
22
"Word" : "因果応報" ,
21
23
}
@@ -25,5 +27,4 @@ func main() {
25
27
fmt .Println (err )
26
28
}
27
29
fmt .Printf ("Response: %+v\n " , response )
28
-
29
30
}
Original file line number Diff line number Diff line change
1
+ package parser
2
+
3
+ import (
4
+ "golangchain/pkg/openai"
5
+ )
6
+
7
+ type StrOutputParser struct {
8
+ }
9
+
10
+ func NewStrOutputParser () * StrOutputParser {
11
+ return & StrOutputParser {}
12
+ }
13
+
14
+ func (p * StrOutputParser ) Invoke (input any ) (any , error ) {
15
+ var output string
16
+ res , ok := input .(* openai.Response )
17
+ if ok {
18
+ output = res .Choices [0 ].Message .Content
19
+ } else {
20
+ return nil , nil
21
+ }
22
+ return output , nil
23
+ }
Original file line number Diff line number Diff line change
1
+ package parser
2
+
3
+ import (
4
+ "golangchain/pkg/openai"
5
+ "testing"
6
+ )
7
+
8
+ func TestInvoke (t * testing.T ) {
9
+ tests := []struct {
10
+ name string
11
+ input any
12
+ expected string
13
+ }{
14
+ {
15
+ name : "template is string" ,
16
+ input : & openai.Response {
17
+ Choices : []openai.Choice {
18
+ {Message : openai.Message {Role : "assistant" , Content : "This is the test response" }},
19
+ },
20
+ },
21
+ expected : "This is the test response" ,
22
+ },
23
+ }
24
+ for _ , tc := range tests {
25
+ t .Run (tc .name , func (t * testing.T ) {
26
+ parser := NewStrOutputParser ()
27
+ have , err := parser .Invoke (tc .input )
28
+ if err != nil {
29
+ t .Fatalf ("Error happens: %v" , err )
30
+ }
31
+ if have != tc .expected {
32
+ t .Fatalf ("unexpected string: %v != %v" , have , tc .expected )
33
+ }
34
+ })
35
+ }
36
+ }
You can’t perform that action at this time.
0 commit comments