Skip to content

Commit

Permalink
feat: support apply chat params in restart action (coaidev#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
zmh-program committed Mar 13, 2024
1 parent 40cb567 commit bd9b06e
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 13 deletions.
4 changes: 2 additions & 2 deletions addition/web/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"chat/manager/conversation"
)

func UsingWebSegment(instance *conversation.Conversation) []globals.Message {
segment := conversation.CopyMessage(instance.GetChatMessage())
func UsingWebSegment(instance *conversation.Conversation, restart bool) []globals.Message {
segment := conversation.CopyMessage(instance.GetChatMessage(restart))

if instance.IsEnableWeb() {
segment = ChatWithWeb(segment)
Expand Down
11 changes: 6 additions & 5 deletions app/src/api/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,20 +128,21 @@ export class Connection {
});
}

public sendEvent(t: any, event: string, data?: string) {
public sendEvent(t: any, event: string, data?: string, props?: ChatProps) {
this.sendWithRetry(t, {
type: event,
message: data || "",
model: "event",
...props,
});
}

public sendStopEvent(t: any) {
this.sendEvent(t, "stop");
}

public sendRestartEvent(t: any) {
this.sendEvent(t, "restart");
public sendRestartEvent(t: any, data?: ChatProps) {
this.sendEvent(t, "restart", undefined, data);
}

public sendMaskEvent(t: any, mask: Mask) {
Expand Down Expand Up @@ -239,9 +240,9 @@ export class ConnectionStack {
conn && conn.sendStopEvent(t);
}

public sendRestartEvent(id: number, t: any) {
public sendRestartEvent(id: number, t: any, data?: ChatProps) {
const conn = this.getConnection(id);
conn && conn.sendRestartEvent(t);
conn && conn.sendRestartEvent(t, data);
}

public sendMaskEvent(id: number, t: any, mask: Mask) {
Expand Down
15 changes: 14 additions & 1 deletion app/src/store/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,20 @@ export function useMessageActions() {
if (!stack.hasConnection(current)) {
stack.createConnection(current);
}
stack.sendRestartEvent(current, t);
stack.sendRestartEvent(current, t, {
web,
model,
context: history,
ignore_context: !context,
max_tokens,
temperature,
top_p,
top_k,
presence_penalty,
frequency_penalty,
repetition_penalty,
message: "",
});

// remove the last message if it's from assistant and create a new message
dispatch(restartMessage(current));
Expand Down
4 changes: 2 additions & 2 deletions manager/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func MockStreamSender(conn *Connection, message string) {
})
}

func ChatHandler(conn *Connection, user *auth.User, instance *conversation.Conversation) string {
func ChatHandler(conn *Connection, user *auth.User, instance *conversation.Conversation, restart bool) string {
defer func() {
if err := recover(); err != nil {
stack := debug.Stack()
Expand All @@ -70,7 +70,7 @@ func ChatHandler(conn *Connection, user *auth.User, instance *conversation.Conve
cache := conn.GetCache()

model := instance.GetModel()
segment := adapter.ClearMessages(model, web.UsingWebSegment(instance))
segment := adapter.ClearMessages(model, web.UsingWebSegment(instance, restart))

check, plan := auth.CanEnableModelWithSubscription(db, cache, user, model)
conn.Send(globals.ChatSegmentResponse{
Expand Down
23 changes: 22 additions & 1 deletion manager/conversation/conversation.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,28 @@ func (c *Conversation) GetMessageSegment(length int) []globals.Message {
return c.Message[len(c.Message)-length:]
}

func (c *Conversation) GetChatMessage() []globals.Message {
func (c *Conversation) GetChatMessage(restart bool) []globals.Message {
if restart {
// remove all last `assistant` role message
cp := CopyMessage(c.Message)

var index int
for index = len(cp) - 1; index >= 0; index-- {
if cp[index].Role != globals.Assistant {
break
}
}
if index >= 0 {
cp = cp[:index+1]
}

if c.GetContextLength() > len(cp) {
return cp
}

return cp[len(cp)-c.GetContextLength():]
}

return c.GetMessageSegment(c.GetContextLength())
}

Expand Down
7 changes: 5 additions & 2 deletions manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,18 @@ func ChatAPI(c *gin.Context) {
switch form.Type {
case ChatType:
if instance.HandleMessage(db, form) {
response := ChatHandler(buf, user, instance)
response := ChatHandler(buf, user, instance, false)
instance.SaveResponse(db, response)
}
case StopType:
break
case ShareType:
instance.LoadSharing(db, form.Message)
case RestartType:
response := ChatHandler(buf, user, instance)
// reset the params if set
instance.ApplyParam(form)

response := ChatHandler(buf, user, instance, true)
instance.SaveResponse(db, response)
case MaskType:
instance.LoadMask(form.Message)
Expand Down

0 comments on commit bd9b06e

Please sign in to comment.