feat: duckduckgo web searching

This commit is contained in:
Zhang Minghan 2023-12-03 17:43:21 +08:00
parent 41eb7c9d1a
commit 9e90acfd26
4 changed files with 51 additions and 7 deletions

View File

@ -11,7 +11,7 @@ import (
type Hook func(message []globals.Message, token int) (string, error) type Hook func(message []globals.Message, token int) (string, error)
func ChatWithWeb(message []globals.Message, long bool) []globals.Message { func ChatWithWeb(message []globals.Message, long bool) []globals.Message {
data := SearchBing(GetPointByLatestMessage(message)) data := SearchWebResult(GetPointByLatestMessage(message))
if long { if long {
data = utils.GetSegmentString(data, 6000) 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{ return utils.Insert(message, 0, globals.Message{
Role: globals.System, Role: globals.System,
Content: fmt.Sprintf("你将扮演AI问答助手你的知识库不是离线的而是可以实时联网的你可以提供实时联网的信息。"+ 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."+
"当前时间: %s, 实时联网搜索结果:%s", "Current time: %s, Real-time internet search results: %s",
time.Now().Format("2006-01-02 15:04:05"), data, time.Now().Format("2006-01-02 15:04:05"), data,
), ),
}) })

View File

@ -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)
}

View File

@ -22,7 +22,13 @@ func RequestWithUA(url string) string {
return data 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) uri := GetBingUrl(q)
if res := CallPilotAPI(uri); res != nil { if res := CallPilotAPI(uri); res != nil {
return utils.Marshal(res.Results) return utils.Marshal(res.Results)

View File

@ -61,9 +61,7 @@ var Plans = []Plan{
// enterprise // enterprise
Level: 4, Level: 4,
Price: 999, Price: 999,
Usage: []PlanUsage{ Usage: []PlanUsage{},
{Id: "sparkdesk", Value: -1, Including: globals.IsSparkDeskModel},
},
}, },
} }