parse_llm_code
This library serves the purpose of parsing code snippets from text, particularly helpful when dealing with outputs generated by large language models. Typically, such outputs include both code snippets and explanatory text. By using this library, one can effectively filter and extract the meaningful code portions, aiding in the extraction of useful and actionable code from generated responses.
USECASE
Extract first code block from answer
from parse_llm_code import extract_first_code
test_string = """
this is llm answer example:
\```python
print('first line')
print('second line')
\```
```typescript
console.log('first line')
\```
"""
# Example of extracting the first code block
first_code = extract_first_code(test_string)
print(first_code.language)
print(first_code.context)
print(first_code.length)
print(first_code.lines)
print(first_code.to_dict())
Output
python
print('first line')
print('second line')
40
2
{'language': 'python', 'context': "print('first line')\nprint('second line')", 'length': 40, 'lines': 2, 'include_try': False, 'include_return': False}
Extract multi code blocks
from parse_llm_code import extract_code_blocks
test_string = """
this is llm answer example:
\```python
print('first line')
print('second line')
\```
```typescript
console.log('first line')
\```
"""
# Example of extracting all code blocks
result = extract_code_blocks(test_string)
print(result.length)
print(result.code_list)
print(result.code_dict_list)
Output
2
[<parse_llm_code.extract_code.CodeBlock object at 0x1049edac0>, <parse_llm_code.extract_code.CodeBlock object at 0x1049edfd0>]
[
{
"language": "python",
"context": "print('first line')\nprint('second line')",
"length": 40,
"lines": 2,
"include_try": False,
"include_return": False,
},
{
"language": "typescript",
"context": "console.log('first line')",
"length": 25,
"lines": 1,
"include_try": False,
"include_return": False,
},
]
Run test
python3 -m unittest discover -s tests
Contributors
Metadata
Release files for parse-llm-code 0.1.31
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| parse_llm_code-0.1.31.tar.gz | 4.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| parse_llm_code-0.1.31-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 9.5 kB
Release files / parse_llm_code-0.1.31.tar.gz
| Download URL | parse_llm_code-0.1.31.tar.gz |
|---|---|
| Size | 4.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d2d9ce14f0e35a4dab669c4e32b15ab5c75ff8dfb0971195e0cf09b87eb5ddfc
|
|
BLAKE2b-256 checksum How to use checksums |
7b9bc314add8cfd3e28c16b5e4dfbed17528f40918c0697caee62d15ebd4e729
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.12.3
|
Release files / parse_llm_code-0.1.31-py3-none-any.whl
| Download URL | parse_llm_code-0.1.31-py3-none-any.whl |
|---|---|
| Size | 5.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
fa5b9b4138e1a450bc3a51e13e592b9dddf577cf004b1f71f764e25a76f4ff33
|
|
BLAKE2b-256 checksum How to use checksums |
92a7cd545473450e176b1b5e6eeb911622a6b2f1d7a98173599432b2f4ec102f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.12.3
|