DSPy: The framework for programming—not prompting—language models
Python
Making the DSP context window thread safe
Thomas Joshi01f88bc2809d
diff --git a/dsp/utils/settings.py b/dsp/utils/settings.pyindex 5457cf0cb..7c0362d4b 100644--- a/dsp/utils/settings.py+++ b/dsp/utils/settings.py@@ -1,29 +1,27 @@+from typing import Callable, Optionalfrom contextlib import contextmanagerfrom dsp.utils.utils import dotdict+import threadingclass Settings(object):"""DSP configuration settings."""-_instance = None- branch_idx: int = 0def __new__(cls):"""Singleton Pattern. See https://python-patterns.guide/gang-of-four/singleton/"""-if cls._instance is None:cls._instance = super().__new__(cls)- cls._instance.stack = []+ cls._instance.main_stack = []+ cls._instance.stack_by_thread = {}+ cls._instance.stack_by_thread[threading.get_ident()] = cls._instance.main_stack- # TODO: remove first-class support for re-ranker and potentially combine with RM to form a pipeline of sorts- # eg: RetrieveThenRerankPipeline(RetrievalModel, Reranker)- # downstream operations like dsp.retrieve would use configs from the defined pipeline.config = dotdict(lm=None,rm=None,- reranker=None,+ branch_idx=0,compiled_lm=None,force_reuse_cached_compilation=False,compiling=False,@@ -34,7 +32,7 @@ class Settings(object):@propertydef config(self):- return self.stack[-1]+ return self.stack_by_thread[threading.get_ident()][-1]def __getattr__(self, name):if hasattr(self.config, name):@@ -46,10 +44,10 @@ class Settings(object):super().__getattr__(name)def __append(self, config):- self.stack.append(config)+ self.stack_by_thread[threading.get_ident()].append(config)def __pop(self):- self.stack.pop()+ self.stack_by_thread[threading.get_ident()].pop()def configure(self, inherit_config: bool = True, **kwargs):"""Set configuration settings.@@ -71,10 +69,9 @@ class Settings(object):try:yieldfinally:- self.__pop()+ self.__pop()def __repr__(self) -> str:return repr(self.config)--settings = Settings()+settings = Settings()\ No newline at end of file