Skip to content

armgit / dspy

Public Imported from GitHub
0

DSPy: The framework for programming—not prompting—language models

Python

feat: allow client to disable dspy write to history when running dspy in prod (#7623)

Hung Phanc6f1da8cea43
diff --git a/dspy/clients/base_lm.py b/dspy/clients/base_lm.py
index d71d384b6..1cb97575e 100644
--- a/dspy/clients/base_lm.py
+++ b/dspy/clients/base_lm.py
@@ -1,5 +1,7 @@
from abc import ABC, abstractmethod
+import dspy
+
GLOBAL_HISTORY = []
class BaseLM(ABC):
@@ -18,6 +20,9 @@ class BaseLM(ABC):
_inspect_history(self.history, n)
def update_global_history(self, entry):
+ if dspy.settings.disable_history:
+ return
+
GLOBAL_HISTORY.append(entry)
@@ -73,4 +78,4 @@ def _inspect_history(history, n: int = 1):
def inspect_history(n: int = 1):
"""The global history shared across all LMs."""
- return _inspect_history(GLOBAL_HISTORY, n)
\ No newline at end of file
+ return _inspect_history(GLOBAL_HISTORY, n)
diff --git a/dspy/clients/lm.py b/dspy/clients/lm.py
index 44d0ae065..a315458aa 100644
--- a/dspy/clients/lm.py
+++ b/dspy/clients/lm.py
@@ -126,6 +126,9 @@ class LM(BaseLM):
else:
outputs = [c.message.content if hasattr(c, "message") else c["text"] for c in response["choices"]]
+ if dspy.settings.disable_history:
+ return outputs
+
# Logging, with removed api key & where `cost` is None on cache hit.
kwargs = {k: v for k, v in kwargs.items() if not k.startswith("api_")}
entry = dict(prompt=prompt, messages=messages, kwargs=kwargs, response=response)
diff --git a/dspy/dsp/utils/settings.py b/dspy/dsp/utils/settings.py
index 2f3b619ae..98d96d6ac 100644
--- a/dspy/dsp/utils/settings.py
+++ b/dspy/dsp/utils/settings.py
@@ -19,6 +19,7 @@ DEFAULT_CONFIG = dotdict(
callbacks=[],
async_max_workers=8,
send_stream=None,
+ disable_history=False,
)
# Global base configuration and owner tracking