Skip to content

Commit 59a2a26

Browse files
committed
prefer prompted user messages over system prompts
This seems to be better supported across different model providers and seems to influence the model in a stronger way. Prompt files have been renamed to avoid having them show up in the DokuWiki translate interface. Multilingual models are fine with prompting in english only.
1 parent c2b7a1f commit 59a2a26

File tree

5 files changed

+28
-19
lines changed

5 files changed

+28
-19
lines changed

helper.php

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -183,14 +183,17 @@ public function askQuestion($question, $history = [])
183183
);
184184
$prompt = $this->getPrompt('question', [
185185
'context' => $context,
186+
'question' => $question,
186187
]);
187188
} else {
188-
$prompt = $this->getPrompt('noanswer');
189+
$prompt = $this->getPrompt('noanswer', [
190+
'question' => $question,
191+
]);
189192
$history = [];
190193
}
191194

192195
$messages = $this->prepareMessages(
193-
$this->getChatModel(), $prompt, $question, $history, $this->getConf('chatHistory')
196+
$this->getChatModel(), $prompt, $history, $this->getConf('chatHistory')
194197
);
195198
$answer = $this->getChatModel()->getAnswer($messages);
196199

@@ -211,9 +214,11 @@ public function askQuestion($question, $history = [])
211214
*/
212215
public function rephraseChatQuestion($question, $history)
213216
{
214-
$prompt = $this->getPrompt('rephrase');
217+
$prompt = $this->getPrompt('rephrase', [
218+
'question' => $question,
219+
]);
215220
$messages = $this->prepareMessages(
216-
$this->getRephraseModel(), $prompt, $question, $history, $this->getConf('rephraseHistory')
221+
$this->getRephraseModel(), $prompt, $history, $this->getConf('rephraseHistory')
217222
);
218223
return $this->getRephraseModel()->getAnswer($messages);
219224
}
@@ -222,32 +227,26 @@ public function rephraseChatQuestion($question, $history)
222227
* Prepare the messages for the AI
223228
*
224229
* @param ChatInterface $model The used model
225-
* @param string $prompt The fully prepared system prompt
226-
* @param string $question The user question
230+
* @param string $promptedQuestion The user question embedded in a prompt
227231
* @param array[] $history The chat history [[user, ai], [user, ai], ...]
228232
* @param int $historySize The maximum number of messages to use from the history
229233
* @return array An OpenAI compatible array of messages
230234
*/
231235
protected function prepareMessages(
232-
ChatInterface $model, string $prompt, string $question, array $history, int $historySize
236+
ChatInterface $model, string $promptedQuestion, array $history, int $historySize
233237
): array
234238
{
235239
// calculate the space for context
236240
$remainingContext = $model->getMaxInputTokenLength();
237-
$remainingContext -= $this->countTokens($prompt);
238-
$remainingContext -= $this->countTokens($question);
241+
$remainingContext -= $this->countTokens($promptedQuestion);
239242
$safetyMargin = $remainingContext * 0.05; // 5% safety margin
240243
$remainingContext -= $safetyMargin;
241244
// FIXME we may want to also have an upper limit for the history and not always use the full context
242245

243246
$messages = $this->historyMessages($history, $remainingContext, $historySize);
244-
$messages[] = [
245-
'role' => 'system',
246-
'content' => $prompt
247-
];
248247
$messages[] = [
249248
'role' => 'user',
250-
'content' => $question
249+
'content' => $promptedQuestion
251250
];
252251
return $messages;
253252
}
@@ -308,7 +307,7 @@ protected function countTokens($text)
308307
*/
309308
protected function getPrompt($type, $vars = [])
310309
{
311-
$template = file_get_contents($this->localFN('prompt_' . $type));
310+
$template = file_get_contents($this->localFN($type, 'prompt'));
312311
$vars['language'] = $this->getLanguagePrompt();
313312

314313
$replace = [];
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
Given the user's question, tell them that you can't answer it because you couldn't find any matching wiki pages, which is likely because the user has insufficient permissions to access them or the question was off-topic. {{LANGUAGE}}
1+
Given the user's question, tell them that you can't answer it because you couldn't find any matching wiki pages, which is likely because the user has insufficient permissions to access them or the question was off-topic.
2+
{{LANGUAGE}}
3+
4+
User Question: {{QUESTION}}

lang/en/prompt_rephrase.txt

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,7 @@ If you don't know the answer, just say that you don't know, don't try to make up
33
{{LANGUAGE}}
44
----------------
55
{{CONTEXT}}
6+
----------------
7+
8+
User Question: {{QUESTION}}
9+
Your Reply:

lang/en/rephrase.prompt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Given the previous conversation and the users follow-up question, rephrase the user's follow-up question to be a standalone question that is understandable without the previous context.
2+
{{LANGUAGE}}
3+
Only reply with the rephrased question, do not answer it.
4+
5+
Follow-up question: {{QUESTION}}
6+
Standalone question:

0 commit comments

Comments
 (0)