toolslm 0.3.17__tar.gz → 0.3.18__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.17
3
+ Version: 0.3.18
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
@@ -1,7 +1,7 @@
1
1
  [DEFAULT]
2
2
  repo = toolslm
3
3
  lib_name = toolslm
4
- version = 0.3.17
4
+ version = 0.3.18
5
5
  min_python = 3.9
6
6
  license = apache2
7
7
  black_formatting = False
@@ -0,0 +1 @@
1
+ __version__ = "0.3.18"
@@ -44,12 +44,14 @@ d = { 'settings': { 'branch': 'main',
44
44
  'toolslm.xml.files2ctx': ('xml.html#files2ctx', 'toolslm/xml.py'),
45
45
  'toolslm.xml.folder2ctx': ('xml.html#folder2ctx', 'toolslm/xml.py'),
46
46
  'toolslm.xml.folder2ctx_cli': ('xml.html#folder2ctx_cli', 'toolslm/xml.py'),
47
+ 'toolslm.xml.get_docstring': ('xml.html#get_docstring', 'toolslm/xml.py'),
47
48
  'toolslm.xml.get_mime_text': ('xml.html#get_mime_text', 'toolslm/xml.py'),
48
49
  'toolslm.xml.json_to_xml': ('xml.html#json_to_xml', 'toolslm/xml.py'),
49
50
  'toolslm.xml.mk_doc': ('xml.html#mk_doc', 'toolslm/xml.py'),
50
51
  'toolslm.xml.mk_doctype': ('xml.html#mk_doctype', 'toolslm/xml.py'),
51
52
  'toolslm.xml.nb2xml': ('xml.html#nb2xml', 'toolslm/xml.py'),
52
53
  'toolslm.xml.parse_gh_url': ('xml.html#parse_gh_url', 'toolslm/xml.py'),
54
+ 'toolslm.xml.py2sigs': ('xml.html#py2sigs', 'toolslm/xml.py'),
53
55
  'toolslm.xml.read_file': ('xml.html#read_file', 'toolslm/xml.py'),
54
56
  'toolslm.xml.repo2ctx': ('xml.html#repo2ctx', 'toolslm/xml.py'),
55
57
  'toolslm.xml.sym2file': ('xml.html#sym2file', 'toolslm/xml.py'),
@@ -1,12 +1,12 @@
1
1
  # AUTOGENERATED! DO NOT EDIT! File to edit: ../00_xml.ipynb.
2
2
 
3
3
  # %% auto 0
4
- __all__ = ['doctype', 'json_to_xml', 'get_mime_text', 'cell2out', 'cell2xml', 'nb2xml', 'mk_doctype', 'mk_doc', 'docs_xml',
5
- 'read_file', 'files2ctx', 'folder2ctx', 'sym2file', 'sym2folderctx', 'sym2pkgpath', 'sym2pkgctx',
6
- 'folder2ctx_cli', 'parse_gh_url', 'repo2ctx']
4
+ __all__ = ['doctype', 'json_to_xml', 'get_mime_text', 'cell2out', 'cell2xml', 'nb2xml', 'get_docstring', 'py2sigs', 'mk_doctype',
5
+ 'mk_doc', 'docs_xml', 'read_file', 'files2ctx', 'folder2ctx', 'sym2file', 'sym2folderctx', 'sym2pkgpath',
6
+ 'sym2pkgctx', 'folder2ctx_cli', 'parse_gh_url', 'repo2ctx']
7
7
 
8
8
  # %% ../00_xml.ipynb
9
- import hashlib, inspect, xml.etree.ElementTree as ET
9
+ import hashlib, inspect, xml.etree.ElementTree as ET, ast
10
10
  from collections import namedtuple
11
11
  from ghapi.all import GhApi
12
12
 
@@ -69,6 +69,28 @@ def nb2xml(fname=None, nb=None, out=True, ids=True):
69
69
  cells_xml = [to_xml(cell2xml(c, out=out, ids=ids), do_escape=False) for c in nb.cells if c.cell_type in ('code','markdown')]
70
70
  return to_xml(Notebook(*cells_xml), do_escape=False)
71
71
 
72
+ # %% ../00_xml.ipynb
73
+ def get_docstring(node, lines):
74
+ "Get docstring from source lines if present"
75
+ if not (node.body and isinstance(node.body[0], ast.Expr) and isinstance(node.body[0].value, ast.Constant)): return None
76
+ doc_node = node.body[0]
77
+ return '\n'.join(lines[doc_node.lineno-1:doc_node.end_lineno])
78
+
79
+ def py2sigs(fname=None, src=None):
80
+ "Return signature+docstring text for all functions and class methods in source"
81
+ if fname: src = Path(fname).read_text()
82
+ tree = ast.parse(src)
83
+ lines = src.splitlines()
84
+ res = []
85
+ for node in ast.walk(tree):
86
+ if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)):
87
+ body_start = max(node.body[0].lineno - 1, node.lineno)
88
+ sig = '\n'.join(lines[node.lineno-1:body_start])
89
+ doc = get_docstring(node, lines)
90
+ cts = f"{sig}\n{doc}" if doc else sig
91
+ res.append(cts.strip('\r\n'))
92
+ return '\n\n'.join(res)
93
+
72
94
  # %% ../00_xml.ipynb
73
95
  doctype = namedtuple('doctype', ['src', 'content'])
74
96
 
@@ -111,15 +133,16 @@ def docs_xml(docs:list[str], # The content of each document
111
133
  pre = 'Here are some documents for you to reference for your task:\n\n' if prefix else ''
112
134
  if srcs is None: srcs = [None]*len(docs)
113
135
  if details is None: details = [{}]*len(docs)
114
- docs = (mk_doc(i+1, d, s, **kw) for i,(d,s,kw) in enumerate(zip(docs,srcs,details)))
136
+ docs = (mk_doc(i+1, d, s, **kw) for i,(d,s,kw) in enumerate(zip(docs,srcs,details)) if d.strip())
115
137
  kw = dict(title=title) if title else {}
116
138
  return pre + to_xml(Documents(*docs, **kw), do_escape=False)
117
139
 
118
140
  # %% ../00_xml.ipynb
119
- def read_file(fname, out=True, max_size=None, ids=True):
141
+ def read_file(fname, out=True, max_size=None, ids=True, sigs_only=False):
120
142
  "Read file content, converting notebooks to XML if needed"
121
143
  fname = Path(fname)
122
144
  if fname.suffix == '.ipynb': res = nb2xml(fname, out=out, ids=ids)
145
+ elif fname.suffix == '.py' and sigs_only: res = py2sigs(fname)
123
146
  else: res = fname.read_text()
124
147
  if max_size and len(res)>max_size: return f"[Skipped: {fname.name} exceeds {max_size} bytes]"
125
148
  return res
@@ -132,11 +155,12 @@ def files2ctx(
132
155
  srcs:Optional[list]=None, # Use the labels instead of `fnames`
133
156
  max_size:int=None, # Skip files larger than this (bytes)
134
157
  ids:bool=True, # Include cell ids in notebooks?
158
+ sigs_only:bool=False, # For .py files, only include signatures and docstrings
135
159
  **kwargs
136
160
  )->str: # XML for LM context
137
161
  "Convert files to XML context, handling notebooks"
138
162
  fnames = [Path(o) for o in fnames]
139
- contents = [read_file(o, out=out, max_size=max_size, ids=ids) for o in fnames]
163
+ contents = [read_file(o, out=out, max_size=max_size, ids=ids, sigs_only=sigs_only) for o in fnames]
140
164
  return docs_xml(contents, srcs or fnames, **kwargs)
141
165
 
142
166
  # %% ../00_xml.ipynb
@@ -151,6 +175,7 @@ def folder2ctx(
151
175
  max_total:int=10_000_000, # Max total output size in bytes
152
176
  readme_first:bool=False, # Prioritize README files at start of context?
153
177
  files_only:bool=False, # Return dict of {filename: size} instead of context?
178
+ sigs_only:bool=False, # Return signatures instead of full text for python files?
154
179
  ids:bool=True, # Include cell ids in notebooks?
155
180
  **kwargs
156
181
  )->Union[str,dict]:
@@ -160,7 +185,7 @@ def folder2ctx(
160
185
  if files_only: return {str(f.relative_to(folder)): f.stat().st_size for f in fnames}
161
186
  if readme_first: fnames = sorted(fnames, key=lambda f: (0 if 'readme' in f.name.lower() else 1, f))
162
187
  srcs = fnames if include_base else [f.relative_to(folder) for f in fnames]
163
- res = files2ctx(fnames, prefix=prefix, out=out, srcs=srcs, title=title, max_size=max_size, ids=ids)
188
+ res = files2ctx(fnames, prefix=prefix, out=out, srcs=srcs, title=title, max_size=max_size, sigs_only=sigs_only, ids=ids)
164
189
  suf = f"\n\n[TRUNCATED: output size {{_outsz_}} exceeded max size {max_total} bytes]"
165
190
  if max_total and len(res) > max_total: res = truncstr(res, max_total, suf=suf, sizevar='_outsz_')
166
191
  return res
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: toolslm
3
- Version: 0.3.17
3
+ Version: 0.3.18
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
@@ -1 +0,0 @@
1
- __version__ = "0.3.17"
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