From ed9d96ad4ecd32ba1e775d4f571096181706d8a6 Mon Sep 17 00:00:00 2001 From: pomelo-nwu Date: Mon, 20 Jan 2025 10:26:49 +0800 Subject: [PATCH] feat: Support multiple languages in prompts --- .../src/components/Report/Intention.tsx | 17 +++++++------ .../src/components/Report/Write.tsx | 6 ++++- .../src/components/Report/index.tsx | 24 +++++-------------- .../src/components/Report/utils.tsx | 6 +++++ 4 files changed, 27 insertions(+), 26 deletions(-) diff --git a/packages/studio-explore/src/components/Report/Intention.tsx b/packages/studio-explore/src/components/Report/Intention.tsx index 42dfe46d..0268fa0e 100644 --- a/packages/studio-explore/src/components/Report/Intention.tsx +++ b/packages/studio-explore/src/components/Report/Intention.tsx @@ -7,7 +7,8 @@ import { useContext } from '@graphscope/studio-graph'; import Summary from './Summary'; import { filterDataByParticalSchema, getAllAttributesByName, getStrSizeInKB, sampleHalf, getCategories } from './utils'; import type { ItentionType } from './index'; -import { debug } from 'console'; + +import { getPrompt } from './utils'; interface IReportProps { task: string; intention: ItentionType; @@ -282,11 +283,10 @@ const Intention: React.FunctionComponent = props => { if (iterate_time === 0) { while (true) { const filtered_nodes = nodes.filter(node => filtered_ids.includes(node.id)); - current_prompt = TEMPLATE_MIND_MAP_GENERATOR_CHN( - JSON.stringify({ filtered_nodes, edges }), - JSON.stringify(filtered_ids), - task, - ); + current_prompt = getPrompt({ + 'zh-CN': TEMPLATE_MIND_MAP_GENERATOR_CHN, + 'en-US': TEMPLATE_MIND_MAP_GENERATOR_EN, + })(JSON.stringify({ filtered_nodes, edges }), JSON.stringify(filtered_ids), task); if (getStrSizeInKB(current_prompt) < prompt_size_bound || filtered_ids.length === 1) { break; } @@ -304,7 +304,10 @@ const Intention: React.FunctionComponent = props => { } else { while (true) { const filtered_nodes = nodes.filter(node => filtered_ids.includes(node.id)); - current_prompt = TEMPLATE_MIND_MAP_GENERATOR_INCREMENTAL_CHN( + current_prompt = getPrompt({ + 'zh-CN': TEMPLATE_MIND_MAP_GENERATOR_INCREMENTAL_CHN, + 'en-US': TEMPLATE_MIND_MAP_GENERATOR_INCREMENTAL_EN, + })( JSON.stringify({ filtered_nodes, edges }), JSON.stringify(filtered_ids), JSON.stringify(category_dict), diff --git a/packages/studio-explore/src/components/Report/Write.tsx b/packages/studio-explore/src/components/Report/Write.tsx index 07ec0401..540c750f 100644 --- a/packages/studio-explore/src/components/Report/Write.tsx +++ b/packages/studio-explore/src/components/Report/Write.tsx @@ -4,6 +4,7 @@ import { query } from '../Copilot/query'; import { Message } from '../Copilot/utils/message'; import ReactMarkdown from 'react-markdown'; import type { SummaryType } from './Intention'; +import { getPrompt } from './utils'; const SECTION_CONSTANT_EXAMPLE_EN = () => { return ` @@ -233,7 +234,10 @@ const WriteReport: React.FunctionComponent< const res = await query([ new Message({ role: 'user', - content: GET_REPORT_PROMPTS_CHN(task, JSON.stringify(categories)), + content: getPrompt({ 'zh-CN': GET_REPORT_PROMPTS_CHN, 'en-US': GET_REPORT_PROMPTS_EN })( + task, + JSON.stringify(categories), + ), }), ]); diff --git a/packages/studio-explore/src/components/Report/index.tsx b/packages/studio-explore/src/components/Report/index.tsx index 38730790..3a8b8a0f 100644 --- a/packages/studio-explore/src/components/Report/index.tsx +++ b/packages/studio-explore/src/components/Report/index.tsx @@ -6,25 +6,9 @@ import { Message } from '../Copilot/utils/message'; import { GraphSchema, useContext } from '@graphscope/studio-graph'; import Intention from './Intention'; import Setting from '../Copilot/setting'; +import { getPrompt } from './utils'; interface IReportProps {} -const GET_DATA_FILTER_RULES = (user_query: string, schema: any) => { - return ` -你是一个很有天赋的 AI 助理,你的任务是根据用户的输入语句,再结合图的 Schema 信息,推断出用户的分析意图。 -考虑到用户后续输入的数据量可能比较大,因此需要你先返回部分 Schema 结构,方便用户对要数据做裁剪。 - -graph_schema :${schema} -user_query:${user_query} - -注意: -- 返回结果只有 JSON!返回结果只有 JSON!返回结果只有 JSON!且不要带 \`\`\`json !且不要带 \`\`\`json !且不要带 \`\`\`json ! -- JSON 的 'description' 字段是必须的,用来描述你理解到的用户分析意图 -- JSON 的 'plan' 字段是必须的,用于描述你计划如何实现的步骤,它必须是一个数组。 -- JSON 的 "schema" 字段是必须的,用来返回分析所需的部分 Schema 结构。不要返回全部 Schema 结构,尤其是属性字段,只考虑分析必备的字段,不要返回全部的属性字段。 -- 如果你还有其他备注,可以放在 'explain' 字段中 - `; -}; - const GET_DATA_FILTER_RULES_EN = (user_query: string, schema: any) => { return ` You are a highly skilled AI graph database expert. Given the user input and the schema of a graph database, your role is to identify the users' intents and which information in the database is necessary to meet the user's requirements. @@ -90,10 +74,14 @@ const Report: React.FunctionComponent = props => { try { setState({ ...state, task: value, loading: true }); + const _res = await query([ new Message({ role: 'user', - content: GET_DATA_FILTER_RULES_CHN(value, JSON.stringify(schema)), + content: getPrompt({ 'zh-CN': GET_DATA_FILTER_RULES_CHN, 'en-US': GET_DATA_FILTER_RULES_EN })( + value, + JSON.stringify(schema), + ), }), ]); debugger; diff --git a/packages/studio-explore/src/components/Report/utils.tsx b/packages/studio-explore/src/components/Report/utils.tsx index a69555b2..a613b394 100644 --- a/packages/studio-explore/src/components/Report/utils.tsx +++ b/packages/studio-explore/src/components/Report/utils.tsx @@ -111,3 +111,9 @@ export const getCategories = (output, categories) => { }; }); }; + +import { Utils } from '@graphscope/studio-components'; +export const getPrompt = obj => { + const locale = (Utils.storage.get('locale') as string) || 'en-US'; + return obj[locale]; +};