OpenRouter: Support reasoning blocks

This commit is contained in:
Cohee
2025-01-24 00:56:44 +02:00
parent 7f9b139ae0
commit 03c98fb55a
7 changed files with 37 additions and 31 deletions

View File

@ -14,8 +14,3 @@ export const debounce_timeout = {
/** [5 sec] For delayed tasks, like auto-saving or completing batch operations that need a significant pause. */
extended: 5000,
};
/**
* Custom boundary for splitting the text between the model's reasoning and the actual response.
*/
export const THINK_BREAK = '##<23>THINK_BREAK<41>##';

View File

@ -2161,6 +2161,11 @@ function getStreamingReply(data, state) {
state.reasoning += (data.choices?.filter(x => x?.delta?.reasoning_content)?.[0]?.delta?.reasoning_content || '');
}
return data.choices?.[0]?.delta?.content || '';
} else if (oai_settings.chat_completion_source === chat_completion_sources.OPENROUTER) {
if (oai_settings.show_thoughts) {
state.reasoning += (data.choices?.filter(x => x?.delta?.reasoning)?.[0]?.delta?.reasoning || '');
}
return data.choices?.[0]?.delta?.content ?? data.choices?.[0]?.message?.content ?? data.choices?.[0]?.text ?? '';
} else {
return data.choices?.[0]?.delta?.content ?? data.choices?.[0]?.message?.content ?? data.choices?.[0]?.text ?? '';
}

View File

@ -235,6 +235,21 @@ async function* parseStreamData(json) {
}
return;
}
else if (typeof json.choices[0].delta.reasoning === 'string' && json.choices[0].delta.reasoning.length > 0) {
for (let j = 0; j < json.choices[0].delta.reasoning.length; j++) {
const str = json.choices[0].delta.reasoning[j];
const isLastSymbol = j === json.choices[0].delta.reasoning.length - 1;
const choiceClone = structuredClone(json.choices[0]);
choiceClone.delta.reasoning = str;
choiceClone.delta.content = isLastSymbol ? choiceClone.delta.content : '';
const choices = [choiceClone];
yield {
data: { ...json, choices },
chunk: str,
};
}
return;
}
else if (typeof json.choices[0].delta.content === 'string' && json.choices[0].delta.content.length > 0) {
for (let j = 0; j < json.choices[0].delta.content.length; j++) {
const str = json.choices[0].delta.content[j];