Skip to content

Commit

Permalink
add save history model feature
Browse files Browse the repository at this point in the history
  • Loading branch information
zmh-program committed Oct 24, 2023
1 parent b9b0b94 commit 7b3880d
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 11 deletions.
9 changes: 9 additions & 0 deletions app/src/components/home/ModelSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { selectAuthenticated } from "../../store/auth.ts";
import { useToast } from "../ui/use-toast.ts";
import { useEffect } from "react";
import { Model } from "../../conversation/types.ts";
import { modelEvent } from "../../events/model.ts";

function GetModel(name: string): Model {
return supportModels.find((model) => model.id === name) as Model;
Expand All @@ -24,6 +25,14 @@ function ModelSelector() {
if (auth && model === "GPT-3.5") dispatch(setModel("GPT-3.5-16k"));
}, [auth]);

modelEvent.bind((target: string) => {
if (supportModels.find((m) => m.id === target)) {
if (model === target) return;
console.debug(`[chat] toggle model from event: ${target}`);
dispatch(setModel(target));
}
});

const list = supportModels.map(
(model: Model): SelectItemProps => ({
name: model.id,
Expand Down
9 changes: 6 additions & 3 deletions app/src/components/home/SideBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
} from "../../conversation/history.ts";
import { Button } from "../ui/button.tsx";
import { setMenu } from "../../store/menu.ts";
import {Copy, Eraser, LogIn, Plus, RotateCw} from "lucide-react";
import { Copy, Eraser, LogIn, Plus, RotateCw } from "lucide-react";
import ConversationSegment from "./ConversationSegment.tsx";
import {
AlertDialog,
Expand All @@ -32,7 +32,8 @@ import {
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle, AlertDialogTrigger,
AlertDialogTitle,
AlertDialogTrigger,
} from "../ui/alert-dialog.tsx";
import {
getSharedLink,
Expand Down Expand Up @@ -85,7 +86,9 @@ function SideBar() {
</AlertDialogTrigger>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>{t("conversation.remove-all-title")}</AlertDialogTitle>
<AlertDialogTitle>
{t("conversation.remove-all-title")}
</AlertDialogTitle>
<AlertDialogDescription>
{t("conversation.remove-all-description")}
</AlertDialogDescription>
Expand Down
2 changes: 1 addition & 1 deletion app/src/conf.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import axios from "axios";
import { Model } from "./conversation/types.ts";

export const version = "3.5.5";
export const version = "3.5.6";
export const dev: boolean = window.location.hostname === "localhost";
export const deploy: boolean = true;
export let rest_api: string = "http://localhost:8094";
Expand Down
22 changes: 21 additions & 1 deletion app/src/conversation/conversation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { ChatProps, Connection, StreamMessage } from "./connection.ts";
import { Message } from "./types.ts";
import { sharingEvent } from "../events/sharing.ts";
import { connectionEvent } from "../events/connection.ts";
import { AppDispatch } from "../store";
import { setMessages } from "../store/chat.ts";
import { modelEvent } from "../events/model.ts";

type ConversationCallback = (idx: number, message: Message[]) => boolean;

Expand All @@ -10,6 +13,7 @@ export class Conversation {
protected callback?: ConversationCallback;
protected idx: number;
public id: number;
public model: string;
public data: Message[];
public end: boolean;

Expand All @@ -18,6 +22,7 @@ export class Conversation {
this.data = [];
this.idx = -1;
this.id = id;
this.model = "";
this.end = true;
this.connection = new Connection(this.id);

Expand Down Expand Up @@ -108,10 +113,25 @@ export class Conversation {
this.callback = callback;
}

public setModel(model?: string) {
if (!model) return;
this.model = model;
}

public getModel(): string {
return this.model;
}

public toggle(dispatch: AppDispatch): void {
dispatch(setMessages(this.copyMessages()));
modelEvent.emit(this.getModel());
}

public triggerCallback() {
if (!this.callback) return;
const interval = setInterval(() => {
const state = this.callback && this.callback(this.id, this.copyMessages());
const state =
this.callback && this.callback(this.id, this.copyMessages());
if (state) clearInterval(interval);
}, 100);
}
Expand Down
6 changes: 5 additions & 1 deletion app/src/conversation/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ export async function loadConversation(
): Promise<ConversationInstance> {
const resp = await axios.get(`/conversation/load?id=${id}`);
if (resp.data.status) return resp.data.data as ConversationInstance;
return { id, name: "", message: [] };
return {
id,
name: "",
message: [{ role: "assistant", content: "load conversation failed" }],
};
}

export async function deleteConversation(
Expand Down
10 changes: 8 additions & 2 deletions app/src/conversation/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ export class Manager {
public createConversation(id: number): Conversation {
console.debug(`[manager] create conversation instance (id: ${id})`);
const _this = this;
return new Conversation(id, function (idx: number, message: Message[]): boolean {
return new Conversation(id, function (
idx: number,
message: Message[],
): boolean {
return _this.callback(idx, message);
});
}
Expand All @@ -66,15 +69,18 @@ export class Manager {
if (this.conversations[id]) return;
const instance = this.createConversation(id);
this.conversations[id] = instance;

const res = await loadConversation(id);
instance.load(res.message);
instance.setModel(res.model);
}

public async toggle(dispatch: AppDispatch, id: number): Promise<void> {
if (!this.conversations[id]) await this.add(id);

this.current = id;
dispatch(setCurrent(id));
dispatch(setMessages(this.get(id)!.copyMessages()));
this.get(id)!.toggle(dispatch);
}

public async delete(dispatch: AppDispatch, id: number): Promise<void> {
Expand Down
2 changes: 2 additions & 0 deletions app/src/conversation/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export type ConversationInstance = {
id: number;
name: string;
message: Message[];
model?: string;
shared?: boolean;
};

export type ConversationMapper = Record<Id, Conversation>;
5 changes: 5 additions & 0 deletions app/src/events/model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { EventCommitter } from "./struct.ts";

export const modelEvent = new EventCommitter<string>({
name: "model",
});
3 changes: 2 additions & 1 deletion app/src/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,8 @@ const resources = {
"remove-title": "是否确定?",
"remove-description": "此操作无法撤消。这将永久删除对话 ",
"remove-all-title": "清除历史",
"remove-all-description": "此操作无法撤消。这将永久删除所有对话,是否继续?",
"remove-all-description":
"此操作无法撤消。这将永久删除所有对话,是否继续?",
cancel: "取消",
delete: "删除",
"delete-conversation": "删除对话",
Expand Down
4 changes: 2 additions & 2 deletions manager/conversation/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ func LoadConversation(db *sql.DB, userId int64, conversationId int64) *Conversat
SELECT conversation_name, model, data FROM conversation
WHERE user_id = ? AND conversation_id = ?
`, userId, conversationId).Scan(&conversation.Name, &model, &data)
if value, ok := model.(string); ok {
conversation.Model = value
if value, ok := model.([]byte); ok {
conversation.Model = string(value)
} else {
conversation.Model = globals.GPT3Turbo
}
Expand Down

0 comments on commit 7b3880d

Please sign in to comment.