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, {
101 switch (textGenType ?? textgenerationwebui_settings.type) {101 switch (textGenType ?? textgenerationwebui_settings.type) {
102 case textgen_types.OPENROUTER:102 case textgen_types.OPENROUTER:
103 return data?.choices?.[0]?.reasoning ?? '';103 return data?.choices?.[0]?.reasoning ?? '';
104 case textgen_types.OLLAMA:
105 return data?.thinking ?? '';
104 }106 }
105 break;107 break;
106108
public/scripts/sse-stream.js+14 -0
@@ -234,6 +234,20 @@ async function* parseStreamData(json) {
234 }234 }
235 return;235 return;
236 }236 }
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 }
237 else if (typeof json.choices[0].delta === 'object') {251 else if (typeof json.choices[0].delta === 'object') {
238 if (typeof json.choices[0].delta.text === 'string' && json.choices[0].delta.text.length > 0) {252 if (typeof json.choices[0].delta.text === 'string' && json.choices[0].delta.text.length > 0) {
239 for (let j = 0; j < json.choices[0].delta.text.length; j++) {253 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) {
1292 const newText = data?.choices?.[0]?.text || data?.content || '';1292 const newText = data?.choices?.[0]?.text || data?.content || '';
1293 text += newText;1293 text += newText;
1294 logprobs = parseTextgenLogprobs(newText, data.choices?.[0]?.logprobs || data?.completion_probabilities);1294 logprobs = parseTextgenLogprobs(newText, data.choices?.[0]?.logprobs || data?.completion_probabilities);
1295 state.reasoning += data?.choices?.[0]?.reasoning ?? '';1295 state.reasoning += data?.choices?.[0]?.reasoning ?? data?.choices?.[0]?.thinking ?? '';
1296 }1296 }
12971297
1298 yield { text, swipes, logprobs, toolCalls, state };1298 yield { text, swipes, logprobs, toolCalls, state };
src/endpoints/backends/text-completions.js+2 -1
@@ -44,7 +44,8 @@ async function parseOllamaStream(jsonStream, request, response) {
44 break;44 break;
45 }45 }
46 const text = json.response || '';46 const text = json.response || '';
47 const chunk = { choices: [{ text }] };47 const thinking = json.thinking || '';
48 const chunk = { choices: [{ text, thinking }] };
48 response.write(`data: ${JSON.stringify(chunk)}\n\n`);49 response.write(`data: ${JSON.stringify(chunk)}\n\n`);
49 partialData = '';50 partialData = '';
50 }51 }