Skip to content

Commit

Permalink
Merge pull request #114 from bespokelabsai/fix-terminal-demo
Browse files Browse the repository at this point in the history
Terminal interactive usage of bespokelabs curator package
  • Loading branch information
CharlieJCJ authored Nov 15, 2024
2 parents 9345c20 + cee4938 commit 4a7ae7f
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/bespokelabs/curator/prompter/prompter.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,11 @@ def _completions(
metadata_db = MetadataDB(metadata_db_path)

# Get the source code of the prompt function
prompt_func_source = inspect.getsource(
prompt_func_source = _get_function_source(
self.prompt_formatter.prompt_func
)
if self.prompt_formatter.parse_func is not None:
parse_func_source = inspect.getsource(
parse_func_source = _get_function_source(
self.prompt_formatter.parse_func
)
else:
Expand Down Expand Up @@ -221,4 +221,18 @@ def _get_function_hash(func) -> str:
if func is None:
return xxh64("").hexdigest()

return xxh64(inspect.getsource(func)).hexdigest()
return xxh64(_get_function_source(func)).hexdigest()


def _get_function_source(func) -> str:
"""Get the source code of a function.
Purpose of this function is that during Python interpreter (REPL),
`inspect.getsource` will fail with an OSError because functions defined in the
interpreter don't have an associated source file. We have to use this wrapper
to gracefully handle this case.
"""
try:
return inspect.getsource(func)
except OSError:
return ""

0 comments on commit 4a7ae7f

Please sign in to comment.