Spaces:
Paused
Paused
Commit ·
ebec74a
1
Parent(s): 9f566ef
bug fixes
Browse filesFiles changed (2)
90 unmodified lines91929394959690 unmodified linesself.inside_tag = Falsereturn processed_content, current_reasoningdef process_streaming_content_with_reasoning_tags(90 unmodified lines91929394959697989910010110210310410510610710810911011111211311411511611711811912012112212390 unmodified linesself.inside_tag = Falsereturn processed_content, current_reasoningdef flush_remaining(self) -> tuple[str, str]:"""Flush any remaining content in the buffer when the stream ends.Returns:A tuple of:- remaining_content: Any content that was buffered but not yet output- remaining_reasoning: Any incomplete reasoning if we were inside a tag"""remaining_content = ""remaining_reasoning = ""if self.tag_buffer and not self.inside_tag:# If we have buffered content and we're not inside a tag,# it's safe to output all of itremaining_content = self.tag_bufferself.tag_buffer = ""elif self.inside_tag:# If we're inside a tag when the stream ends, we have an unclosed tag# Return the partial content as regular content (including the opening tag)remaining_content = f"<{self.tag_name}>{self.reasoning_buffer}{self.tag_buffer}"self.reasoning_buffer = ""self.tag_buffer = ""self.inside_tag = Falsereturn remaining_content, remaining_reasoningdef process_streaming_content_with_reasoning_tags(
119 unmodified lines1201211221231241251261276 unmodified lines13413513613713813914014114214314414 unmodified lines159160161162163164165166167168169170171172173174175176177178119 unmodified lines# Create processor for tag-based extraction across chunksreasoning_processor = StreamingReasoningProcessor(VERTEX_REASONING_TAG)async for chunk in stream_response:try:chunk_as_dict = chunk.model_dump(exclude_unset=True, exclude_none=True)6 unmodified linesdel delta['extra_content']content = delta.get('content', '')print(content)if content:# Use the processor to extract reasoningprocessed_content, current_reasoning = reasoning_processor.process_chunk(content)# Update delta with processed contentif current_reasoning:delta['reasoning_content'] = current_reasoning14 unmodified linesyield "data: [DONE]\n\n"return# Handle any remaining buffer contentif reasoning_processor.tag_buffer and not reasoning_processor.inside_tag:# Output any remaining contentfinal_chunk = {"id": f"chatcmpl-{int(time.time())}","object": "chat.completion.chunk","created": int(time.time()),"model": request.model,"choices": [{"index": 0, "delta": {"content": reasoning_processor.tag_buffer}, "finish_reason": None}]}yield f"data: {json.dumps(final_chunk)}\n\n"elif reasoning_processor.inside_tag and reasoning_processor.reasoning_buffer:# We were inside a tag but never found the closing tagprint(f"WARNING: Unclosed reasoning tag detected. Partial reasoning: {reasoning_processor.reasoning_buffer[:100]}...")yield "data: [DONE]\n\n"119 unmodified lines1201211221231241251261271281296 unmodified lines13613713813914014114214314414514614714814915014 unmodified lines165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201119 unmodified lines# Create processor for tag-based extraction across chunksreasoning_processor = StreamingReasoningProcessor(VERTEX_REASONING_TAG)chunk_count = 0async for chunk in stream_response:chunk_count += 1try:chunk_as_dict = chunk.model_dump(exclude_unset=True, exclude_none=True)6 unmodified linesdel delta['extra_content']content = delta.get('content', '')if content:print(f"DEBUG: Chunk {chunk_count} - Raw content: '{content}'")# Use the processor to extract reasoningprocessed_content, current_reasoning = reasoning_processor.process_chunk(content)# Debug logging for processing resultsif processed_content or current_reasoning:print(f"DEBUG: Chunk {chunk_count} - Processed content: '{processed_content}', Reasoning: '{current_reasoning[:50]}...' if len(current_reasoning) > 50 else '{current_reasoning}'")# Update delta with processed contentif current_reasoning:delta['reasoning_content'] = current_reasoning14 unmodified linesyield "data: [DONE]\n\n"return# Debug logging for buffer state and chunk countprint(f"DEBUG: Stream ended after {chunk_count} chunks. Buffer state - tag_buffer: '{reasoning_processor.tag_buffer}', "f"inside_tag: {reasoning_processor.inside_tag}, "f"reasoning_buffer: '{reasoning_processor.reasoning_buffer[:50]}...' if reasoning_processor.reasoning_buffer else ''")# Flush any remaining buffered contentremaining_content, remaining_reasoning = reasoning_processor.flush_remaining()if remaining_content:print(f"DEBUG: Flushing remaining content: '{remaining_content}'")final_chunk = {"id": f"chatcmpl-{int(time.time())}","object": "chat.completion.chunk","created": int(time.time()),"model": request.model,"choices": [{"index": 0, "delta": {"content": remaining_content}, "finish_reason": None}]}yield f"data: {json.dumps(final_chunk)}\n\n"# Send a proper finish reason chunkfinish_chunk = {"id": f"chatcmpl-{int(time.time())}","object": "chat.completion.chunk","created": int(time.time()),"model": request.model,"choices": [{"index": 0, "delta": {}, "finish_reason": "stop"}]}yield f"data: {json.dumps(finish_chunk)}\n\n"# Note: remaining_reasoning is not used here since incomplete reasoning# is treated as regular content when tags are unclosedyield "data: [DONE]\n\n"
app/api_helpers.py+27
app/openai_handler.py+31-8