Skip to content

Commit 2c267b5

Browse files
committed
feat: AI助手现在可以知道用户当前所在页面
1 parent c0305ed commit 2c267b5

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

src/components/AI.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ const GREETTING =
2929
'你好, 我是 PsychPen 的 AI 助手, 可以帮你讲解 PsychPen 的使用方法、探索你的数据集、导出数据、跳转页面、变量标准化/中心化/离散化、生成新变量、筛选数据等. 请问有什么可以帮你的?'
3030
const INSTRUCTION =
3131
'你是在线统计分析和数据可视化软件"PsychPen"中的AI助手. \n\n你将收到用户的提问、当前用户导入到软件中的数据集中的变量的信息、PsychPen的使用和开发文档、可以供你调用的工具 (函数) 信息. \n\n你的任务是按照用户的要求, 对用户进行回复, 或调用工具 (函数). 在调用工具 (函数) 前, 请确保你已经明确知晓了用户的意图, 否则请通过进一步和用户对话来确认细节. \n\n你的回复中如果包含数学公式和符号, 请使用 TeX 语法, 并将行内公式用 `$` 包裹 (类似于 Markdown 的行内代码), 将块级公式用 `$$` 包裹 (类似于 Markdown 的代码块).'
32-
function GET_PROMPT(vars: Variable[]): string {
32+
function GET_PROMPT(
33+
vars: Variable[],
34+
page: string,
35+
): string {
3336
const varsInfo = vars.map((col) => {
3437
if (col.type === '称名或等级数据') {
3538
return `| ${col.name} | ${col.type} | ${col.valid} | ${col.missing} | ${col.unique} | - | - | - | - | - | - |`
@@ -47,9 +50,10 @@ function GET_PROMPT(vars: Variable[]): string {
4750
: '未定义任何子变量'
4851
return `| ${col.name} | ${col.type} | ${col.valid} | ${col.missing} | ${col.unique} | ${col.mean?.toFixed(4) || '-'} | ${col.std?.toFixed(4) || '-'} | ${col.q2?.toFixed(4) || '-'} | ${col.min?.toFixed(4) || '-'} | ${col.max?.toFixed(4) || '-'} | ${subVarInfo} |`
4952
})
53+
const userText = `\n\n# 用户信息\n\n用户当前所处的页面为: ${page}`
5054
const varsText = `\n\n# 变量信息\n\n| 变量名 | 变量类型 | 有效值数量 | 缺失值数量 | 唯一值数量 | 均值 | 标准差 | 中位数 | 最小值 | 最大值 | 子变量信息 |\n| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |\n${varsInfo.join('\n')}`
5155
const docsText = `\n\n# 使用文档\n\n\`\`\`markdown\n${readme}\n\`\`\``
52-
return INSTRUCTION + varsText + docsText
56+
return INSTRUCTION + userText + varsText + docsText
5357
}
5458

5559
export function AI() {
@@ -96,7 +100,7 @@ export function AI() {
96100
setMessages([...old, user])
97101
setInput('')
98102
})
99-
const system = GET_PROMPT(dataCols)
103+
const system = GET_PROMPT(dataCols, nav.currentPageInfo())
100104

101105
const stream = await ai.chat.completions.create({
102106
model: model,

src/lib/hooks/useNav.tsx

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,8 @@ export const TOOLS_VIEW_SUB_PAGES_ELEMENTS: Record<
231231
}
232232

233233
type NavState = {
234+
currentPageInfo: () => string
235+
234236
mainPage: React.ReactElement
235237
activeMainPage: MAIN_PAGES_LABELS
236238
setMainPage: (page: MAIN_PAGES_LABELS) => void
@@ -258,7 +260,24 @@ const DEFAULT_STATISTICS_SUB_PAGE =
258260
STATISTICS_SUB_PAGES_LABELS.ONE_SAMPLE_T_TEST
259261
const DEFAULT_TOOLS_SUB_PAGE = TOOLS_VIEW_SUB_PAGES_LABELS.STATISTIC_TO_PVALUE
260262

261-
export const useNav = create<NavState>()((setState) => ({
263+
export const useNav = create<NavState>()((setState, getState) => ({
264+
currentPageInfo: () => {
265+
const state = getState()
266+
switch (state.activeMainPage) {
267+
case MAIN_PAGES_LABELS.DATA:
268+
return '数据视图'
269+
case MAIN_PAGES_LABELS.VARIABLE:
270+
return `变量视图的"${state.activeVariableViewSubPage}"页面`
271+
case MAIN_PAGES_LABELS.PLOTS:
272+
return `绘图视图的"${state.activePlotsViewSubPage}"页面`
273+
case MAIN_PAGES_LABELS.STATISTICS:
274+
return `统计视图的"${state.activeStatisticsViewSubPage}"页面`
275+
case MAIN_PAGES_LABELS.TOOLS:
276+
return `工具视图的"${state.activeToolsViewSubPage}"页面`
277+
default:
278+
throw new Error('发现未知页面')
279+
}
280+
},
262281
mainPage: MAIN_PAGES_ELEMENTS[MAIN_PAGES_LABELS.DATA],
263282
activeMainPage: MAIN_PAGES_LABELS.DATA,
264283
setMainPage: (page) =>

0 commit comments

Comments
 (0)