Skip to content

Commit

Permalink
clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinZonda committed Apr 17, 2024
1 parent d42ca61 commit dab9d4f
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 29 deletions.
20 changes: 4 additions & 16 deletions backend/exec/svr/handler/continuousAsk.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
},
}

Expand Down
12 changes: 3 additions & 9 deletions backend/exec/svr/handler/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
},
}

Expand All @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions backend/exec/svr/handler/query_history.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -25,7 +25,7 @@ func QueryHistory(c *gin.Context) {
})
return
}
utils.GinErrorMsg(c, errors.New("db error"))
utils.GinErrorMsgTxt(c, "db error")
return
}

Expand Down
2 changes: 1 addition & 1 deletion backend/rag/serp/simple_spider.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func NewSimpleSpider() *SimpleSpider {
Timeout: 5 * time.Second,
},
Timeout: 5 * time.Second,
HttpReq: GoogleBotHeader,
HttpReq: RandomBotHeader,
}
}

Expand Down
19 changes: 18 additions & 1 deletion backend/rag/serp/ua.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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)
}
}
24 changes: 24 additions & 0 deletions backend/utils/chat.go
Original file line number Diff line number Diff line change
@@ -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,
}
}
6 changes: 6 additions & 0 deletions backend/utils/gin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
}

0 comments on commit dab9d4f

Please sign in to comment.