bing search
This commit is contained in:
69
pkg/search/bing.go
Normal file
69
pkg/search/bing.go
Normal file
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
文档 https://www.microsoft.com/en-us/bing/apis/bing-web-search-api
|
||||
价格 https://www.microsoft.com/en-us/bing/apis/pricing
|
||||
|
||||
curl -H "Ocp-Apim-Subscription-Key: <yourkeygoeshere>" https://api.bing.microsoft.com/v7.0/search?q=今天上海天气怎么样
|
||||
curl -H "Ocp-Apim-Subscription-Key: 6fc7c97ebed54f75a5e383ee2272c917" https://api.bing.microsoft.com/v7.0/search?q=今天上海天气怎么样
|
||||
*/
|
||||
|
||||
package search
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
|
||||
"github.com/tidwall/gjson"
|
||||
)
|
||||
|
||||
const (
|
||||
bingEndpoint = "https://api.bing.microsoft.com/v7.0/search"
|
||||
)
|
||||
|
||||
var subscriptionKey string
|
||||
|
||||
func init() {
|
||||
if os.Getenv("bing") != "" {
|
||||
subscriptionKey = os.Getenv("bing")
|
||||
}
|
||||
}
|
||||
|
||||
func BingSearch(searchParams SearchParams) (any, error) {
|
||||
params := url.Values{}
|
||||
params.Set("q", searchParams.Query)
|
||||
params.Set("count", "5")
|
||||
if searchParams.Num > 0 {
|
||||
params.Set("count", fmt.Sprintf("%d", searchParams.Num))
|
||||
}
|
||||
|
||||
reqURL, _ := url.Parse(bingEndpoint)
|
||||
reqURL.RawQuery = params.Encode()
|
||||
|
||||
req, _ := http.NewRequest("GET", reqURL.String(), nil)
|
||||
req.Header.Set("Ocp-Apim-Subscription-Key", subscriptionKey)
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
defer resp.Body.Close()
|
||||
if err != nil {
|
||||
fmt.Println("Error sending request:", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
fmt.Println("Error reading response:", err)
|
||||
return nil, err
|
||||
}
|
||||
result := gjson.ParseBytes(body).Get("webPages.value")
|
||||
|
||||
return result.Raw, nil
|
||||
|
||||
}
|
||||
|
||||
type SearchParams struct {
|
||||
Query string `form:"q"`
|
||||
Num int `form:"num,default=5"`
|
||||
}
|
||||
@@ -1,12 +1,14 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"opencatd-open/pkg/claude"
|
||||
"opencatd-open/pkg/google"
|
||||
"opencatd-open/pkg/openai"
|
||||
"opencatd-open/pkg/search"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
@@ -18,6 +20,26 @@ func ChatHandler(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
if chatreq.Messages[len(chatreq.Messages)-1].Role == "user" {
|
||||
result, err := search.BingSearch(search.SearchParams{Query: string(chatreq.Messages[len(chatreq.Messages)-1].Content)})
|
||||
if err == nil {
|
||||
var msgs []openai.ChatCompletionMessage
|
||||
for _, m := range chatreq.Messages {
|
||||
var buf bytes.Buffer
|
||||
buf.WriteString("根据我提问的语言回答我,我将提供一些从搜索引擎获取的信息。你自行判断是否使用搜索引擎获取的内容。不要原封不动照抄,根据你自己的知识库提炼信息之后回答我\n\n")
|
||||
if m.Role == "system" {
|
||||
buf.Write(m.Content)
|
||||
msgs = append(msgs, openai.ChatCompletionMessage{Role: m.Role, Content: buf.Bytes()})
|
||||
} else {
|
||||
msgs = append(msgs, openai.ChatCompletionMessage{Role: m.Role, Content: buf.Bytes()})
|
||||
}
|
||||
msgs = append(msgs, openai.ChatCompletionMessage{Role: m.Role, Content: m.Content})
|
||||
}
|
||||
msgs = append(msgs, openai.ChatCompletionMessage{Role: "tool", Content: []byte(result.(string))})
|
||||
chatreq.Messages = msgs
|
||||
}
|
||||
}
|
||||
|
||||
if strings.HasPrefix(chatreq.Model, "gpt") {
|
||||
openai.ChatProxy(c, &chatreq)
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user