Skip to content

Commit

Permalink
add bing-creative, bing-balanced, bing-precise model (unstable)
Browse files Browse the repository at this point in the history
  • Loading branch information
zmh-program committed Oct 5, 2023
1 parent c9aa30b commit ce503ef
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 9 deletions.
6 changes: 6 additions & 0 deletions adapter/adapter.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package adapter

import (
"chat/adapter/bing"
"chat/adapter/chatgpt"
"chat/adapter/claude"
"chat/adapter/palm2"
Expand Down Expand Up @@ -61,6 +62,11 @@ func NewChatRequest(props *ChatProps, hook globals.Hook) error {
return slack.NewChatInstanceFromConfig().CreateStreamChatRequest(&slack.ChatProps{
Message: props.Message,
}, hook)
} else if globals.IsBingModel(props.Model) {
return bing.NewChatInstanceFromConfig().CreateStreamChatRequest(&bing.ChatProps{
Model: props.Model,
Message: props.Message,
}, hook)
}

return nil
Expand Down
56 changes: 56 additions & 0 deletions adapter/bing/chat.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package bing

import (
"chat/globals"
"chat/utils"
"fmt"
"strings"
)

type ChatProps struct {
Message []globals.Message
Model string
}

func (c *ChatInstance) CreateStreamChatRequest(props *ChatProps, hook globals.Hook) error {
var conn *utils.WebSocket
if conn = utils.NewWebsocketClient(c.GetEndpoint()); conn == nil {
return fmt.Errorf("sparkdesk error: websocket connection failed")
}
defer conn.DeferClose()

model, _ := strings.CutPrefix(props.Model, "bing-")
if err := conn.SendJSON(&ChatRequest{
Prompt: props.Message[len(props.Message)-1].Content,
Cookies: c.Cookies,
Model: model,
}); err != nil {
return err
}

for {
form := utils.ReadForm[ChatResponse](conn)
if form == nil {
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
}
}
}
}
22 changes: 18 additions & 4 deletions adapter/bing/struct.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
package bing

import (
"chat/globals"
"chat/utils"
"fmt"
"github.com/spf13/viper"
)

type ChatInstance struct {
Endpoint string
Cookies map[string]string
Cookies *map[string]interface{}
}

type ChatProps struct {
Message []globals.Message
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)
return &ChatInstance{
Endpoint: endpoint,
Cookies: form,
}
}

func NewChatInstanceFromConfig() *ChatInstance {
return NewChatInstance(viper.GetString("bing.endpoint"), viper.GetString("bing.cookies"))
}
16 changes: 16 additions & 0 deletions adapter/bing/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
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"`
}

type ChatResponse struct {
Response string `json:"response"`
Suggested []string `json:"suggested"`
Error string `json:"error"`
End bool `json:"end"`
}
13 changes: 13 additions & 0 deletions globals/variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ const (
ClaudeSlack = "claude-slack"
SparkDesk = "spark-desk"
ChatBison001 = "chat-bison-001"
BingCreative = "bing-creative"
BingBalanced = "bing-balanced"
BingPrecise = "bing-precise"
)

var GPT3TurboArray = []string{
Expand Down Expand Up @@ -63,6 +66,12 @@ var ClaudeModelArray = []string{
Claude2100k,
}

var BingModelArray = []string{
BingCreative,
BingBalanced,
BingPrecise,
}

var LongContextModelArray = []string{
GPT3Turbo16k,
GPT3Turbo16k0613,
Expand Down Expand Up @@ -119,6 +128,10 @@ func IsPalm2Model(model string) bool {
return model == ChatBison001
}

func IsBingModel(model string) bool {
return in(model, BingModelArray)
}

func IsLongContextModel(model string) bool {
return in(model, LongContextModelArray)
}
6 changes: 3 additions & 3 deletions manager/transhipment.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,9 @@ func getStreamTranshipmentForm(id string, created int64, form TranshipmentForm,
},
},
Usage: Usage{
PromptTokens: buffer.CountInputToken(),
CompletionTokens: buffer.CountOutputToken(),
TotalTokens: buffer.CountToken(),
PromptTokens: utils.MultiF(end, func() int { return buffer.CountInputToken() }, 0),
CompletionTokens: utils.MultiF(end, func() int { return buffer.CountOutputToken() }, 0),
TotalTokens: utils.MultiF(end, func() int { return buffer.CountToken() }, 0),
},
Quota: buffer.GetQuota(),
}
Expand Down
4 changes: 2 additions & 2 deletions utils/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ func Multi[T comparable](condition bool, tval, fval T) T {
}
}

func MultiF[T comparable](condition bool, tval, fval func() T) T {
func MultiF[T comparable](condition bool, tval func() T, fval T) T {
if condition {
return tval()
} else {
return fval()
return fval
}
}

0 comments on commit ce503ef

Please sign in to comment.