Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Python][DSPy] trace LM Calls #124

Closed
Tracked by #109
mikeldking opened this issue Jan 20, 2024 · 1 comment · Fixed by #126
Closed
Tracked by #109

[Python][DSPy] trace LM Calls #124

mikeldking opened this issue Jan 20, 2024 · 1 comment · Fixed by #126
Assignees
Labels
language: python Related to Python integration

Comments

@mikeldking
Copy link
Contributor

mikeldking commented Jan 20, 2024

Add instrumentation for the language model calls

Copy link

dosubot bot commented Jan 20, 2024

🤖

Sure, I can guide you on how to add instrumentation for the language model calls in Python using DSPy in the OpenInference repository.

Firstly, you need to import the necessary modules. This includes openai for the language model, OpenAIInstrumentor from openinference.instrumentation.openai for the instrumentation, and various modules from opentelemetry for the tracing.

Next, you need to set up the tracing provider. This involves creating a Resource object, a TracerProvider object, an OTLPSpanExporter object, and a SimpleSpanProcessor object. The OTLPSpanExporter is set up to send traces to the endpoint at http://127.0.0.1:6006/v1/traces. The SimpleSpanProcessor is set up with the OTLPSpanExporter, and is then added to the TracerProvider. Finally, the TracerProvider is set as the global tracer provider.

After setting up the tracing provider, you need to instrument the OpenAI library. This is done by creating an OpenAIInstrumentor object and calling its instrument method.

Finally, you can make the language model call. This is done by creating an OpenAI object and calling its embeddings.create method with the desired model and input. The response is then printed.

Here is a code snippet that demonstrates these steps:

import openai
from openinference.instrumentation.openai import OpenAIInstrumentor
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 SimpleSpanProcessor

resource = Resource(attributes={})
tracer_provider = trace_sdk.TracerProvider(resource=resource)
span_exporter = OTLPSpanExporter(endpoint="http://127.0.0.1:6006/v1/traces")
span_processor = SimpleSpanProcessor(span_exporter=span_exporter)
tracer_provider.add_span_processor(span_processor=span_processor)
trace_api.set_tracer_provider(tracer_provider=tracer_provider)

OpenAIInstrumentor().instrument()

if __name__ == "__main__":
    response = openai.OpenAI().embeddings.create(
        model="text-embedding-ada-002",
        input="hello world",
    )
    print(response.data[0].embedding)

This code will instrument the OpenAI library and trace the language model call, sending the trace data to the specified endpoint. You can find more details in the embeddings.py example in the OpenInference repository.

As for the OpenAIInstrumentor class, it is an instrumentor for OpenAI that extends the BaseInstrumentor class from the opentelemetry.instrumentation.instrumentor module. Its main role is to instrument the OpenAI requests for tracing purposes by wrapping the OpenAI.request and AsyncOpenAI.request methods with the _Request and _AsyncRequest wrappers respectively. These wrappers are used to add tracing functionality to the requests. You can find more details in the OpenAIInstrumentor source code in the OpenInference repository.

I hope this helps! If you have any further questions, feel free to ask.

Sources


This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

If you want to continue the conversation, start your reply with @dosu-bot.

@dosubot dosubot bot added enhancement New feature or request language: python Related to Python integration labels Jan 20, 2024
@mikeldking mikeldking removed the enhancement New feature or request label Jan 20, 2024
@github-project-automation github-project-automation bot moved this to 📘 Todo in phoenix Jan 23, 2024
@mikeldking mikeldking self-assigned this Jan 23, 2024
@mikeldking mikeldking moved this from 📘 Todo to 🔍. Needs Review in phoenix Jan 24, 2024
@github-project-automation github-project-automation bot moved this from 🔍. Needs Review to ✅ Done in phoenix Jan 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
language: python Related to Python integration
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

1 participant