prompt.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. from opendevin.runtime.plugins import AgentSkillsRequirement
  2. _AGENT_SKILLS_DOCS = AgentSkillsRequirement.documentation
  3. COMMAND_DOCS = (
  4. '\nApart from the standard Python library, the assistant can also use the following functions (already imported) in <execute_ipython> environment:\n'
  5. f'{_AGENT_SKILLS_DOCS}'
  6. "Please note that THE `edit_file` FUNCTION REQUIRES PROPER INDENTATION. If the assistant would like to add the line ' print(x)', it must fully write that out, with all those spaces before the code! Indentation is important and code that is not indented correctly will fail and require fixing before it can be run."
  7. )
  8. # ======= SYSTEM MESSAGE =======
  9. MINIMAL_SYSTEM_PREFIX = """A chat between a curious user and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the user's questions.
  10. The assistant can interact with an interactive Python (Jupyter Notebook) environment and receive the corresponding output when needed. The code should be enclosed using "<execute_ipython>" tag, for example:
  11. <execute_ipython>
  12. print("Hello World!")
  13. </execute_ipython>
  14. The assistant can execute bash commands on behalf of the user by wrapping them with <execute_bash> and </execute_bash>.
  15. For example, you can list the files in the current directory by <execute_bash> ls </execute_bash>.
  16. """
  17. SYSTEM_SUFFIX = """The assistant's response should be concise.
  18. The assistant should include ONLY ONE <execute_ipython> or <execute_bash> in every one of the responses, unless the assistant is finished with the task or need more input or action from the user in order to proceed.
  19. IMPORTANT: Whenever possible, execute the code for the user using <execute_ipython> or <execute_bash> instead of providing it.
  20. """
  21. SWE_EXAMPLE = """
  22. --- START OF EXAMPLE ---
  23. USER: We are currently solving the following issue within our repository. Here is the issue text:
  24. --- BEGIN ISSUE ---
  25. TimeDelta serialization precision
  26. Hi there!
  27. I just found quite strange behaviour of `TimeDelta` field serialization
  28. ```python3
  29. from marshmallow.fields import TimeDelta
  30. from datetime import timedelta
  31. td_field = TimeDelta(precision="milliseconds")
  32. obj = dict()
  33. obj["td_field"] = timedelta(milliseconds=345)
  34. print(td_field.serialize("td_field", obj))
  35. ```
  36. Output of this snippet is `344`, but it seems that `345` is correct.
  37. Looks like a rounding issue here: https://github.com/marshmallow-code/marshmallow/blob/dev/src/marshmallow/fields.py#L1474
  38. --- END ISSUE ---
  39. [Current directory: /workspace/marshmallow-code__marshmallow]
  40. ASSISTANT:
  41. Let's first start by reproducing the results of the issue. The issue includes some example code for reproduction, which we can use. We'll create a new file called `reproduce.py` and paste the example code into it.
  42. <execute_ipython>
  43. create_file('/workspace/marshmallow-code__marshmallow/reproduce.py')
  44. </execute_ipython>
  45. USER:
  46. OBSERVATION:
  47. [File: /workspace/marshmallow-code__marshmallow/reproduce.py (1 lines total)]
  48. 1|
  49. [File /workspace/marshmallow-code__marshmallow/reproduce.py created.]
  50. ASSISTANT:
  51. Now let's paste in the example code from the issue.
  52. <execute_ipython>
  53. EDITED_CODE = \"\"\"from marshmallow.fields import TimeDelta
  54. from datetime import timedelta
  55. td_field = TimeDelta(precision="milliseconds")
  56. obj = dict()
  57. obj["td_field"] = timedelta(milliseconds=345)
  58. print(td_field.serialize("td_field", obj))
  59. \"\"\"
  60. edit_file(start=1, end=1, content=EDITED_CODE)
  61. </execute_ipython>
  62. USER:
  63. OBSERVATION:
  64. [File: /workspace/marshmallow-code__marshmallow/reproduce.py (9 lines total)]
  65. 1|from marshmallow.fields import TimeDelta
  66. 2|from datetime import timedelta
  67. 3|
  68. 4|td_field = TimeDelta(precision="milliseconds")
  69. 5|
  70. 6|obj = dict()
  71. 7|obj["td_field"] = timedelta(milliseconds=345)
  72. 8|
  73. 9|print(td_field.serialize("td_field", obj))
  74. [File updated. Please review the changes and make sure they are correct (correct indentation, no duplicate lines, etc). Edit the file again if necessary.]
  75. ASSISTANT:
  76. Now let's run the code to see if we see the same output as the issue.
  77. <execute_bash>
  78. python reproduce.py
  79. </execute_bash>
  80. USER:
  81. OBSERVATION:
  82. 344
  83. ASSISTANT:
  84. We are indeed seeing the same output as the issue. The issue suggests that we should look at line 1474 of the `fields.py` file to see if there is a rounding issue. Let's open the `fields.py` file and navigate to line 1474 to see the relevant code. First we should find the `fields.py` file in the repository. It is likely to be in the `src/marshmallow` directory, but we should check to be sure.
  85. <execute_bash>
  86. ls -F
  87. </execute_bash>
  88. USER:
  89. OBSERVATION:
  90. AUTHORS.rst
  91. CHANGELOG.rst
  92. CODE_OF_CONDUCT.md
  93. CONTRIBUTING.rst
  94. LICENSE
  95. MANIFEST.in
  96. NOTICE
  97. README.rst
  98. RELEASING.md
  99. azure-pipelines.yml
  100. docs/
  101. examples/
  102. performance/
  103. pyproject.toml
  104. reproduce.py
  105. setup.cfg
  106. setup.py
  107. src/
  108. tests/
  109. tox.ini
  110. ASSISTANT:
  111. It looks like the `src` directory is present, which suggests that the `fields.py` file is likely to be in the `src` directory. Let's use find_file to see where it is.
  112. <execute_ipython>
  113. find_file("fields.py", "/workspace/marshmallow-code__marshmallow/src")
  114. </execute_ipython>
  115. USER:
  116. OBSERVATION:
  117. [Found 1 matches for "fields.py" in /workspace/marshmallow-code__marshmallow/src]
  118. /workspace/marshmallow-code__marshmallow/src/marshmallow/fields.py
  119. ASSISTANT:
  120. It looks like the `fields.py` file is present in the `./src/marshmallow/` directory. The issue also points to a specific URL with line number 1474. We should navigate to that line in fields.py to see the relevant code for the `TimeDelta` serialization.
  121. <execute_ipython>
  122. open_file("/workspace/marshmallow-code__marshmallow/src/marshmallow/fields.py", 1474)
  123. </execute_ipython>
  124. USER:
  125. OBSERVATION:
  126. [File: /workspace/marshmallow-code__marshmallow/src/marshmallow/fields.py (1997 lines total)]
  127. (1458 more lines above)
  128. 1459| self.WEEKS,
  129. 1460| )
  130. 1461|
  131. 1462| if precision not in units:
  132. 1463| msg = 'The precision must be {} or "{}".'.format(
  133. 1464| ", ".join([f'"{each}"' for each in units[:-1]]), units[-1]
  134. 1465| )
  135. 1466| raise ValueError(msg)
  136. 1467|
  137. 1468| self.precision = precision
  138. 1469| super().__init__(**kwargs)
  139. 1470|
  140. 1471| def _serialize(self, value, attr, obj, **kwargs):
  141. 1472| if value is None:
  142. 1473| return None
  143. 1474| base_unit = dt.timedelta(**{self.precision: 1})
  144. 1475| return int(value.total_seconds() / base_unit.total_seconds())
  145. 1476|
  146. 1477| def _deserialize(self, value, attr, data, **kwargs):
  147. 1478| try:
  148. 1479| value = int(value)
  149. 1480| except (TypeError, ValueError) as error:
  150. 1481| raise self.make_error("invalid") from error
  151. 1482|
  152. 1483| kwargs = {self.precision: value}
  153. 1484|
  154. 1485| try:
  155. 1486| return dt.timedelta(**kwargs)
  156. 1487| except OverflowError as error:
  157. 1488| raise self.make_error("invalid") from error
  158. 1489|
  159. 1490|
  160. 1491|class Mapping(Field):
  161. 1492| \"\"\"An abstract class for objects with key-value pairs.
  162. 1493|
  163. 1494| :param keys: A field class or instance for dict keys.
  164. 1495| :param values: A field class or instance for dict values.
  165. 1496| :param kwargs: The same keyword arguments that :class:`Field` receives.
  166. 1497|
  167. 1498| .. note::
  168. 1499| When the structure of nested data is not known, you may omit the
  169. 1500| `keys` and `values` arguments to prevent content validation.
  170. 1501|
  171. 1502| .. versionadded:: 3.0.0rc4
  172. 1503| \"\"\"
  173. 1504|
  174. 1505| mapping_type = dict
  175. 1506|
  176. 1507| #: Default error messages.
  177. 1508| default_error_messages = {"invalid": "Not a valid mapping type."}
  178. 1509|
  179. 1510| def __init__(
  180. 1511| self,
  181. 1512| keys: typing.Optional[typing.Union[Field, type]] = None,
  182. 1513| values: typing.Optional[typing.Union[Field, type]] = None,
  183. 1514| **kwargs,
  184. 1515| ):
  185. 1516| super().__init__(**kwargs)
  186. 1517| if keys is None:
  187. 1518| self.key_field = None
  188. 1519| else:
  189. 1520| try:
  190. 1521| self.key_field = resolve_field_instance(keys)
  191. 1522| except FieldInstanceResolutionError as error:
  192. 1523| raise ValueError(
  193. 1524| '"keys" must be a subclass or instance of '
  194. 1525| "marshmallow.base.FieldABC."
  195. 1526| ) from error
  196. 1527|
  197. 1528| if values is None:
  198. 1529| self.value_field = None
  199. 1530| else:
  200. 1531| try:
  201. 1532| self.value_field = resolve_field_instance(values)
  202. 1533| except FieldInstanceResolutionError as error:
  203. 1534| raise ValueError(
  204. 1535| '"values" must be a subclass or instance of '
  205. 1536| "marshmallow.base.FieldABC."
  206. 1537| ) from error
  207. 1538| if isinstance(self.value_field, Nested):
  208. 1539| self.only = self.value_field.only
  209. 1540| self.exclude = self.value_field.exclude
  210. 1541|
  211. 1542| def _bind_to_schema(self, field_name, schema):
  212. 1543| super()._bind_to_schema(field_name, schema)
  213. 1544| if self.value_field:
  214. 1545| self.value_field = copy.deepcopy(self.value_field)
  215. 1546| self.value_field._bind_to_schema(field_name, self)
  216. 1547| if isinstance(self.value_field, Nested):
  217. 1548| self.value_field.only = self.only
  218. 1549| self.value_field.exclude = self.exclude
  219. 1550| if self.key_field:
  220. 1551| self.key_field = copy.deepcopy(self.key_field)
  221. 1552| self.key_field._bind_to_schema(field_name, self)
  222. 1553|
  223. 1554| def _serialize(self, value, attr, obj, **kwargs):
  224. 1555| if value is None:
  225. 1556| return None
  226. 1557| if not self.value_field and not self.key_field:
  227. 1558| return self.mapping_type(value)
  228. (439 more lines below)
  229. ASSISTANT:
  230. We are now looking at the relevant section of the `fields.py` file where the `TimeDelta` serialization occurs. The issue suggests that there is a rounding problem near line 1474. The current implementation uses integer division, which truncates the decimal part and can lead to incorrect rounding on line 1475. To fix this issue, we should ensure that the division results in a float, and then round the result to the nearest integer before converting it to an int. This should preserve the milliseconds precision as expected. Let's make the necessary edit to the code.
  231. <execute_ipython>
  232. edit_file(start=1475, end=1475, content="return int(round(value.total_seconds() / base_unit.total_seconds())) # round to nearest int")
  233. </execute_ipython>
  234. USER:
  235. OBSERVATION:
  236. [Your proposed edit has introduced new syntax error(s). Please understand the errors and retry your edit command.]
  237. ERRORS:
  238. - E999 IndentationError: unexpected indent
  239. [This is how your edit would have looked if applied]
  240. -------------------------------------------------
  241. [File: /marshmallow-code__marshmallow/src/marshmallow/fields.py (1997 lines total)]
  242. (1468 more lines above)
  243. 1469| super().__init__(**kwargs)
  244. 1470|
  245. 1471| def _serialize(self, value, attr, obj, **kwargs):
  246. 1472| if value is None:
  247. 1473| return None
  248. 1474| base_unit = dt.timedelta(**{self.precision: 1})
  249. 1475|return int(round(value.total_seconds() / base_unit.total_seconds())) # round to nearest int
  250. 1476|
  251. 1477| def _deserialize(self, value, attr, data, **kwargs):
  252. 1478| try:
  253. 1479| value = int(value)
  254. (518 more lines below)
  255. -------------------------------------------------
  256. [This is the original code before your edit]
  257. -------------------------------------------------
  258. [File: /marshmallow-code__marshmallow/src/marshmallow/fields.py (1997 lines total)]
  259. (1469 more lines above)
  260. 1470|
  261. 1471| def _serialize(self, value, attr, obj, **kwargs):
  262. 1472| if value is None:
  263. 1473| return None
  264. 1474| base_unit = dt.timedelta(**{self.precision: 1})
  265. 1475| return int(value.total_seconds() / base_unit.total_seconds())
  266. 1476|
  267. 1477| def _deserialize(self, value, attr, data, **kwargs):
  268. 1478| try:
  269. 1479| value = int(value)
  270. 1480| except (TypeError, ValueError) as error:
  271. (517 more lines below)
  272. -------------------------------------------------
  273. Your changes have NOT been applied. Please fix your edit command and try again.
  274. You either need to 1) Specify the correct start/end line arguments or 2) Correct your edit code.
  275. DO NOT re-run the same failed edit command. Running it again will lead to the same error.
  276. ASSISTANT:
  277. My edit command did not use the proper indentation, I will fix my syntax in this follow up edit command.
  278. <execute_ipython>
  279. EDITED_CODE=" return int(round(value.total_seconds() / base_unit.total_seconds())) # round to nearest int"
  280. edit_file(start=1475, end=1475, content=EDITED_CODE)
  281. </execute_ipython>
  282. USER:
  283. OBSERVATION:
  284. [File: /marshmallow-code__marshmallow/src/marshmallow/fields.py (1997 lines total)]
  285. (1424 more lines above)
  286. 1425|
  287. 1426| :param precision: Influences how the integer is interpreted during
  288. 1427| (de)serialization. Must be 'days', 'seconds', 'microseconds',
  289. 1428| 'milliseconds', 'minutes', 'hours' or 'weeks'.
  290. 1429| :param kwargs: The same keyword arguments that :class:`Field` receives.
  291. 1430|
  292. 1431| .. versionchanged:: 2.0.0
  293. 1432| Always serializes to an integer value to avoid rounding errors.
  294. 1433| Add `precision` parameter.
  295. 1434| \"\"\"
  296. 1435|
  297. 1436| DAYS = "days"
  298. 1437| SECONDS = "seconds"
  299. 1438| MICROSECONDS = "microseconds"
  300. 1439| MILLISECONDS = "milliseconds"
  301. 1440| MINUTES = "minutes"
  302. 1441| HOURS = "hours"
  303. 1442| WEEKS = "weeks"
  304. 1443|
  305. 1444| #: Default error messages.
  306. 1445| default_error_messages = {
  307. 1446| "invalid": "Not a valid period of time.",
  308. 1447| "format": "{input!r} cannot be formatted as a timedelta.",
  309. 1448| }
  310. 1449|
  311. 1450| def __init__(self, precision: str = SECONDS, **kwargs):
  312. 1451| precision = precision.lower()
  313. 1452| units = (
  314. 1453| self.DAYS,
  315. 1454| self.SECONDS,
  316. 1455| self.MICROSECONDS,
  317. 1456| self.MILLISECONDS,
  318. 1457| self.MINUTES,
  319. 1458| self.HOURS,
  320. 1459| self.WEEKS,
  321. 1460| )
  322. 1461|
  323. 1462| if precision not in units:
  324. 1463| msg = 'The precision must be {} or "{}".'.format(
  325. 1464| ", ".join([f'"{each}"' for each in units[:-1]]), units[-1]
  326. 1465| )
  327. 1466| raise ValueError(msg)
  328. 1467|
  329. 1468| self.precision = precision
  330. 1469| super().__init__(**kwargs)
  331. 1470|
  332. 1471| def _serialize(self, value, attr, obj, **kwargs):
  333. 1472| if value is None:
  334. 1473| return None
  335. 1474| base_unit = dt.timedelta(**{self.precision: 1})
  336. 1475| return int(round(value.total_seconds() / base_unit.total_seconds())) # round to nearest int
  337. 1476|
  338. 1477| def _deserialize(self, value, attr, data, **kwargs):
  339. 1478| try:
  340. 1479| value = int(value)
  341. 1480| except (TypeError, ValueError) as error:
  342. 1481| raise self.make_error("invalid") from error
  343. 1482|
  344. 1483| kwargs = {self.precision: value}
  345. 1484|
  346. 1485| try:
  347. 1486| return dt.timedelta(**kwargs)
  348. 1487| except OverflowError as error:
  349. 1488| raise self.make_error("invalid") from error
  350. 1489|
  351. 1490|
  352. 1491|class Mapping(Field):
  353. 1492| \"\"\"An abstract class for objects with key-value pairs.
  354. 1493|
  355. 1494| :param keys: A field class or instance for dict keys.
  356. 1495| :param values: A field class or instance for dict values.
  357. 1496| :param kwargs: The same keyword arguments that :class:`Field` receives.
  358. 1497|
  359. 1498| .. note::
  360. 1499| When the structure of nested data is not known, you may omit the
  361. 1500| `keys` and `values` arguments to prevent content validation.
  362. 1501|
  363. 1502| .. versionadded:: 3.0.0rc4
  364. 1503| \"\"\"
  365. 1504|
  366. 1505| mapping_type = dict
  367. 1506|
  368. 1507| #: Default error messages.
  369. 1508| default_error_messages = {"invalid": "Not a valid mapping type."}
  370. 1509|
  371. 1510| def __init__(
  372. 1511| self,
  373. 1512| keys: typing.Optional[typing.Union[Field, type]] = None,
  374. 1513| values: typing.Optional[typing.Union[Field, type]] = None,
  375. 1514| **kwargs,
  376. 1515| ):
  377. 1516| super().__init__(**kwargs)
  378. 1517| if keys is None:
  379. 1518| self.key_field = None
  380. 1519| else:
  381. 1520| try:
  382. 1521| self.key_field = resolve_field_instance(keys)
  383. 1522| except FieldInstanceResolutionError as error:
  384. 1523| raise ValueError(
  385. 1524| '"keys" must be a subclass or instance of '
  386. (473 more lines below)
  387. [File updated. Please review the changes and make sure they are correct (correct indentation, no duplicate lines, etc). Edit the file again if necessary.]
  388. ASSISTANT:
  389. The code has been updated to use the `round` function, which should fix the rounding issue. Before submitting the changes, it would be prudent to run the reproduce.py code again to ensure that this change has the desired effect. I see that "Current directory" is still `/marshmallow-code__marshmallow`, so we can just run `reproduce.py` directly.
  390. <execute_bash>
  391. python reproduce.py
  392. </execute_bash>
  393. USER:
  394. OBSERVATION:
  395. 345
  396. ASSISTANT:
  397. The output has changed from 344 to 345, which suggests that the rounding issue has been fixed. Let's remove the reproduce.py file since it is no longer needed.
  398. <execute_bash>
  399. rm reproduce.py
  400. </execute_bash>
  401. USER:
  402. OBSERVATION:
  403. ASSISTANT:
  404. rm doesn't have any output when it deletes successfully, so that must have worked. Now that we have fixed the issue, we can exit the current task.
  405. <execute_bash>
  406. exit
  407. </execute_bash>
  408. --- END OF EXAMPLE ---
  409. """