-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: dspy instrumentation enhancements (#158)
- Loading branch information
1 parent
ca3f796
commit ff828bb
Showing
6 changed files
with
707 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# vendored virtual environments | ||
.venv |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
mypy == 1.8.0 | ||
pytest == 7.4.4 | ||
ruff == 0.1.11 | ||
mypy == 1.8.0 |
56 changes: 56 additions & 0 deletions
56
python/instrumentation/openinference-instrumentation-dspy/examples/rag_module.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import logging | ||
import os | ||
import sys | ||
|
||
import dspy | ||
from openinference.instrumentation.dspy import DSPyInstrumentor | ||
from opentelemetry import trace as trace_api | ||
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter | ||
from opentelemetry.sdk import trace as trace_sdk | ||
from opentelemetry.sdk.resources import Resource | ||
from opentelemetry.sdk.trace.export import ConsoleSpanExporter, SimpleSpanProcessor | ||
|
||
logging.basicConfig(level=logging.DEBUG, stream=sys.stdout) | ||
|
||
resource = Resource(attributes={}) | ||
tracer_provider = trace_sdk.TracerProvider(resource=resource) | ||
span_console_exporter = ConsoleSpanExporter() | ||
tracer_provider.add_span_processor(SimpleSpanProcessor(span_exporter=span_console_exporter)) | ||
|
||
# Logs to the Phoenix Collector if running locally | ||
if phoenix_collector_endpoint := os.environ.get("PHOENIX_COLLECTOR_ENDPOINT"): | ||
endpoint = phoenix_collector_endpoint + "/v1/traces" | ||
span_otlp_exporter = OTLPSpanExporter(endpoint=endpoint) | ||
tracer_provider.add_span_processor(SimpleSpanProcessor(span_exporter=span_otlp_exporter)) | ||
|
||
|
||
trace_api.set_tracer_provider(tracer_provider=tracer_provider) | ||
DSPyInstrumentor().instrument() | ||
|
||
|
||
class BasicQA(dspy.Signature): | ||
answer = dspy.OutputField(desc="often between 1 and 5 words") | ||
|
||
|
||
class RAG(dspy.Module): | ||
def __init__(self, num_passages=3): | ||
super().__init__() | ||
self.retrieve = dspy.Retrieve(k=num_passages) | ||
self.generate_answer = dspy.ChainOfThought(BasicQA) | ||
|
||
def forward(self, question): | ||
context = self.retrieve(question).passages | ||
prediction = self.generate_answer(context=context, question=question) | ||
return dspy.Prediction(context=context, answer=prediction.answer) | ||
|
||
|
||
if __name__ == "__main__": | ||
turbo = dspy.OpenAI(model="gpt-3.5-turbo") | ||
colbertv2_wiki17_abstracts = dspy.ColBERTv2(url="http://20.102.90.50:2017/wiki17_abstracts") | ||
dspy.settings.configure( | ||
lm=turbo, | ||
rm=colbertv2_wiki17_abstracts, | ||
) | ||
rag = RAG() | ||
output = rag("What's the capital of the united states?") | ||
print(output) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.