bing search

This commit is contained in:
Sakurasan
2024-06-04 00:57:37 +08:00
parent a3920f11fa
commit afbc8e008b
2 changed files with 91 additions and 0 deletions

View File

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