Skip to main content

Tools to make language models a bit easier to use

Project description

toolslm

This is a work in progress…

Install

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'))
<documents>
<document index="1">
<source>
samples/sample_core.py
</source>
<document_content>
import inspect
empty = inspect.Parameter.empty
models = 'claude-3-opus-20240229','claude-3-sonnet-20240229','claude-3-haiku-20240307'
</document_content>
</document>
</documents>

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'),
    hr(),
    img(src='http://example.prg'),
    div([
        h1('This is a header'),
        h2('This is a sub-header', style='k:v'),
    ], _class='foo')
])
a
('html',
 [('p', 'This is a paragraph', {}),
  ('hr', None, {}),
  ('img', None, {'src': 'http://example.prg'}),
  ('div',
   [('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)
<html>
  <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>
  </div>
</html>

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'],
         address=dict(state='Queensland',country='Australia'))
print(json_to_xml(a, 'person'))
<person>
  <surname>Howard</surname>
  <firstnames>
    <item>Jeremy</item>
    <item>Peter</item>
  </firstnames>
  <address>
    <state>Queensland</state>
    <country>Australia</country>
  </address>
</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.7.tar.gz (17.4 kB view details)

Uploaded Source

Built Distribution

toolslm-0.0.7-py3-none-any.whl (16.4 kB view details)

Uploaded Python 3

File details

Details for the file toolslm-0.0.7.tar.gz.

File metadata

  • Download URL: toolslm-0.0.7.tar.gz
  • Upload date:
  • Size: 17.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.8

File hashes

Hashes for toolslm-0.0.7.tar.gz
Algorithm Hash digest
SHA256 674200c368caed88a55306ef557639726f3b8445c0c708b30a128c85cef352b4
MD5 634a979b8a4d948e7b7931745b86ff59
BLAKE2b-256 57d68d71bcb2e42a81c90cebcda49cde3f13bd16b80be2d3bfd36f838ad13039

See more details on using hashes here.

File details

Details for the file toolslm-0.0.7-py3-none-any.whl.

File metadata

  • Download URL: toolslm-0.0.7-py3-none-any.whl
  • Upload date:
  • Size: 16.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.8

File hashes

Hashes for toolslm-0.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 cc22bf7ac41d45b9a250e6d322a0dfe662ec82a12a7c8eac22397ba0f8ec2d26
MD5 8f867ce574e6c12eff79bc9e6871a931
BLAKE2b-256 c7bcc46f03b62880791d34f8cc6d801217d3b808fb315b9d3ec88c75fe3eb06d

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