XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet
公开
关注 0 Fork 0 Star 0
返回提交历史

XFEstudio/gpt4free

Add comprehensive reasoning field standardization tests

Co-authored-by: hlohaus <983577+hlohaus@users.noreply.github.com>

9b59ca7b
copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
提交于

代码差异

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