Add quick start and utils for extracting code blocks (#404)

* Add quick start and utils for extracting code blocks

* format

* Spelling
This commit is contained in:
Eric Zhu 2024-08-26 17:23:27 -07:00 committed by GitHub
parent c779d4177b
commit c8084ada4e
5 changed files with 304 additions and 2 deletions

File diff suppressed because one or more lines are too long

View File

@ -9,8 +9,7 @@ You can implement agents in
different programming languages and deploy them on different machines across organizational boundaries.
You can also implement agents using other agent frameworks and run them in AGNext.
To get you started quickly, we offers
`a suite of samples <https://github.com/microsoft/agnext/tree/main/python/samples>`_.
To start quickly, read the `quick start <getting-started/quickstart.html>`_.
To learn about the core concepts of AGNext, read the `overview <core-concepts/overview.html>`_.
@ -25,6 +24,7 @@ To learn about the core concepts of AGNext, read the `overview <core-concepts/ov
:hidden:
getting-started/installation
getting-started/quickstart
getting-started/agent-and-agent-runtime
getting-started/message-and-communication
getting-started/model-clients

View File

@ -3,6 +3,7 @@ from ._func_with_reqs import Alias, FunctionWithRequirements, Import, ImportFrom
from ._impl.azure_container_code_executor import AzureContainerCodeExecutor
from ._impl.command_line_code_result import CommandLineCodeResult
from ._impl.local_commandline_code_executor import LocalCommandLineCodeExecutor
from ._utils import extract_markdown_code_blocks
__all__ = [
"AzureContainerCodeExecutor",
@ -16,4 +17,5 @@ __all__ = [
"Import",
"FunctionWithRequirements",
"with_requirements",
"extract_markdown_code_blocks",
]

View File

@ -0,0 +1,15 @@
import re
from typing import List
from . import CodeBlock
def extract_markdown_code_blocks(markdown_text: str) -> List[CodeBlock]:
pattern = re.compile(r"```(?:\s*([\w\+\-]+))?\n([\s\S]*?)```")
matches = pattern.findall(markdown_text)
code_blocks: List[CodeBlock] = []
for match in matches:
language = match[0].strip() if match[0] else ""
code_content = match[1]
code_blocks.append(CodeBlock(code=code_content, language=language))
return code_blocks

View File

@ -0,0 +1,37 @@
from agnext.components.code_executor import extract_markdown_code_blocks
def test_extract_markdown_code_blocks() -> None:
text = """# This is a markdown text
```python
print("Hello World")
```
"""
code_blocks = extract_markdown_code_blocks(text)
assert len(code_blocks) == 1
assert code_blocks[0].language == "python"
assert code_blocks[0].code == 'print("Hello World")\n'
text = """More markdown text
```python
print("Hello World")
```
Another code block.
```python
print("Hello World 2")
```
"""
code_blocks = extract_markdown_code_blocks(text)
assert len(code_blocks) == 2
assert code_blocks[0].language == "python"
assert code_blocks[0].code == 'print("Hello World")\n'
assert code_blocks[1].language == "python"
assert code_blocks[1].code == 'print("Hello World 2")\n'