Skip to content

armgit / dspy

Public Imported from GitHub
0

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

Python

Support dspy.configure(adapter=...) as well as Adapter.format returning a string prompt

Omar Khattab599d0293aed1
diff --git a/dsp/utils/settings.py b/dsp/utils/settings.py
index e7fe58cbd..df073c707 100644
--- a/dsp/utils/settings.py
+++ b/dsp/utils/settings.py
@@ -27,6 +27,7 @@ class Settings:
# downstream operations like dsp.retrieve would use configs from the defined pipeline.
config = dotdict(
lm=None,
+ adapter=None,
rm=None,
branch_idx=0,
reranker=None,
diff --git a/dspy/__init__.py b/dspy/__init__.py
index 9919fc7dc..46d2f8169 100644
--- a/dspy/__init__.py
+++ b/dspy/__init__.py
@@ -11,6 +11,7 @@ from .utils.logging import logger, set_log_output
from .functional import * # isort: skip
from dspy.evaluate import Evaluate # isort: skip
from dspy.clients import * # isort: skip
+from dspy.adapters import * # isort: skip
settings = dsp.settings
diff --git a/dspy/adapters/base.py b/dspy/adapters/base.py
index 5010a8c76..54bebeaac 100644
--- a/dspy/adapters/base.py
+++ b/dspy/adapters/base.py
@@ -1,7 +1,9 @@
class Adapter:
def __call__(self, lm, lm_kwargs, signature, demos, inputs):
- messages = self.format(signature, demos, inputs)
- outputs = lm(messages=messages, **lm_kwargs)
+ inputs = self.format(signature, demos, inputs)
+ inputs = dict(prompt=inputs) if isinstance(inputs, str) else dict(messages=inputs)
+
+ outputs = lm(**inputs, **lm_kwargs)
values = []
for output in outputs:
@@ -9,4 +11,4 @@ class Adapter:
assert set(value.keys()) == set(signature.output_fields.keys()), f"Expected {signature.output_fields.keys()} but got {value.keys()}"
values.append(value)
- return values
\ No newline at end of file
+ return values
diff --git a/dspy/clients/lm.py b/dspy/clients/lm.py
index bdcdda7e6..9de69cb3c 100644
--- a/dspy/clients/lm.py
+++ b/dspy/clients/lm.py
@@ -3,12 +3,19 @@ import ujson
import functools
try:
- import litellm
+ import warnings
+ with warnings.catch_warnings():
+ warnings.filterwarnings("ignore", category=UserWarning)
+ import litellm
+
from litellm.caching import Cache
-
litellm.cache = Cache(disk_cache_dir=".dspy_cache", type="disk")
+
except ImportError:
- litellm = None
+ class LitellmPlaceholder:
+ def __getattr__(self, _): raise ImportError("The LiteLLM package is not installed. Run `pip install litellm`.")
+
+ litellm = LitellmPlaceholder()
class LM:
def __init__(self, model, model_type='chat', temperature=0.0, cache=True, **kwargs):
diff --git a/dspy/predict/predict.py b/dspy/predict/predict.py
index a7e5b7309..374a657ba 100644
--- a/dspy/predict/predict.py
+++ b/dspy/predict/predict.py
@@ -210,9 +210,9 @@ def new_generate(lm, signature, example, max_depth=6, **kwargs):
def v2_5_generate(lm, lm_kwargs, signature, demos, inputs):
- from dspy.adapters import ChatAdapter
+ import dspy
+ adapter = dspy.settings.adapter or dspy.ChatAdapter()
- adapter = ChatAdapter()
return adapter(lm, lm_kwargs=lm_kwargs, signature=signature, demos=demos, inputs=inputs)