toolslm 0.3.6__tar.gz → 0.3.7__tar.gz

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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: toolslm
3
- Version: 0.3.6
3
+ Version: 0.3.7
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.8.11
19
+ Requires-Dist: fastcore>=1.9.6
20
20
  Requires-Dist: httpx
21
21
  Provides-Extra: dev
22
22
  Dynamic: author
@@ -1,11 +1,11 @@
1
1
  [DEFAULT]
2
2
  repo = toolslm
3
3
  lib_name = toolslm
4
- version = 0.3.6
4
+ version = 0.3.7
5
5
  min_python = 3.9
6
6
  license = apache2
7
7
  black_formatting = False
8
- requirements = fastcore>=1.8.11 httpx
8
+ requirements = fastcore>=1.9.6 httpx
9
9
  doc_path = _docs
10
10
  lib_path = toolslm
11
11
  nbs_path = .
@@ -0,0 +1 @@
1
+ __version__ = "0.3.7"
@@ -49,4 +49,5 @@ d = { 'settings': { 'branch': 'main',
49
49
  'toolslm.xml.mk_doc': ('xml.html#mk_doc', 'toolslm/xml.py'),
50
50
  'toolslm.xml.mk_doctype': ('xml.html#mk_doctype', 'toolslm/xml.py'),
51
51
  'toolslm.xml.nb2xml': ('xml.html#nb2xml', 'toolslm/xml.py'),
52
- 'toolslm.xml.read_file': ('xml.html#read_file', 'toolslm/xml.py')}}}
52
+ 'toolslm.xml.read_file': ('xml.html#read_file', 'toolslm/xml.py'),
53
+ 'toolslm.xml.repo2ctx': ('xml.html#repo2ctx', 'toolslm/xml.py')}}}
@@ -2,19 +2,18 @@
2
2
 
3
3
  # %% auto 0
4
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
+ 'read_file', 'files2ctx', 'folder2ctx', 'repo2ctx', 'folder2ctx_cli']
6
6
 
7
7
  # %% ../00_xml.ipynb
8
8
  import hashlib,xml.etree.ElementTree as ET
9
9
  from collections import namedtuple
10
+ from ghapi.all import GhApi
10
11
 
11
12
  from fastcore.utils import *
12
13
  from fastcore.meta import delegates
13
14
  from fastcore.xtras import hl_md
14
- from fastcore.xml import to_xml, Document, Documents, Document_content, Src, Source,Out,Outs,Cell
15
+ from fastcore.xml import to_xml, Document, Documents, Document_content, Src, Source,Out,Outs,Cell,Notebook,Md,Code
15
16
  from fastcore.script import call_parse
16
- try: from IPython import display
17
- except: display=None
18
17
 
19
18
  # %% ../00_xml.ipynb
20
19
  def json_to_xml(d:dict, # JSON dictionary to convert
@@ -50,20 +49,23 @@ def cell2out(o):
50
49
  if hasattr(o, 'ename'): return Out(f"{o.ename}: {o.evalue}", type='error')
51
50
 
52
51
  # %% ../00_xml.ipynb
53
- def cell2xml(cell):
52
+ def cell2xml(cell, out=True):
54
53
  "Convert notebook cell to concise XML format"
55
- cts = Source(''.join(cell.source)) if hasattr(cell, 'source') and cell.source else None
54
+ src = ''.join(getattr(cell, 'source', ''))
55
+ f = Code if cell.cell_type=='code' else Md
56
+ if not out: return f(src)
57
+ parts = [Source(src)]
56
58
  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)
59
+ if out_items: parts.append(Outs(*out_items))
60
+ return f(*parts)
61
61
 
62
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)
63
+ def nb2xml(fname=None, nb=None, out=True):
64
+ "Convert notebook to XML format"
65
+ assert bool(fname)^bool(nb), "Pass either `fname` or `nb`"
66
+ if not nb: nb = dict2obj(fname.read_json())
67
+ cells_xml = [to_xml(cell2xml(c, out=out), do_escape=False) for c in nb.cells if c.cell_type in ('code','markdown')]
68
+ return to_xml(Notebook(*cells_xml), do_escape=False)
67
69
 
68
70
  # %% ../00_xml.ipynb
69
71
  doctype = namedtuple('doctype', ['src', 'content'])
@@ -110,37 +112,65 @@ def docs_xml(docs:list[str], # The content of each document
110
112
  return pre + to_xml(Documents(docs), do_escape=False)
111
113
 
112
114
  # %% ../00_xml.ipynb
113
- def read_file(fname):
115
+ def read_file(fname, out=True):
114
116
  "Read file content, converting notebooks to XML if needed"
115
117
  fname = Path(fname)
116
- if fname.suffix == '.ipynb': return nb2xml(fname)
118
+ if fname.suffix == '.ipynb': return nb2xml(fname, out=out)
117
119
  return fname.read_text()
118
120
 
119
121
  # %% ../00_xml.ipynb
120
122
  def files2ctx(
121
123
  fnames:list[Union[str,Path]], # List of file names to add to context
122
- prefix:bool=True # Include Anthropic's suggested prose intro?
124
+ prefix:bool=True, # Include Anthropic's suggested prose intro?
125
+ out:bool=True, # Include notebook cell outputs?
126
+ srcs:Optional[list]=None # Use the labels instead of `fnames`
123
127
  )->str: # XML for LM context
124
128
  "Convert files to XML context, handling notebooks"
125
129
  fnames = [Path(o) for o in fnames]
126
- contents = [read_file(o) for o in fnames]
127
- return docs_xml(contents, fnames, prefix=prefix)
130
+ contents = [read_file(o, out=out) for o in fnames]
131
+ return docs_xml(contents, srcs or fnames, prefix=prefix)
128
132
 
129
133
  # %% ../00_xml.ipynb
130
134
  @delegates(globtastic)
131
135
  def folder2ctx(
132
- folder:Union[str,Path], # Folder name containing files to add to context
133
- prefix:bool=True, # Include Anthropic's suggested prose intro?
134
- **kwargs # Passed to `globtastic`
135
- )->str: # XML for Claude context
136
+ folder:Union[str,Path],
137
+ prefix:bool=True,
138
+ out:bool=True,
139
+ include_base:bool=True,
140
+ **kwargs
141
+ )->str:
142
+ "Convert folder contents to XML context, handling notebooks"
143
+ folder = Path(folder)
136
144
  fnames = globtastic(folder, **kwargs)
137
- return files2ctx(fnames, prefix=prefix)
145
+ srcs = fnames if include_base else [Path(f).relative_to(folder) for f in fnames]
146
+ return files2ctx(fnames, prefix=prefix, out=out, srcs=srcs)
147
+
148
+ # %% ../00_xml.ipynb
149
+ @delegates(folder2ctx)
150
+ def repo2ctx(
151
+ owner:str, # GitHub repo owner
152
+ repo:str, # GitHub repo name
153
+ ref:str=None, # Git ref (branch/tag/sha); defaults to repo's default branch
154
+ **kwargs # Passed to `folder2ctx`
155
+ )->str: # XML for LM context
156
+ "Convert GitHub repo to XML context without cloning"
157
+ import tempfile, tarfile, io
158
+ api = GhApi()
159
+ if ref is None: ref = api.repos.get(owner, repo).default_branch
160
+ data = api.repos.download_tarball_archive(owner, repo, ref)
161
+ tf = tarfile.open(fileobj=io.BytesIO(data))
162
+ with tempfile.TemporaryDirectory() as tmp:
163
+ tf.extractall(tmp, filter='data')
164
+ subdir = Path(tmp) / tf.getmembers()[0].name.split('/')[0]
165
+ return folder2ctx(subdir, include_base=False, **kwargs)
138
166
 
139
167
  # %% ../00_xml.ipynb
140
168
  @call_parse
141
169
  @delegates(folder2ctx)
142
170
  def folder2ctx_cli(
143
171
  folder:str, # Folder name containing files to add to context
172
+ out:bool=True, # Include notebook cell outputs?
144
173
  **kwargs # Passed to `folder2ctx`
145
174
  )->str: # XML for Claude context
146
- print(folder2ctx(folder, **kwargs))
175
+ "CLI to convert folder contents to XML context, handling notebooks"
176
+ print(folder2ctx(folder, out=out, **kwargs))
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: toolslm
3
- Version: 0.3.6
3
+ Version: 0.3.7
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.8.11
19
+ Requires-Dist: fastcore>=1.9.6
20
20
  Requires-Dist: httpx
21
21
  Provides-Extra: dev
22
22
  Dynamic: author
@@ -0,0 +1,4 @@
1
+ fastcore>=1.9.6
2
+ httpx
3
+
4
+ [dev]
@@ -1 +0,0 @@
1
- __version__ = "0.3.6"
@@ -1,4 +0,0 @@
1
- fastcore>=1.8.11
2
- httpx
3
-
4
- [dev]
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes