Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeldking committed Jan 23, 2024
1 parent 43f6e6c commit bf512a0
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
from typing import Collection
from importlib import import_module
from typing import Any, Callable, Collection, Mapping, Tuple

from openinference.instrumentation.dspy.package import _instruments
from openinference.instrumentation.dspy.version import __version__
from opentelemetry import trace as trace_api
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
from wrapt import wrap_function_wrapper

_DSPY_MODULE = "dspy"
_DSP_MODULE = "dsp"

class DSPyInstrumentor(BaseInstrumentor):
"""
OpenInference Instrumentor for DSPy
"""



def instrumentation_dependencies(self) -> Collection[str]:
return _instruments

Expand All @@ -22,9 +24,29 @@ def _instrument(self, **kwargs):
if not (tracer_provider := kwargs.get("tracer_provider")):
tracer_provider = trace_api.get_tracer_provider()
tracer = trace_api.get_tracer(__name__, __version__, tracer_provider)

dspy = import_module(_DSPY_MODULE)
from dsp.modules.lm import LM
language_model_classes = LM.__subclasses__()
for lm in language_model_classes:
wrap_function_wrapper(
module=_DSP_MODULE,
name=lm.__name__ + ".request",
wrapper=_LMRequest(),
)

def _uninstrument(self, **kwargs):
from dspy import DSPy

DSPy.request = self._original_request
dspy = import_module(_DSPY_MODULE)


class _LMRequest():
def __call__(
self,
wrapped: Callable[..., Any],
instance: Any,
args: Tuple[type, Any],
kwargs: Mapping[str, Any],
) -> None:
print(args)
print(kwargs)
response = wrapped(*args, **kwargs)
return response
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
_instruments = ("dspy-ai >= 2.1.0",)
_supports_metrics = False

0 comments on commit bf512a0

Please sign in to comment.