Skip to content

Commit

Permalink
feat: dspy instrumentation enhancements (#158)
Browse files Browse the repository at this point in the history
  • Loading branch information
axiomofjoy authored Feb 1, 2024
1 parent ca3f796 commit ff828bb
Show file tree
Hide file tree
Showing 6 changed files with 707 additions and 59 deletions.
2 changes: 2 additions & 0 deletions python/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# vendored virtual environments
.venv
2 changes: 1 addition & 1 deletion python/dev-requirements.txt
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
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)
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ instruments = [
test = [
"dspy-ai==2.1.0",
"opentelemetry-sdk",
"requests-mock",
"responses",
]

[project.urls]
Expand Down
Loading

0 comments on commit ff828bb

Please sign in to comment.