mirror of
https://github.com/coaidev/coai.git
synced 2025-05-21 05:50:14 +09:00
feat: duckduckgo web searching
This commit is contained in:
parent
41eb7c9d1a
commit
9e90acfd26
@ -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,
|
||||
),
|
||||
})
|
||||
|
40
addition/web/duckduckgo.go
Normal file
40
addition/web/duckduckgo.go
Normal 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)
|
||||
}
|
@ -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)
|
||||
|
@ -61,9 +61,7 @@ var Plans = []Plan{
|
||||
// enterprise
|
||||
Level: 4,
|
||||
Price: 999,
|
||||
Usage: []PlanUsage{
|
||||
{Id: "sparkdesk", Value: -1, Including: globals.IsSparkDeskModel},
|
||||
},
|
||||
Usage: []PlanUsage{},
|
||||
},
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user