2024-06-05 15:36:15 +08:00
|
|
|
import json
|
2024-06-12 15:12:11 +08:00
|
|
|
import unittest
|
2024-06-05 15:36:15 +08:00
|
|
|
|
|
|
|
|
from rdagent.oai.llm_utils import APIBackend
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestChatCompletion(unittest.TestCase):
|
2024-06-12 15:12:11 +08:00
|
|
|
def test_chat_completion(self) -> None:
|
2024-06-05 15:36:15 +08:00
|
|
|
system_prompt = "You are a helpful assistant."
|
|
|
|
|
user_prompt = "What is your name?"
|
|
|
|
|
response = APIBackend().build_messages_and_create_chat_completion(
|
2024-06-28 11:45:23 +08:00
|
|
|
system_prompt=system_prompt,
|
|
|
|
|
user_prompt=user_prompt,
|
2024-06-05 15:36:15 +08:00
|
|
|
)
|
|
|
|
|
assert response is not None
|
2024-06-12 15:12:11 +08:00
|
|
|
assert isinstance(response, str)
|
2024-06-05 15:36:15 +08:00
|
|
|
|
2024-06-12 15:12:11 +08:00
|
|
|
def test_chat_completion_json_mode(self) -> None:
|
2024-06-05 15:36:15 +08:00
|
|
|
system_prompt = "You are a helpful assistant. answer in Json format."
|
|
|
|
|
user_prompt = "What is your name?"
|
|
|
|
|
response = APIBackend().build_messages_and_create_chat_completion(
|
2024-06-28 11:45:23 +08:00
|
|
|
system_prompt=system_prompt,
|
|
|
|
|
user_prompt=user_prompt,
|
|
|
|
|
json_mode=True,
|
2024-06-05 15:36:15 +08:00
|
|
|
)
|
|
|
|
|
assert response is not None
|
2024-06-12 15:12:11 +08:00
|
|
|
assert isinstance(response, str)
|
2024-06-05 15:36:15 +08:00
|
|
|
json.loads(response)
|
|
|
|
|
|
2025-02-13 15:16:18 +08:00
|
|
|
def test_build_messages_and_calculate_token(self) -> None:
|
2024-10-21 11:32:20 +08:00
|
|
|
system_prompt = "You are a helpful assistant."
|
2025-02-13 15:16:18 +08:00
|
|
|
user_prompt = "What is your name?"
|
|
|
|
|
token = APIBackend().build_messages_and_calculate_token(user_prompt=user_prompt, system_prompt=system_prompt)
|
|
|
|
|
assert token is not None
|
|
|
|
|
assert isinstance(token, int)
|
2024-10-21 11:32:20 +08:00
|
|
|
|
2024-06-05 15:36:15 +08:00
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
unittest.main()
|