-
-
Notifications
You must be signed in to change notification settings - Fork 538
/
document_qa.go
57 lines (49 loc) · 1.56 KB
/
document_qa.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
package main
import (
"context"
"fmt"
"os"
"github.com/tmc/langchaingo/chains"
"github.com/tmc/langchaingo/llms/openai"
"github.com/tmc/langchaingo/schema"
)
func main() {
if err := run(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func run() error {
llm, err := openai.New()
if err != nil {
return err
}
// We can use LoadStuffQA to create a chain that takes input documents and a question,
// stuffs all the documents into the prompt of the llm and returns an answer to the
// question. It is suitable for a small number of documents.
stuffQAChain := chains.LoadStuffQA(llm)
docs := []schema.Document{
{PageContent: "Harrison went to Harvard."},
{PageContent: "Ankush went to Princeton."},
}
answer, err := chains.Call(context.Background(), stuffQAChain, map[string]any{
"input_documents": docs,
"question": "Where did Harrison go to collage?",
})
if err != nil {
return err
}
fmt.Println(answer)
// Another option is to use the refine documents chain for question answering. This
// chain iterates over the input documents one by one, updating an intermediate answer
// with each iteration. It uses the previous version of the answer and the next document
// as context. The downside of this type of chain is that it uses multiple llm calls that
// cant be done in parallel.
refineQAChain := chains.LoadRefineQA(llm)
answer, err = chains.Call(context.Background(), refineQAChain, map[string]any{
"input_documents": docs,
"question": "Where did Ankush go to collage?",
})
fmt.Println(answer)
return nil
}