Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions py/src/braintrust/functions/invoke.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,13 +211,20 @@ def init_function(project_name: str, slug: str, version: Optional[str] = None):
:return: A function that can be used as a task or scorer.
"""

def f(*args: Any, **kwargs: Any) -> Any:
if len(args) > 0:
# Task.
return invoke(project_name=project_name, slug=slug, version=version, input=args[0])
def f(input, hooks=None):
Copy link
Collaborator

Choose a reason for hiding this comment

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

i'm not sure if input / hooks are the only possible args, but if other things were passed in e.g.f(foo=123) this will be backwards incompatible.

# When used as a task, hooks may be provided with metadata
# When used as a scorer, all args come via kwargs in the input dict
if hooks is not None and not isinstance(hooks, str):
# Task mode with hooks object
metadata = hooks.metadata if hasattr(hooks, 'metadata') else None
return invoke(project_name=project_name, slug=slug, version=version, input=input, metadata=metadata)
elif isinstance(input, dict) and ('output' in input or 'expected' in input or 'metadata' in input):
# Scorer mode - input is a dict with scorer args
metadata = input.get('metadata')
return invoke(project_name=project_name, slug=slug, version=version, input=input, metadata=metadata)
else:
# Scorer.
return invoke(project_name=project_name, slug=slug, version=version, input=kwargs)
# Task mode without hooks (backward compatibility) or hooks is not actually hooks
return invoke(project_name=project_name, slug=slug, version=version, input=input)

f.__name__ = f"init_function-{project_name}-{slug}-{version or 'latest'}"
return f
Loading