toolslm 0.3.2__py3-none-any.whl → 0.3.4__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
toolslm/__init__.py CHANGED
@@ -1 +1 @@
1
- __version__ = "0.3.2"
1
+ __version__ = "0.3.4"
toolslm/_modidx.py CHANGED
@@ -35,10 +35,15 @@ d = { 'settings': { 'branch': 'main',
35
35
  'toolslm/shell.py'),
36
36
  'toolslm.shell.get_shell': ('shell.html#get_shell', 'toolslm/shell.py')},
37
37
  'toolslm.xml': { 'toolslm.xml._add_nls': ('xml.html#_add_nls', 'toolslm/xml.py'),
38
+ 'toolslm.xml.cell2out': ('xml.html#cell2out', 'toolslm/xml.py'),
39
+ 'toolslm.xml.cell2xml': ('xml.html#cell2xml', 'toolslm/xml.py'),
38
40
  'toolslm.xml.docs_xml': ('xml.html#docs_xml', 'toolslm/xml.py'),
39
41
  'toolslm.xml.files2ctx': ('xml.html#files2ctx', 'toolslm/xml.py'),
40
42
  'toolslm.xml.folder2ctx': ('xml.html#folder2ctx', 'toolslm/xml.py'),
41
43
  'toolslm.xml.folder2ctx_cli': ('xml.html#folder2ctx_cli', 'toolslm/xml.py'),
44
+ 'toolslm.xml.get_mime_text': ('xml.html#get_mime_text', 'toolslm/xml.py'),
42
45
  'toolslm.xml.json_to_xml': ('xml.html#json_to_xml', 'toolslm/xml.py'),
43
46
  'toolslm.xml.mk_doc': ('xml.html#mk_doc', 'toolslm/xml.py'),
44
- 'toolslm.xml.mk_doctype': ('xml.html#mk_doctype', 'toolslm/xml.py')}}}
47
+ 'toolslm.xml.mk_doctype': ('xml.html#mk_doctype', 'toolslm/xml.py'),
48
+ 'toolslm.xml.nb2xml': ('xml.html#nb2xml', 'toolslm/xml.py'),
49
+ 'toolslm.xml.read_file': ('xml.html#read_file', 'toolslm/xml.py')}}}
toolslm/funccall.py CHANGED
@@ -107,7 +107,9 @@ def _get_nested_schema(obj):
107
107
  if n != 'return' and n != 'self':
108
108
  _process_property(n, o, props, req, defs)
109
109
 
110
- schema = dict(type='object', properties=props, title=obj.__name__ if isinstance(obj, type) else None)
110
+ tkw = {}
111
+ if isinstance(obj, type): tkw['title']=obj.__name__
112
+ schema = dict(type='object', properties=props, **tkw)
111
113
  if req: schema['required'] = list(req)
112
114
  if defs: schema['$defs'] = defs
113
115
  return schema
toolslm/xml.py CHANGED
@@ -1,7 +1,8 @@
1
1
  # AUTOGENERATED! DO NOT EDIT! File to edit: ../00_xml.ipynb.
2
2
 
3
3
  # %% auto 0
4
- __all__ = ['doctype', 'json_to_xml', 'mk_doctype', 'mk_doc', 'docs_xml', 'files2ctx', 'folder2ctx', 'folder2ctx_cli']
4
+ __all__ = ['doctype', 'json_to_xml', 'get_mime_text', 'cell2out', 'cell2xml', 'nb2xml', 'mk_doctype', 'mk_doc', 'docs_xml',
5
+ 'read_file', 'files2ctx', 'folder2ctx', 'folder2ctx_cli']
5
6
 
6
7
  # %% ../00_xml.ipynb
7
8
  import hashlib,xml.etree.ElementTree as ET
@@ -10,7 +11,7 @@ from collections import namedtuple
10
11
  from fastcore.utils import *
11
12
  from fastcore.meta import delegates
12
13
  from fastcore.xtras import hl_md
13
- from fastcore.xml import to_xml, Document, Documents, Document_content, Src
14
+ from fastcore.xml import to_xml, Document, Documents, Document_content, Src, Source,Out,Outs,Cell
14
15
  from fastcore.script import call_parse
15
16
  try: from IPython import display
16
17
  except: display=None
@@ -31,6 +32,39 @@ def json_to_xml(d:dict, # JSON dictionary to convert
31
32
  ET.indent(root)
32
33
  return ET.tostring(root, encoding='unicode')
33
34
 
35
+ # %% ../00_xml.ipynb
36
+ def get_mime_text(data):
37
+ "Get text from MIME bundle, preferring markdown over plain"
38
+ if 'text/markdown' in data: return ''.join(list(data['text/markdown']))
39
+ if 'text/plain' in data: return ''.join(list(data['text/plain']))
40
+
41
+ # %% ../00_xml.ipynb
42
+ def cell2out(o):
43
+ "Convert single notebook output to XML format"
44
+ if hasattr(o, 'data'):
45
+ txt = get_mime_text(o.data)
46
+ if txt: return Out(txt, mime='markdown' if 'text/markdown' in o.data else 'plain')
47
+ if hasattr(o, 'text'):
48
+ txt = o.text if isinstance(o.text, str) else ''.join(o.text)
49
+ return Out(txt, type='stream', name=o.get('name', 'stdout'))
50
+ if hasattr(o, 'ename'): return Out(f"{o.ename}: {o.evalue}", type='error')
51
+
52
+ # %% ../00_xml.ipynb
53
+ def cell2xml(cell):
54
+ "Convert notebook cell to concise XML format"
55
+ cts = Source(''.join(cell.source)) if hasattr(cell, 'source') and cell.source else None
56
+ out_items = L(getattr(cell,'outputs',[])).map(cell2out).filter()
57
+ outs = []
58
+ if out_items: outs = Outs(*out_items)
59
+ parts = [p for p in [cts, outs] if p]
60
+ return Cell(*parts, type=cell.cell_type)
61
+
62
+ # %% ../00_xml.ipynb
63
+ def nb2xml(fname):
64
+ nb = dict2obj(fname.read_json())
65
+ cells_xml = [to_xml(cell2xml(c), do_escape=False) for c in nb.cells if c.cell_type in ('code','markdown')]
66
+ return '\n'.join(cells_xml)
67
+
34
68
  # %% ../00_xml.ipynb
35
69
  doctype = namedtuple('doctype', ['src', 'content'])
36
70
 
@@ -73,15 +107,23 @@ def docs_xml(docs:list[str], # The content of each document
73
107
  if srcs is None: srcs = [None]*len(docs)
74
108
  if details is None: details = [{}]*len(docs)
75
109
  docs = (mk_doc(i+1, d, s, **kw) for i,(d,s,kw) in enumerate(zip(docs,srcs,details)))
76
- return pre + to_xml(Documents(docs))
110
+ return pre + to_xml(Documents(docs), do_escape=False)
111
+
112
+ # %% ../00_xml.ipynb
113
+ def read_file(fname):
114
+ "Read file content, converting notebooks to XML if needed"
115
+ fname = Path(fname)
116
+ if fname.suffix == '.ipynb': return nb2xml(fname)
117
+ return fname.read_text()
77
118
 
78
119
  # %% ../00_xml.ipynb
79
120
  def files2ctx(
80
121
  fnames:list[Union[str,Path]], # List of file names to add to context
81
122
  prefix:bool=True # Include Anthropic's suggested prose intro?
82
123
  )->str: # XML for LM context
124
+ "Convert files to XML context, handling notebooks"
83
125
  fnames = [Path(o) for o in fnames]
84
- contents = [o.read_text() for o in fnames]
126
+ contents = [read_file(o) for o in fnames]
85
127
  return docs_xml(contents, fnames, prefix=prefix)
86
128
 
87
129
  # %% ../00_xml.ipynb
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: toolslm
3
- Version: 0.3.2
3
+ Version: 0.3.4
4
4
  Summary: Tools to make language models a bit easier to use
5
5
  Home-page: https://github.com/AnswerDotAI/toolslm
6
6
  Author: Jeremy Howard
@@ -16,7 +16,7 @@ Classifier: License :: OSI Approved :: Apache Software License
16
16
  Requires-Python: >=3.9
17
17
  Description-Content-Type: text/markdown
18
18
  License-File: LICENSE
19
- Requires-Dist: fastcore>=1.5.47
19
+ Requires-Dist: fastcore>=1.8.11
20
20
  Requires-Dist: httpx
21
21
  Provides-Extra: dev
22
22
  Dynamic: author
@@ -0,0 +1,13 @@
1
+ toolslm/__init__.py,sha256=oYLGMpySamd16KLiaBTfRyrAS7_oyp-TOEHmzmeumwg,22
2
+ toolslm/_modidx.py,sha256=KeI16uj9ZfA5ZWlmov5ATQwf3291dayJ42Bp1dh69gU,4856
3
+ toolslm/download.py,sha256=g3BxUSxylC_575M7RFSJ1GI3Co3EwPDdEeWzxaf2Czk,4451
4
+ toolslm/funccall.py,sha256=PSmQ9kZsJvYh3egH7k1vQPYFn-XBm5E_CCKDVdX9tfg,8506
5
+ toolslm/md_hier.py,sha256=Havk9Hf0t2Xt67n_r7ZxCsS0pciR85iLcE5quShvkTg,10032
6
+ toolslm/shell.py,sha256=dGInuRKvexu21VmtZkw_0S3BGiTsbAongUG-yG4YHpc,1566
7
+ toolslm/xml.py,sha256=2QGVGrTWfyyf0duBDn_Of5kFzYFoBjvl8PrZKgNxbUU,5806
8
+ toolslm-0.3.4.dist-info/licenses/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
9
+ toolslm-0.3.4.dist-info/METADATA,sha256=y9ERYYAn6SAv_ozBqAeoiMgGjorCUGHQA1eLPWDSdlA,2404
10
+ toolslm-0.3.4.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
11
+ toolslm-0.3.4.dist-info/entry_points.txt,sha256=xFz0Eymlo5X7BGpaO6DI9gMxvN5A7faebzrlr8ctp5I,95
12
+ toolslm-0.3.4.dist-info/top_level.txt,sha256=4hRTrFWayz_Kz5221XjvlpCwVFrW3WPi1P0fllkTq9s,8
13
+ toolslm-0.3.4.dist-info/RECORD,,
@@ -1,13 +0,0 @@
1
- toolslm/__init__.py,sha256=vNiWJ14r_cw5t_7UDqDQIVZvladKFGyHH2avsLpN7Vg,22
2
- toolslm/_modidx.py,sha256=-D-B5o30VGs11gBKf96lpADVXnZhdiVEshJpLzmUnDs,4378
3
- toolslm/download.py,sha256=g3BxUSxylC_575M7RFSJ1GI3Co3EwPDdEeWzxaf2Czk,4451
4
- toolslm/funccall.py,sha256=7nPfbcvDRMWiVKBKMLlCOMInoUJgDs5e38ef2T7QBHY,8485
5
- toolslm/md_hier.py,sha256=Havk9Hf0t2Xt67n_r7ZxCsS0pciR85iLcE5quShvkTg,10032
6
- toolslm/shell.py,sha256=dGInuRKvexu21VmtZkw_0S3BGiTsbAongUG-yG4YHpc,1566
7
- toolslm/xml.py,sha256=D665Nk7NzyZlXyXrpnIRqfK2xQ-6Gf0bCSgocjF7zik,4061
8
- toolslm-0.3.2.dist-info/licenses/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
9
- toolslm-0.3.2.dist-info/METADATA,sha256=5lWEv7BWTwdd5cvXgGsQXqr0j6tk8UIcGpRTlcjV3V4,2404
10
- toolslm-0.3.2.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
11
- toolslm-0.3.2.dist-info/entry_points.txt,sha256=xFz0Eymlo5X7BGpaO6DI9gMxvN5A7faebzrlr8ctp5I,95
12
- toolslm-0.3.2.dist-info/top_level.txt,sha256=4hRTrFWayz_Kz5221XjvlpCwVFrW3WPi1P0fllkTq9s,8
13
- toolslm-0.3.2.dist-info/RECORD,,