Add json_schema decorator

This commit is contained in:
Matt 2024-05-09 15:55:15 +01:00
parent ee288915d7
commit 156eed9024
2 changed files with 10 additions and 0 deletions

View File

@ -1817,6 +1817,11 @@ class PreTrainedTokenizerBase(SpecialTokensMixin, PushToHubMixin):
conversations = [conversation]
is_batched = False
# The add_json_schema decorator for tools adds a schema under the `json_schema` attribute. If we're passed
# decorated functions, let's extract the schema decoration now
if tools is not None:
tools = [tool.json_schema if hasattr(tool, "json_schema") else tool for tool in tools]
rendered = []
template_kwargs = {**self.special_tokens_map, **kwargs} # kwargs overwrite special tokens if both are present
for chat in conversations:

View File

@ -24,6 +24,11 @@ def get_json_schema(func):
return {"name": func.__name__, "description": main_doc, "parameters": json_schema}
def add_json_schema(func):
func.json_schema = get_json_schema(func)
return func
def _get_argument_descriptions_from_docstring(doc):
param_pattern = r":param (\w+): (.+)"
params = re.findall(param_pattern, doc)