From 45003caaf2313e1bd308dcae3fe063121fecfa0e Mon Sep 17 00:00:00 2001 From: Charlie Cheng-Jie Ji Date: Fri, 15 Nov 2024 03:12:42 +0000 Subject: [PATCH 1/2] fix for terminal run demo --- src/bespokelabs/curator/prompter/prompter.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/bespokelabs/curator/prompter/prompter.py b/src/bespokelabs/curator/prompter/prompter.py index c84ae66a..a190ea8d 100644 --- a/src/bespokelabs/curator/prompter/prompter.py +++ b/src/bespokelabs/curator/prompter/prompter.py @@ -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: @@ -221,4 +221,12 @@ 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.""" + try: + return inspect.getsource(func) + except OSError: + return "" From cee49384d8a44d677aeb65662a244fc0a2afd025 Mon Sep 17 00:00:00 2001 From: Charlie Cheng-Jie Ji Date: Fri, 15 Nov 2024 03:42:54 +0000 Subject: [PATCH 2/2] add comments --- src/bespokelabs/curator/prompter/prompter.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/bespokelabs/curator/prompter/prompter.py b/src/bespokelabs/curator/prompter/prompter.py index a190ea8d..63710b28 100644 --- a/src/bespokelabs/curator/prompter/prompter.py +++ b/src/bespokelabs/curator/prompter/prompter.py @@ -225,7 +225,13 @@ def _get_function_hash(func) -> str: def _get_function_source(func) -> str: - """Get the source code of a function.""" + """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: