Skip to main content

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


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.24.tar.gz (3.0 kB view details)

Uploaded Source

Built Distribution

parse_llm_code-0.1.24-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file parse_llm_code-0.1.24.tar.gz.

File metadata

  • Download URL: parse_llm_code-0.1.24.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

Hashes for parse_llm_code-0.1.24.tar.gz
Algorithm Hash digest
SHA256 b4520d073c9028d0c49e85d798f04ff09952c4622b456f87b383a3f8315ae22d
MD5 5c05ce14edd1a02c3a53f0528433bb45
BLAKE2b-256 6c82d66c1b0761be263b5828bee44a30d2fa6773eab04491449e3d1bb137e47a

See more details on using hashes here.

File details

Details for the file parse_llm_code-0.1.24-py3-none-any.whl.

File metadata

File hashes

Hashes for parse_llm_code-0.1.24-py3-none-any.whl
Algorithm Hash digest
SHA256 aa717af13f73830799b730fba8d0903de68ded0ea724b92154886087f4e399f2
MD5 a9dc6fdfcd3ad48d92716403d7545e8f
BLAKE2b-256 def0a6634e80b7f900b285404193f38fd9083a4c5b98f5d73672c4c3276e9664

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page