File size: 4,697 Bytes
a4b70d9 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
#!/usr/bin/env python3
"""
Create a comprehensive test for reasoning field standardization
"""
import sys
import unittest
import json
from g4f.providers.response import Reasoning
from g4f.client.stubs import ChatCompletionDelta, ChatCompletionChunk
class TestReasoningFieldStandardization(unittest.TestCase):
def test_reasoning_object_structure(self):
"""Test the basic Reasoning object structure"""
reasoning = Reasoning("thinking content", status="processing")
expected_dict = {
'token': 'thinking content',
'status': 'processing'
}
self.assertEqual(reasoning.get_dict(), expected_dict)
self.assertEqual(str(reasoning), "thinking content")
def test_streaming_delta_with_reasoning(self):
"""Test ChatCompletionDelta with Reasoning object"""
reasoning = Reasoning("I need to think about this...", status="thinking")
delta = ChatCompletionDelta.model_construct(reasoning)
# Check the delta structure
self.assertEqual(delta.role, "assistant")
self.assertIsNone(delta.content)
self.assertEqual(delta.reasoning, "I need to think about this...")
def test_current_api_format_consistency(self):
"""Test what the API should output for reasoning"""
reasoning = Reasoning("thinking token", status="processing")
# Simulate the _format_json function from api.py
def format_json(response_type: str, content=None, **kwargs):
if content is not None and isinstance(response_type, str):
return {
'type': response_type,
response_type: content,
**kwargs
}
return {
'type': response_type,
**kwargs
}
# Test current format
formatted = format_json("reasoning", **reasoning.get_dict())
expected = {
'type': 'reasoning',
'token': 'thinking token',
'status': 'processing'
}
self.assertEqual(formatted, expected)
def test_openai_compatible_streaming_format(self):
"""Test what an OpenAI-compatible format would look like"""
reasoning = Reasoning("step by step reasoning", status="thinking")
# What OpenAI format would look like
openai_format = {
"id": "chatcmpl-test",
"object": "chat.completion.chunk",
"choices": [{
"index": 0,
"delta": {
"role": "assistant",
"reasoning": str(reasoning) # OpenAI uses 'reasoning' field
},
"finish_reason": None
}]
}
self.assertEqual(openai_format["choices"][0]["delta"]["reasoning"], "step by step reasoning")
def test_deepseek_compatible_format(self):
"""Test what a DeepSeek-compatible format would look like"""
reasoning = Reasoning("analytical reasoning", status="thinking")
# What DeepSeek format would look like
deepseek_format = {
"id": "chatcmpl-test",
"object": "chat.completion.chunk",
"choices": [{
"index": 0,
"delta": {
"role": "assistant",
"reasoning_content": str(reasoning) # DeepSeek uses 'reasoning_content' field
},
"finish_reason": None
}]
}
self.assertEqual(deepseek_format["choices"][0]["delta"]["reasoning_content"], "analytical reasoning")
def test_proposed_standardization(self):
"""Test the proposed standardized format"""
reasoning = Reasoning("standardized reasoning", status="thinking")
# Proposed: Use OpenAI's 'reasoning' field name for consistency
# But support both input formats (already done in OpenaiTemplate)
# Current g4f streaming should use 'reasoning' field in delta
proposed_format = {
"id": "chatcmpl-test",
"object": "chat.completion.chunk",
"choices": [{
"index": 0,
"delta": {
"role": "assistant",
"reasoning": str(reasoning) # Standardize on OpenAI format
},
"finish_reason": None
}]
}
self.assertEqual(proposed_format["choices"][0]["delta"]["reasoning"], "standardized reasoning")
if __name__ == "__main__":
unittest.main() |