|
1 | 1 | from typing import TYPE_CHECKING, Any, cast |
2 | 2 |
|
3 | 3 | import pytest |
4 | | -from openai.types.chat import ChatCompletionMessage |
| 4 | +from openai.types.chat import ChatCompletion, ChatCompletionMessage |
| 5 | +from openai.types.chat.chat_completion import Choice |
5 | 6 | from openai.types.chat.chat_completion_message_tool_call import ChatCompletionMessageToolCall, Function |
6 | 7 |
|
7 | 8 | from cleanlab_tlm.utils.chat import ( |
8 | 9 | _form_prompt_chat_completions_api, |
9 | 10 | _form_prompt_responses_api, |
10 | 11 | form_prompt_string, |
| 12 | + form_response_string_chat_completions, |
11 | 13 | form_response_string_chat_completions_api, |
12 | 14 | ) |
13 | 15 |
|
@@ -1589,3 +1591,91 @@ def test_form_response_string_chat_completions_api_chatcompletion_message_none_c |
1589 | 1591 | expected = "" |
1590 | 1592 | result = form_response_string_chat_completions_api(message) |
1591 | 1593 | assert result == expected |
| 1594 | + |
| 1595 | + |
| 1596 | +def test_form_response_string_chat_completions_just_content() -> None: |
| 1597 | + """Test form_response_string_chat_completions with ChatCompletion containing just content.""" |
| 1598 | + |
| 1599 | + content = "Hello, how can I help you today?" |
| 1600 | + |
| 1601 | + message = ChatCompletionMessage(role="assistant", content=content) |
| 1602 | + response = ChatCompletion( |
| 1603 | + id="test", |
| 1604 | + choices=[ |
| 1605 | + Choice( |
| 1606 | + index=0, |
| 1607 | + message=message, |
| 1608 | + finish_reason="stop", |
| 1609 | + ) |
| 1610 | + ], |
| 1611 | + created=1234567890, |
| 1612 | + model="test-model", |
| 1613 | + object="chat.completion", |
| 1614 | + ) |
| 1615 | + |
| 1616 | + result = form_response_string_chat_completions(response) |
| 1617 | + assert result == content |
| 1618 | + |
| 1619 | + assert result == form_response_string_chat_completions_api(message) |
| 1620 | + |
| 1621 | + |
| 1622 | +def test_form_response_string_chat_completions_multiple_choices() -> None: |
| 1623 | + """Test form_response_string_chat_completions with ChatCompletion containing multiple choices.""" |
| 1624 | + |
| 1625 | + content_first = "Hello, how can I help you today?" |
| 1626 | + content_second = "Hi there! What can I do for you?" |
| 1627 | + |
| 1628 | + message_first = ChatCompletionMessage(role="assistant", content=content_first) |
| 1629 | + message_second = ChatCompletionMessage(role="assistant", content=content_second) |
| 1630 | + response = ChatCompletion( |
| 1631 | + id="test", |
| 1632 | + choices=[ |
| 1633 | + Choice( |
| 1634 | + index=0, |
| 1635 | + message=message_first, |
| 1636 | + finish_reason="stop", |
| 1637 | + ), |
| 1638 | + Choice( |
| 1639 | + index=1, |
| 1640 | + message=message_second, |
| 1641 | + finish_reason="stop", |
| 1642 | + ), |
| 1643 | + ], |
| 1644 | + created=1234567890, |
| 1645 | + model="test-model", |
| 1646 | + object="chat.completion", |
| 1647 | + ) |
| 1648 | + |
| 1649 | + result = form_response_string_chat_completions(response) |
| 1650 | + assert result == content_first |
| 1651 | + |
| 1652 | + assert result == form_response_string_chat_completions_api(message_first) |
| 1653 | + |
| 1654 | + |
| 1655 | +def test_form_response_string_chat_completions_uses_api_function() -> None: |
| 1656 | + """Test that form_response_string_chat_completions calls form_response_string_chat_completions_api.""" |
| 1657 | + from unittest.mock import patch |
| 1658 | + |
| 1659 | + message = ChatCompletionMessage(role="assistant", content="Test response") |
| 1660 | + response = ChatCompletion( |
| 1661 | + id="test", |
| 1662 | + choices=[ |
| 1663 | + Choice( |
| 1664 | + index=0, |
| 1665 | + message=message, |
| 1666 | + finish_reason="stop", |
| 1667 | + ) |
| 1668 | + ], |
| 1669 | + created=1234567890, |
| 1670 | + model="test-model", |
| 1671 | + object="chat.completion", |
| 1672 | + ) |
| 1673 | + |
| 1674 | + # Mock the api function and test that it's called |
| 1675 | + with patch("cleanlab_tlm.utils.chat.form_response_string_chat_completions_api") as mock_api_func: |
| 1676 | + mock_api_func.return_value = "Mocked response" |
| 1677 | + |
| 1678 | + result = form_response_string_chat_completions(response) |
| 1679 | + |
| 1680 | + mock_api_func.assert_called_once_with(message) |
| 1681 | + assert result == "Mocked response" |
0 commit comments