Skip to content

Commit

Permalink
feat: Support multiple languages in prompts
Browse files Browse the repository at this point in the history
  • Loading branch information
pomelo-nwu committed Jan 20, 2025
1 parent 3eef544 commit ed9d96a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 26 deletions.
17 changes: 10 additions & 7 deletions packages/studio-explore/src/components/Report/Intention.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -282,11 +283,10 @@ const Intention: React.FunctionComponent<IReportProps> = 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;
}
Expand All @@ -304,7 +304,10 @@ const Intention: React.FunctionComponent<IReportProps> = 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),
Expand Down
6 changes: 5 additions & 1 deletion packages/studio-explore/src/components/Report/Write.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 `
Expand Down Expand Up @@ -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),
),
}),
]);

Expand Down
24 changes: 6 additions & 18 deletions packages/studio-explore/src/components/Report/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -90,10 +74,14 @@ const Report: React.FunctionComponent<IReportProps> = 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;
Expand Down
6 changes: 6 additions & 0 deletions packages/studio-explore/src/components/Report/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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];
};

0 comments on commit ed9d96a

Please sign in to comment.