dspy
dspy copied to clipboard
TypeError: cannot pickle 'module' object - teleprompter.compile
likely a user error but i think I have followed the documentation. Thus, I am not sure why it is giving me this error message.
def list_parser(x:str) -> list:
"""Takes a comma-separated list as a string and returns a list of words"""
if not isinstance(x, str):
return []
return x.replace("[", "").replace("]","").replace("'", "").replace('"',"").replace(" ", "").split(",")
def precision(example:dspy.Example, prediction:dspy.Prediction, trace=None)->float:
"""Computes the precision based on 2 comma-separated lists (as str)"""
# Transform to list of words
example = list_parser(example.ct_ids)
prediction = list_parser(prediction.ct_ids)
# list -> set
example = set(example)
prediction = set(prediction)
# compute prediction precision. Proportion of words in prediction appear in the example list
score = len(prediction.intersection(example))/len(prediction)
print(f"Precision : {score}")
return score
class ReActPipeline(dspy.Module):
def __init__(self):
super().__init__()
self.signature = PatientEligibility
self.predictor = dspy.ReAct(self.signature, tools=tools, max_iters=3)
def forward(self, patient_note):
return self.predictor(patient_note=patient_note)
# react_module = dspy.ReAct(PatientEligibility, tools=tools, max_iters=3)
#---- Load the LLM
lm = dspy.HFClientVLLM(model=args.vllm, port=args.port, url=args.host, max_tokens=1_000, timeout_s=2_000,
stop=['\n\n', '<|eot_id|>'],
# model_type='chat',
)
dspy.settings.configure(lm=lm, temperature=0.3)
# Create input and output examples
trainset = []
for i, row in training.iterrows():
trainset.append(dspy.Example(patient_note=row["patient_note"], ct_ids=row["2"]).with_inputs("patient_note"))
devset = []
for i, row in evaluation.iterrows():
devset.append(dspy.Example(patient_note=row["patient_note"], ct_ids=row["2"]).with_inputs("patient_note"))
#---- Evaluation
evaluate_program = Evaluate(devset=devset, metric=precision, num_threads=2, display_progress=True, display_table=5)
print("---- Evaluation starting ReAct pipeline ----")
evaluate_program(ReActPipeline())
#---- Training
config = dict(max_bootstrapped_demos=3, max_labeled_demos=3, num_candidate_programs=10, num_threads=4)
teleprompter = BootstrapFewShotWithRandomSearch(metric=precision, **config)
optimized_program = teleprompter.compile(ReActPipeline(), trainset=trainset, valset=devset)
optimized_program.save(f"./models/trialGPT.React{args.method}.json")
Traceback (most recent call last):
File "/lustre/scratch/scratch/rmhijpo/ctgov_rag/./src/rag/ReAct.trialgpt.py", line 259, in <module>
main(args)
File "/lustre/scratch/scratch/rmhijpo/ctgov_rag/./src/rag/ReAct.trialgpt.py", line 178, in main
optimized_program = teleprompter.compile(ReActPipeline(), trainset=trainset, valset=devset)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/lustre/scratch/scratch/rmhijpo/ctgov_rag/.venv/lib/python3.11/site-packages/dspy/teleprompt/random_search.py", line 79, in compile
program2 = student.reset_copy()
^^^^^^^^^^^^^^^^^^^^
File "/lustre/scratch/scratch/rmhijpo/ctgov_rag/.venv/lib/python3.11/site-packages/dspy/primitives/module.py", line 63, in reset_copy
obj = copy.deepcopy(self)
^^^^^^^^^^^^^^^^^^^
File "/shared/ucl/apps/python/3.11.3/gnu-4.9.2/lib/python3.11/copy.py", line 172, in deepcopy
y = _reconstruct(x, memo, *rv)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/shared/ucl/apps/python/3.11.3/gnu-4.9.2/lib/python3.11/copy.py", line 271, in _reconstruct
state = deepcopy(state, memo)
^^^^^^^^^^^^^^^^^^^^^
File "/shared/ucl/apps/python/3.11.3/gnu-4.9.2/lib/python3.11/copy.py", line 146, in deepcopy
y = copier(x, memo)
^^^^^^^^^^^^^^^
File "/shared/ucl/apps/python/3.11.3/gnu-4.9.2/lib/python3.11/copy.py", line 231, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
^^^^^^^^^^^^^^^^^^^^^
File "/shared/ucl/apps/python/3.11.3/gnu-4.9.2/lib/python3.11/copy.py", line 172, in deepcopy
y = _reconstruct(x, memo, *rv)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/shared/ucl/apps/python/3.11.3/gnu-4.9.2/lib/python3.11/copy.py", line 271, in _reconstruct
state = deepcopy(state, memo)
^^^^^^^^^^^^^^^^^^^^^
File "/shared/ucl/apps/python/3.11.3/gnu-4.9.2/lib/python3.11/copy.py", line 146, in deepcopy
y = copier(x, memo)
^^^^^^^^^^^^^^^
File "/shared/ucl/apps/python/3.11.3/gnu-4.9.2/lib/python3.11/copy.py", line 231, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
^^^^^^^^^^^^^^^^^^^^^
File "/shared/ucl/apps/python/3.11.3/gnu-4.9.2/lib/python3.11/copy.py", line 146, in deepcopy
y = copier(x, memo)
^^^^^^^^^^^^^^^
File "/shared/ucl/apps/python/3.11.3/gnu-4.9.2/lib/python3.11/copy.py", line 231, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
^^^^^^^^^^^^^^^^^^^^^
File "/shared/ucl/apps/python/3.11.3/gnu-4.9.2/lib/python3.11/copy.py", line 172, in deepcopy
y = _reconstruct(x, memo, *rv)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/shared/ucl/apps/python/3.11.3/gnu-4.9.2/lib/python3.11/copy.py", line 271, in _reconstruct
state = deepcopy(state, memo)
^^^^^^^^^^^^^^^^^^^^^
File "/shared/ucl/apps/python/3.11.3/gnu-4.9.2/lib/python3.11/copy.py", line 146, in deepcopy
y = copier(x, memo)
^^^^^^^^^^^^^^^
File "/shared/ucl/apps/python/3.11.3/gnu-4.9.2/lib/python3.11/copy.py", line 231, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
^^^^^^^^^^^^^^^^^^^^^
File "/shared/ucl/apps/python/3.11.3/gnu-4.9.2/lib/python3.11/copy.py", line 172, in deepcopy
y = _reconstruct(x, memo, *rv)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/shared/ucl/apps/python/3.11.3/gnu-4.9.2/lib/python3.11/copy.py", line 271, in _reconstruct
state = deepcopy(state, memo)
^^^^^^^^^^^^^^^^^^^^^
File "/shared/ucl/apps/python/3.11.3/gnu-4.9.2/lib/python3.11/copy.py", line 146, in deepcopy
y = copier(x, memo)
^^^^^^^^^^^^^^^
File "/shared/ucl/apps/python/3.11.3/gnu-4.9.2/lib/python3.11/copy.py", line 231, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
^^^^^^^^^^^^^^^^^^^^^
File "/shared/ucl/apps/python/3.11.3/gnu-4.9.2/lib/python3.11/copy.py", line 172, in deepcopy
y = _reconstruct(x, memo, *rv)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/shared/ucl/apps/python/3.11.3/gnu-4.9.2/lib/python3.11/copy.py", line 271, in _reconstruct
state = deepcopy(state, memo)
^^^^^^^^^^^^^^^^^^^^^
File "/shared/ucl/apps/python/3.11.3/gnu-4.9.2/lib/python3.11/copy.py", line 146, in deepcopy
y = copier(x, memo)
^^^^^^^^^^^^^^^
File "/shared/ucl/apps/python/3.11.3/gnu-4.9.2/lib/python3.11/copy.py", line 231, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
^^^^^^^^^^^^^^^^^^^^^
File "/shared/ucl/apps/python/3.11.3/gnu-4.9.2/lib/python3.11/copy.py", line 172, in deepcopy
y = _reconstruct(x, memo, *rv)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/shared/ucl/apps/python/3.11.3/gnu-4.9.2/lib/python3.11/copy.py", line 271, in _reconstruct
state = deepcopy(state, memo)
^^^^^^^^^^^^^^^^^^^^^
File "/shared/ucl/apps/python/3.11.3/gnu-4.9.2/lib/python3.11/copy.py", line 146, in deepcopy
y = copier(x, memo)
^^^^^^^^^^^^^^^
File "/shared/ucl/apps/python/3.11.3/gnu-4.9.2/lib/python3.11/copy.py", line 231, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
^^^^^^^^^^^^^^^^^^^^^
File "/shared/ucl/apps/python/3.11.3/gnu-4.9.2/lib/python3.11/copy.py", line 172, in deepcopy
y = _reconstruct(x, memo, *rv)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/shared/ucl/apps/python/3.11.3/gnu-4.9.2/lib/python3.11/copy.py", line 271, in _reconstruct
state = deepcopy(state, memo)
^^^^^^^^^^^^^^^^^^^^^
File "/shared/ucl/apps/python/3.11.3/gnu-4.9.2/lib/python3.11/copy.py", line 146, in deepcopy
y = copier(x, memo)
^^^^^^^^^^^^^^^
File "/shared/ucl/apps/python/3.11.3/gnu-4.9.2/lib/python3.11/copy.py", line 231, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
^^^^^^^^^^^^^^^^^^^^^
File "/shared/ucl/apps/python/3.11.3/gnu-4.9.2/lib/python3.11/copy.py", line 172, in deepcopy
y = _reconstruct(x, memo, *rv)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/shared/ucl/apps/python/3.11.3/gnu-4.9.2/lib/python3.11/copy.py", line 271, in _reconstruct
state = deepcopy(state, memo)
^^^^^^^^^^^^^^^^^^^^^
File "/shared/ucl/apps/python/3.11.3/gnu-4.9.2/lib/python3.11/copy.py", line 146, in deepcopy
y = copier(x, memo)
^^^^^^^^^^^^^^^
File "/shared/ucl/apps/python/3.11.3/gnu-4.9.2/lib/python3.11/copy.py", line 231, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
^^^^^^^^^^^^^^^^^^^^^
File "/shared/ucl/apps/python/3.11.3/gnu-4.9.2/lib/python3.11/copy.py", line 172, in deepcopy
y = _reconstruct(x, memo, *rv)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/shared/ucl/apps/python/3.11.3/gnu-4.9.2/lib/python3.11/copy.py", line 271, in _reconstruct
state = deepcopy(state, memo)
^^^^^^^^^^^^^^^^^^^^^
File "/shared/ucl/apps/python/3.11.3/gnu-4.9.2/lib/python3.11/copy.py", line 146, in deepcopy
y = copier(x, memo)
^^^^^^^^^^^^^^^
File "/shared/ucl/apps/python/3.11.3/gnu-4.9.2/lib/python3.11/copy.py", line 231, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
^^^^^^^^^^^^^^^^^^^^^
File "/shared/ucl/apps/python/3.11.3/gnu-4.9.2/lib/python3.11/copy.py", line 172, in deepcopy
y = _reconstruct(x, memo, *rv)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/shared/ucl/apps/python/3.11.3/gnu-4.9.2/lib/python3.11/copy.py", line 271, in _reconstruct
state = deepcopy(state, memo)
^^^^^^^^^^^^^^^^^^^^^
File "/shared/ucl/apps/python/3.11.3/gnu-4.9.2/lib/python3.11/copy.py", line 146, in deepcopy
y = copier(x, memo)
^^^^^^^^^^^^^^^
File "/shared/ucl/apps/python/3.11.3/gnu-4.9.2/lib/python3.11/copy.py", line 231, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
^^^^^^^^^^^^^^^^^^^^^
File "/shared/ucl/apps/python/3.11.3/gnu-4.9.2/lib/python3.11/copy.py", line 161, in deepcopy
rv = reductor(4)
^^^^^^^^^^^
TypeError: cannot pickle 'module' object
Similar issue: https://github.com/stanfordnlp/dspy/issues/1011
ah that's unfortunate, we need to look into deepcopy for pydantic types?
What is PatientEligibility?
PatientEligibility is a signature
class PatientEligibility(dspy.Signature):
"Given a patient description, produce a list of 5 or less clinical trials ids where tha patient would be eligible for enrolment."
patient_note:str = dspy.InputField(prefix="Patient Note:", desc="description of the patient medical characteristics and conditions")
clinical_trial_ids_list:str = dspy.OutputField(prefix="Clinical Trials ids:", desc="a comma separated list of clinical trials e.g. NCT0001,NCT0002,NCT0003")