|
| 1 | +import pytest |
| 2 | +from openai.types.chat.chat_completion import ChatCompletion |
| 3 | + |
| 4 | +from astrbot.core.provider.sources.openai_source import ProviderOpenAIOfficial |
| 5 | + |
| 6 | + |
| 7 | +@pytest.mark.asyncio |
| 8 | +@pytest.mark.parametrize( |
| 9 | + ("content", "reasoning_field", "answer", "reasoning"), |
| 10 | + [ |
| 11 | + ("<think>First\nsecond</think>Answer", None, "Answer", "First\nsecond"), |
| 12 | + ("<thinking>First\nsecond</thinking>Answer", None, "Answer", "First\nsecond"), |
| 13 | + ("<thinking></thinking>Answer", None, "Answer", ""), |
| 14 | + ("<thinking>Only reasoning</thinking>", None, "", "Only reasoning"), |
| 15 | + ( |
| 16 | + "<think>One</think><thinking>Two</thinking>Answer", |
| 17 | + None, |
| 18 | + "Answer", |
| 19 | + "One\nTwo", |
| 20 | + ), |
| 21 | + ("Answer</think>", None, "Answer", None), |
| 22 | + ("Answer</thinking> ", None, "Answer", None), |
| 23 | + ( |
| 24 | + "Ordinary thinking and reasoning", |
| 25 | + None, |
| 26 | + "Ordinary thinking and reasoning", |
| 27 | + None, |
| 28 | + ), |
| 29 | + ("<thinker>Example</thinker>", None, "<thinker>Example</thinker>", None), |
| 30 | + ("<thinking>Inline</thinking>Answer", "Structured", "Answer", "Structured"), |
| 31 | + ("<thinking>Inline</thinking>Answer", "", "Answer", ""), |
| 32 | + ("<thinking>Unfinished", None, "<thinking>Unfinished", None), |
| 33 | + ( |
| 34 | + "<think>Example</thinking>Answer", |
| 35 | + None, |
| 36 | + "<think>Example</thinking>Answer", |
| 37 | + None, |
| 38 | + ), |
| 39 | + ], |
| 40 | +) |
| 41 | +async def test_openai_reasoning_tag_compatibility( |
| 42 | + content, reasoning_field, answer, reasoning |
| 43 | +): |
| 44 | + provider = ProviderOpenAIOfficial.__new__(ProviderOpenAIOfficial) |
| 45 | + provider.reasoning_key = "reasoning_content" |
| 46 | + message = {"role": "assistant", "content": content} |
| 47 | + if reasoning_field is not None: |
| 48 | + message["reasoning_content"] = reasoning_field |
| 49 | + completion = ChatCompletion.model_validate( |
| 50 | + { |
| 51 | + "id": "reasoning-tags", |
| 52 | + "object": "chat.completion", |
| 53 | + "created": 0, |
| 54 | + "model": "test-model", |
| 55 | + "choices": [ |
| 56 | + { |
| 57 | + "index": 0, |
| 58 | + "finish_reason": "stop", |
| 59 | + "message": message, |
| 60 | + } |
| 61 | + ], |
| 62 | + } |
| 63 | + ) |
| 64 | + response = await provider._parse_openai_completion(completion, None) |
| 65 | + assert response.completion_text == answer |
| 66 | + if reasoning is not None: |
| 67 | + assert response.reasoning_content == reasoning |
| 68 | + else: |
| 69 | + assert not response.reasoning_content |
0 commit comments