factor extraction pipeline ready (#16)

* run the code

* update code

* remove some redundant code

---------

Co-authored-by: xuyang1 <xuyang1@microsoft.com>
This commit is contained in:
Xu Yang
2024-06-05 15:36:15 +08:00
committed by GitHub
parent 62a2f7a742
commit 1e77557293
30 changed files with 633 additions and 440 deletions
+48
View File
@@ -0,0 +1,48 @@
import pickle
import unittest
from pathlib import Path
import json
import random
from rdagent.oai.llm_utils import APIBackend
class TestChatCompletion(unittest.TestCase):
def test_chat_completion(self):
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
)
assert response is not None
assert type(response) == str
def test_chat_completion_json_mode(self):
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
)
assert response is not None
assert type(response) == str
json.loads(response)
def test_chat_multi_round(self):
system_prompt = "You are a helpful assistant."
fruit_name = ["apple", "banana", "orange", "grape", "watermelon"][random.randint(0, 4)]
user_prompt_1 = f"I will tell you a name of fruit, please remember them and tell me later. The name is {fruit_name}. Once you remembeer it, please answer OK."
user_prompt_2 = f"What is the name of the fruit I told you before?"
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
assert fruit_name in response2.lower()
if __name__ == "__main__":
unittest.main()
View File
+25
View File
@@ -0,0 +1,25 @@
import pickle
import unittest
from pathlib import Path
import json
import random
from rdagent.oai.llm_utils import APIBackend, calculate_embedding_distance_between_str_list
class TestEmbedding(unittest.TestCase):
def test_embedding(self):
emb = APIBackend().create_embedding("hello")
assert emb is not None
assert type(emb) == list
assert len(emb) > 0
def test_embedding_similarity(self):
similarity = calculate_embedding_distance_between_str_list(["Hello"], ["Hi"])[0][0]
assert similarity is not None
assert type(similarity) == float
assert similarity >= 0.8
if __name__ == "__main__":
unittest.main()