Skip to content

Commit

Permalink
feat: 更新版本schema
Browse files Browse the repository at this point in the history
  • Loading branch information
bincooo committed May 13, 2024
1 parent 3c3c3ac commit 7055727
Show file tree
Hide file tree
Showing 10 changed files with 564 additions and 388 deletions.
108 changes: 65 additions & 43 deletions chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,28 @@ type wsConn struct {
IsClose bool
}

func (ws *wsConn) Close() {
ws.IsClose = true
_ = ws.Conn.Close()
func (conn *wsConn) Close() {
conn.IsClose = true
_ = conn.Conn.Close()
}

func (conn *wsConn) ping() {
const s5 = 5 * time.Second
t := time.Now().Add(s5)
for {
if conn.IsClose {
return
}
// 5秒执行一次心跳
if time.Now().After(t) {
t = time.Now().Add(s5)
err := conn.WriteMessage(websocket.TextMessage, ping)
if err != nil {
return
}
}
time.Sleep(time.Second)
}
}

func NewDefaultOptions(cookie, middle string) (*Options, error) {
Expand Down Expand Up @@ -113,6 +132,17 @@ func (opts *Options) KievAuth(kievRPSSecAuth, rwBf string) *Options {
return opts
}

// 写作混合模式
func (opts *Options) Compose(flag bool, obj struct {
Fmt string
Length string
Tone string
}) *Options {
opts.compose = flag
opts.composeObj = obj
return opts
}

// 创建会话实例
func New(opts *Options) Chat {
if opts == nil {
Expand Down Expand Up @@ -152,7 +182,7 @@ func (c *Chat) GetSession() Conversation {

// 对话并回复
//
// ctx Context 控制器,promp string 当前对话,image KBlob 图片信息,previousMessages []map[string]string 历史记录
// ctx Context 控制器,promp string 当前对话,image KBlob 图片信息,previousMessages[] ChatMessage 历史记录
//
// previousMessages:
//
Expand All @@ -166,7 +196,7 @@ func (c *Chat) GetSession() Conversation {
// "text": "Hello, this is Bing. I am a chat mode ..."
// }
// ]
func (c *Chat) Reply(ctx context.Context, prompt string, image *KBlob, previousMessages []ChatMessage) (chan ChatResponse, error) {
func (c *Chat) Reply(ctx context.Context, text string, previousMessages []ChatMessage) (chan ChatResponse, error) {
c.mu.Lock()
if c.session == nil || c.session.ConversationId == "" || c.model == ModelSydney {
count := 1
Expand All @@ -183,14 +213,16 @@ func (c *Chat) Reply(ctx context.Context, prompt string, image *KBlob, previousM
c.session = conv
}

h, err := c.newHub(c.model, *c.session, prompt, image, previousMessages)
h, err := c.newHub(c.model, *c.session, text, previousMessages)
if err != nil {
c.mu.Unlock()
return nil, &ChatError{"data", err}
}

hub := map[string]any{
"arguments": []any{h},
"arguments": []any{
h,
},
"invocationId": strconv.Itoa(c.session.invocationId),
"target": "chat",
"type": 4,
Expand All @@ -216,24 +248,7 @@ func (c *Chat) Reply(ctx context.Context, prompt string, image *KBlob, previousM

message := make(chan ChatResponse)
go c.resolve(ctx, conn, message)
go func() {
const s5 = 5 * time.Second
t := time.Now().Add(s5)
for {
if conn.IsClose {
return
}
// 5秒执行一次心跳
if time.Now().After(t) {
t = time.Now().Add(s5)
err = conn.WriteMessage(websocket.TextMessage, ping)
if err != nil {
return
}
}
time.Sleep(time.Second)
}
}()
go conn.ping()
return message, nil
}

Expand All @@ -245,7 +260,7 @@ func (c *Chat) resolve(ctx context.Context, conn *wsConn, message chan ChatRespo

normal := false

h := func() bool {
eventHandler := func() bool {
// 轮询回复消息
_, marshal, err := conn.ReadMessage()
if err != nil {
Expand Down Expand Up @@ -322,12 +337,12 @@ func (c *Chat) resolve(ctx context.Context, conn *wsConn, message chan ChatRespo
}

// 处理消息
args0 := response.Args[0]
if args0.Messages == nil || len(*args0.Messages) == 0 {
arg0 := response.Args[0]
if arg0.Messages == nil || len(*arg0.Messages) == 0 {
return false
}

m := (*args0.Messages)[0]
m := (*arg0.Messages)[0]
if m.MessageType != "" && strings.Contains("InternalSearchQuery,InternalSearchResult,InternalLoaderMessage", m.MessageType) {
return false
}
Expand Down Expand Up @@ -361,7 +376,7 @@ func (c *Chat) resolve(ctx context.Context, conn *wsConn, message chan ChatRespo
}
return
default:
if h() {
if eventHandler() {
return
}
}
Expand Down Expand Up @@ -463,7 +478,7 @@ func (c *Chat) newConn() (*wsConn, error) {
}

// 构建对接参数
func (c *Chat) newHub(model string, conv Conversation, prompt string, image *KBlob, previousMessages []ChatMessage) (map[string]any, error) {
func (c *Chat) newHub(model string, conv Conversation, text string, previousMessages []ChatMessage) (map[string]any, error) {
var hub map[string]any
if c.notebook {
if err := json.Unmarshal(nbkHub, &hub); err != nil {
Expand All @@ -486,26 +501,37 @@ func (c *Chat) newHub(model string, conv Conversation, prompt string, image *KBl
} else {
tone = ModelPrecise
}
messageTypes := hub["allowedMessageTypes"].([]any)
h := func(str string) func(any) bool {
return func(item any) bool {
h := func(str string) func(interface{}) bool {
return func(item interface{}) bool {
return item == str
}
}
messageTypes := hub["allowedMessageTypes"].([]interface{})
messageTypes = del(messageTypes, h("SearchQuery"))
messageTypes = del(messageTypes, h("RenderCardRequest"))
messageTypes = del(messageTypes, h("InternalSearchQuery"))
messageTypes = del(messageTypes, h("InternalSearchResult"))
hub["allowedMessageTypes"] = messageTypes
hub["tone"] = tone

// if !c.notebook {
// hub["sliceIds"] = sliceIds
// }
} else {
hub["tone"] = model
}

if c.compose {
optionsSets := hub["optionsSets"].([]interface{})
optionsSets = append(optionsSets, "edgecompose")
hub["optionsSets"] = optionsSets

extraExtensionParameters := hub["extraExtensionParameters"].(map[string]interface{})
extraExtensionParameters["edge_compose_generate"] = map[string]string{
"Format": c.composeObj.Fmt,
"Length": c.composeObj.Length,
"Tone": c.composeObj.Tone,
"Action": "generate",
}
hub["extraExtensionParameters"] = extraExtensionParameters
}

plugins, err := c.LoadPlugins(c.plugins...)
if err != nil {
return nil, err
Expand Down Expand Up @@ -537,11 +563,7 @@ func (c *Chat) newHub(model string, conv Conversation, prompt string, image *KBl
message["timestamp"] = time.Now().Format("2006-01-02T15:04:05+08:00")
message["requestId"] = messageId
message["messageId"] = messageId
if image != nil {
message["imageUrl"] = "https://www.bing.com/images/blob?bcid=" + image.ProcessedBlobId
message["originalImageUrl"] = "https://www.bing.com/images/blob?bcid=" + image.BlobId
}
message["text"] = prompt
message["text"] = text

if conv.invocationId == 0 || model == ModelSydney {
// 处理历史消息
Expand Down
93 changes: 41 additions & 52 deletions chat.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"disable_emoji_spoken_text",
"enablemm",
"dv3sugg",
"autosave",
"iyxapbing",
"iycapbing",
"h3imaginative",
Expand All @@ -18,21 +17,13 @@
"dlbuc07",
"dlbuf03",
"preclsngnp",
"codeintfilev2",
"enflst",
"enpcktrk",
"rcaltimeans",
"imgsanitize",
"dv3suggv4",
"gasupcard",
"ldsummary",
"ldqa",
"sdretrieval",
"enable_user_consent",
"uquopt",
"enstmdfcf",
"gndlogcf",
"localreducehho",
"fluxmemcst"
],
"allowedMessageTypes": [
"ActionRequest",
"Chat",
"Context",
"Disengaged",
Expand All @@ -43,54 +34,44 @@
"AdsQuery",
"SemanticSerp",
"GenerateContentQuery",
"SearchQuery",
"GeneratedCode"
],
"sliceIds": [
"fast2cf",
"ntbkcf3",
"uxsimgcomb",
"visperfcf",
"qapsat40",
"ctenin",
"crtrcrtv",
"abv2specjs",
"codecrtv2",
"ttsq16128",
"tcrtrv3",
"voiceres2cf",
"caccra3pbrcf",
"bootstrapcf",
"inputdestf",
"suppelpol-c",
"cacprqlty2cf",
"rwt2",
"ctrlfiltconf",
"translsafa",
"424bicbceproc",
"124multi2ts0",
"0408defrai_v3",
"dismsg",
"0411trimbanv3",
"0215wcrwip",
"sunoupsellcf",
"0411dylbp",
"fpallsticy",
"0423suggv4",
"scprompt2",
"0306flowvaca",
"228pyfilev3",
"ecipc",
"418paydetecs0",
"0420bicmgs0",
"413bicsup",
"3daytonecf",
"cacshortcac5",
"sstopcf",
"0319incipcls0",
"schurmscf"
"0429winnoprs0",
"kcgmm2cf",
"0404redhoo",
"cacttcsabl"
],
"verbosity": "verbose",
"scenario": "SERP",
"plugins": [
],
"traceId": "662c72adfc494c8e82ad58def0f3e0af",
"plugins": [],
"traceId": "66411cfe050f4852b86b5e1d485d0a87",
"conversationHistoryOptionsSets": [
"autosave",
"savemem",
"uprofupd",
"uprofgen"
],
"gptId": "copilot",
"isStartOfSession": true,
"requestId": "3b32af30-81d6-9101-864d-496d291c7fcf",
"requestId": "1da4ff2c-a78e-d941-c1c7-81a9b9245b2c",
"message": {
"locale": "en-US",
"market": "en-US",
Expand Down Expand Up @@ -119,21 +100,29 @@
}
],
"userIpAddress": "67.21.82.247",
"timestamp": "2024-04-27T11:36:17+08:00",
"adaptiveCards": [
],
"timestamp": "2024-05-13T03:48:10+08:00",
"adaptiveCards": [],
"author": "user",
"inputMethod": "Keyboard",
"text": "",
"text": "hi",
"messageType": "Chat",
"requestId": "3b32af30-81d6-9101-864d-496d291c7fcf",
"messageId": "3b32af30-81d6-9101-864d-496d291c7fcf"
"requestId": "1da4ff2c-a78e-d941-c1c7-81a9b9245b2c",
"messageId": "1da4ff2c-a78e-d941-c1c7-81a9b9245b2c"
},
"tone": "Creative",
"extraExtensionParameters": {
"gpt-creator-persona": {
"personaId": "copilot"
},
"edge_compose_generate": {
"Action": "generate",
"Format": "paragraph",
"Length": "medium",
"Tone": "enthusiastic"
}
},
"spokenTextMode": "None",
"conversationId": "51D|BingProd|E0049A1A242C81724D3E499C3B67387F52F633255F5062C6921A59E890A9AF39",
"previousMessages": [
],
"conversationId": "51D|BingProd|216A33125D2312F36EA354F51152958ED920093525952210B03011C534EB6B91",
"participant": {
"id": "914802122017678"
}
Expand Down
Loading

0 comments on commit 7055727

Please sign in to comment.