Skip to content

Commit 2eaf6fe

Browse files
committed
Include raw text for completions
Because we are no longer applying the prompt template. Signed-off-by: Graham King <[email protected]>
1 parent 9027380 commit 2eaf6fe

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

lib/llm/src/preprocessor.rs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -223,17 +223,18 @@ impl OpenAIPreprocessor {
223223
.nvext()
224224
.is_some_and(|ext| ext.use_raw_prompt.unwrap_or(false));
225225

226-
if use_raw_prompt {
226+
let formatted_prompt = if use_raw_prompt {
227227
match request.raw_prompt() {
228-
Some(prompt) => Ok(Some(prompt)),
228+
Some(prompt) => prompt,
229229
None => {
230230
tracing::warn!("Raw prompt requested but not available");
231-
Ok(Some(self.formatter.render(request)?))
231+
self.formatter.render(request)?
232232
}
233233
}
234234
} else {
235-
Ok(Some(self.formatter.render(request)?))
236-
}
235+
self.formatter.render(request)?
236+
};
237+
Ok(Some(formatted_prompt))
237238
} else {
238239
Ok(None)
239240
}
@@ -275,8 +276,16 @@ impl OpenAIPreprocessor {
275276
PromptInput::Text(_) => {
276277
if let Some(text_input) = request.extract_text() {
277278
match text_input {
278-
TextInput::Single(_) => {
279-
let formatted_prompt = formatted_prompt.expect("Could not find a prompt. The paired match statements earlier should make this unreachable");
279+
TextInput::Single(raw_prompt) => {
280+
if let Some(f) = formatted_prompt.as_ref()
281+
&& request.has_annotation(ANNOTATION_FORMATTED_PROMPT)
282+
{
283+
annotations
284+
.insert(ANNOTATION_FORMATTED_PROMPT.to_string(), f.to_string());
285+
}
286+
287+
// Completions will use raw_prompt, no template
288+
let prompt = formatted_prompt.unwrap_or(raw_prompt);
280289

281290
// Check if backend_instance_id is present and token_data is provided
282291
let has_backend_instance_id = request
@@ -299,22 +308,15 @@ impl OpenAIPreprocessor {
299308
tracing::warn!(
300309
"backend_instance_id provided but no token_data; tokenizing prompt"
301310
);
302-
let encoding = self.tokenizer.encode(&formatted_prompt)?;
311+
let encoding = self.tokenizer.encode(&prompt)?;
303312
(encoding.token_ids().to_vec(), false)
304313
}
305314
} else {
306315
// No backend_instance_id provided, continue the normal flow.
307-
let encoding = self.tokenizer.encode(&formatted_prompt)?;
316+
let encoding = self.tokenizer.encode(&prompt)?;
308317
(encoding.token_ids().to_vec(), false)
309318
};
310319

311-
if request.has_annotation(ANNOTATION_FORMATTED_PROMPT) {
312-
annotations.insert(
313-
ANNOTATION_FORMATTED_PROMPT.to_string(),
314-
formatted_prompt,
315-
);
316-
}
317-
318320
if request.has_annotation(ANNOTATION_TOKEN_IDS)
319321
&& !skip_token_annotation
320322
{

0 commit comments

Comments
 (0)