|
| 1 | +""" |
| 2 | +Copyright (c) 2024 Scale3 Labs |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +""" |
| 16 | + |
| 17 | +from opentelemetry.instrumentation.instrumentor import BaseInstrumentor |
| 18 | +from opentelemetry.trace import get_tracer |
| 19 | +from wrapt import wrap_function_wrapper as _W |
| 20 | +from typing import Collection |
| 21 | +from importlib_metadata import version as v |
| 22 | +from .patch import patch_bootstrapfewshot_optimizer, patch_signature, patch_evaluate |
| 23 | + |
| 24 | + |
| 25 | +class DspyInstrumentor(BaseInstrumentor): |
| 26 | + """ |
| 27 | + The DspyInstrumentor class represents the DSPy instrumentation""" |
| 28 | + |
| 29 | + def instrumentation_dependencies(self) -> Collection[str]: |
| 30 | + return ["dspy >= 0.1.5"] |
| 31 | + |
| 32 | + def _instrument(self, **kwargs): |
| 33 | + tracer_provider = kwargs.get("tracer_provider") |
| 34 | + tracer = get_tracer(__name__, "", tracer_provider) |
| 35 | + version = v("dspy") |
| 36 | + _W( |
| 37 | + "dspy.teleprompt.bootstrap", |
| 38 | + "BootstrapFewShot.compile", |
| 39 | + patch_bootstrapfewshot_optimizer( |
| 40 | + "BootstrapFewShot.compile", version, tracer |
| 41 | + ), |
| 42 | + ) |
| 43 | + _W( |
| 44 | + "dspy.predict.predict", |
| 45 | + "Predict.forward", |
| 46 | + patch_signature("Predict.forward", version, tracer), |
| 47 | + ) |
| 48 | + _W( |
| 49 | + "dspy.predict.chain_of_thought", |
| 50 | + "ChainOfThought.forward", |
| 51 | + patch_signature("ChainOfThought.forward", version, tracer), |
| 52 | + ) |
| 53 | + _W( |
| 54 | + "dspy.predict.chain_of_thought_with_hint", |
| 55 | + "ChainOfThoughtWithHint.forward", |
| 56 | + patch_signature("ChainOfThoughtWithHint.forward", version, tracer), |
| 57 | + ) |
| 58 | + _W( |
| 59 | + "dspy.predict.react", |
| 60 | + "ReAct.forward", |
| 61 | + patch_signature("ReAct.forward", version, tracer), |
| 62 | + ) |
| 63 | + _W( |
| 64 | + "dspy.predict.program_of_thought", |
| 65 | + "ProgramOfThought.forward", |
| 66 | + patch_signature("ProgramOfThought.forward", version, tracer), |
| 67 | + ) |
| 68 | + _W( |
| 69 | + "dspy.predict.multi_chain_comparison", |
| 70 | + "MultiChainComparison.forward", |
| 71 | + patch_signature("MultiChainComparison.forward", version, tracer), |
| 72 | + ) |
| 73 | + _W( |
| 74 | + "dspy.predict.retry", |
| 75 | + "Retry.forward", |
| 76 | + patch_signature("Retry.forward", version, tracer), |
| 77 | + ) |
| 78 | + _W( |
| 79 | + "dspy.evaluate.evaluate", |
| 80 | + "Evaluate.__call__", |
| 81 | + patch_evaluate("Evaluate", version, tracer), |
| 82 | + ) |
| 83 | + |
| 84 | + def _uninstrument(self, **kwargs): |
| 85 | + pass |
0 commit comments