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
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
parse_llm_code-0.1.25.tar.gz
(3.0 kB
view details)
Built Distribution
File details
Details for the file parse_llm_code-0.1.25.tar.gz
.
File metadata
- Download URL: parse_llm_code-0.1.25.tar.gz
- Upload date:
- Size: 3.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 | 467f40fa3be9a99efe36e7f587d7bf124a7d6e6a1009bf7143f22afb870cd265 |
|
MD5 | 08829e0f3fdffd61397499685359ab71 |
|
BLAKE2b-256 | 96cb5db6dc36e04727fdd32a6a9fa4c6b4757b0bf078cf7a5468d8fe416b10c7 |
File details
Details for the file parse_llm_code-0.1.25-py3-none-any.whl
.
File metadata
- Download URL: parse_llm_code-0.1.25-py3-none-any.whl
- Upload date:
- Size: 4.3 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 | 042745f2e4e6c9b3eaab444b6c5d9d7f46926f1adaa961bbc21cc066589864de |
|
MD5 | 487476b1e5d24d916bbf894d71fa46b2 |
|
BLAKE2b-256 | eaea5e0d01ffccc715cec16e392eec8956f81625bdd81ed1b0672e6627ab5c1b |