Skip to content

Commit

Permalink
feat: better stop signal (#181)
Browse files Browse the repository at this point in the history
feat: better stop signal (#181)
Co-Authored-By: Minghan Zhang <[email protected]>
  • Loading branch information
Sh1n3zZ and zmh-program committed Jun 27, 2024
1 parent 9c596a9 commit 401de5a
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 232 deletions.
60 changes: 14 additions & 46 deletions addition/web/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ package web

import (
"chat/globals"
"chat/manager/conversation"
"chat/utils"
"fmt"
"strings"
"time"
)

type Hook func(message []globals.Message, token int) (string, error)

func ChatWithWeb(message []globals.Message) []globals.Message {
data := utils.GetSegmentString(
SearchWebResult(GetPointByLatestMessage(message)), 2048,
SearchWebResult(message[len(message)-1].Content), 2048,
)

return utils.Insert(message, 0, globals.Message{
Expand All @@ -24,52 +24,20 @@ func ChatWithWeb(message []globals.Message) []globals.Message {
})
}

func StringCleaner(content string) string {
for _, replacer := range []string{",", "、", ",", "。", ":", ":", ";", ";", "!", "!", "=", "?", "?", "(", ")", "(", ")", "关键字", "空", "1+1"} {
content = strings.ReplaceAll(content, replacer, " ")
}
return strings.TrimSpace(content)
}
func UsingWebSegment(instance *conversation.Conversation, restart bool) []globals.Message {
segment := conversation.CopyMessage(instance.GetChatMessage(restart))

func GetKeywordPoint(hook Hook, message []globals.Message) string {
resp, _ := hook([]globals.Message{{
Role: globals.System,
Content: "If the user input content require ONLINE SEARCH to get the results, please output these keywords to refine the data Interval with space, remember not to answer other content, json format return, format {\"keyword\": \"...\" }",
}, {
Role: globals.User,
Content: "你是谁",
}, {
Role: globals.Assistant,
Content: "{\"keyword\":\"\"}",
}, {
Role: globals.User,
Content: "那fystart起始页是什么 和深能科创有什么关系",
}, {
Role: globals.Assistant,
Content: "{\"keyword\":\"fystart起始页 深能科创 关系\"}",
}, {
Role: globals.User,
Content: "1+1=?",
}, {
Role: globals.Assistant,
Content: "{\"keyword\":\"\"}",
}, {
Role: globals.User,
Content: "?",
}, {
Role: globals.Assistant,
Content: "{\"keyword\":\"\"}",
}, {
Role: globals.User,
Content: message[len(message)-1].Content,
}}, 40)
keyword := utils.UnmarshalJson[map[string]interface{}](resp)
if keyword == nil {
return ""
if instance.IsEnableWeb() {
segment = ChatWithWeb(segment)
}
return StringCleaner(keyword["keyword"].(string))

return segment
}

func GetPointByLatestMessage(message []globals.Message) string {
return StringCleaner(message[len(message)-1].Content)
func UsingWebNativeSegment(enable bool, message []globals.Message) []globals.Message {
if enable {
return ChatWithWeb(message)
} else {
return message
}
}
68 changes: 0 additions & 68 deletions addition/web/parser.go

This file was deleted.

28 changes: 0 additions & 28 deletions addition/web/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,8 @@ import (
"chat/globals"
"chat/utils"
"fmt"
"net/url"
)

func GetBingUrl(q string) string {
return "https://bing.com/search?q=" + url.QueryEscape(q)
}

func RequestWithUA(url string) string {
data, err := utils.GetRaw(url, map[string]string{
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/116.0",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
})

if err != nil {
return ""
}

return data
}

func SearchReverse(q string) string {
// deprecated
uri := GetBingUrl(q)
if res := CallPilotAPI(uri); res != nil {
return utils.Marshal(res.Results)
}
data := RequestWithUA(uri)
return ParseBing(data)
}

func SearchWebResult(q string) string {
res, err := CallDuckDuckGoAPI(q)
if err != nil {
Expand Down
File renamed without changes.
24 changes: 0 additions & 24 deletions addition/web/utils.go

This file was deleted.

36 changes: 0 additions & 36 deletions addition/web/webpilot.go

This file was deleted.

Loading

0 comments on commit 401de5a

Please sign in to comment.