Ollama: Add reasoning content extraction

b7760ddaaac524ef11f68a4467f9fb42e6300191

Cohee <18619528+Cohee1207@users.noreply.github.com>

4 files changed, +19 -2Ignore whitespace
public/scripts/reasoning.js+2 -0
@@ -101,6 +101,8 @@ export function extractReasoningFromData(data, {
101101 switch (textGenType ?? textgenerationwebui_settings.type) {
102102 case textgen_types.OPENROUTER:
103103 return data?.choices?.[0]?.reasoning ?? '';
104+ case textgen_types.OLLAMA:
105+ return data?.thinking ?? '';
104106 }
105107 break;
106108
public/scripts/sse-stream.js+14 -0
@@ -234,6 +234,20 @@ async function* parseStreamData(json) {
234234 }
235235 return;
236236 }
237+ else if (typeof json.choices[0].thinking === 'string' && json.choices[0].thinking.length > 0) {
238+ for (let j = 0; j < json.choices[0].thinking.length; j++) {
239+ const str = json.choices[0].thinking[j];
240+ const choiceClone = structuredClone(json.choices[0]);
241+ choiceClone.thinking = str;
242+ const choices = [choiceClone];
243+ yield {
244+ data: { ...json, choices },
245+ chunk: str,
246+ reasoning: true,
247+ };
248+ }
249+ return;
250+ }
237251 else if (typeof json.choices[0].delta === 'object') {
238252 if (typeof json.choices[0].delta.text === 'string' && json.choices[0].delta.text.length > 0) {
239253 for (let j = 0; j < json.choices[0].delta.text.length; j++) {
public/scripts/textgen-settings.js+1 -1
@@ -1292,7 +1292,7 @@ export async function generateTextGenWithStreaming(generate_data, signal) {
12921292 const newText = data?.choices?.[0]?.text || data?.content || '';
12931293 text += newText;
12941294 logprobs = parseTextgenLogprobs(newText, data.choices?.[0]?.logprobs || data?.completion_probabilities);
12951295 state.reasoning += data?.choices?.[0]?.reasoning ?? data?.choices?.[0]?.thinking ?? '';
12961296 }
12971297
12981298 yield { text, swipes, logprobs, toolCalls, state };
src/endpoints/backends/text-completions.js+2 -1
@@ -44,7 +44,8 @@ async function parseOllamaStream(jsonStream, request, response) {
4444 break;
4545 }
4646 const text = json.response || '';
47- const chunk = { choices: [{ text }] };
47+ const thinking = json.thinking || '';
48+ const chunk = { choices: [{ text, thinking }] };
4849 response.write(`data: ${JSON.stringify(chunk)}\n\n`);
4950 partialData = '';
5051 }