@@ -437,6 +437,9 @@ def get_model_response_iterator(
437
437
438
438
439
439
class OllamaChatCompletionResponseIterator (BaseModelResponseIterator ):
440
+ started_reasoning_content : bool = False
441
+ finished_reasoning_content : bool = False
442
+
440
443
def _is_function_call_complete (self , function_args : Union [str , dict ]) -> bool :
441
444
if isinstance (function_args , dict ):
442
445
return True
@@ -490,8 +493,49 @@ def chunk_parser(self, chunk: dict) -> ModelResponseStream:
490
493
if is_function_call_complete :
491
494
tool_call ["id" ] = str (uuid .uuid4 ())
492
495
496
+ # PROCESS REASONING CONTENT
497
+ reasoning_content : Optional [str ] = None
498
+ content : Optional [str ] = None
499
+ if chunk ["message" ].get ("thinking" ) is not None :
500
+ if self .started_reasoning_content is False :
501
+ reasoning_content = chunk ["message" ].get ("thinking" )
502
+ self .started_reasoning_content = True
503
+ elif self .finished_reasoning_content is False :
504
+ reasoning_content = chunk ["message" ].get ("thinking" )
505
+ self .finished_reasoning_content = True
506
+ elif chunk ["message" ].get ("content" ) is not None :
507
+ if "<think>" in chunk ["message" ].get ("content" ):
508
+ reasoning_content = (
509
+ chunk ["message" ].get ("content" ).replace ("<think>" , "" )
510
+ )
511
+
512
+ self .started_reasoning_content = True
513
+
514
+ if (
515
+ "</think>" in chunk ["message" ].get ("content" )
516
+ and self .started_reasoning_content
517
+ ):
518
+ reasoning_content = chunk ["message" ].get ("content" )
519
+ remaining_content = (
520
+ chunk ["message" ].get ("content" ).split ("</think>" )
521
+ )
522
+ if len (remaining_content ) > 1 :
523
+ content = remaining_content [1 ]
524
+ self .finished_reasoning_content = True
525
+
526
+ if (
527
+ self .started_reasoning_content is True
528
+ and self .finished_reasoning_content is False
529
+ ):
530
+ reasoning_content = (
531
+ chunk ["message" ].get ("content" ).replace ("<think>" , "" )
532
+ )
533
+ else :
534
+ content = chunk ["message" ].get ("content" )
535
+
493
536
delta = Delta (
494
- content = chunk ["message" ].get ("content" , "" ),
537
+ content = content ,
538
+ reasoning_content = reasoning_content ,
495
539
tool_calls = tool_calls ,
496
540
)
497
541
0 commit comments