dialoghelper 0.0.11__py3-none-any.whl → 0.0.13__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.

Potentially problematic release.


This version of dialoghelper might be problematic. Click here for more details.

dialoghelper/__init__.py CHANGED
@@ -1,2 +1,2 @@
1
- __version__ = "0.0.11"
1
+ __version__ = "0.0.13"
2
2
  from .core import *
dialoghelper/_modidx.py CHANGED
@@ -5,10 +5,11 @@ d = { 'settings': { 'branch': 'main',
5
5
  'doc_host': 'https://AnswerDotAI.github.io',
6
6
  'git_url': 'https://github.com/AnswerDotAI/dialoghelper',
7
7
  'lib_path': 'dialoghelper'},
8
- 'syms': { 'dialoghelper.core': { 'dialoghelper.core._msg': ('core.html#_msg', 'dialoghelper/core.py'),
8
+ 'syms': { 'dialoghelper.core': { 'dialoghelper.core._add_msg_unsafe': ('core.html#_add_msg_unsafe', 'dialoghelper/core.py'),
9
+ 'dialoghelper.core._msg': ('core.html#_msg', 'dialoghelper/core.py'),
9
10
  'dialoghelper.core._umsg': ('core.html#_umsg', 'dialoghelper/core.py'),
10
- 'dialoghelper.core.add_html': ('core.html#add_html', 'dialoghelper/core.py'),
11
11
  'dialoghelper.core.add_msg': ('core.html#add_msg', 'dialoghelper/core.py'),
12
+ 'dialoghelper.core.del_msg': ('core.html#del_msg', 'dialoghelper/core.py'),
12
13
  'dialoghelper.core.export_dialog': ('core.html#export_dialog', 'dialoghelper/core.py'),
13
14
  'dialoghelper.core.find_dialog_id': ('core.html#find_dialog_id', 'dialoghelper/core.py'),
14
15
  'dialoghelper.core.find_msg_id': ('core.html#find_msg_id', 'dialoghelper/core.py'),
@@ -19,9 +20,12 @@ d = { 'settings': { 'branch': 'main',
19
20
  'dialoghelper.core.import_dialog': ('core.html#import_dialog', 'dialoghelper/core.py'),
20
21
  'dialoghelper.core.import_gist': ('core.html#import_gist', 'dialoghelper/core.py'),
21
22
  'dialoghelper.core.import_string': ('core.html#import_string', 'dialoghelper/core.py'),
23
+ 'dialoghelper.core.is_usable_tool': ('core.html#is_usable_tool', 'dialoghelper/core.py'),
22
24
  'dialoghelper.core.load_gist': ('core.html#load_gist', 'dialoghelper/core.py'),
25
+ 'dialoghelper.core.mk_toollist': ('core.html#mk_toollist', 'dialoghelper/core.py'),
23
26
  'dialoghelper.core.msg_idx': ('core.html#msg_idx', 'dialoghelper/core.py'),
24
27
  'dialoghelper.core.read_msg': ('core.html#read_msg', 'dialoghelper/core.py'),
25
28
  'dialoghelper.core.read_msg_ids': ('core.html#read_msg_ids', 'dialoghelper/core.py'),
29
+ 'dialoghelper.core.tool_info': ('core.html#tool_info', 'dialoghelper/core.py'),
26
30
  'dialoghelper.core.update_msg': ('core.html#update_msg', 'dialoghelper/core.py')},
27
31
  'dialoghelper.db_dc': {}}}
dialoghelper/core.py CHANGED
@@ -1,12 +1,12 @@
1
1
  # AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/00_core.ipynb.
2
2
 
3
3
  # %% auto 0
4
- __all__ = ['Placements', 'get_db', 'find_var', 'find_dialog_id', 'find_msgs', 'find_msg_id', 'read_msg_ids', 'msg_idx',
5
- 'read_msg', 'add_msg', 'update_msg', 'add_html', 'load_gist', 'gist_file', 'import_string', 'import_gist',
6
- 'export_dialog', 'import_dialog', 'asdict']
4
+ __all__ = ['Placements', 'empty', 'get_db', 'find_var', 'find_dialog_id', 'find_msgs', 'find_msg_id', 'read_msg_ids', 'msg_idx',
5
+ 'read_msg', 'del_msg', 'add_msg', 'update_msg', 'load_gist', 'gist_file', 'import_string', 'is_usable_tool',
6
+ 'mk_toollist', 'import_gist', 'export_dialog', 'import_dialog', 'tool_info', 'asdict']
7
7
 
8
8
  # %% ../nbs/00_core.ipynb
9
- import inspect, json, importlib, linecache
9
+ import json, importlib, linecache
10
10
  from typing import Dict
11
11
  from tempfile import TemporaryDirectory
12
12
  from ipykernel_helper import *
@@ -17,6 +17,10 @@ from fastcore.meta import delegates
17
17
  from ghapi.all import *
18
18
  from fastlite import *
19
19
  from fastcore.xtras import asdict
20
+ from inspect import currentframe,Parameter,signature
21
+ from httpx import get as xget, post as xpost
22
+ from .core import __all__ as _all
23
+ from IPython.display import display,Markdown
20
24
 
21
25
  # %% ../nbs/00_core.ipynb
22
26
  _all_ = ["asdict"]
@@ -35,7 +39,7 @@ def get_db(ns:dict=None):
35
39
  # %% ../nbs/00_core.ipynb
36
40
  def find_var(var:str):
37
41
  "Search for var in all frames of the call stack"
38
- frame = inspect.currentframe()
42
+ frame = currentframe()
39
43
  while frame:
40
44
  dv = frame.f_globals.get(var, frame.f_locals.get(var, None))
41
45
  if dv: return dv
@@ -92,7 +96,14 @@ def read_msg(n:int=-1, # Message index (if relative, +ve is downwards)
92
96
  if not 0<=idx<len(ids): return None
93
97
  else: idx = n
94
98
  db = get_db()
95
- return db.t.message.fetchone('sid=?', [ids[idx]])
99
+ return db.t.message.selectone('sid=?', [ids[idx]])
100
+
101
+ # %% ../nbs/00_core.ipynb
102
+ def del_msg(
103
+ sid:str=None, # sid (stable id -- pk) of message that placement is relative to (if None, uses current message)
104
+ ):
105
+ "Delete a message from the dialog. Be sure to pass a `sid`, not a `mid`."
106
+ xpost('http://localhost:5001/rm_msg_', data=dict(msid=sid)).raise_for_status()
96
107
 
97
108
  # %% ../nbs/00_core.ipynb
98
109
  def _msg(
@@ -118,13 +129,25 @@ def add_msg(
118
129
  **kwargs
119
130
  ):
120
131
  "Add/update a message to the queue to show after code execution completes. Be sure to pass a `sid` (stable id) not a `mid` (which is used only for sorting, and can change)."
121
- assert msg_type in ('note', 'code', 'prompt'), "msg_type must be 'code', 'note', or 'prompt'."
122
- assert msg_type not in ('note') or not output, "'note' messages cannot have an output."
123
- run_cmd('add_msg', content=content, msg_type=msg_type, output=output, placement=placement, sid=sid, **kwargs)
132
+ if msg_type not in ('note', 'code', 'prompt'): return "msg_type must be 'code', 'note', or 'prompt'."
133
+ if msg_type=='note' and output: return "note messages cannot have an output."
134
+ if not sid: sid = find_msg_id()
135
+ data = dict(content=content, msg_type=msg_type, output=output, placement=placement, sid=sid, **kwargs)
136
+ return xpost('http://localhost:5001/add_relative_', data=data).text
137
+
138
+ # %% ../nbs/00_core.ipynb
139
+ @delegates(add_msg)
140
+ def _add_msg_unsafe(
141
+ content:str, # Content of the message (i.e the message prompt, code, or note text)
142
+ run:bool=False, # For prompts, send it to the AI; for code, execute it (*DANGEROUS -- be careful of what you run!)
143
+ **kwargs
144
+ ):
145
+ """Add/update a message to the queue to show after code execution completes, and optionally run it. Be sure to pass a `sid` (stable id) not a `mid` (which is used only for sorting, and can change).
146
+ *WARNING*--This can execute arbitrary code, so check carefully what you run!--*WARNING"""
147
+ add_msg(content=content, run=run, **kwargs)
124
148
 
125
149
  # %% ../nbs/00_core.ipynb
126
150
  def _umsg(
127
- content:str|None = None, # Content of the message (i.e the message prompt, code, or note text)
128
151
  msg_type: str|None = None, # Message type, can be 'code', 'note', or 'prompt'
129
152
  output:str|None = None, # For prompts/code, the output
130
153
  time_run: str | None = None, # When was message executed
@@ -139,8 +162,9 @@ def _umsg(
139
162
  # %% ../nbs/00_core.ipynb
140
163
  @delegates(_umsg)
141
164
  def update_msg(
142
- msg:Optional[Dict]=None, # Dictionary of field keys/values to update
143
165
  sid:str=None, # sid (stable id -- pk) of message to update (if None, uses current message)
166
+ content:str|None = None, # Content of the message (i.e the message prompt, code, or note text)
167
+ msg:Optional[Dict]=None, # Dictionary of field keys/values to update
144
168
  **kwargs):
145
169
  """Update an existing message. Provide either `msg` OR field key/values to update.
146
170
  Use `content` param to update contents. Be sure to pass a `sid` (stable id -- the pk) not a `mid`
@@ -150,14 +174,7 @@ def update_msg(
150
174
  sid = kw.pop('sid', sid)
151
175
  if not sid: raise TypeError("update_msg needs either a dict message or `sid=...`")
152
176
  kw.pop('did', None)
153
- run_cmd('add_msg', placement='update', sid=sid, **kw)
154
-
155
- # %% ../nbs/00_core.ipynb
156
- def add_html(
157
- html:str, # HTML to add to the DOM
158
- ):
159
- "Dynamically add HTML to the current web page. Supports HTMX attrs too."
160
- run_cmd('add_ft', html=html)
177
+ add_msg(content, placement='update', sid=sid, **kw)
161
178
 
162
179
  # %% ../nbs/00_core.ipynb
163
180
  def load_gist(gist_id:str):
@@ -189,17 +206,40 @@ def import_string(
189
206
  spec.loader.exec_module(module)
190
207
  return module
191
208
 
209
+ # %% ../nbs/00_core.ipynb
210
+ empty = Parameter.empty
211
+
212
+ def is_usable_tool(func:callable):
213
+ "True if the function has a docstring and all parameters have types, meaning that it can be used as an LLM tool."
214
+ if not func.__doc__ or not callable(func): return False
215
+ return all(p.annotation != empty for p in signature(func).parameters.values())
216
+
217
+ # %% ../nbs/00_core.ipynb
218
+ def mk_toollist(syms):
219
+ return "\n".join(f"- &`{sym.__name__}`: {sym.__doc__}" for sym in syms if is_usable_tool(sym))
220
+
192
221
  # %% ../nbs/00_core.ipynb
193
222
  def import_gist(
194
223
  gist_id:str, # user/id or just id of gist to import as a module
195
224
  mod_name:str=None, # module name to create (taken from gist filename if not passed)
196
- add_global:bool=True # add module to caller's globals?
225
+ add_global:bool=True, # add module to caller's globals?
226
+ import_wildcard:bool=False, # import all exported symbols to caller's globals
227
+ create_msg:bool=False # Add a message that lists usable tools
197
228
  ):
198
229
  "Import gist directly from string without saving to disk"
199
230
  fil = gist_file(gist_id)
200
231
  mod_name = mod_name or Path(fil['filename']).stem
201
232
  module = import_string(fil['content'], mod_name)
202
- if add_global: inspect.currentframe().f_back.f_globals[mod_name] = module
233
+ glbs = currentframe().f_back.f_globals
234
+ if add_global: glbs[mod_name] = module
235
+ syms = getattr(module, '__all__', None)
236
+ if syms is None: syms = [o for o in dir(module) if not o.startswith('_')]
237
+ syms = [getattr(module, nm) for nm in syms]
238
+ if import_wildcard:
239
+ for sym in syms: glbs[sym.__name__] = sym
240
+ if create_msg:
241
+ pref = getattr(module, '__doc__', "Tools added to dialog:")
242
+ add_msg(f"{pref}\n\n{mk_toollist(syms)}")
203
243
  return module
204
244
 
205
245
  # %% ../nbs/00_core.ipynb
@@ -227,3 +267,16 @@ def import_dialog(fname, add_header=True):
227
267
  add_msg(msg.get('content',''), msg.get('msg_type','note'), msg.get('output',''), 'at_end', **opts)
228
268
  if add_header: add_msg(f"# Imported Dialog `{fname}`", 'note', placement='at_end')
229
269
  return f"Imported {len(data['messages'])} messages"
270
+
271
+ # %% ../nbs/00_core.ipynb
272
+ def tool_info():
273
+ cts='''Tools available from `dialoghelper`:
274
+
275
+ - &`find_dialog_id`: Get the current dialog id.
276
+ - &`find_msg_id`: Get the current message id.
277
+ - &`find_msgs`: Find messages in current specific dialog that contain the given information.
278
+ - &`read_msg`: Get the message indexed in the current dialog.
279
+ - &`del_msg`: Delete a message from the dialog.
280
+ - &`add_msg`: Add/update a message to the queue to show after code execution completes.
281
+ - &`update_msg`: Update an existing message.'''
282
+ add_msg(cts)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: dialoghelper
3
- Version: 0.0.11
3
+ Version: 0.0.13
4
4
  Summary: Helper functions for solveit dialogs
5
5
  Home-page: https://github.com/AnswerDotAI/dialoghelper
6
6
  Author: Jeremy Howard
@@ -0,0 +1,10 @@
1
+ dialoghelper/__init__.py,sha256=7byX0N8qjtMnanYWGgpBm__8aWTi6sy6Thzt4kYvjaQ,43
2
+ dialoghelper/_modidx.py,sha256=BmhVoIv53snN6JQm_EzJp-bu793QuY7EZg2hKaq5_7E,3013
3
+ dialoghelper/core.py,sha256=IOnjRTU8oaJPpzt9sOBSmYGumZuZT0j1QOmBhql9kJY,12205
4
+ dialoghelper/db_dc.py,sha256=ieSie_K1HHXACEJVZ0rMkD_uo42GgmMqqKDYrodjhtM,2297
5
+ dialoghelper-0.0.13.dist-info/licenses/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
6
+ dialoghelper-0.0.13.dist-info/METADATA,sha256=GwWpTw4e3hkbKus7m_Ks-5P4YfrwGLvo-t1vo_rmjjo,2558
7
+ dialoghelper-0.0.13.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
8
+ dialoghelper-0.0.13.dist-info/entry_points.txt,sha256=wvDeX-XTS_XVjWiiPQe6yVfmyNwy9eCr36ewp9baFIg,46
9
+ dialoghelper-0.0.13.dist-info/top_level.txt,sha256=VXLlkgltFs_q-XB9imt2G64I_-MPm1RnxlpvUWPuLKM,13
10
+ dialoghelper-0.0.13.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (80.8.0)
2
+ Generator: setuptools (80.9.0)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5
 
@@ -1,10 +0,0 @@
1
- dialoghelper/__init__.py,sha256=pot8RlG0U4OhR3W-uFE3FF_SkXBM1i-NXq4JI4v6zdU,43
2
- dialoghelper/_modidx.py,sha256=fZ9-eTMaLdG-nvxxTe9_qjLwKIGS9_FU7KpFyCiF7DU,2529
3
- dialoghelper/core.py,sha256=515ndWh4b2vNEm1H6l5DBt4GWcqk3ILVo9zk8cEr3ig,9552
4
- dialoghelper/db_dc.py,sha256=ieSie_K1HHXACEJVZ0rMkD_uo42GgmMqqKDYrodjhtM,2297
5
- dialoghelper-0.0.11.dist-info/licenses/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
6
- dialoghelper-0.0.11.dist-info/METADATA,sha256=PY8bUCy8IiRCCmkHM_bW0hTtSdsbtZvMnoBxkBTnR0o,2558
7
- dialoghelper-0.0.11.dist-info/WHEEL,sha256=zaaOINJESkSfm_4HQVc5ssNzHCPXhJm0kEUakpsEHaU,91
8
- dialoghelper-0.0.11.dist-info/entry_points.txt,sha256=wvDeX-XTS_XVjWiiPQe6yVfmyNwy9eCr36ewp9baFIg,46
9
- dialoghelper-0.0.11.dist-info/top_level.txt,sha256=VXLlkgltFs_q-XB9imt2G64I_-MPm1RnxlpvUWPuLKM,13
10
- dialoghelper-0.0.11.dist-info/RECORD,,