Skip to main content

Tools to make language models a bit easier to use

Project description


This is a work in progress…


pip install toolslm

How to use

Context creation

toolslm has some helpers to make it easier to generate XML context from files, for instance folder2ctx:

print(folder2ctx('samples', prefix=False, file_glob='*.py'))
<document index="1">
import inspect
empty = inspect.Parameter.empty
models = 'claude-3-opus-20240229','claude-3-sonnet-20240229','claude-3-haiku-20240307'

XML helpers

Many language models work well with XML inputs, but XML can be a bit clunky to work with manually. Therefore, toolslm includes a couple of more streamlined approaches for XML generation.

An XML node contains a tag, optional children, and optional attributes. xt creates a tuple of these three things, which we will use to general XML shortly. Attributes are passed as kwargs; since these might conflict with reserved words in Python, you can optionally add a _ prefix and it’ll be stripped off.

xt('x-custom', ['hi'], _class='bar')
('x-custom', ['hi'], {'class': 'bar'})

Claudette has functions defined for some common HTML elements to create xt tuples more easily, including these:

from toolslm.xml import div,img,h1,h2,p,hr,html
a = html([
    p('This is a paragraph'),
        h1('This is a header'),
        h2('This is a sub-header', style='k:v'),
    ], _class='foo')
 [('p', 'This is a paragraph', {}),
  ('hr', None, {}),
  ('img', None, {'src': 'http://example.prg'}),
   [('h1', 'This is a header', {}),
    ('h2', 'This is a sub-header', {'style': 'k:v'})],
   {'class': 'foo'})],

To convert a tuple data structure created with xt and friends into XML, use to_xml, adding the hl parameter to optionally add syntax highlighting:

to_xml(a, hl=True)
  <p>This is a paragraph</p>
  <hr />
  <img src="http://example.prg" />
  <div class="foo">
    <h1>This is a header</h1>
    <h2 style="k:v">This is a sub-header</h2>

JSON doesn’t map as nicely to XML as the xt data structure, but for simple XML trees it can be convenient. The json_to_xml function handles that conversion:

a = dict(surname='Howard', firstnames=['Jeremy','Peter'],
print(json_to_xml(a, 'person'))

See the xml source section for a walkthru of XML and document context generation functionality.

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

toolslm-0.0.3.tar.gz (13.7 kB view hashes)

Uploaded Source

Built Distribution

toolslm-0.0.3-py3-none-any.whl (12.4 kB view hashes)

Uploaded Python 3

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