Files
NexQuant/test/oai/test_completion.py
T

51 lines
1.8 KiB
Python
Raw Normal View History

2024-06-05 15:36:15 +08:00
import json
import random
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)
2024-06-12 15:12:11 +08:00
def test_chat_multi_round(self) -> None:
2024-06-05 15:36:15 +08:00
system_prompt = "You are a helpful assistant."
2024-06-12 15:12:11 +08:00
fruit_name = random.SystemRandom().choice(["apple", "banana", "orange", "grape", "watermelon"])
user_prompt_1 = (
f"I will tell you a name of fruit, please remember them and tell me later. "
f"The name is {fruit_name}. Once you remember it, please answer OK."
)
user_prompt_2 = "What is the name of the fruit I told you before?"
2024-06-05 15:36:15 +08:00
session = APIBackend().build_chat_session(session_system_prompt=system_prompt)
response_1 = session.build_chat_completion(user_prompt=user_prompt_1)
assert response_1 is not None
assert "ok" in response_1.lower()
response2 = session.build_chat_completion(user_prompt=user_prompt_2)
assert response2 is not None
if __name__ == "__main__":
unittest.main()