toolslm 0.1.2__py3-none-any.whl → 0.2.0__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.1.2"
1
+ __version__ = "0.2.0"
toolslm/download.py CHANGED
@@ -7,10 +7,6 @@ __all__ = ['clean_md', 'read_md', 'html2md', 'read_html', 'get_llmstxt', 'split_
7
7
  from fastcore.utils import *
8
8
  from httpx import get
9
9
  from fastcore.meta import delegates
10
- from llms_txt import *
11
-
12
- from html2text import HTML2Text
13
- from bs4 import BeautifulSoup
14
10
  from urllib.parse import urlparse, urljoin
15
11
 
16
12
  # %% ../03_download.ipynb 4
@@ -29,7 +25,8 @@ def read_md(url, rm_comments=True, rm_details=True, **kwargs):
29
25
  # %% ../03_download.ipynb 7
30
26
  def html2md(s:str, ignore_links=True):
31
27
  "Convert `s` from HTML to markdown"
32
- o = HTML2Text(bodywidth=5000)
28
+ import html2text
29
+ o = html2text.HTML2Text(bodywidth=5000)
33
30
  o.ignore_links = ignore_links
34
31
  o.mark_code = True
35
32
  o.ignore_images = True
@@ -47,6 +44,7 @@ def read_html(url, # URL to read
47
44
  "Get `url`, optionally selecting CSS selector `sel`, and convert to clean markdown"
48
45
  page = get(url).text
49
46
  if sel:
47
+ from bs4 import BeautifulSoup
50
48
  soup = BeautifulSoup(page, 'html.parser')
51
49
  if multi:
52
50
  page = [str(el) for el in soup.select(sel)]
@@ -56,14 +54,14 @@ def read_html(url, # URL to read
56
54
  if wrap_tag: return '\n'.join([f"\n<{wrap_tag}>\n{o}</{wrap_tag}>\n" for o in mds])
57
55
  else: return'\n'.join(mds)
58
56
 
59
-
60
57
  # %% ../03_download.ipynb 13
61
58
  def get_llmstxt(url, optional=False, n_workers=None):
62
59
  "Get llms.txt file from and expand it with `llms_txt.create_ctx()`"
63
60
  if not url.endswith('llms.txt'): return None
61
+ import llms_txt
64
62
  resp = get(url)
65
63
  if resp.status_code!=200: return None
66
- return create_ctx(resp.text, optional=optional, n_workers=n_workers)
64
+ return llms_txt.create_ctx(resp.text, optional=optional, n_workers=n_workers)
67
65
 
68
66
  # %% ../03_download.ipynb 15
69
67
  def split_url(url):
toolslm/funccall.py CHANGED
@@ -141,7 +141,7 @@ def _copy_loc(new, orig):
141
141
  return new
142
142
 
143
143
  # %% ../01_funccall.ipynb 69
144
- def _run(code:str ):
144
+ def _run(code:str, glb:dict=None, loc:dict=None):
145
145
  "Run `code`, returning final expression (similar to IPython)"
146
146
  tree = ast.parse(code)
147
147
  last_node = tree.body[-1] if tree.body else None
@@ -153,30 +153,34 @@ def _run(code:str ):
153
153
  tree.body[-1] = _copy_loc(assign_node, last_node)
154
154
 
155
155
  compiled_code = compile(tree, filename='<ast>', mode='exec')
156
- namespace = {}
156
+ glb = glb or {}
157
157
  stdout_buffer = io.StringIO()
158
158
  saved_stdout = sys.stdout
159
159
  sys.stdout = stdout_buffer
160
- try: exec(compiled_code, namespace)
160
+ try: exec(compiled_code, glb, loc)
161
161
  finally: sys.stdout = saved_stdout
162
- _result = namespace.get('_result', None)
162
+ _result = glb.get('_result', None)
163
163
  if _result is not None: return _result
164
164
  return stdout_buffer.getvalue().strip()
165
165
 
166
166
  # %% ../01_funccall.ipynb 74
167
- def python(code, # Code to execute
168
- timeout=5 # Maximum run time in seconds before a `TimeoutError` is raised
167
+ def python(code:str, # Code to execute
168
+ glb:Optional[dict]=None, # Globals namespace
169
+ loc:Optional[dict]=None, # Locals namespace
170
+ timeout:int=3600 # Maximum run time in seconds before a `TimeoutError` is raised
169
171
  ): # Result of last node, if it's an expression, or `None` otherwise
170
172
  """Executes python `code` with `timeout` and returning final expression (similar to IPython).
171
173
  Raised exceptions are returned as a string, with a stack trace."""
172
174
  def handler(*args): raise TimeoutError()
175
+ if glb is None: glb = inspect.currentframe().f_back.f_globals
176
+ if loc is None: loc=glb
173
177
  signal.signal(signal.SIGALRM, handler)
174
178
  signal.alarm(timeout)
175
- try: return _run(code)
179
+ try: return _run(code, glb, loc)
176
180
  except Exception as e: return traceback.format_exc()
177
181
  finally: signal.alarm(0)
178
182
 
179
- # %% ../01_funccall.ipynb 81
183
+ # %% ../01_funccall.ipynb 85
180
184
  def mk_ns(*funcs_or_objs):
181
185
  merged = {}
182
186
  for o in funcs_or_objs:
@@ -185,14 +189,14 @@ def mk_ns(*funcs_or_objs):
185
189
  if callable(o) and hasattr(o, '__name__'): merged |= {o.__name__: o}
186
190
  return merged
187
191
 
188
- # %% ../01_funccall.ipynb 90
192
+ # %% ../01_funccall.ipynb 94
189
193
  def call_func(fc_name, fc_inputs, ns):
190
194
  "Call the function `fc_name` with the given `fc_inputs` using namespace `ns`."
191
195
  if not isinstance(ns, abc.Mapping): ns = mk_ns(*ns)
192
196
  func = ns[fc_name]
193
197
  return func(**fc_inputs)
194
198
 
195
- # %% ../01_funccall.ipynb 97
199
+ # %% ../01_funccall.ipynb 101
196
200
  async def call_func_async(fc_name, fc_inputs, ns):
197
201
  "Awaits the function `fc_name` with the given `fc_inputs` using namespace `ns`."
198
202
  if not isinstance(ns, abc.Mapping): ns = mk_ns(*ns)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: toolslm
3
- Version: 0.1.2
3
+ Version: 0.2.0
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
@@ -17,10 +17,7 @@ Requires-Python: >=3.9
17
17
  Description-Content-Type: text/markdown
18
18
  License-File: LICENSE
19
19
  Requires-Dist: fastcore>=1.5.47
20
- Requires-Dist: beautifulsoup4
21
- Requires-Dist: html2text
22
20
  Requires-Dist: httpx
23
- Requires-Dist: llms_txt
24
21
  Provides-Extra: dev
25
22
  Dynamic: author
26
23
  Dynamic: author-email
@@ -0,0 +1,13 @@
1
+ toolslm/__init__.py,sha256=Zn1KFblwuFHiDRdRAiRnDBRkbPttWh44jKa5zG2ov0E,22
2
+ toolslm/_modidx.py,sha256=-D-B5o30VGs11gBKf96lpADVXnZhdiVEshJpLzmUnDs,4378
3
+ toolslm/download.py,sha256=Ak7xzzCplU4RFW2cvUiuvhnL0agQqwF4lVH2hQhNNmc,4476
4
+ toolslm/funccall.py,sha256=bzVlcTpgQuuBIG-I1HRidnXV5mp0FfGV10_Mk1DSQuc,8460
5
+ toolslm/md_hier.py,sha256=4uC12443tPBduYJgIZZIcEat2VG0x7JYC8-SwDdS2JY,6360
6
+ toolslm/shell.py,sha256=GVqfL74NHw66zzZ7jvGVLjE55ZNJGBPvEb8kLz4aoYc,1576
7
+ toolslm/xml.py,sha256=QNwUavoMkFK84D7dMwnBjqlYJwN-pJ7u3BxOeDuNAmk,4088
8
+ toolslm-0.2.0.dist-info/licenses/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
9
+ toolslm-0.2.0.dist-info/METADATA,sha256=L2bwDiXXiMvKYeR8SSApCVBF7YrwwOE4Nbry4DJfMOk,2404
10
+ toolslm-0.2.0.dist-info/WHEEL,sha256=CmyFI0kx5cdEMTLiONQRbGQwjIoR1aIYB7eCAQ4KPJ0,91
11
+ toolslm-0.2.0.dist-info/entry_points.txt,sha256=xFz0Eymlo5X7BGpaO6DI9gMxvN5A7faebzrlr8ctp5I,95
12
+ toolslm-0.2.0.dist-info/top_level.txt,sha256=4hRTrFWayz_Kz5221XjvlpCwVFrW3WPi1P0fllkTq9s,8
13
+ toolslm-0.2.0.dist-info/RECORD,,
@@ -1,13 +0,0 @@
1
- toolslm/__init__.py,sha256=YvuYzWnKtqBb-IqG8HAu-nhIYAsgj9Vmc_b9o7vO-js,22
2
- toolslm/_modidx.py,sha256=-D-B5o30VGs11gBKf96lpADVXnZhdiVEshJpLzmUnDs,4378
3
- toolslm/download.py,sha256=d84O8PCX7uAbLg0HTbNNfCdANPcnhXFu4qzOtcfJfHU,4465
4
- toolslm/funccall.py,sha256=yAZmu2gIRtND-5pOuLCp7P4dor0FV_xntcacnZ81j0M,8210
5
- toolslm/md_hier.py,sha256=4uC12443tPBduYJgIZZIcEat2VG0x7JYC8-SwDdS2JY,6360
6
- toolslm/shell.py,sha256=GVqfL74NHw66zzZ7jvGVLjE55ZNJGBPvEb8kLz4aoYc,1576
7
- toolslm/xml.py,sha256=QNwUavoMkFK84D7dMwnBjqlYJwN-pJ7u3BxOeDuNAmk,4088
8
- toolslm-0.1.2.dist-info/licenses/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
9
- toolslm-0.1.2.dist-info/METADATA,sha256=gGurWuj5SWNPQvCPKbd8oc5Iz9x4NPxv8bx_C8w3Kps,2483
10
- toolslm-0.1.2.dist-info/WHEEL,sha256=CmyFI0kx5cdEMTLiONQRbGQwjIoR1aIYB7eCAQ4KPJ0,91
11
- toolslm-0.1.2.dist-info/entry_points.txt,sha256=xFz0Eymlo5X7BGpaO6DI9gMxvN5A7faebzrlr8ctp5I,95
12
- toolslm-0.1.2.dist-info/top_level.txt,sha256=4hRTrFWayz_Kz5221XjvlpCwVFrW3WPi1P0fllkTq9s,8
13
- toolslm-0.1.2.dist-info/RECORD,,