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()
model, _ := strings.CutPrefix(props.Model, "bing-")
prompt := props.Message[len(props.Message)-1].Content
if err := conn.SendJSON(&ChatRequest{
Prompt: props.Message[len(props.Message)-1].Content,
Cookies: c.Cookies,
Model: model,
Prompt: prompt,
Hash: utils.Md5Encrypt(fmt.Sprintf(prompt + c.Secret)),
Model: model,
}); err != nil {
return err
}
@ -34,23 +35,8 @@ func (c *ChatInstance) CreateStreamChatRequest(props *ChatProps, hook globals.Ho
return nil
}
if form.Error != "" && form.End {
return fmt.Errorf("bing error: %s", form.Error)
}
if err := hook(form.Response); err != nil {
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
import (
"chat/utils"
"fmt"
"github.com/spf13/viper"
)
type ChatInstance struct {
Endpoint string
Cookies *map[string]interface{}
Secret string
}
func (c *ChatInstance) GetEndpoint() string {
return fmt.Sprintf("%s/chat", c.Endpoint)
}
func NewChatInstance(endpoint, cookies string) *ChatInstance {
form := utils.UnmarshalForm[map[string]interface{}](cookies)
func NewChatInstance(endpoint, secret string) *ChatInstance {
return &ChatInstance{
Endpoint: endpoint,
Cookies: form,
Secret: secret,
}
}
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
type ChatRequest struct {
Prompt string `json:"prompt"`
Cookies *map[string]interface{} `json:"cookies"`
Model string `json:"model"`
Prompt string `json:"prompt"`
Hash string `json:"hash"`
Model string `json:"model"`
}
type ChatResponse struct {
Response string `json:"response"`
Suggested []string `json:"suggested"`
Error string `json:"error"`
End bool `json:"end"`
Response string `json:"response"`
}