a lib to parse llm answer to code
Project description
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file parse_llm_code-0.1.31.tar.gz.
File metadata
- Download URL: parse_llm_code-0.1.31.tar.gz
- Upload date:
- Size: 4.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d2d9ce14f0e35a4dab669c4e32b15ab5c75ff8dfb0971195e0cf09b87eb5ddfc
|
|
| MD5 |
f0b7ccfad7399c3a7a83d12ea99951b7
|
|
| BLAKE2b-256 |
7b9bc314add8cfd3e28c16b5e4dfbed17528f40918c0697caee62d15ebd4e729
|
File details
Details for the file parse_llm_code-0.1.31-py3-none-any.whl.
File metadata
- Download URL: parse_llm_code-0.1.31-py3-none-any.whl
- Upload date:
- Size: 5.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fa5b9b4138e1a450bc3a51e13e592b9dddf577cf004b1f71f764e25a76f4ff33
|
|
| MD5 |
1842e268cfbcfe6d000ce14d9eab538e
|
|
| BLAKE2b-256 |
92a7cd545473450e176b1b5e6eeb911622a6b2f1d7a98173599432b2f4ec102f
|