Files
NexQuant/test/oai/test_completion.py
T

40 lines
1.3 KiB
Python
Raw Normal View History

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(
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(
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)
def test_build_messages_and_calculate_token(self) -> None:
system_prompt = "You are a helpful assistant."
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-06-05 15:36:15 +08:00
if __name__ == "__main__":
unittest.main()