@@ -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