update chatnio bing service

This commit is contained in:
Zhang Minghan 2023-10-06 10:49:40 +08:00
parent 80adbca078
commit a8dad47dd0
3 changed files with 12 additions and 31 deletions

View File

@ -20,10 +20,11 @@ func (c *ChatInstance) CreateStreamChatRequest(props *ChatProps, hook globals.Ho
defer conn.DeferClose() defer conn.DeferClose()
model, _ := strings.CutPrefix(props.Model, "bing-") model, _ := strings.CutPrefix(props.Model, "bing-")
prompt := props.Message[len(props.Message)-1].Content
if err := conn.SendJSON(&ChatRequest{ if err := conn.SendJSON(&ChatRequest{
Prompt: props.Message[len(props.Message)-1].Content, Prompt: prompt,
Cookies: c.Cookies, Hash: utils.Md5Encrypt(fmt.Sprintf(prompt + c.Secret)),
Model: model, Model: model,
}); err != nil { }); err != nil {
return err return err
} }
@ -34,23 +35,8 @@ func (c *ChatInstance) CreateStreamChatRequest(props *ChatProps, hook globals.Ho
return nil return nil
} }
if form.Error != "" && form.End {
return fmt.Errorf("bing error: %s", form.Error)
}
if err := hook(form.Response); err != nil { if err := hook(form.Response); err != nil {
return err return err
} }
if len(form.Suggested) > 0 {
message := ""
for _, suggested := range form.Suggested {
message += fmt.Sprintf("- %s\n", suggested)
}
if err := hook(fmt.Sprintf("\n\n%s", message)); err != nil {
return err
}
}
} }
} }

View File

@ -1,28 +1,26 @@
package bing package bing
import ( import (
"chat/utils"
"fmt" "fmt"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
type ChatInstance struct { type ChatInstance struct {
Endpoint string Endpoint string
Cookies *map[string]interface{} Secret string
} }
func (c *ChatInstance) GetEndpoint() string { func (c *ChatInstance) GetEndpoint() string {
return fmt.Sprintf("%s/chat", c.Endpoint) return fmt.Sprintf("%s/chat", c.Endpoint)
} }
func NewChatInstance(endpoint, cookies string) *ChatInstance { func NewChatInstance(endpoint, secret string) *ChatInstance {
form := utils.UnmarshalForm[map[string]interface{}](cookies)
return &ChatInstance{ return &ChatInstance{
Endpoint: endpoint, Endpoint: endpoint,
Cookies: form, Secret: secret,
} }
} }
func NewChatInstanceFromConfig() *ChatInstance { func NewChatInstanceFromConfig() *ChatInstance {
return NewChatInstance(viper.GetString("bing.endpoint"), viper.GetString("bing.cookies")) return NewChatInstance(viper.GetString("bing.endpoint"), viper.GetString("bing.secret"))
} }

View File

@ -3,14 +3,11 @@ package bing
// see https://github.com/Deeptrain-Community/chatnio-bing-service // see https://github.com/Deeptrain-Community/chatnio-bing-service
type ChatRequest struct { type ChatRequest struct {
Prompt string `json:"prompt"` Prompt string `json:"prompt"`
Cookies *map[string]interface{} `json:"cookies"` Hash string `json:"hash"`
Model string `json:"model"` Model string `json:"model"`
} }
type ChatResponse struct { type ChatResponse struct {
Response string `json:"response"` Response string `json:"response"`
Suggested []string `json:"suggested"`
Error string `json:"error"`
End bool `json:"end"`
} }