mirror of
https://github.com/sqlchat/sqlchat.git
synced 2025-09-24 08:36:18 +08:00
feat: implement general assistant
This commit is contained in:
1
assistants/README.md
Normal file
1
assistants/README.md
Normal file
@ -0,0 +1 @@
|
|||||||
|
# Assistant bots
|
1
assistants/general-bot/README.md
Normal file
1
assistants/general-bot/README.md
Normal file
@ -0,0 +1 @@
|
|||||||
|
# General bot
|
10
assistants/general-bot/index.ts
Normal file
10
assistants/general-bot/index.ts
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
export default {
|
||||||
|
id: "general-bot",
|
||||||
|
name: "General bot",
|
||||||
|
description: "A general bot of SQL Chat.",
|
||||||
|
avatar: "",
|
||||||
|
getPrompt: (): string => {
|
||||||
|
const basicPrompt = `Please be careful to return only key information, and try not to make it too long.`;
|
||||||
|
return basicPrompt;
|
||||||
|
},
|
||||||
|
};
|
3
assistants/index.ts
Normal file
3
assistants/index.ts
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
export * as generalBot from "./general-bot";
|
||||||
|
export * as sqlchatBot from "./sql-chat-bot";
|
||||||
|
export * as migrationBot from "./migration-bot";
|
10
assistants/migration-bot/index.ts
Normal file
10
assistants/migration-bot/index.ts
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
export default {
|
||||||
|
id: "migration-bot",
|
||||||
|
name: "Migration bot",
|
||||||
|
description: "A bot focused on database migration.",
|
||||||
|
avatar: "",
|
||||||
|
getPrompt: (input?: string): string => {
|
||||||
|
// TODO: update prompt.
|
||||||
|
return "";
|
||||||
|
},
|
||||||
|
};
|
14
assistants/sql-chat-bot/index.ts
Normal file
14
assistants/sql-chat-bot/index.ts
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
import generalBot from "../general-bot";
|
||||||
|
|
||||||
|
export default {
|
||||||
|
id: "sql-chat-bot",
|
||||||
|
name: "SQL Chat bot",
|
||||||
|
description: "The wonderful SQL Chat bot.",
|
||||||
|
avatar: "",
|
||||||
|
getPrompt: (schema?: string): string => {
|
||||||
|
const generalPrompt = generalBot.getPrompt();
|
||||||
|
const basicPrompt = `Please follow the instructions to answer the questions:
|
||||||
|
1. Set the language to the markdown code block for each code block. For example, \`SELECT * FROM table\` is SQL.`;
|
||||||
|
return `${generalPrompt}\nThis is my database schema"${schema}". You will see the tables and columns in the database. And please answer the following questions about the database.\n${basicPrompt}`;
|
||||||
|
},
|
||||||
|
};
|
@ -1,30 +1,18 @@
|
|||||||
import { first } from "lodash-es";
|
import { first } from "lodash-es";
|
||||||
import { Id, User } from "@/types";
|
import { Assistant, Id } from "@/types";
|
||||||
|
import * as customAssistantList from "../../assistants";
|
||||||
|
|
||||||
// Assistant is a special user.
|
const assistantList: Assistant[] = Object.keys(customAssistantList).map((name) => {
|
||||||
export const assistantList: User[] = [
|
return {
|
||||||
{
|
...((customAssistantList as any)[name].default as Assistant),
|
||||||
id: "sql-assistant",
|
};
|
||||||
name: "SQL Chat",
|
});
|
||||||
description: "🤖️ I'm an expert in SQL. I can answer your questions about databases and SQL.",
|
|
||||||
avatar: "",
|
|
||||||
},
|
|
||||||
];
|
|
||||||
|
|
||||||
export const getAssistantById = (id: Id) => {
|
export const getAssistantById = (id: Id) => {
|
||||||
const user = assistantList.find((user) => user.id === id);
|
const assistant = assistantList.find((assistant) => assistant.id === id);
|
||||||
return user || (first(assistantList) as User);
|
return assistant || (first(assistantList) as Assistant);
|
||||||
};
|
};
|
||||||
|
|
||||||
// getPromptOfAssistant define the special prompt for each assistant.
|
export const getPromptGeneratorOfAssistant = (assistant: Assistant) => {
|
||||||
export const getPromptGeneratorOfAssistant = (assistant: User) => {
|
return assistant.getPrompt;
|
||||||
const basicPrompt = `Please follow the instructions to answer the questions:
|
|
||||||
1. Set the language to the markdown code block for each code block. For example, \`SELECT * FROM table\` is SQL.
|
|
||||||
2. Please be careful to return only key information, and try not to make it too long.
|
|
||||||
`;
|
|
||||||
if (assistant.id === "sql-assistant") {
|
|
||||||
return (schema: string) =>
|
|
||||||
`This is my database schema"${schema}". You will see the tables and columns in the database. And please answer the following questions about the database.\n${basicPrompt}`;
|
|
||||||
}
|
|
||||||
return () => `\n${basicPrompt}`;
|
|
||||||
};
|
};
|
||||||
|
@ -60,6 +60,22 @@ export const useConversationStore = create<ConversationState>()(
|
|||||||
}),
|
}),
|
||||||
{
|
{
|
||||||
name: "conversation-storage",
|
name: "conversation-storage",
|
||||||
|
version: 1,
|
||||||
|
migrate: (persistedState: any, version: number) => {
|
||||||
|
let state = persistedState as ConversationState;
|
||||||
|
if (version === 0) {
|
||||||
|
for (const conversation of state.conversationList) {
|
||||||
|
if (!conversation.connectionId) {
|
||||||
|
conversation.assistantId = "general-bot";
|
||||||
|
} else {
|
||||||
|
conversation.assistantId = "sql-chat-bot";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
state.currentConversation = undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
return state;
|
||||||
|
},
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
7
src/types/assistant.ts
Normal file
7
src/types/assistant.ts
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
export interface Assistant {
|
||||||
|
id: string;
|
||||||
|
name: string;
|
||||||
|
description: string;
|
||||||
|
avatar: string;
|
||||||
|
getPrompt: (input?: string) => string;
|
||||||
|
}
|
@ -7,3 +7,4 @@ export * from "./message";
|
|||||||
export * from "./setting";
|
export * from "./setting";
|
||||||
export * from "./api";
|
export * from "./api";
|
||||||
export * from "./connector";
|
export * from "./connector";
|
||||||
|
export * from "./assistant";
|
||||||
|
Reference in New Issue
Block a user