Skip to content

Commit 2277592

Browse files
committed
feat(gpt-runner-web): add system prompt as user prompt feature
1 parent c2b2a78 commit 2277592

File tree

14 files changed

+62
-7
lines changed

14 files changed

+62
-7
lines changed

packages/gpt-runner-core/src/langchain/llm-chain/llm-chain.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
ChatPromptTemplate, HumanMessagePromptTemplate, SystemMessagePromptTemplate,
44
} from 'langchain/prompts'
55
import { LLMChain } from 'langchain/chains'
6+
import { ChatRole } from '@nicepkg/gpt-runner-shared/common'
67
import { mapStoredMessageToChatTemplateMessages } from '../helper'
78
import type { GetLLMChainParams } from './type'
89
import { getLLM } from './get-llm'
@@ -13,13 +14,29 @@ export async function llmChain(params: LLmChainParams) {
1314
const {
1415
messages,
1516
systemPrompt,
17+
systemPromptAsUserPrompt,
1618
} = params
1719

1820
const llm = getLLM(params)
1921

22+
const DEFAULT_SYSTEM_PROMPT = 'You are a friendly assistant.'
23+
const finalMessages = [...messages]
24+
let finalSystemPrompt = systemPrompt || DEFAULT_SYSTEM_PROMPT
25+
26+
if (systemPromptAsUserPrompt) {
27+
finalMessages.unshift({
28+
text: `Now I am user, you will answer my questions as follows setup or role play: \n\n${finalSystemPrompt}`,
29+
name: ChatRole.User,
30+
}, {
31+
text: 'Ok',
32+
name: ChatRole.Assistant,
33+
})
34+
finalSystemPrompt = DEFAULT_SYSTEM_PROMPT
35+
}
36+
2037
const chatPrompt = ChatPromptTemplate.fromPromptMessages([
21-
SystemMessagePromptTemplate.fromTemplate(systemPrompt || 'You are a friendly assistant.'),
22-
...mapStoredMessageToChatTemplateMessages(messages),
38+
SystemMessagePromptTemplate.fromTemplate(finalSystemPrompt),
39+
...mapStoredMessageToChatTemplateMessages(finalMessages),
2340
HumanMessagePromptTemplate.fromTemplate('{global.input}'),
2441
])
2542

packages/gpt-runner-core/src/langchain/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export interface BaseStreamChainParams<Message> {
22
messages: Message[]
33
systemPrompt?: string
4+
systemPromptAsUserPrompt?: boolean
45
onTokenStream?: (token: string) => void
56
onComplete?: () => void
67
onError?: (err: any) => void

packages/gpt-runner-shared/src/common/types/server.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ export interface ChatStreamReqParams {
2727
*/
2828
systemPrompt?: string
2929
appendSystemPrompt?: string
30+
31+
/**
32+
* send system prompt as user prompt
33+
* @default false
34+
*/
35+
systemPromptAsUserPrompt?: boolean
3036
singleFilePath?: string
3137

3238
/**

packages/gpt-runner-shared/src/common/zod/server.zod.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export const ChatStreamReqParamsSchema = z.object({
88
prompt: z.string(),
99
systemPrompt: z.string().optional(),
1010
appendSystemPrompt: z.string().optional(),
11+
systemPromptAsUserPrompt: z.boolean().optional(),
1112
singleFilePath: z.string().optional(),
1213
singleFileConfig: SingleFileConfigSchema.optional(),
1314
overrideModelType: ChatModelTypeSchema.optional(),

packages/gpt-runner-web/client/public/locales/de.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
"override_settings": "Einstellungen überschreiben",
5757
"override_all_settings": "Alle Einstellungen überschreiben",
5858
"context_settings": "Kontext-Einstellungen",
59+
"context_settings_system_prompt_as_user_prompt_tips": "Senden Sie Systemprompt als Nachricht, nützlich für APIs, die keine Systemprompt unterstützen",
5960
"context_settings_selected_text_checkbox_tips": "Ausgewählter Text als Aufforderung<TokenNumWrapper>{{tokenNum}}</TokenNumWrapper>Token.",
6061
"context_settings_opening_ide_file_contents_checkbox_tips": "Öffnen von IDE-Dateiinhalten als Aufforderung<FileNumWrapper>{{fileNum}}</FileNumWrapper>Dateien<TokenNumWrapper>{{tokenNum}}</TokenNumWrapper>Token.",
6162
"context_settings_active_ide_file_contents_checkbox_tips": "Aktive IDE-Dateiinhalte als Aufforderung<TokenNumWrapper>{{tokenNum}}</TokenNumWrapper>Token.",
@@ -98,4 +99,4 @@
9899
"file_editor_forgot_save_tips_title": "Möchten Sie die Änderungen an {{fileName}} speichern?",
99100
"file_editor_forgot_save_tips_content": "Ihre Änderungen gehen verloren, wenn Sie sie nicht speichern."
100101
}
101-
}
102+
}

packages/gpt-runner-web/client/public/locales/en.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
"override_settings": "Override Settings",
5757
"override_all_settings": "Override All Settings",
5858
"context_settings": "Context Settings",
59+
"context_settings_system_prompt_as_user_prompt_tips": "Send System Prompt as message, useful for APIs that don't support System Prompt",
5960
"context_settings_selected_text_checkbox_tips": "Selected Text As Prompt <TokenNumWrapper>{{tokenNum}}</TokenNumWrapper> Tokens.",
6061
"context_settings_opening_ide_file_contents_checkbox_tips": "Opening IDE File Contents As Prompt <FileNumWrapper>{{fileNum}}</FileNumWrapper> Files <TokenNumWrapper>{{tokenNum}}</TokenNumWrapper> Tokens.",
6162
"context_settings_active_ide_file_contents_checkbox_tips": "Active IDE File Contents As Prompt <TokenNumWrapper>{{tokenNum}}</TokenNumWrapper> Tokens.",
@@ -98,4 +99,4 @@
9899
"file_editor_forgot_save_tips_title": "Do you want to save changes to {{fileName}}?",
99100
"file_editor_forgot_save_tips_content": "Your changes will be lost if you don't save them."
100101
}
101-
}
102+
}

packages/gpt-runner-web/client/public/locales/ja.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
"override_settings": "設定を上書き",
5757
"override_all_settings": "すべての設定を上書きする",
5858
"context_settings": "コンテキスト設定",
59+
"context_settings_system_prompt_as_user_prompt_tips": "システムプロンプトをメッセージとして送信します。システムプロンプトをサポートしていないAPIに便利です",
5960
"context_settings_selected_text_checkbox_tips": "選択したテキストをプロンプトとして使用<TokenNumWrapper>{{tokenNum}}</TokenNumWrapper>トークン。",
6061
"context_settings_opening_ide_file_contents_checkbox_tips": "開いているIDEファイルの内容をプロンプトとして使用<FileNumWrapper>{{fileNum}}</FileNumWrapper>ファイル<TokenNumWrapper>{{tokenNum}}</TokenNumWrapper>トークン。",
6162
"context_settings_active_ide_file_contents_checkbox_tips": "アクティブなIDEファイルの内容をプロンプトとして使用<TokenNumWrapper>{{tokenNum}}</TokenNumWrapper>トークン。",
@@ -98,4 +99,4 @@
9899
"file_editor_forgot_save_tips_title": "変更を{{fileName}}に保存しますか?",
99100
"file_editor_forgot_save_tips_content": "保存しない場合、変更は失われます。"
100101
}
101-
}
102+
}

packages/gpt-runner-web/client/public/locales/zh_CN.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
"override_settings": "覆盖设置",
5757
"override_all_settings": "覆盖所有设置",
5858
"context_settings": "上下文设置",
59+
"context_settings_system_prompt_as_user_prompt_tips": "将系统提示作为消息发送,对于不支持系统提示的 API 很有用",
5960
"context_settings_selected_text_checkbox_tips": "将选定的文本作为提示,<TokenNumWrapper>{{tokenNum}}</TokenNumWrapper> tokens",
6061
"context_settings_opening_ide_file_contents_checkbox_tips": "将 IDE 正在打开的文件内容作为提示,<FileNumWrapper>{{fileNum}}</FileNumWrapper> 个文件, <TokenNumWrapper>{{tokenNum}}</TokenNumWrapper> tokens",
6162
"context_settings_active_ide_file_contents_checkbox_tips": "将 IDE 正在编辑的文件内容作为提示,<TokenNumWrapper>{{tokenNum}}</TokenNumWrapper> tokens",
@@ -98,4 +99,4 @@
9899
"file_editor_forgot_save_tips_title": "你想要保存对{{fileName}}的更改吗?",
99100
"file_editor_forgot_save_tips_content": "如果你不保存,你的改动将会丢失."
100101
}
101-
}
102+
}

packages/gpt-runner-web/client/public/locales/zh_Hant.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
"override_settings": "覆寫設置",
5757
"override_all_settings": "覆蓋所有設置",
5858
"context_settings": "上下文設置",
59+
"context_settings_system_prompt_as_user_prompt_tips": "將系統提示作為訊息發送,對於不支援系統提示的 API 很有用",
5960
"context_settings_selected_text_checkbox_tips": "將選定的文本作為提示,<TokenNumWrapper>{{tokenNum}}</TokenNumWrapper> tokens",
6061
"context_settings_opening_ide_file_contents_checkbox_tips": "將 IDE 正在打開的文件內容作為提示,<FileNumWrapper>{{fileNum}}</FileNumWrapper> 個文件, <TokenNumWrapper>{{tokenNum}}</TokenNumWrapper> tokens",
6162
"context_settings_active_ide_file_contents_checkbox_tips": "將 IDE 正在編輯的文件內容作為提示,<TokenNumWrapper>{{tokenNum}}</TokenNumWrapper> tokens",
@@ -98,4 +99,4 @@
9899
"file_editor_forgot_save_tips_title": "你想要保存對{{fileName}}的更改嗎?",
99100
"file_editor_forgot_save_tips_content": "如果你不保存,你的改動將會丟失."
100101
}
101-
}
102+
}

packages/gpt-runner-web/client/src/networks/llm.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export async function fetchLlmStream(
2020
prompt,
2121
systemPrompt,
2222
appendSystemPrompt,
23+
systemPromptAsUserPrompt,
2324
singleFilePath,
2425
singleFileConfig,
2526
contextFilePaths,
@@ -55,6 +56,7 @@ export async function fetchLlmStream(
5556
messages,
5657
systemPrompt,
5758
appendSystemPrompt,
59+
systemPromptAsUserPrompt,
5860
singleFilePath,
5961
singleFileConfig,
6062
contextFilePaths,

0 commit comments

Comments
 (0)