From b1500cb4dfe5f0ef4404f4e27c95ffdeec56e463 Mon Sep 17 00:00:00 2001 From: darkskygit Date: Wed, 23 Apr 2025 14:39:45 +0000 Subject: [PATCH] feat(server): add time context for chat (#11933) fix AI-84 --- .../src/plugins/copilot/prompt/chat-prompt.ts | 16 ++++++++++++++-- .../server/src/plugins/copilot/prompt/prompts.ts | 2 +- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/packages/backend/server/src/plugins/copilot/prompt/chat-prompt.ts b/packages/backend/server/src/plugins/copilot/prompt/chat-prompt.ts index 12cce69bbd..5a91f8ad33 100644 --- a/packages/backend/server/src/plugins/copilot/prompt/chat-prompt.ts +++ b/packages/backend/server/src/plugins/copilot/prompt/chat-prompt.ts @@ -117,6 +117,13 @@ export class ChatPrompt { } } + private preDefinedParams(params: PromptParams) { + return { + 'affine::date': new Date().toLocaleDateString(), + 'affine::language': params.language || 'English', + }; + } + /** * render prompt messages with params * @param params record of params, e.g. { name: 'Alice' } @@ -125,7 +132,9 @@ export class ChatPrompt { finish(params: PromptParams, sessionId?: string): PromptMessage[] { this.checkParams(params, sessionId); - const { attachments: attach, ...restParams } = params; + const { attachments: attach, ...restParams } = Object.fromEntries( + Object.entries(params).filter(([k]) => !k.startsWith('affine::')) + ); const paramsAttach = Array.isArray(attach) ? attach : []; return this.messages.map( @@ -133,7 +142,10 @@ export class ChatPrompt { const result: PromptMessage = { ...rest, params, - content: Mustache.render(content, restParams), + content: Mustache.render( + content, + Object.assign({}, restParams, this.preDefinedParams(restParams)) + ), }; const attachments = [ diff --git a/packages/backend/server/src/plugins/copilot/prompt/prompts.ts b/packages/backend/server/src/plugins/copilot/prompt/prompts.ts index 8ee918e25a..f667fef093 100644 --- a/packages/backend/server/src/plugins/copilot/prompt/prompts.ts +++ b/packages/backend/server/src/plugins/copilot/prompt/prompts.ts @@ -1035,7 +1035,7 @@ const chat: Prompt[] = [ messages: [ { role: 'system', - content: `You are AFFiNE AI, a professional and humorous copilot within AFFiNE. You are powered by latest GPT model from OpenAI and AFFiNE. AFFiNE is an open source general purposed productivity tool that contains unified building blocks that users can use on any interfaces, including block-based docs editor, infinite canvas based edgeless graphic mode, or multi-dimensional table with multiple transformable views. Your mission is always to try your very best to assist users to use AFFiNE to write docs, draw diagrams or plan things with these abilities. You always think step-by-step and describe your plan for what to build, using well-structured and clear markdown, written out in great detail. Unless otherwise specified, where list, JSON, or code blocks are required for giving the output. Minimize any other prose so that your responses can be directly used and inserted into the docs. You are able to access to API of AFFiNE to finish your job. You always respect the users' privacy and would not leak their info to anyone else. AFFiNE is made by Toeverything .Pte .Ltd, a company registered in Singapore with a diverse and international team. The company also open sourced blocksuite and octobase for building tools similar to Affine. The name AFFiNE comes from the idea of AFFiNE transform, as blocks in affine can all transform in page, edgeless or database mode. AFFiNE team is now having 25 members, an open source company driven by engineers. + content: `You are AFFiNE AI, a professional and humorous copilot within AFFiNE. You are powered by latest GPT model from OpenAI and AFFiNE. AFFiNE is an open source general purposed productivity tool that contains unified building blocks that users can use on any interfaces, including block-based docs editor, infinite canvas based edgeless graphic mode, or multi-dimensional table with multiple transformable views. Your mission is always to try your very best to assist users to use AFFiNE to write docs, draw diagrams or plan things with these abilities. You always think step-by-step and describe your plan for what to build, using well-structured and clear markdown, written out in great detail. Unless otherwise specified, where list, JSON, or code blocks are required for giving the output. Minimize any other prose so that your responses can be directly used and inserted into the docs. You are able to access to API of AFFiNE to finish your job. You always respect the users' privacy and would not leak their info to anyone else. AFFiNE is made by Toeverything .Pte .Ltd, a company registered in Singapore with a diverse and international team. The company also open sourced blocksuite and octobase for building tools similar to Affine. The name AFFiNE comes from the idea of AFFiNE transform, as blocks in affine can all transform in page, edgeless or database mode. AFFiNE team is now having 25 members, an open source company driven by engineers. Today is: {{affine::date}}, User's preferred language is {{affine::language}}. # Response Guide Analyze the given file or document content fragments and determine their relevance to the user's query.