Skip to content

Commit 5427d17

Browse files
committed
feat: set search system message as config
Signed-off-by: Bob Du <i@bobdu.cc>
1 parent 7f72f6c commit 5427d17

File tree

6 files changed

+32
-72
lines changed

6 files changed

+32
-72
lines changed

service/src/chatgpt/index.ts

Lines changed: 4 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -17,74 +17,8 @@ import type { ChatMessage, RequestOptions } from './types'
1717

1818
dotenv.config()
1919

20-
function systemMessageWithSearchResult(currentTime: string): string {
21-
return `You are an intelligent assistant that needs to answer user questions based on search results.
22-
23-
**Search Results Format Description:**
24-
- Search results may contain irrelevant information, please filter and use accordingly
25-
26-
**Context Information:**
27-
- Current time: ${currentTime}
28-
29-
**Response Requirements:**
30-
31-
1. **Content Processing**
32-
- Screen and filter search results, selecting content most relevant to the question
33-
- Synthesize information from multiple web pages, avoiding repetitive citations from a single source
34-
- When using web page information, please indicate the source in your answer (post it back to the application in the form of a link) to facilitate users' verification of the information source
35-
36-
2. **Response Strategy**
37-
- **Listing questions**: Limit to within 10 key points, prioritize providing the most relevant and complete information
38-
- **Creative questions**: Make full use of search results to generate in-depth professional long-form answers
39-
- **Objective Q&A**: Brief answers may appropriately supplement 1-2 sentences of related information
40-
41-
3. **Format Requirements**
42-
- Respond using markdown (latex start with $).
43-
- Use structured, paragraph-based answer format
44-
- When answering in points, limit to within 5 points, merging related content
45-
- Ensure answers are aesthetically pleasing and highly readable
46-
47-
4. **Language Standards**
48-
- Keep answer language consistent with user's question language
49-
- Do not change language unless specifically requested by the user
50-
51-
**Notes:**
52-
- Not all search results are relevant, need to judge based on the question
53-
- For listing questions, inform users they can check search sources for complete information
54-
- Creative answers need to be multi-perspective, information-rich, and thoroughly discussed`
55-
}
56-
57-
function systemMessageGetSearchQuery(currentTime: string): string {
58-
return `You are an intelligent search assistant.
59-
Current time: ${currentTime}
60-
61-
Before formally answering user questions, you need to analyze the user's questions and conversation context to determine whether you need to obtain more information through internet search to provide accurate answers.
62-
63-
**Task Flow:**
64-
1. Carefully analyze the user's question content and previous conversation history
65-
2. Based on the current time, determine whether the question involves time-sensitive information. If it involves time-sensitive issues, please inform the specific date to be searched in the returned results instead of referring to pronouns such as today or yesterday
66-
3. Evaluate whether existing knowledge is sufficient to answer the question
67-
4. If search is needed, generate a precise search query
68-
5. If search is not needed, return empty result
69-
70-
**Output Format Requirements:**
71-
- If search is needed: return <search_query>example search query keywords</search_query>
72-
- If search is not needed: return <search_query></search_query>
73-
- Do not include any other explanations or answer content
74-
- Search query should be concise and clear, able to obtain the most relevant information
75-
76-
**Judgment Criteria:**
77-
- Time-sensitive information (such as latest news, stock prices, weather, real-time data, etc.): search needed
78-
- Latest policies, regulations, technological developments: may need search
79-
- Common sense questions, historical facts, basic knowledge: usually no search needed
80-
- Latest research or developments in professional fields: search recommended
81-
82-
**Notes:**
83-
- Search query should target the core needs of user questions
84-
- Consider the timeliness and accuracy requirements of information
85-
- Prioritize obtaining the latest and most authoritative information sources
86-
87-
Please strictly return results according to the above format.`
20+
function renderSystemMessage(template: string, currentTime: string): string {
21+
return template.replace(/{current_time}/g, currentTime)
8822
}
8923

9024
const ErrorCodeMessage: Record<string, string> = {
@@ -167,7 +101,7 @@ async function chatReplyProcess(options: RequestOptions) {
167101
let hasSearchResult = false
168102
const searchConfig = globalConfig.searchConfig
169103
if (searchConfig.enabled && searchConfig?.options?.apiKey && searchEnabled) {
170-
messages[0].content = systemMessageGetSearchQuery(dayjs().format('YYYY-MM-DD HH:mm:ss'))
104+
messages[0].content = renderSystemMessage(searchConfig.systemMessageGetSearchQuery, dayjs().format('YYYY-MM-DD HH:mm:ss'))
171105
const completion = await openai.chat.completions.create({
172106
model,
173107
messages,
@@ -201,7 +135,7 @@ search query: <search_query>${searchQuery}</search_query>
201135
search result: <search_result>${searchResult}</search_result>`,
202136
})
203137

204-
messages[0].content = systemMessageWithSearchResult(dayjs().format('YYYY-MM-DD HH:mm:ss'))
138+
messages[0].content = renderSystemMessage(searchConfig.systemMessageWithSearchResult, dayjs().format('YYYY-MM-DD HH:mm:ss'))
205139
hasSearchResult = true
206140
}
207141
}

service/src/storage/model.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,8 @@ export class SearchConfig {
177177
public enabled: boolean
178178
public provider?: SearchServiceProvider
179179
public options?: SearchServiceOptions
180+
public systemMessageWithSearchResult?: string
181+
public systemMessageGetSearchQuery?: string
180182
}
181183

182184
export enum SearchServiceProvider {

src/components/common/Setting/Search.vue

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ async function fetchConfig() {
2424
loading.value = true
2525
const { data } = await fetchChatConfig<ConfigState>()
2626
if (!data.searchConfig)
27-
data.searchConfig = new SearchConfig(false, '', { apiKey: '' })
27+
data.searchConfig = new SearchConfig(false, '', { apiKey: '' }, '', '')
2828
if (!data.searchConfig.options)
2929
data.searchConfig.options = { apiKey: '' }
3030
config.value = data.searchConfig
@@ -107,6 +107,18 @@ onMounted(() => {
107107
/>
108108
</div>
109109
</div>
110+
<div v-if="config && config.enabled" class="flex items-center space-x-4">
111+
<span class="shrink-0 w-[100px]">{{ $t('setting.systemMessageWithSearchResult') }}</span>
112+
<div class="flex-1">
113+
<NInput v-model:value="config.systemMessageWithSearchResult" type="textarea" :autosize="{ minRows: 2 }" :placeholder="t('setting.systemMessageWithSearchResultPlaceholder')" />
114+
</div>
115+
</div>
116+
<div v-if="config && config.enabled" class="flex items-center space-x-4">
117+
<span class="shrink-0 w-[100px]">{{ $t('setting.systemMessageGetSearchQuery') }}</span>
118+
<div class="flex-1">
119+
<NInput v-model:value="config.systemMessageGetSearchQuery" type="textarea" :autosize="{ minRows: 2 }" :placeholder="t('setting.systemMessageGetSearchQueryPlaceholder')" />
120+
</div>
121+
</div>
110122
<div class="flex items-center space-x-4">
111123
<span class="shrink-0 w-[100px]" />
112124
<div class="flex flex-wrap items-center gap-4">

src/components/common/Setting/model.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,13 @@ export class SearchConfig {
192192
enabled: boolean
193193
provider: SearchServiceProvider
194194
options: SearchServiceOptions
195-
constructor(enabled: boolean, provider: SearchServiceProvider, options: SearchServiceOptions) {
195+
systemMessageWithSearchResult: string
196+
systemMessageGetSearchQuery: string
197+
constructor(enabled: boolean, provider: SearchServiceProvider, options: SearchServiceOptions, systemMessageWithSearchResult: string, systemMessageGetSearchQuery: string) {
196198
this.enabled = enabled
197199
this.provider = provider
198200
this.options = options
201+
this.systemMessageWithSearchResult = systemMessageWithSearchResult
202+
this.systemMessageGetSearchQuery = systemMessageGetSearchQuery
199203
}
200204
}

src/locales/en-US.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,10 @@ export default {
167167
searchEnabled: 'Search Enabled',
168168
searchProvider: 'Search Provider',
169169
searchApiKey: 'Search API Key',
170+
systemMessageWithSearchResult: 'System message for conversations with search results',
171+
systemMessageGetSearchQuery: 'System message for getting search query',
172+
systemMessageWithSearchResultPlaceholder: 'System message template when with search results. Use {\'{current_time}\'} as placeholder for current time.',
173+
systemMessageGetSearchQueryPlaceholder: 'System message template for generating search query word. Use {\'{current_time}\'} as placeholder for current time. Require LLM to return the query word in <search_query>example search query</search_query> tags or return empty <search_query></search_query> tag if not need search.',
170174
searchTest: 'Test Search',
171175
accessTokenExpiredTime: 'Expired Time',
172176
userConfig: 'Users',

src/locales/zh-CN.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,10 @@ export default {
167167
searchEnabled: '搜索功能',
168168
searchProvider: '搜索提供商',
169169
searchApiKey: '搜索 API 密钥',
170+
systemMessageWithSearchResult: '包含搜索结果时的对话系统提示词',
171+
systemMessageGetSearchQuery: '用于获取搜索查询词的系统提示词',
172+
systemMessageWithSearchResultPlaceholder: '携带搜索结果时的系统消息模板, 使用 {\'{current_time}\'} 作为当前时间的占位符',
173+
systemMessageGetSearchQueryPlaceholder: '用于生成搜索查询的系统消息模板, 使用 {\'{current_time}\'} 作为当前时间的占位符, 要求 LLM 在 <search_query>example search query</search_query> 标签中返回查询词,如果不需要搜索则返回空<search_query></search_query>标签',
170174
searchTest: '测试搜索',
171175
accessTokenExpiredTime: '过期时间',
172176
userConfig: '用户管理',

0 commit comments

Comments
 (0)