Skip to content

Commit

Permalink
Merge remote-tracking branch 'github-desktop-hudson-ai/ir_refactor' i…
Browse files Browse the repository at this point in the history
…nto pr/1122
  • Loading branch information
nopdive committed Feb 21, 2025
2 parents 750d5fe + 631a0be commit 82c2e97
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 22 deletions.
18 changes: 16 additions & 2 deletions guidance/library/_role.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
from contextlib import AbstractContextManager
from ..models._base import role
from contextlib import AbstractContextManager, contextmanager
from typing import Iterator

from ..experimental.ast import RoleStart
from ..models._base._model import _active_role


# TODO HN: Add a docstring to better describe arbitrary role functions
@contextmanager
def role(role: str) -> Iterator[None]:
role_start = RoleStart(role)
token = _active_role.set(role_start)
try:
yield
finally:
_active_role.reset(token)


def system() -> AbstractContextManager[None]:
Expand Down
1 change: 0 additions & 1 deletion guidance/models/_base/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from ._model import Model
from ._role import role
from ._state import BaseState, Message

__all__ = [
Expand Down
3 changes: 2 additions & 1 deletion guidance/models/_base/_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import re
from abc import ABC, abstractmethod
from base64 import b64encode
from contextvars import ContextVar
from copy import deepcopy
from typing import TYPE_CHECKING, Any, Generic, Iterator, Optional, TypeVar, Union

Expand All @@ -22,9 +23,9 @@
TraceNode,
)
from ...visual import TraceMessage
from ._role import _active_role
from ._state import BaseState

_active_role: ContextVar[Optional["RoleStart"]] = ContextVar("active_role", default=None)
_id_counter: int = 0


Expand Down
18 changes: 0 additions & 18 deletions guidance/models/_base/_role.py
Original file line number Diff line number Diff line change
@@ -1,18 +0,0 @@
from contextlib import contextmanager
from contextvars import ContextVar
from typing import Iterator, Optional

from ...experimental.ast import RoleStart

_active_role: ContextVar[Optional["RoleStart"]] = ContextVar("active_role", default=None)


# TODO HN: Add a docstring to better describe arbitrary role functions
@contextmanager
def role(role: str) -> Iterator[None]:
role_start = RoleStart(role)
token = _active_role.set(role_start)
try:
yield
finally:
_active_role.reset(token)

0 comments on commit 82c2e97

Please sign in to comment.