-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.go
77 lines (64 loc) · 1.66 KB
/
query.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
package main
import (
"context"
"github.com/tmc/langchaingo/chains"
"github.com/tmc/langchaingo/vectorstores"
)
type RagSearchResponse struct {
FinalAnswer string `json:"final_answer"`
}
type SemanticSearchResult struct {
PageContent string `json:"page_content"`
Score float64 `json:"score"`
}
type SemanticSearchResponse struct {
Results []SemanticSearchResult `json:"results"`
}
type LoadRequest struct {
Source string `json:"source"`
}
type SearchRequest struct {
Query string `json:"query"`
MaxResults int `json:"maxResults"`
}
func ragSearch(question string, numOfResults int) (RagSearchResponse, error) {
store, err := getVectorStore()
if err != nil {
return RagSearchResponse{}, err
}
llm, err = getOllama()
if err != nil {
return RagSearchResponse{}, err
}
result, err := chains.Run(
context.Background(),
chains.NewRetrievalQAFromLLM(
llm,
vectorstores.ToRetriever(store, numOfResults),
),
question,
chains.WithMaxTokens(5120),
)
if err != nil {
return RagSearchResponse{}, err
}
return RagSearchResponse{FinalAnswer: result}, nil
}
func semanticSearch(searchQuery string, maxResults int) (SemanticSearchResponse, error) {
store, err := getVectorStore()
if err != nil {
return SemanticSearchResponse{}, err
}
searchResults, err := store.SimilaritySearch(context.Background(), searchQuery, maxResults)
if err != nil {
return SemanticSearchResponse{}, err
}
var results []SemanticSearchResult
for _, doc := range searchResults {
results = append(results, SemanticSearchResult{
PageContent: doc.PageContent,
Score: float64(doc.Score),
})
}
return SemanticSearchResponse{Results: results}, nil
}