Skip to content

Commit 47b3339

Browse files
committed
Switch to using attribute instead of variable to track indentation
1 parent 4584bb2 commit 47b3339

File tree

5 files changed

+53
-5
lines changed

5 files changed

+53
-5
lines changed

guidance/library/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from ._format import monospace
1818
from ._silent import silent
1919
from ._set_var import set_var
20+
from ._set_attribute import set_attribute
2021
# from ..models._model import context_free
2122

2223
# stateless library functions

guidance/library/_role.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import guidance
22
from ._block import block
3-
from ._set_var import set_var
3+
from ._set_attribute import set_attribute
44

55
nodisp_start = "<||_#NODISP_||>"
66
nodisp_end = "<||_/NODISP_||>"
@@ -10,7 +10,7 @@
1010

1111
@guidance
1212
def role_opener(lm, role_name, **kwargs):
13-
indent = lm.get("__role_indent", True)
13+
indent = getattr(lm, "indent_roles", True)
1414
if not hasattr(lm, "get_role_start"):
1515
raise Exception(
1616
f"You need to use a chat model in order the use role blocks like `with {role_name}():`! Perhaps you meant to use the {type(lm).__name__}Chat class?"
@@ -39,7 +39,7 @@ def role_opener(lm, role_name, **kwargs):
3939

4040
@guidance
4141
def role_closer(lm, role_name, **kwargs):
42-
indent = lm.get("__role_indent", True)
42+
indent = getattr(lm, "indent_roles", True)
4343
# Start of either debug or HTML no disp block
4444
if indent:
4545
lm += nodisp_start
@@ -92,4 +92,4 @@ def instruction(text=None, **kwargs):
9292
return role("instruction", text, **kwargs)
9393

9494
def indent_roles(indent=True):
95-
return set_var("__role_indent", indent)
95+
return set_attribute("indent_roles", indent)

guidance/library/_set_attribute.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import guidance
2+
from ._block import block
3+
4+
@guidance
5+
def set_attr_opener(lm, name, value):
6+
if hasattr(lm, name):
7+
lm = lm.setattr("__save" + name, getattr(lm, name))
8+
return lm.setattr(name, value)
9+
10+
@guidance
11+
def set_attr_closer(lm, name):
12+
if hasattr(lm, "__save" + name):
13+
return lm.setattr(name, lm["__save" + name]).delattr("__save" + name)
14+
else:
15+
return lm.delattr(name)
16+
17+
def set_attribute(name, value=True):
18+
return block(
19+
opener=set_attr_opener(name, value),
20+
closer=set_attr_closer(name),
21+
)

guidance/models/_model.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,32 @@ def get(self, key, default=None):
380380
The value to return if the variable is not current set.
381381
'''
382382
return self._variables.get(key, default)
383+
384+
def setattr(self, key, value):
385+
'''Return a new model with the given model attribute set.
386+
387+
Parameters
388+
----------
389+
key : str
390+
The name of the attribute to be set.
391+
value : any
392+
The value to set the attribute to.
393+
'''
394+
copy = self.copy()
395+
setattr(copy, key, value)
396+
return copy
397+
398+
def delattr(self, key):
399+
'''Return a new model with the given attribute deleted.
400+
401+
Parameters
402+
----------
403+
key : str
404+
The attribute name to remove.
405+
'''
406+
copy = self.copy()
407+
delattr(copy, key)
408+
return copy
383409

384410
def set(self, key, value):
385411
'''Return a new model with the given variable value set.

notebooks/tutorials/adding_new_models.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
{
8282
"data": {
8383
"application/vnd.jupyter.widget-view+json": {
84-
"model_id": "5698dac461ba479ba684d12328f7947c",
84+
"model_id": "3e2eb7c444ba4d92a5f29593faa919e9",
8585
"version_major": 2,
8686
"version_minor": 0
8787
},

0 commit comments

Comments
 (0)