You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_markdown_code_extractor.py 2.7 kB

Code executors (#1405) * code executor * test * revert to main conversable agent * prepare for pr * kernel * run open ai tests only when it's out of draft status * update workflow file * revert workflow changes * ipython executor * check kernel installed; fix tests * fix tests * fix tests * update system prompt * Update notebook, more tests * notebook * raise instead of return None * allow user provided code executor. * fixing types * wip * refactoring * polishing * fixed failing tests * resolved merge conflict * fixing failing test * wip * local command line executor and embedded ipython executor * revert notebook * fix format * fix merged error * fix lmm test * fix lmm test * move warning * name and description should be part of the agent protocol, reset is not as it is only used for ConversableAgent; removing accidentally commited file * version for dependency * Update autogen/agentchat/conversable_agent.py Co-authored-by: Jack Gerrits <jackgerrits@users.noreply.github.com> * ordering of protocol * description * fix tests * make ipython executor dependency optional * update document optional dependencies * Remove exclude from Agent protocol * Make ConversableAgent consistent with Agent * fix tests * add doc string * add doc string * fix notebook * fix interface * merge and update agents * disable config usage in reply function * description field setter * customize system message update * update doc --------- Co-authored-by: Davor Runje <davor@airt.ai> Co-authored-by: Jack Gerrits <jackgerrits@users.noreply.github.com> Co-authored-by: Aaron <aaronlaptop12@hotmail.com> Co-authored-by: Chi Wang <wang.chi@microsoft.com>
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. from autogen.coding import MarkdownCodeExtractor
  2. _message_1 = """
  3. Example:
  4. ```
  5. print("hello extract code")
  6. ```
  7. """
  8. _message_2 = """Example:
  9. ```python
  10. def scrape(url):
  11. import requests
  12. from bs4 import BeautifulSoup
  13. response = requests.get(url)
  14. soup = BeautifulSoup(response.text, "html.parser")
  15. title = soup.find("title").text
  16. text = soup.find("div", {"id": "bodyContent"}).text
  17. return title, text
  18. ```
  19. Test:
  20. ```python
  21. url = "https://en.wikipedia.org/wiki/Web_scraping"
  22. title, text = scrape(url)
  23. print(f"Title: {title}")
  24. print(f"Text: {text}")
  25. ```
  26. """
  27. _message_3 = """
  28. Example:
  29. ```python
  30. def scrape(url):
  31. import requests
  32. from bs4 import BeautifulSoup
  33. response = requests.get(url)
  34. soup = BeautifulSoup(response.text, "html.parser")
  35. title = soup.find("title").text
  36. text = soup.find("div", {"id": "bodyContent"}).text
  37. return title, text
  38. ```
  39. """
  40. _message_4 = """
  41. Example:
  42. ``` python
  43. def scrape(url):
  44. import requests
  45. from bs4 import BeautifulSoup
  46. response = requests.get(url)
  47. soup = BeautifulSoup(response.text, "html.parser")
  48. title = soup.find("title").text
  49. text = soup.find("div", {"id": "bodyContent"}).text
  50. return title, text
  51. ```
  52. """.replace(
  53. "\n", "\r\n"
  54. )
  55. _message_5 = """
  56. Test bash script:
  57. ```bash
  58. echo 'hello world!'
  59. ```
  60. """
  61. _message_6 = """
  62. Test some C# code, expecting ""
  63. ```
  64. using System;
  65. using System.Collections.Generic;
  66. using System.Linq;
  67. using System.Text;
  68. namespace ConsoleApplication1
  69. {
  70. class Program
  71. {
  72. static void Main(string[] args)
  73. {
  74. Console.WriteLine("Hello World");
  75. }
  76. }
  77. }
  78. ```
  79. """
  80. _message_7 = """
  81. Test some message that has no code block.
  82. """
  83. def test_extract_code() -> None:
  84. extractor = MarkdownCodeExtractor()
  85. code_blocks = extractor.extract_code_blocks(_message_1)
  86. assert len(code_blocks) == 1 and code_blocks[0].language == "python"
  87. code_blocks = extractor.extract_code_blocks(_message_2)
  88. assert len(code_blocks) == 2 and code_blocks[0].language == "python" and code_blocks[1].language == "python"
  89. code_blocks = extractor.extract_code_blocks(_message_3)
  90. assert len(code_blocks) == 1 and code_blocks[0].language == "python"
  91. code_blocks = extractor.extract_code_blocks(_message_4)
  92. assert len(code_blocks) == 1 and code_blocks[0].language == "python"
  93. code_blocks = extractor.extract_code_blocks(_message_5)
  94. assert len(code_blocks) == 1 and code_blocks[0].language == "bash"
  95. code_blocks = extractor.extract_code_blocks(_message_6)
  96. assert len(code_blocks) == 1 and code_blocks[0].language == ""
  97. code_blocks = extractor.extract_code_blocks(_message_7)
  98. assert len(code_blocks) == 0