diff --git a/addition/web/call.go b/addition/web/call.go index 1d4ef17..a3c4131 100644 --- a/addition/web/call.go +++ b/addition/web/call.go @@ -11,7 +11,7 @@ import ( type Hook func(message []globals.Message, token int) (string, error) func ChatWithWeb(message []globals.Message, long bool) []globals.Message { - data := SearchBing(GetPointByLatestMessage(message)) + data := SearchWebResult(GetPointByLatestMessage(message)) if long { data = utils.GetSegmentString(data, 6000) @@ -20,8 +20,8 @@ func ChatWithWeb(message []globals.Message, long bool) []globals.Message { } return utils.Insert(message, 0, globals.Message{ Role: globals.System, - Content: fmt.Sprintf("你将扮演AI问答助手,你的知识库不是离线的,而是可以实时联网的,你可以提供实时联网的信息。"+ - "当前时间: %s, 实时联网搜索结果:%s", + Content: fmt.Sprintf("You will play the role of an AI Q&A assistant, where your knowledge base is not offline, but can be networked in real time, and you can provide real-time networked information with links to networked search sources."+ + "Current time: %s, Real-time internet search results: %s", time.Now().Format("2006-01-02 15:04:05"), data, ), }) diff --git a/addition/web/duckduckgo.go b/addition/web/duckduckgo.go new file mode 100644 index 0000000..d2b6a73 --- /dev/null +++ b/addition/web/duckduckgo.go @@ -0,0 +1,40 @@ +package web + +import ( + "chat/utils" + "fmt" + "github.com/spf13/viper" + "net/url" + "strings" +) + +type DDGResponse struct { + Results []struct { + Body string `json:"body"` + Href string `json:"href"` + Title string `json:"title"` + } `json:"results"` +} + +func formatResponse(data *DDGResponse) string { + res := make([]string, 0) + for _, item := range data.Results { + if item.Body == "" || item.Href == "" || item.Title == "" { + continue + } + + res = append(res, fmt.Sprintf("%s (%s): %s", item.Title, item.Href, item.Body)) + } + + return strings.Join(res, "\n") +} + +func CallDuckDuckGoAPI(query string) *DDGResponse { + query = url.QueryEscape(query) + data, err := utils.Get(fmt.Sprintf("%s/search?q=%s&max_results=%d", viper.GetString("system.ddg"), query, viper.GetInt("system.ddg_max_results")), nil) + if err != nil { + return nil + } + + return utils.MapToStruct[DDGResponse](data) +} diff --git a/addition/web/search.go b/addition/web/search.go index 458a390..7e6ad20 100644 --- a/addition/web/search.go +++ b/addition/web/search.go @@ -22,7 +22,13 @@ func RequestWithUA(url string) string { return data } -func SearchBing(q string) string { +func SearchWebResult(q string) string { + if res := CallDuckDuckGoAPI(q); res != nil { + if resp := formatResponse(res); resp != "" { + return resp + } + } + uri := GetBingUrl(q) if res := CallPilotAPI(uri); res != nil { return utils.Marshal(res.Results) diff --git a/auth/plan.go b/auth/plan.go index f656cb9..b271f95 100644 --- a/auth/plan.go +++ b/auth/plan.go @@ -61,9 +61,7 @@ var Plans = []Plan{ // enterprise Level: 4, Price: 999, - Usage: []PlanUsage{ - {Id: "sparkdesk", Value: -1, Including: globals.IsSparkDeskModel}, - }, + Usage: []PlanUsage{}, }, }