|
3 | 3 | import pytest |
4 | 4 | from pydantic import BaseModel |
5 | 5 |
|
6 | | -from ragas.prompt.utils import get_all_strings, update_strings |
| 6 | +from ragas.prompt.utils import extract_json, get_all_strings, update_strings |
7 | 7 |
|
8 | 8 |
|
9 | 9 | class Category(BaseModel): |
@@ -122,3 +122,93 @@ def test_update_strings(obj, old_strings, new_strings): |
122 | 122 |
|
123 | 123 | assert get_all_strings(updated_obj) == new_strings |
124 | 124 | assert get_all_strings(obj) == old_strings |
| 125 | + |
| 126 | + |
| 127 | +class TestExtractJson: |
| 128 | + prefix = "Here's the generated abstract conceptual question in the requested JSON format: " |
| 129 | + suffix = "Would you like me to explain in more detail?" |
| 130 | + object = """{"key": "value"}""" |
| 131 | + array = """[1, 2, 3]""" |
| 132 | + nested = """{"outer": {"inner": [1, 2, 3]}}""" |
| 133 | + |
| 134 | + test_cases = [ |
| 135 | + (object, object), |
| 136 | + (array, array), |
| 137 | + (nested, nested), |
| 138 | + (prefix + object, object), |
| 139 | + (object + suffix, object), |
| 140 | + (prefix + object + suffix, object), |
| 141 | + (prefix + array, array), |
| 142 | + (array + suffix, array), |
| 143 | + (prefix + array + suffix, array), |
| 144 | + (prefix + nested, nested), |
| 145 | + (nested + suffix, nested), |
| 146 | + (prefix + nested + suffix, nested), |
| 147 | + (object + array + nested, object), |
| 148 | + (nested + object + array, nested), |
| 149 | + ] |
| 150 | + |
| 151 | + @pytest.mark.parametrize("text, expected", test_cases) |
| 152 | + def test_extract_json(self, text, expected): |
| 153 | + assert extract_json(text) == expected |
| 154 | + |
| 155 | + def test_extract_empty_array(self): |
| 156 | + text = "Here is an empty array: [] and some text." |
| 157 | + expected = "[]" |
| 158 | + assert extract_json(text) == expected |
| 159 | + |
| 160 | + def test_extract_empty_object(self): |
| 161 | + text = "Here is an empty object: {} and more text." |
| 162 | + expected = "{}" |
| 163 | + assert extract_json(text) == expected |
| 164 | + |
| 165 | + def test_extract_incomplete_json(self): |
| 166 | + text = 'Not complete: {"key": "value", "array": [1, 2, 3' |
| 167 | + expected = 'Not complete: {"key": "value", "array": [1, 2, 3' |
| 168 | + assert extract_json(text) == expected |
| 169 | + |
| 170 | + def test_markdown_json(self): |
| 171 | + text = """ |
| 172 | + ```python |
| 173 | + import json |
| 174 | +
|
| 175 | + def modify_query(input_data): |
| 176 | + query = input_data["query"] |
| 177 | + style = input_data["style"] |
| 178 | + length = input_data["length"] |
| 179 | +
|
| 180 | + if style == "Poor grammar": |
| 181 | + # Poor grammar modifications (simplified for brevity) |
| 182 | + query = query.replace("How", "how") |
| 183 | + query = query.replace("do", "does") |
| 184 | + query = query.replace("terms of", "in terms of") |
| 185 | + query = query.replace("and", "") |
| 186 | +
|
| 187 | + if length == "long": |
| 188 | + # Long text modifications (simplified for brevity) |
| 189 | + query += "?" |
| 190 | +
|
| 191 | + return { |
| 192 | + "text": query |
| 193 | + } |
| 194 | +
|
| 195 | + input_data = { |
| 196 | + "query": "How can the provided commands be used to manage and troubleshoot namespaces in a Kubernetes environment?", |
| 197 | + "style": "Poor grammar", |
| 198 | + "length": "long" |
| 199 | + } |
| 200 | +
|
| 201 | + output = modify_query(input_data) |
| 202 | + print(json.dumps(output, indent=4)) |
| 203 | + ``` |
| 204 | +
|
| 205 | + Output: |
| 206 | + ```json |
| 207 | + {"text": "how does the provided commands be used to manage and troubleshoot namespaces in a Kubernetes environment?"} |
| 208 | + ``` |
| 209 | + This Python function `modify_query` takes an input dictionary with query, style, and length as keys. It applies modifications based on the specified style (Poor grammar) and length (long). The modified query is then returned as a JSON object. |
| 210 | +
|
| 211 | + Note: This implementation is simplified for brevity and may not cover all possible edge cases or nuances of natural language processing. |
| 212 | + """ |
| 213 | + expected = """{"text": "how does the provided commands be used to manage and troubleshoot namespaces in a Kubernetes environment?"}""" |
| 214 | + assert extract_json(text) == expected |
0 commit comments