-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
157 lines (146 loc) · 3.77 KB
/
main.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package main
import (
"LinguaDB/initialization"
"LinguaDB/lingua"
"LinguaDB/model"
"fmt"
"github.com/gin-gonic/gin"
"github.com/spf13/pflag"
"log"
"net/http"
)
// question := "司龄4年,工龄25年,我有多少天年假"
// question2 := "我迟到了半小时会怎么样"
// question3 := "中午几点点外卖比较合适"
// question4 := "公司可以抽烟吗"
// question5 := "内部分享是什么"
// question6 := "我想联系HR或者行政,他们叫什么名字,怎么联系"
// question7 := "什么时候发工资"
// question8 := "公司的组织架构"
// question9 := "公司有哪些福利呀"
// question10 := "产假怎么休?"
// question11 := "应届生刚工作如何适应"
// question12 := "我入职5个月了,我的团建费攒多少了"
var config *initialization.Config
func main() {
var (
cfg = pflag.StringP("config", "c", "./config.yaml", "config file path")
)
pflag.Parse()
config = initialization.LoadConfig(*cfg)
//lingua.LoadDOC(*config)
r := gin.Default()
r.GET("/ping", PingHandler)
r.POST("/insert", InsertHandler)
r.POST("/query", QueryHandler)
r.POST("/deleteDB", DeleteDBHandler)
r.POST("/deletePoints", DeletePointsHandler)
err := initialization.StartHTTPServer(*config, r)
if err != nil {
log.Printf("failed to start server: %v", err)
}
}
func PingHandler(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
})
}
func QueryHandler(context *gin.Context) {
var query model.Query
query.UseCache = false
err := context.ShouldBindJSON(&query)
if err != nil {
context.JSON(http.StatusBadRequest, gin.H{
"err": err.Error(),
})
return
}
fmt.Printf("info: %#v\n", query)
question := query.Question
useCache := query.UseCache
dbName := query.DbName
answer, filename, err := lingua.Query(*config, question, useCache, dbName)
if err != nil {
log.Printf("回答失败:%v", err.Error())
context.JSON(http.StatusInternalServerError, gin.H{
"err": err.Error(),
})
return
}
context.JSON(http.StatusOK, gin.H{
"answer": answer,
"filename": filename,
})
}
func InsertHandler(context *gin.Context) {
var insert model.Insert
err := context.ShouldBindJSON(&insert)
if err != nil {
context.JSON(http.StatusBadRequest, gin.H{
"err": err.Error(),
})
return
}
fmt.Printf("info: %#v\n", insert)
dbName := insert.DbName
dirName := insert.DirName
err = lingua.LoadDOC(*config, dbName, dirName)
if err != nil {
log.Printf("插入失败:%v", err.Error())
context.JSON(http.StatusInternalServerError, gin.H{
"err": err.Error(),
})
return
}
context.JSON(http.StatusOK, gin.H{
"info": "文档插入成功",
})
}
func DeleteDBHandler(context *gin.Context) {
var deleteDB model.DeleteDB
err := context.ShouldBindJSON(&deleteDB)
if err != nil {
fmt.Println(err.Error())
context.JSON(http.StatusBadRequest, gin.H{
"err": err.Error(),
})
return
}
fmt.Printf("info: %#v\n", deleteDB)
dbName := deleteDB.DbName
err = lingua.DeleteDB(*config, dbName)
if err != nil {
log.Printf("删除目标库失败:%v", err.Error())
context.JSON(http.StatusInternalServerError, gin.H{
"err": err.Error(),
})
return
}
context.JSON(http.StatusOK, gin.H{
"info": "删除目标库成功",
})
}
func DeletePointsHandler(context *gin.Context) {
var deletePoints model.DeletePoints
err := context.ShouldBindJSON(&deletePoints)
if err != nil {
context.JSON(http.StatusBadRequest, gin.H{
"err": err.Error(),
})
return
}
fmt.Printf("info: %#v\n", deletePoints)
dbName := deletePoints.DbName
dirName := deletePoints.DirName
err = lingua.DeletePoints(*config, dbName, dirName)
if err != nil {
log.Printf("删除失败:%v", err.Error())
context.JSON(http.StatusInternalServerError, gin.H{
"err": err.Error(),
})
return
}
context.JSON(http.StatusOK, gin.H{
"info": "删除成功",
})
}