From dab9d4fa22f590ae62f7098f05a3e8357d0a8a0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?KevinZ=C3=B8nda?= <33132228+KevinZonda@users.noreply.github.com> Date: Wed, 17 Apr 2024 14:50:30 +0100 Subject: [PATCH] clean code --- backend/exec/svr/handler/continuousAsk.go | 20 ++++--------------- backend/exec/svr/handler/query.go | 12 +++--------- backend/exec/svr/handler/query_history.go | 4 ++-- backend/rag/serp/simple_spider.go | 2 +- backend/rag/serp/ua.go | 19 +++++++++++++++++- backend/utils/chat.go | 24 +++++++++++++++++++++++ backend/utils/gin.go | 6 ++++++ 7 files changed, 58 insertions(+), 29 deletions(-) create mode 100644 backend/utils/chat.go diff --git a/backend/exec/svr/handler/continuousAsk.go b/backend/exec/svr/handler/continuousAsk.go index d3ea118..162330c 100644 --- a/backend/exec/svr/handler/continuousAsk.go +++ b/backend/exec/svr/handler/continuousAsk.go @@ -73,22 +73,10 @@ func ContinuousAsk(c *gin.Context) { N: 1, Model: openai.GPT3Dot5Turbo1106, Messages: []openai.ChatCompletionMessage{ - { - Content: content, - Role: openai.ChatMessageRoleSystem, - }, - { - Content: prevAns.Question, - Role: openai.ChatMessageRoleUser, - }, - { - Content: prevAns.FirstAnswer, - Role: openai.ChatMessageRoleAssistant, - }, - { - Content: query.Question, - Role: openai.ChatMessageRoleUser, - }, + utils.ChatMsgFromSystem(content), + utils.ChatMsgFromUser(prevAns.Question), + utils.ChatMsgFromAssistant(prevAns.FirstAnswer), + utils.ChatMsgFromUser(query.Question), }, } diff --git a/backend/exec/svr/handler/query.go b/backend/exec/svr/handler/query.go index bfb8dfe..11396fe 100644 --- a/backend/exec/svr/handler/query.go +++ b/backend/exec/svr/handler/query.go @@ -51,14 +51,8 @@ func Search(c *gin.Context) { N: 1, Model: openai.GPT3Dot5Turbo1106, Messages: []openai.ChatCompletionMessage{ - { - Content: content, - Role: openai.ChatMessageRoleSystem, - }, - { - Content: query.Question, - Role: openai.ChatMessageRoleUser, - }, + utils.ChatMsgFromSystem(content), + utils.ChatMsgFromUser(query.Question), }, } @@ -74,7 +68,7 @@ func chatStreamToGin(c *gin.Context, req openai.ChatCompletionRequest) (complete resp, err := shared.Cli.CreateChatCompletionStream(context.Background(), req) if err != nil { - utils.GinErrorMsg(c, errors.New("LLM backend broken")) + utils.GinErrorMsgTxt(c, "LLM backend broken") log.Println(err) return } diff --git a/backend/exec/svr/handler/query_history.go b/backend/exec/svr/handler/query_history.go index fd7f36e..b75d402 100644 --- a/backend/exec/svr/handler/query_history.go +++ b/backend/exec/svr/handler/query_history.go @@ -13,7 +13,7 @@ import ( func QueryHistory(c *gin.Context) { id := c.Query("id") if id == "" { - utils.GinErrorMsg(c, errors.New("id not set")) + utils.GinErrorMsgTxt(c, "id not set") return } @@ -25,7 +25,7 @@ func QueryHistory(c *gin.Context) { }) return } - utils.GinErrorMsg(c, errors.New("db error")) + utils.GinErrorMsgTxt(c, "db error") return } diff --git a/backend/rag/serp/simple_spider.go b/backend/rag/serp/simple_spider.go index b4ba821..7e17029 100644 --- a/backend/rag/serp/simple_spider.go +++ b/backend/rag/serp/simple_spider.go @@ -89,7 +89,7 @@ func NewSimpleSpider() *SimpleSpider { Timeout: 5 * time.Second, }, Timeout: 5 * time.Second, - HttpReq: GoogleBotHeader, + HttpReq: RandomBotHeader, } } diff --git a/backend/rag/serp/ua.go b/backend/rag/serp/ua.go index 610c55d..e328621 100644 --- a/backend/rag/serp/ua.go +++ b/backend/rag/serp/ua.go @@ -1,6 +1,9 @@ package serp -import "net/http" +import ( + "math/rand/v2" + "net/http" +) const UA_GoogleBot = "Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; Googlebot/2.1; +http://www.google.com/bot.html) Chrome/120.0.0.0 Safari/537.36" const UA_BingBot = "Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm) Chrome//120.0.0.0 Safari/537.36" @@ -24,3 +27,17 @@ func BaiduBotHeader(req *http.Request) { req.Header.Add("Accept", "text/plain,text/html,*/*") req.Header.Add("Connection", "keep-alive") } + +func RandomBotHeader(req *http.Request) { + n := rand.IntN(3) + switch n { + case 0: + GoogleBotHeader(req) + case 1: + BingBotHeader(req) + case 2: + BaiduBotHeader(req) + default: + GoogleBotHeader(req) + } +} diff --git a/backend/utils/chat.go b/backend/utils/chat.go new file mode 100644 index 0000000..d8cc424 --- /dev/null +++ b/backend/utils/chat.go @@ -0,0 +1,24 @@ +package utils + +import "github.com/sashabaranov/go-openai" + +func ChatMsgFromUser(txt string) openai.ChatCompletionMessage { + return openai.ChatCompletionMessage{ + Role: openai.ChatMessageRoleUser, + Content: txt, + } +} + +func ChatMsgFromSystem(txt string) openai.ChatCompletionMessage { + return openai.ChatCompletionMessage{ + Role: openai.ChatMessageRoleSystem, + Content: txt, + } +} + +func ChatMsgFromAssistant(txt string) openai.ChatCompletionMessage { + return openai.ChatCompletionMessage{ + Role: openai.ChatMessageRoleAssistant, + Content: txt, + } +} diff --git a/backend/utils/gin.go b/backend/utils/gin.go index ec2c3ff..7dd78e9 100644 --- a/backend/utils/gin.go +++ b/backend/utils/gin.go @@ -7,3 +7,9 @@ func GinErrorMsg(c *gin.Context, err error) { "message": err.Error(), }) } + +func GinErrorMsgTxt(c *gin.Context, err string) { + c.JSON(500, gin.H{ + "message": err, + }) +}