Skip to content

Commit 87090e4

Browse files
committed
ask the rephrased question only if it has more context
Otherwise it's usually better to let the LLM reevaluate the context.
1 parent 6beed32 commit 87090e4

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

cli/simulate.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ protected function simulate($questions, $model)
9292

9393
$record = [
9494
'question' => $q,
95-
'rephrased' => $result['question'],
95+
'rephrased' => $result['contextQuestion'],
9696
'answer' => $result['answer'],
9797
'source.list' => implode("\n", $result['sources']),
9898
'source.time' => $this->helper->getEmbeddings()->timeSpent,

helper.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -170,24 +170,30 @@ public function getStorage()
170170
public function askChatQuestion($question, $history = [])
171171
{
172172
if ($history && $this->getConf('rephraseHistory') > 0) {
173-
$standaloneQuestion = $this->rephraseChatQuestion($question, $history);
173+
$contextQuestion = $this->rephraseChatQuestion($question, $history);
174+
175+
// Only use the rephrased question if it has more history than the chat history provides
176+
if ($this->getConf('rephraseHistory') > $this->getConf('chatHistory')) {
177+
$question = $contextQuestion;
178+
}
174179
} else {
175-
$standaloneQuestion = $question;
180+
$contextQuestion = $question;
176181
}
177-
return $this->askQuestion($standaloneQuestion, $history);
182+
return $this->askQuestion($question, $history, $contextQuestion);
178183
}
179184

180185
/**
181186
* Ask a single standalone question
182187
*
183-
* @param string $question
188+
* @param string $question The question to ask
184189
* @param array $history [user, ai] of the previous question
190+
* @param string $contextQuestion The question to use for context search
185191
* @return array ['question' => $question, 'answer' => $answer, 'sources' => $sources]
186192
* @throws Exception
187193
*/
188-
public function askQuestion($question, $history = [])
194+
public function askQuestion($question, $history = [], $contextQuestion = '')
189195
{
190-
$similar = $this->getEmbeddings()->getSimilarChunks($question, $this->getLanguageLimit());
196+
$similar = $this->getEmbeddings()->getSimilarChunks($contextQuestion ?: $question, $this->getLanguageLimit());
191197
if ($similar) {
192198
$context = implode(
193199
"\n",
@@ -214,6 +220,7 @@ public function askQuestion($question, $history = [])
214220

215221
return [
216222
'question' => $question,
223+
'contextQuestion' => $contextQuestion,
217224
'answer' => $answer,
218225
'sources' => $similar,
219226
];

0 commit comments

Comments
 (0)