autogen/test/coding/test_markdown_code_extractor.py
Eric Zhu 609ba7c649
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>
2024-02-10 04:52:16 +00:00

116 lines
2.7 KiB
Python

from autogen.coding import MarkdownCodeExtractor
_message_1 = """
Example:
```
print("hello extract code")
```
"""
_message_2 = """Example:
```python
def scrape(url):
import requests
from bs4 import BeautifulSoup
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
title = soup.find("title").text
text = soup.find("div", {"id": "bodyContent"}).text
return title, text
```
Test:
```python
url = "https://en.wikipedia.org/wiki/Web_scraping"
title, text = scrape(url)
print(f"Title: {title}")
print(f"Text: {text}")
```
"""
_message_3 = """
Example:
```python
def scrape(url):
import requests
from bs4 import BeautifulSoup
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
title = soup.find("title").text
text = soup.find("div", {"id": "bodyContent"}).text
return title, text
```
"""
_message_4 = """
Example:
``` python
def scrape(url):
import requests
from bs4 import BeautifulSoup
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
title = soup.find("title").text
text = soup.find("div", {"id": "bodyContent"}).text
return title, text
```
""".replace(
"\n", "\r\n"
)
_message_5 = """
Test bash script:
```bash
echo 'hello world!'
```
"""
_message_6 = """
Test some C# code, expecting ""
```
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World");
}
}
}
```
"""
_message_7 = """
Test some message that has no code block.
"""
def test_extract_code() -> None:
extractor = MarkdownCodeExtractor()
code_blocks = extractor.extract_code_blocks(_message_1)
assert len(code_blocks) == 1 and code_blocks[0].language == "python"
code_blocks = extractor.extract_code_blocks(_message_2)
assert len(code_blocks) == 2 and code_blocks[0].language == "python" and code_blocks[1].language == "python"
code_blocks = extractor.extract_code_blocks(_message_3)
assert len(code_blocks) == 1 and code_blocks[0].language == "python"
code_blocks = extractor.extract_code_blocks(_message_4)
assert len(code_blocks) == 1 and code_blocks[0].language == "python"
code_blocks = extractor.extract_code_blocks(_message_5)
assert len(code_blocks) == 1 and code_blocks[0].language == "bash"
code_blocks = extractor.extract_code_blocks(_message_6)
assert len(code_blocks) == 1 and code_blocks[0].language == ""
code_blocks = extractor.extract_code_blocks(_message_7)
assert len(code_blocks) == 0