Spaces:
Running
Running
Added unit test.
Browse files
tests/test_task_description_generator.py
ADDED
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
import unittest
|
3 |
+
from unittest.mock import MagicMock, patch
|
4 |
+
from langchain.chat_models import ChatOpenAI
|
5 |
+
from meta_prompt.sample_generator import TaskDescriptionGenerator
|
6 |
+
|
7 |
+
class TestTaskDescriptionGenerator(unittest.TestCase):
|
8 |
+
|
9 |
+
def setUp(self):
|
10 |
+
self.model = ChatOpenAI(model="llama3-70b-8192", temperature=1.0, max_retries=3)
|
11 |
+
self.generator = TaskDescriptionGenerator(self.model)
|
12 |
+
|
13 |
+
@patch.object(ChatOpenAI, "call")
|
14 |
+
def test_generate_description(self, mock_call):
|
15 |
+
mock_call.return_value = "Task Description: Describe a cat."
|
16 |
+
input_json = json.dumps({"input": "A cat", "output": "A furry animal"})
|
17 |
+
description = self.generator.generate_description(input_json)
|
18 |
+
self.assertEqual(description, "Task Description: Describe a cat.")
|
19 |
+
|
20 |
+
@patch.object(ChatOpenAI, "call")
|
21 |
+
def test_analyze_input(self, mock_call):
|
22 |
+
mock_call.return_value = "Input Analysis: The input is an animal."
|
23 |
+
description = "Task Description: Describe a cat."
|
24 |
+
input_analysis = self.generator.analyze_input(description)
|
25 |
+
self.assertEqual(input_analysis, "Input Analysis: The input is an animal.")
|
26 |
+
|
27 |
+
@patch.object(ChatOpenAI, "call")
|
28 |
+
def test_generate_briefs(self, mock_call):
|
29 |
+
mock_call.return_value = '{"new_example_briefs": [{"example_brief": "Brief 1"}, {"example_brief": "Brief 2"}]}'
|
30 |
+
description = "Task Description: Describe a cat."
|
31 |
+
input_analysis = "Input Analysis: The input is an animal."
|
32 |
+
generating_batch_size = 2
|
33 |
+
briefs = self.generator.generate_briefs(description, input_analysis, generating_batch_size)
|
34 |
+
self.assertEqual(briefs, {"new_example_briefs": [{"example_brief": "Brief 1"}, {"example_brief": "Brief 2"}]})
|
35 |
+
|
36 |
+
@patch.object(ChatOpenAI, "call")
|
37 |
+
def test_generate_examples_from_briefs(self, mock_call):
|
38 |
+
mock_call.return_value = '{"examples": [{"input": "Input 1", "output": "Output 1"}, {"input": "Input 2", "output": "Output 2"}]}'
|
39 |
+
description = "Task Description: Describe a cat."
|
40 |
+
new_example_briefs = {"new_example_briefs": [{"example_brief": "Brief 1"}, {"example_brief": "Brief 2"}]}
|
41 |
+
raw_example = json.dumps({"input": "A cat", "output": "A furry animal"})
|
42 |
+
generating_batch_size = 2
|
43 |
+
examples = self.generator.generate_examples_from_briefs(description, new_example_briefs, raw_example, generating_batch_size)
|
44 |
+
self.assertEqual(examples, {"examples": [{"input": "Input 1", "output": "Output 1"}, {"input": "Input 2", "output": "Output 2"}]})
|
45 |
+
|
46 |
+
@patch.object(ChatOpenAI, "call")
|
47 |
+
def test_generate_examples(self, mock_call):
|
48 |
+
mock_call.return_value = '{"examples": [{"input": "Input 1", "output": "Output 1"}, {"input": "Input 2", "output": "Output 2"}]}'
|
49 |
+
description = "Task Description: Describe a cat."
|
50 |
+
raw_example = json.dumps({"input": "A cat", "output": "A furry animal"})
|
51 |
+
generating_batch_size = 2
|
52 |
+
examples = self.generator.generate_examples(description, raw_example, generating_batch_size)
|
53 |
+
self.assertEqual(examples, {"examples": [{"input": "Input 1", "output": "Output 1"}, {"input": "Input 2", "output": "Output 2"}]})
|
54 |
+
|
55 |
+
if __name__ == '__main__':
|
56 |
+
unittest.main()
|