DSPy: The framework for programming—not prompting—language models
Python
Fix dspy code reading
Michael Ryand3394e7c0f67
diff --git a/dspy/teleprompt/utils.py b/dspy/teleprompt/utils.pyindex a4eef45a0..2d9ed0a78 100644--- a/dspy/teleprompt/utils.py+++ b/dspy/teleprompt/utils.py@@ -348,9 +348,37 @@ def create_n_fewshot_demo_sets(return demo_candidates-def new_getfile(object, _old_getfile=inspect.getfile):+def old_getfile(object):+ """Work out which source or compiled file an object was defined in."""+ if inspect.ismodule(object):+ if getattr(object, '__file__', None):+ return object.__file__+ raise TypeError('{!r} is a built-in module'.format(object))+ if inspect.isclass(object):+ if hasattr(object, '__module__'):+ module = sys.modules.get(object.__module__)+ if getattr(module, '__file__', None):+ return module.__file__+ if object.__module__ == '__main__':+ raise OSError('source code not available')+ raise TypeError('{!r} is a built-in class'.format(object))+ if inspect.ismethod(object):+ object = object.__func__+ if inspect.isfunction(object):+ object = object.__code__+ if inspect.istraceback(object):+ object = object.tb_frame+ if inspect.isframe(object):+ object = object.f_code+ if inspect.iscode(object):+ return object.co_filename+ raise TypeError('module, class, method, function, traceback, frame, or '+ 'code object was expected, got {}'.format(+ type(object).__name__))++def new_getfile(object):if not inspect.isclass(object):- return _old_getfile(object)+ return old_getfile(object)# Lookup by parent module (as in current inspect)if hasattr(object, '__module__'):@@ -363,19 +391,22 @@ def new_getfile(object, _old_getfile=inspect.getfile):if inspect.isfunction(member) and object.__qualname__ + '.' + member.__name__ == member.__qualname__:return inspect.getfile(member)raise TypeError(f'Source for {object!r} not found')-+inspect.getfile = new_getfiledef get_dspy_source_code(module):header = []base_code = ""- try:- base_code = inspect.getsource(type(module))- except TypeError:- obj = type(module)- cell_code = "".join(inspect.linecache.getlines(new_getfile(obj)))- class_code = extract_symbols(cell_code, obj.__name__)[0][0]- base_code = str(class_code)++ # Don't get source code for Predict or ChainOfThought modules (NOTE we will need to extend this list as more DSPy.modules are added)+ if not type(module).__name__ == "Predict" and not type(module).__name__ == "ChainOfThought":+ try:+ base_code = inspect.getsource(type(module))+ except TypeError:+ obj = type(module)+ cell_code = "".join(inspect.linecache.getlines(new_getfile(obj)))+ class_code = extract_symbols(cell_code, obj.__name__)[0][0]+ base_code = str(class_code)completed_set = set()for attribute in module.__dict__.keys():@@ -388,16 +419,18 @@ def get_dspy_source_code(module):if item in completed_set:continueif isinstance(item, Parameter):- if item.signature is not None and item.signature.__pydantic_parent_namespace__['signature_name'] + "_sig" not in completed_set:+ if hasattr(item, 'signature') and item.signature is not None and item.signature.__pydantic_parent_namespace__['signature_name'] + "_sig" not in completed_set:try:header.append(inspect.getsource(item.signature))+ print(inspect.getsource(item.signature))except (TypeError, OSError):header.append(str(item.signature))completed_set.add(item.signature.__pydantic_parent_namespace__['signature_name'] + "_sig")if isinstance(item, dspy.Module):- if type(item) not in completed_set:- header.append(get_dspy_source_code(item))- completed_set.add(type(item))+ code = get_dspy_source_code(item).strip()+ if code not in completed_set:+ header.append(code)+ completed_set.add(code)completed_set.add(item)return '\n\n'.join(header) + '\n\n' + base_code\ No newline at end of file