-
Notifications
You must be signed in to change notification settings - Fork 203
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
299 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
""" | ||
Utility functions for working with max_tokens hints | ||
as produced by LMQL constraint operations. | ||
""" | ||
from typing import List, Dict | ||
|
||
TokenHint = Dict[str, int] | ||
|
||
def dict_min_token_hint(hints: List[TokenHint]) -> TokenHint: | ||
""" | ||
Takes the element-wise minimum of the given token hints. | ||
""" | ||
if len(hints) == 0: | ||
return {} | ||
elif len(hints) == 1: | ||
return hints[0] | ||
else: | ||
merged = {} | ||
for h in hints: | ||
for k,v in h.items(): | ||
if v != 0: | ||
merged[k] = min(h.get(k, v), v) | ||
return merged | ||
|
||
def concrete_hints(hints: dict): | ||
""" | ||
Returns the concrete hints from the given list of token hints. | ||
""" | ||
return {k:v for k,v in hints.items() if v != 0} | ||
|
||
def most_restrictive_hint(hints: List[int]): | ||
""" | ||
Takes the element-wise minimum of the given token hints. | ||
""" | ||
concrete = [h for h in hints if h != 0] | ||
if len(concrete) == 0: | ||
return 0 | ||
else: | ||
return min(concrete) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.