lionagi 0.0.306__py3-none-any.whl → 0.0.307__py3-none-any.whl

Sign up to get free protection for your applications and to get access to all the features.
Files changed (78) hide show
  1. lionagi/__init__.py +2 -5
  2. lionagi/core/__init__.py +7 -5
  3. lionagi/core/agent/__init__.py +3 -0
  4. lionagi/core/agent/base_agent.py +10 -12
  5. lionagi/core/branch/__init__.py +4 -0
  6. lionagi/core/branch/base_branch.py +81 -81
  7. lionagi/core/branch/branch.py +16 -28
  8. lionagi/core/branch/branch_flow_mixin.py +3 -7
  9. lionagi/core/branch/executable_branch.py +86 -56
  10. lionagi/core/branch/util.py +77 -162
  11. lionagi/core/{flow/direct → direct}/__init__.py +1 -1
  12. lionagi/core/{flow/direct/predict.py → direct/parallel_predict.py} +39 -17
  13. lionagi/core/direct/parallel_react.py +0 -0
  14. lionagi/core/direct/parallel_score.py +0 -0
  15. lionagi/core/direct/parallel_select.py +0 -0
  16. lionagi/core/direct/parallel_sentiment.py +0 -0
  17. lionagi/core/direct/predict.py +174 -0
  18. lionagi/core/{flow/direct → direct}/react.py +2 -2
  19. lionagi/core/{flow/direct → direct}/score.py +28 -23
  20. lionagi/core/{flow/direct → direct}/select.py +48 -45
  21. lionagi/core/direct/utils.py +83 -0
  22. lionagi/core/flow/monoflow/ReAct.py +6 -5
  23. lionagi/core/flow/monoflow/__init__.py +9 -0
  24. lionagi/core/flow/monoflow/chat.py +10 -10
  25. lionagi/core/flow/monoflow/chat_mixin.py +11 -10
  26. lionagi/core/flow/monoflow/followup.py +6 -5
  27. lionagi/core/flow/polyflow/__init__.py +1 -0
  28. lionagi/core/flow/polyflow/chat.py +15 -3
  29. lionagi/core/mail/mail_manager.py +18 -19
  30. lionagi/core/mail/schema.py +5 -4
  31. lionagi/core/messages/schema.py +18 -20
  32. lionagi/core/prompt/__init__.py +0 -0
  33. lionagi/core/prompt/prompt_template.py +0 -0
  34. lionagi/core/schema/__init__.py +2 -2
  35. lionagi/core/schema/action_node.py +11 -3
  36. lionagi/core/schema/base_mixin.py +56 -59
  37. lionagi/core/schema/base_node.py +35 -38
  38. lionagi/core/schema/condition.py +24 -0
  39. lionagi/core/schema/data_logger.py +96 -99
  40. lionagi/core/schema/data_node.py +19 -19
  41. lionagi/core/schema/prompt_template.py +0 -0
  42. lionagi/core/schema/structure.py +171 -169
  43. lionagi/core/session/__init__.py +1 -3
  44. lionagi/core/session/session.py +196 -214
  45. lionagi/core/tool/tool_manager.py +95 -103
  46. lionagi/integrations/__init__.py +1 -3
  47. lionagi/integrations/bridge/langchain_/documents.py +17 -18
  48. lionagi/integrations/bridge/langchain_/langchain_bridge.py +14 -14
  49. lionagi/integrations/bridge/llamaindex_/llama_index_bridge.py +22 -22
  50. lionagi/integrations/bridge/llamaindex_/node_parser.py +12 -12
  51. lionagi/integrations/bridge/llamaindex_/reader.py +11 -11
  52. lionagi/integrations/bridge/llamaindex_/textnode.py +7 -7
  53. lionagi/integrations/config/openrouter_configs.py +0 -1
  54. lionagi/integrations/provider/oai.py +26 -26
  55. lionagi/integrations/provider/services.py +38 -38
  56. lionagi/libs/__init__.py +34 -1
  57. lionagi/libs/ln_api.py +211 -221
  58. lionagi/libs/ln_async.py +53 -60
  59. lionagi/libs/ln_convert.py +118 -120
  60. lionagi/libs/ln_dataframe.py +32 -33
  61. lionagi/libs/ln_func_call.py +334 -342
  62. lionagi/libs/ln_nested.py +99 -107
  63. lionagi/libs/ln_parse.py +161 -165
  64. lionagi/libs/sys_util.py +52 -52
  65. lionagi/tests/test_core/test_session.py +254 -266
  66. lionagi/tests/test_core/test_session_base_util.py +299 -300
  67. lionagi/tests/test_core/test_tool_manager.py +70 -74
  68. lionagi/tests/test_libs/test_nested.py +2 -7
  69. lionagi/tests/test_libs/test_parse.py +2 -2
  70. lionagi/version.py +1 -1
  71. {lionagi-0.0.306.dist-info → lionagi-0.0.307.dist-info}/METADATA +4 -2
  72. lionagi-0.0.307.dist-info/RECORD +115 -0
  73. lionagi/core/flow/direct/utils.py +0 -43
  74. lionagi-0.0.306.dist-info/RECORD +0 -106
  75. /lionagi/core/{flow/direct → direct}/sentiment.py +0 -0
  76. {lionagi-0.0.306.dist-info → lionagi-0.0.307.dist-info}/LICENSE +0 -0
  77. {lionagi-0.0.306.dist-info → lionagi-0.0.307.dist-info}/WHEEL +0 -0
  78. {lionagi-0.0.306.dist-info → lionagi-0.0.307.dist-info}/top_level.txt +0 -0
@@ -1,313 +1,312 @@
1
- from lionagi.core.branch.util import MessageUtil
2
- from lionagi.core.messages.schema import System, Instruction, Response
3
-
4
- import unittest
5
- import pandas as pd
6
- import json
7
- from datetime import datetime
8
-
9
-
10
- class TestCreateMessage(unittest.TestCase):
11
-
12
- def test_create_system_message(self):
13
- """Test creating a System message."""
14
- system_info = {"system_info": "System information"}
15
- message = MessageUtil.create_message(system=system_info["system_info"])
16
- self.assertIsInstance(message, System)
17
- self.assertEqual(message.content, system_info)
18
-
19
- def test_create_instruction_message(self):
20
- """Test creating an Instruction message with context."""
21
- instruction_info = {"task": "Do something"}
22
- context = {"additional": "context"}
23
- message = MessageUtil.create_message(
24
- instruction=instruction_info, context=context
25
- )
26
- self.assertIsInstance(message, Instruction)
27
- self.assertEqual(message.content["instruction"], instruction_info)
28
- self.assertEqual(message.content["context"], context)
29
-
30
- def test_create_response_message(self):
31
- """Test creating a Response message."""
32
- response_info = {"message": {"content": "This is a response"}}
33
- message = MessageUtil.create_message(response=response_info)
34
- self.assertIsInstance(message, Response)
35
- self.assertEqual(
36
- message.content["response"], response_info["message"]["content"]
37
- )
38
-
39
- def test_error_on_multiple_roles(self):
40
- """Test error is raised when multiple roles are provided."""
41
- with self.assertRaises(ValueError):
42
- MessageUtil.create_message(
43
- system={"info": "info"}, instruction={"task": "task"}
44
- )
45
-
46
- def test_return_existing_base_message_instance(self):
47
- """Test returning an existing BaseMessage instance if provided."""
48
- existing_message = System(system={"info": "Already created"})
49
- message = MessageUtil.create_message(system=existing_message)
50
- self.assertEqual(message.content, existing_message.content)
51
-
52
-
53
- class TestValidateMessages(unittest.TestCase):
54
-
55
- # def test_validate_messages_correct_format(self):
56
- # """Test messages DataFrame with the correct format."""
57
- # messages = pd.DataFrame({
58
- # "node_id": ["1"],
59
- # "role": ["user"],
60
- # "sender": ["test"],
61
- # "timestamp": ["2020-01-01T00:00:00"],
62
- # "content": ['{"message": "test"}']
63
- # })
64
- # self.assertTrue(MessageUtil.validate_messages(messages))
65
-
66
- def test_validate_messages_incorrect_columns(self):
67
- """Test messages DataFrame with incorrect columns raises ValueError."""
68
- messages = pd.DataFrame(
69
- {
70
- "id": ["1"],
71
- "type": ["user"],
72
- "source": ["test"],
73
- "time": ["2020-01-01T00:00:00"],
74
- "data": ['{"message": "test"}'],
75
- }
76
- )
77
- with self.assertRaises(ValueError):
78
- MessageUtil.validate_messages(messages)
79
-
80
- def test_validate_messages_null_values(self):
81
- """Test messages DataFrame with null values raises ValueError."""
82
- messages = pd.DataFrame(
83
- {
84
- "node_id": [None],
85
- "role": ["user"],
86
- "sender": ["test"],
87
- "timestamp": ["2020-01-01T00:00:00"],
88
- "content": ['{"message": "test"}'],
89
- }
90
- )
91
- with self.assertRaises(ValueError):
92
- MessageUtil.validate_messages(messages)
93
-
94
-
95
- class TestSignMessage(unittest.TestCase):
96
-
97
- def test_sign_message(self):
98
- """Test signing message content with sender."""
99
- messages = pd.DataFrame(
100
- {
101
- "node_id": ["1"],
102
- "role": ["user"],
103
- "sender": ["test"],
104
- "timestamp": ["2020-01-01T00:00:00"],
105
- "content": ["Original message"],
106
- }
107
- )
108
- sender = "system"
109
- signed_messages = MessageUtil.sign_message(messages, sender)
110
- expected_content = "Sender system: Original message"
111
- self.assertEqual(signed_messages["content"][0], expected_content)
112
-
113
- def test_sign_message_invalid_sender(self):
114
- """Test signing message with an invalid sender raises ValueError."""
115
- messages = pd.DataFrame(
116
- {
117
- "node_id": ["1"],
118
- "role": ["user"],
119
- "sender": ["test"],
120
- "timestamp": ["2020-01-01T00:00:00"],
121
- "content": ["Original message"],
122
- }
123
- )
124
- with self.assertRaises(ValueError):
125
- MessageUtil.sign_message(messages, None)
126
-
127
-
128
- class TestFilterMessagesBy(unittest.TestCase):
129
-
130
- def setUp(self):
131
- self.messages = pd.DataFrame(
132
- {
133
- "node_id": ["1", "2"],
134
- "role": ["user", "assistant"],
135
- "sender": ["test", "assistant"],
136
- "timestamp": [datetime(2020, 1, 1), datetime(2020, 1, 2)],
137
- "content": ['{"message": "test"}', '{"response": "ok"}'],
138
- }
139
- )
140
-
141
- def test_filter_by_role(self):
142
- """Test filtering messages by role."""
143
- filtered = MessageUtil.filter_messages_by(self.messages, role="assistant")
144
- self.assertEqual(len(filtered), 1)
145
- self.assertEqual(filtered.iloc[0]["sender"], "assistant")
146
-
147
- def test_filter_by_sender(self):
148
- """Test filtering messages by sender."""
149
- filtered = MessageUtil.filter_messages_by(self.messages, sender="test")
150
- self.assertEqual(len(filtered), 1)
151
- self.assertEqual(filtered.iloc[0]["sender"], "test")
152
-
153
- def test_filter_by_time_range(self):
154
- """Test filtering messages by time range."""
155
- start_time = datetime(2020, 1, 1, 12)
156
- end_time = datetime(2020, 1, 2, 12)
157
- filtered = MessageUtil.filter_messages_by(
158
- self.messages, start_time=start_time, end_time=end_time
159
- )
160
- self.assertEqual(len(filtered), 1)
161
- self.assertTrue(start_time <= filtered.iloc[0]["timestamp"] <= end_time)
162
-
163
-
164
- class TestRemoveMessage(unittest.TestCase):
165
-
166
- def test_remove_message(self):
167
- """Test removing a message by node_id."""
168
- messages = pd.DataFrame(
169
- {
170
- "node_id": ["1", "2"],
171
- "role": ["user", "assistant"],
172
- "content": ["message1", "message2"],
173
- }
174
- )
175
- updated_messages = MessageUtil.remove_message(messages, "1")
176
- self.assertTrue(updated_messages)
177
-
178
-
179
- class TestGetMessageRows(unittest.TestCase):
180
-
181
- def test_get_message_rows(self):
182
- """Test retrieving the last 'n' message rows based on criteria."""
183
- messages = pd.DataFrame(
184
- {
185
- "node_id": ["1", "2", "3"],
186
- "role": ["user", "assistant", "user"],
187
- "sender": ["A", "B", "A"],
188
- "content": ["message1", "message2", "message3"],
189
- }
190
- )
191
- rows = MessageUtil.get_message_rows(messages, sender="A", role="user", n=2)
192
- self.assertEqual(len(rows), 2)
193
-
194
-
195
- # class TestExtend(unittest.TestCase):
196
-
197
- # def test_extend(self):
198
- # """Test extending one DataFrame with another, ensuring no duplicate 'node_id'."""
199
- # df1 = pd.DataFrame({
200
- # "node_id": ["1"],
201
- # "role": ["user"],
202
- # "sender": ["test"],
203
- # "timestamp": ["2020-01-01T00:00:00"],
204
- # "content": ['{"message": "test"}']
205
- # })
206
- # df2 = pd.DataFrame({
207
- # "node_id": ["2"],
208
- # "role": ["user"],
209
- # "sender": ["test"],
210
- # "timestamp": ["2020-01-02T00:00:00"],
211
- # "content": ['{"message": "test2"}']
212
- # })
213
- # combined = MessageUtil.extend(df1, df2)
214
- # self.assertEqual(len(combined), 2)
215
-
216
-
217
- class TestToMarkdownString(unittest.TestCase):
218
-
219
- def test_to_markdown_string(self):
220
- """Test converting messages to a Markdown-formatted string."""
221
- messages = pd.DataFrame(
222
- {
223
- "node_id": ["1"],
224
- "role": ["user"],
225
- "content": [json.dumps({"instruction": "Hello, World!"})],
226
- }
227
- )
228
- markdown_str = MessageUtil.to_markdown_string(messages)
229
- self.assertIn("Hello, World!", markdown_str)
230
-
231
-
232
- # class TestSearchKeywords(unittest.TestCase):
233
-
234
- # def test_search_keywords(self):
235
- # """Test filtering DataFrame for rows containing specified keywords."""
236
- # messages = pd.DataFrame(
237
- # {"node_id": ["1", "2"], "content": ["Hello world", "Goodbye world"]}
238
- # )
239
- # filtered = MessageUtil.search_keywords(messages, "Hello")
240
- # print(filtered)
241
- # self.assertEqual(len(filtered), 1)
242
-
243
-
244
- # class TestReplaceKeyword(unittest.TestCase):
245
-
246
- # def test_replace_keyword(self):
247
- # """Test replacing a keyword in DataFrame's specified column."""
248
- # messages = pd.DataFrame({"content": ["Hello world", "Goodbye world"]})
249
- # MessageUtil.replace_keyword(messages, "world", "universe")
250
- # self.assertTrue(all(messages["content"].str.contains("universe")))
251
-
252
-
253
- # class TestReadCsv(unittest.TestCase):
254
-
255
- # @patch("pandas.read_csv")
256
- # def test_read_csv(self, mock_read_csv):
257
- # """Test reading a CSV file into a DataFrame."""
258
- # mock_df = pd.DataFrame(
259
- # {"node_id": ["1", "2"], "content": ["Hello, World!", "Goodbye, World!"]}
260
- # )
261
- # mock_read_csv.return_value = mock_df
262
-
263
- # df = MessageUtil.read_csv("path/to/nonexistent/file.csv")
264
-
265
- # mock_read_csv.assert_called_once_with("path/to/nonexistent/file.csv")
266
-
267
- # self.assertTrue(isinstance(df, pd.DataFrame))
268
- # self.assertEqual(len(df), 2)
269
- # self.assertEqual(list(df.columns), ["node_id", "content"])
1
+ # from lionagi.core.branch.util import MessageUtil
2
+ # from lionagi.core.messages.schema import System, Instruction, Response
3
+
4
+ # import unittest
5
+ # import pandas as pd
6
+ # import json
7
+ # from datetime import datetime
8
+
9
+
10
+ # class TestCreateMessage(unittest.TestCase):
11
+
12
+ # def test_create_system_message(self):
13
+ # """Test creating a System message."""
14
+ # system_info = {"system_info": "System information"}
15
+ # message = MessageUtil.create_message(
16
+ # system=system_info["system_info"]
17
+ # )
18
+ # self.assertIsInstance(message, System)
19
+ # self.assertEqual(message.content, system_info)
20
+
21
+ # def test_create_instruction_message(self):
22
+ # """Test creating an Instruction message with context."""
23
+ # instruction_info = {"task": "Do something"}
24
+ # context = {"additional": "context"}
25
+ # message = MessageUtil.create_message(
26
+ # instruction=instruction_info, context=context
27
+ # )
28
+ # self.assertIsInstance(message, Instruction)
29
+ # self.assertEqual(message.content["instruction"], instruction_info)
30
+ # self.assertEqual(message.content["context"], context)
31
+
32
+ # def test_create_response_message(self):
33
+ # """Test creating a Response message."""
34
+ # response_info = {"message": {"content": "This is a response"}}
35
+ # message = MessageUtil.create_message(response=response_info)
36
+ # self.assertIsInstance(message, Response)
37
+ # self.assertEqual(
38
+ # message.content["response"], response_info["message"]["content"]
39
+ # )
40
+
41
+ # def test_error_on_multiple_roles(self):
42
+ # """Test error is raised when multiple roles are provided."""
43
+ # with self.assertRaises(ValueError):
44
+ # MessageUtil.create_message(
45
+ # system={"info": "info"}, instruction={"task": "task"}
46
+ # )
47
+
48
+ # def test_return_existing_base_message_instance(self):
49
+ # """Test returning an existing BaseMessage instance if provided."""
50
+ # existing_message = System(system={"info": "Already created"})
51
+ # message = MessageUtil.create_message(system=existing_message)
52
+ # self.assertEqual(message.content, existing_message.content)
53
+
54
+
55
+ # class TestValidateMessages(unittest.TestCase):
56
+
57
+ # # def test_validate_messages_correct_format(self):
58
+ # # """Test messages DataFrame with the correct format."""
59
+ # # messages = pd.DataFrame({
60
+ # # "node_id": ["1"],
61
+ # # "role": ["user"],
62
+ # # "sender": ["test"],
63
+ # # "timestamp": ["2020-01-01T00:00:00"],
64
+ # # "content": ['{"message": "test"}']
65
+ # # })
66
+ # # self.assertTrue(MessageUtil.validate_messages(messages))
67
+
68
+ # def test_validate_messages_incorrect_columns(self):
69
+ # """Test messages DataFrame with incorrect columns raises ValueError."""
70
+ # messages = pd.DataFrame(
71
+ # {
72
+ # "id": ["1"], "type": ["user"], "source": ["test"],
73
+ # "time": ["2020-01-01T00:00:00"],
74
+ # "data": ['{"message": "test"}'],
75
+ # }
76
+ # )
77
+ # with self.assertRaises(ValueError):
78
+ # MessageUtil.validate_messages(messages)
79
+
80
+ # def test_validate_messages_null_values(self):
81
+ # """Test messages DataFrame with null values raises ValueError."""
82
+ # messages = pd.DataFrame(
83
+ # {
84
+ # "node_id": [None], "role": ["user"], "sender": ["test"],
85
+ # "timestamp": ["2020-01-01T00:00:00"],
86
+ # "content": ['{"message": "test"}'],
87
+ # }
88
+ # )
89
+ # with self.assertRaises(ValueError):
90
+ # MessageUtil.validate_messages(messages)
91
+
92
+
93
+ # class TestSignMessage(unittest.TestCase):
94
+
95
+ # def test_sign_message(self):
96
+ # """Test signing message content with sender."""
97
+ # messages = pd.DataFrame(
98
+ # {
99
+ # "node_id": ["1"], "role": ["user"], "sender": ["test"],
100
+ # "timestamp": ["2020-01-01T00:00:00"],
101
+ # "content": ["Original message"],
102
+ # }
103
+ # )
104
+ # sender = "system"
105
+ # signed_messages = MessageUtil.sign_message(messages, sender)
106
+ # expected_content = "Sender system: Original message"
107
+ # self.assertEqual(signed_messages["content"][0], expected_content)
108
+
109
+ # def test_sign_message_invalid_sender(self):
110
+ # """Test signing message with an invalid sender raises ValueError."""
111
+ # messages = pd.DataFrame(
112
+ # {
113
+ # "node_id": ["1"], "role": ["user"], "sender": ["test"],
114
+ # "timestamp": ["2020-01-01T00:00:00"],
115
+ # "content": ["Original message"],
116
+ # }
117
+ # )
118
+ # with self.assertRaises(ValueError):
119
+ # MessageUtil.sign_message(messages, None)
120
+
121
+
122
+ # class TestFilterMessagesBy(unittest.TestCase):
123
+
124
+ # def setUp(self):
125
+ # self.messages = pd.DataFrame(
126
+ # {
127
+ # "node_id": ["1", "2"], "role": ["user", "assistant"],
128
+ # "sender": ["test", "assistant"],
129
+ # "timestamp": [datetime(2020, 1, 1), datetime(2020, 1, 2)],
130
+ # "content": ['{"message": "test"}', '{"response": "ok"}'],
131
+ # }
132
+ # )
133
+
134
+ # def test_filter_by_role(self):
135
+ # """Test filtering messages by role."""
136
+ # filtered = MessageUtil.filter_messages_by(
137
+ # self.messages, role="assistant"
138
+ # )
139
+ # self.assertEqual(len(filtered), 1)
140
+ # self.assertEqual(filtered.iloc[0]["sender"], "assistant")
141
+
142
+ # def test_filter_by_sender(self):
143
+ # """Test filtering messages by sender."""
144
+ # filtered = MessageUtil.filter_messages_by(
145
+ # self.messages, sender="test"
146
+ # )
147
+ # self.assertEqual(len(filtered), 1)
148
+ # self.assertEqual(filtered.iloc[0]["sender"], "test")
149
+
150
+ # def test_filter_by_time_range(self):
151
+ # """Test filtering messages by time range."""
152
+ # start_time = datetime(2020, 1, 1, 12)
153
+ # end_time = datetime(2020, 1, 2, 12)
154
+ # filtered = MessageUtil.filter_messages_by(
155
+ # self.messages, start_time=start_time, end_time=end_time
156
+ # )
157
+ # self.assertEqual(len(filtered), 1)
158
+ # self.assertTrue(
159
+ # start_time <= filtered.iloc[0]["timestamp"] <= end_time
160
+ # )
161
+
162
+
163
+ # class TestRemoveMessage(unittest.TestCase):
164
+
165
+ # def test_remove_message(self):
166
+ # """Test removing a message by node_id."""
167
+ # messages = pd.DataFrame(
168
+ # {
169
+ # "node_id": ["1", "2"], "role": ["user", "assistant"],
170
+ # "content": ["message1", "message2"],
171
+ # }
172
+ # )
173
+ # updated_messages = MessageUtil.remove_message(messages, "1")
174
+ # self.assertTrue(updated_messages)
175
+
176
+
177
+ # class TestGetMessageRows(unittest.TestCase):
178
+
179
+ # def test_get_message_rows(self):
180
+ # """Test retrieving the last 'n' message rows based on criteria."""
181
+ # messages = pd.DataFrame(
182
+ # {
183
+ # "node_id": ["1", "2", "3"],
184
+ # "role": ["user", "assistant", "user"],
185
+ # "sender": ["A", "B", "A"],
186
+ # "content": ["message1", "message2", "message3"],
187
+ # }
188
+ # )
189
+ # rows = MessageUtil.get_message_rows(
190
+ # messages, sender="A", role="user", n=2
191
+ # )
192
+ # self.assertEqual(len(rows), 2)
193
+
194
+
195
+ # # class TestExtend(unittest.TestCase):
196
+
197
+ # # def test_extend(self):
198
+ # # """Test extending one DataFrame with another, ensuring no duplicate 'node_id'."""
199
+ # # df1 = pd.DataFrame({
200
+ # # "node_id": ["1"],
201
+ # # "role": ["user"],
202
+ # # "sender": ["test"],
203
+ # # "timestamp": ["2020-01-01T00:00:00"],
204
+ # # "content": ['{"message": "test"}']
205
+ # # })
206
+ # # df2 = pd.DataFrame({
207
+ # # "node_id": ["2"],
208
+ # # "role": ["user"],
209
+ # # "sender": ["test"],
210
+ # # "timestamp": ["2020-01-02T00:00:00"],
211
+ # # "content": ['{"message": "test2"}']
212
+ # # })
213
+ # # combined = MessageUtil.extend(df1, df2)
214
+ # # self.assertEqual(len(combined), 2)
215
+
216
+
217
+ # class TestToMarkdownString(unittest.TestCase):
218
+
219
+ # def test_to_markdown_string(self):
220
+ # """Test converting messages to a Markdown-formatted string."""
221
+ # messages = pd.DataFrame(
222
+ # {
223
+ # "node_id": ["1"], "role": ["user"],
224
+ # "content": [json.dumps({"instruction": "Hello, World!"})],
225
+ # }
226
+ # )
227
+ # markdown_str = MessageUtil.to_markdown_string(messages)
228
+ # self.assertIn("Hello, World!", markdown_str)
229
+
230
+
231
+ # # class TestSearchKeywords(unittest.TestCase):
232
+
233
+ # # def test_search_keywords(self):
234
+ # # """Test filtering DataFrame for rows containing specified keywords."""
235
+ # # messages = pd.DataFrame(
236
+ # # {"node_id": ["1", "2"], "content": ["Hello world", "Goodbye world"]}
237
+ # # )
238
+ # # filtered = MessageUtil.search_keywords(messages, "Hello")
239
+ # # print(filtered)
240
+ # # self.assertEqual(len(filtered), 1)
241
+
242
+
243
+ # # class TestReplaceKeyword(unittest.TestCase):
244
+
245
+ # # def test_replace_keyword(self):
246
+ # # """Test replacing a keyword in DataFrame's specified column."""
247
+ # # messages = pd.DataFrame({"content": ["Hello world", "Goodbye world"]})
248
+ # # MessageUtil.replace_keyword(messages, "world", "universe")
249
+ # # self.assertTrue(all(messages["content"].str.contains("universe")))
250
+
251
+
252
+ # # class TestReadCsv(unittest.TestCase):
253
+
254
+ # # @patch("pandas.read_csv")
255
+ # # def test_read_csv(self, mock_read_csv):
256
+ # # """Test reading a CSV file into a DataFrame."""
257
+ # # mock_df = pd.DataFrame(
258
+ # # {"node_id": ["1", "2"], "content": ["Hello, World!", "Goodbye, World!"]}
259
+ # # )
260
+ # # mock_read_csv.return_value = mock_df
261
+
262
+ # # df = MessageUtil.read_csv("path/to/nonexistent/file.csv")
263
+
264
+ # # mock_read_csv.assert_called_once_with("path/to/nonexistent/file.csv")
265
+
266
+ # # self.assertTrue(isinstance(df, pd.DataFrame))
267
+ # # self.assertEqual(len(df), 2)
268
+ # # self.assertEqual(list(df.columns), ["node_id", "content"])
270
269
 
271
270
 
272
- # class TestReadJson(unittest.TestCase):
271
+ # # class TestReadJson(unittest.TestCase):
273
272
 
274
- # @patch("pandas.read_json")
275
- # def test_read_json(self, mock_read_json):
276
- # """Test reading a JSON file into a DataFrame."""
277
- # mock_df = pd.DataFrame(
278
- # {"node_id": ["1", "2"], "content": ["JSON Message 1", "JSON Message 2"]}
279
- # )
280
- # mock_read_json.return_value = mock_df
281
-
282
- # df = MessageUtil.read_json("path/to/nonexistent/file.json")
273
+ # # @patch("pandas.read_json")
274
+ # # def test_read_json(self, mock_read_json):
275
+ # # """Test reading a JSON file into a DataFrame."""
276
+ # # mock_df = pd.DataFrame(
277
+ # # {"node_id": ["1", "2"], "content": ["JSON Message 1", "JSON Message 2"]}
278
+ # # )
279
+ # # mock_read_json.return_value = mock_df
280
+
281
+ # # df = MessageUtil.read_json("path/to/nonexistent/file.json")
283
282
 
284
- # mock_read_json.assert_called_once_with("path/to/nonexistent/file.json")
283
+ # # mock_read_json.assert_called_once_with("path/to/nonexistent/file.json")
285
284
 
286
- # self.assertTrue(isinstance(df, pd.DataFrame))
287
- # self.assertEqual(len(df), 2)
288
- # self.assertEqual(list(df.columns), ["node_id", "content"])
285
+ # # self.assertTrue(isinstance(df, pd.DataFrame))
286
+ # # self.assertEqual(len(df), 2)
287
+ # # self.assertEqual(list(df.columns), ["node_id", "content"])
289
288
 
290
289
 
291
- # class TestRemoveLastNRows(unittest.TestCase):
290
+ # # class TestRemoveLastNRows(unittest.TestCase):
292
291
 
293
- # def test_remove_last_n_rows(self):
294
- # """Test removing the last 'n' rows from a DataFrame."""
295
- # messages = pd.DataFrame({"content": ["message1", "message2", "message3"]})
296
- # updated = MessageUtil.remove_last_n_rows(messages, 2)
297
- # self.assertEqual(len(updated), 1)
292
+ # # def test_remove_last_n_rows(self):
293
+ # # """Test removing the last 'n' rows from a DataFrame."""
294
+ # # messages = pd.DataFrame({"content": ["message1", "message2", "message3"]})
295
+ # # updated = MessageUtil.remove_last_n_rows(messages, 2)
296
+ # # self.assertEqual(len(updated), 1)
298
297
 
299
298
 
300
- # class TestUpdateRow(unittest.TestCase):
299
+ # # class TestUpdateRow(unittest.TestCase):
301
300
 
302
- # def test_update_row(self):
303
- # """Test updating a row's value for a specified column."""
304
- # messages = pd.DataFrame(
305
- # {"node_id": ["1", "2"], "content": ["message1", "message2"]}
306
- # )
307
- # success = MessageUtil.update_row(messages, 0, "node_id", "3")
308
- # self.assertTrue(success)
309
- # self.assertTrue("3" in messages["node_id"].values)
301
+ # # def test_update_row(self):
302
+ # # """Test updating a row's value for a specified column."""
303
+ # # messages = pd.DataFrame(
304
+ # # {"node_id": ["1", "2"], "content": ["message1", "message2"]}
305
+ # # )
306
+ # # success = MessageUtil.update_row(messages, 0, "node_id", "3")
307
+ # # self.assertTrue(success)
308
+ # # self.assertTrue("3" in messages["node_id"].values)
310
309
 
311
310
 
312
- if __name__ == "__main__":
313
- unittest.main()
311
+ # if __name__ == "__main__":
312
+ # unittest.main()