Browse Source

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

* Add quick start and utils for extracting code blocks

* format

* Spelling
tags/v0.4.0.dev0
Eric Zhu GitHub 1 year ago
parent
commit
c8084ada4e
5 changed files with 304 additions and 2 deletions
  1. +248
    -0
      python/docs/src/getting-started/quickstart.ipynb
  2. +2
    -2
      python/docs/src/index.rst
  3. +2
    -0
      python/src/agnext/components/code_executor/__init__.py
  4. +15
    -0
      python/src/agnext/components/code_executor/_utils.py
  5. +37
    -0
      python/tests/execution/test_extract_code_blocks.py

+ 248
- 0
python/docs/src/getting-started/quickstart.ipynb
File diff suppressed because it is too large
View File


+ 2
- 2
python/docs/src/index.rst 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


+ 2
- 0
python/src/agnext/components/code_executor/__init__.py 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",
]

+ 15
- 0
python/src/agnext/components/code_executor/_utils.py 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

+ 37
- 0
python/tests/execution/test_extract_code_blocks.py 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'

Loading…
Cancel
Save