1
- import copy
2
1
import threading
3
-
4
2
from contextlib import contextmanager
3
+ from copy import deepcopy
4
+
5
5
from dsp .utils .utils import dotdict
6
- from functools import lru_cache
7
6
8
7
DEFAULT_CONFIG = dotdict (
9
8
lm = None ,
28
27
async_max_workers = 8 ,
29
28
)
30
29
31
- @lru_cache (maxsize = None )
32
- def warn_once (msg : str ):
33
- import logging
34
- logger = logging .getLogger (__name__ )
35
- logger .warning (msg )
36
-
37
30
38
31
class Settings :
39
32
"""DSP configuration settings."""
@@ -56,21 +49,18 @@ def __new__(cls):
56
49
# TODO: remove first-class support for re-ranker and potentially combine with RM to form a pipeline of sorts
57
50
# eg: RetrieveThenRerankPipeline(RetrievalModel, Reranker)
58
51
# downstream operations like dsp.retrieve would use configs from the defined pipeline.
59
- config = copy .deepcopy (DEFAULT_CONFIG )
60
- cls ._instance .__append (config )
52
+
53
+ # make a deepcopy of the default config to avoid modifying the default config
54
+ cls ._instance .__append (deepcopy (DEFAULT_CONFIG ))
61
55
62
56
return cls ._instance
63
57
64
58
@property
65
59
def config (self ):
66
60
thread_id = threading .get_ident ()
67
- # if thread_id not in self.stack_by_thread:
68
- # self.stack_by_thread[thread_id] = [self.main_stack[-1].copy()]
69
- try :
70
- return self .stack_by_thread [thread_id ][- 1 ]
71
- except Exception :
72
- warn_once ("Warning: You seem to be creating DSPy threads in an unsupported way." )
73
- return self .main_stack [- 1 ]
61
+ if thread_id not in self .stack_by_thread :
62
+ self .stack_by_thread [thread_id ] = [self .main_stack [- 1 ].copy ()]
63
+ return self .stack_by_thread [thread_id ][- 1 ]
74
64
75
65
def __getattr__ (self , name ):
76
66
if hasattr (self .config , name ):
@@ -83,16 +73,14 @@ def __getattr__(self, name):
83
73
84
74
def __append (self , config ):
85
75
thread_id = threading .get_ident ()
86
- # if thread_id not in self.stack_by_thread:
87
- # self.stack_by_thread[thread_id] = [self.main_stack[-1].copy()]
88
-
89
- assert thread_id in self .stack_by_thread , "Error: You seem to be creating DSPy threads in an unsupported way."
76
+ if thread_id not in self .stack_by_thread :
77
+ self .stack_by_thread [thread_id ] = [self .main_stack [- 1 ].copy ()]
90
78
self .stack_by_thread [thread_id ].append (config )
91
79
92
80
def __pop (self ):
93
81
thread_id = threading .get_ident ()
94
- # if thread_id in self.stack_by_thread:
95
- self .stack_by_thread [thread_id ].pop ()
82
+ if thread_id in self .stack_by_thread :
83
+ self .stack_by_thread [thread_id ].pop ()
96
84
97
85
def configure (self , inherit_config : bool = True , ** kwargs ):
98
86
"""Set configuration settings.
@@ -120,4 +108,4 @@ def __repr__(self) -> str:
120
108
return repr (self .config )
121
109
122
110
123
- settings = Settings ()
111
+ settings = Settings ()
0 commit comments