Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run black on codebase #765

Merged
merged 9 commits into from
Apr 17, 2024
5 changes: 5 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# .git-blame-ignore-revs
# Ran black on major files to standardize codebase
57da386795bc94a34275b333da586f171f96d7c8
# Ran black on tests and other ancillary python files in code
083fb9877b507ed27136441c683ce051edf37e81
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ node_modules
/client
.eggs/
.env
.DS_Store
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Someone has a Mac....


# Ignore native library built by setup
guidance/*.so
Expand Down
23 changes: 18 additions & 5 deletions guidance/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,29 @@
from . import models
from ._guidance import _decorator, guidance

from ._grammar import (Placeholder, RawFunction, GrammarFunction,
Terminal, replace_grammar_node, string)
from ._grammar import (
Placeholder,
RawFunction,
GrammarFunction,
Terminal,
replace_grammar_node,
string,
)
from ._utils import strip_multiline_string_indents
from ._server import Server


# This makes the guidance module callable
class _Guidance(types.ModuleType):
def __call__(self, f=None, *, stateless=False, cache=None, dedent=True, model=models.Model):
return _decorator(f, stateless=stateless, cache=cache, dedent=dedent, model=model)
def __call__(
self, f=None, *, stateless=False, cache=None, dedent=True, model=models.Model
):
return _decorator(
f, stateless=stateless, cache=cache, dedent=dedent, model=model
)


sys.modules[__name__].__class__ = _Guidance

# we expose all the library functions at the top level of the module
from .library import *
from .library import *
2 changes: 1 addition & 1 deletion guidance/_cpp/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .byte_trie import ByteTrie
from .byte_trie import ByteTrie
18 changes: 9 additions & 9 deletions guidance/_cpp/byte_trie.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@


class ByteTrie:
"""A python implementation mirroring the C++ ByteTrie class."""

def __init__(self, byte_strings=None, values=None, parent=None):
self._parent = parent
self.match_version = -1
Expand All @@ -16,44 +15,45 @@ def __init__(self, byte_strings=None, values=None, parent=None):
for s in byte_strings:
self.insert(s, 0)
else:
for i,s in enumerate(byte_strings):
for i, s in enumerate(byte_strings):
self.insert(s, values[i])

def keys(self):
return self.children.keys()

def has_child(self, byte):
return byte in self.children

def child(self, byte):
return self.children[byte]

def parent(self):
return self._parent

def size(self):
return len(self.children)

def __len__(self):
return self.size()

def insert(self, s, value, pos=0):
if len(s) <= pos:
if self.value < 0:
self.value = value
else:
first_byte = s[pos:pos+1]
first_byte = s[pos : pos + 1]
if first_byte not in self.children:
self.children[first_byte] = ByteTrie(parent=self)
self.children[first_byte].insert(s, value, pos + 1)

def compute_probs(self, probs):
self.prob = 0.0

if self.value != -1:
self.prob += probs[self.value]

if self.children:
for k in self.children:
child = self.children[k]
child.compute_probs(probs)
self.prob += child.prob
self.prob += child.prob
Loading
Loading