From afbc8e008b22df7073d4d7028d81b0aaa74185a0 Mon Sep 17 00:00:00 2001 From: Sakurasan <26715255+Sakurasan@users.noreply.github.com> Date: Tue, 4 Jun 2024 00:57:37 +0800 Subject: [PATCH] bing search --- pkg/search/bing.go | 69 ++++++++++++++++++++++++++++++++++++++++++++++ router/chat.go | 22 +++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 pkg/search/bing.go diff --git a/pkg/search/bing.go b/pkg/search/bing.go new file mode 100644 index 0000000..a65dade --- /dev/null +++ b/pkg/search/bing.go @@ -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: " 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"` +} diff --git a/router/chat.go b/router/chat.go index 8d2cd8f..ec20464 100644 --- a/router/chat.go +++ b/router/chat.go @@ -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