mirror of
https://github.com/NicolasBohn/NexQuant.git
synced 2026-08-05 03:07:42 +00:00
fix: improve the logic of json_schema and refine the reasoning extraction logic for reasoning model (#1044)
* fix: fix a small bug in response_schema * feat: support response_format parameter in chat completion * fix: fix between json_mode and response_format * Update base.py * Update deprec.py * add unittest and refine logic * fix the reasoning extraction logic and refine prompt for deepseek adaptation * refactor: introduce workflow_check and streamline task parsing * refine prompt --------- Co-authored-by: Young <afe.young@gmail.com>
This commit is contained in:
@@ -1,9 +1,20 @@
|
||||
import json
|
||||
import unittest
|
||||
from typing import Any, Dict, List, Union
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from rdagent.oai.llm_utils import APIBackend
|
||||
|
||||
|
||||
class TestPersonModel(BaseModel):
|
||||
"""This is a test Pydantic model"""
|
||||
|
||||
name: str = Field(description="name")
|
||||
age: int = Field(description="age")
|
||||
skills: List[str] = Field(description="skills")
|
||||
|
||||
|
||||
class TestChatCompletion(unittest.TestCase):
|
||||
def test_chat_completion(self) -> None:
|
||||
system_prompt = "You are a helpful assistant."
|
||||
@@ -34,6 +45,57 @@ class TestChatCompletion(unittest.TestCase):
|
||||
assert token is not None
|
||||
assert isinstance(token, int)
|
||||
|
||||
def test_json_mode_with_specific_target_type(self) -> None:
|
||||
"""Test json_mode=True with specific json_target_type"""
|
||||
system_prompt = "You are a helpful assistant. Please respond according to requirements."
|
||||
user_prompt = "Generate programmer information including name, age, and skills list"
|
||||
|
||||
response = APIBackend().build_messages_and_create_chat_completion(
|
||||
system_prompt=system_prompt,
|
||||
user_prompt=user_prompt,
|
||||
json_mode=True,
|
||||
json_target_type=Dict[str, Union[str, int, List[str]]],
|
||||
)
|
||||
|
||||
# Verify response format
|
||||
assert response is not None
|
||||
assert isinstance(response, str)
|
||||
|
||||
# Verify JSON format
|
||||
parsed = json.loads(response)
|
||||
assert isinstance(parsed, dict)
|
||||
|
||||
def test_response_format_with_basemodel(self) -> None:
|
||||
"""Test response_format with BaseModel (if supported)"""
|
||||
backend = APIBackend()
|
||||
|
||||
system_prompt = "You are a helpful assistant. Please respond according to requirements."
|
||||
user_prompt = "Generate programmer information including name, age, and skills list"
|
||||
|
||||
if backend.supports_response_schema():
|
||||
# Use BaseModel when response_schema is supported
|
||||
response = backend.build_messages_and_create_chat_completion(
|
||||
system_prompt=system_prompt,
|
||||
user_prompt=user_prompt,
|
||||
response_format=TestPersonModel,
|
||||
)
|
||||
else:
|
||||
# Use dict + json_target_type when not supported
|
||||
response = backend.build_messages_and_create_chat_completion(
|
||||
system_prompt=system_prompt,
|
||||
user_prompt=user_prompt,
|
||||
response_format={"type": "json_object"},
|
||||
json_target_type=Dict[str, Union[str, int, List[str]]],
|
||||
)
|
||||
|
||||
# Verify response format
|
||||
assert response is not None
|
||||
assert isinstance(response, str)
|
||||
|
||||
# Verify JSON format
|
||||
parsed = json.loads(response)
|
||||
assert isinstance(parsed, dict)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user