-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(smolagents): updates to latest and makes examples use openteleme…
…try-instrument (#1277) Signed-off-by: Adrian Cole <[email protected]>
- Loading branch information
1 parent
2106acf
commit b151bc9
Showing
14 changed files
with
148 additions
and
167 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 |
---|---|---|
@@ -1,2 +1,5 @@ | ||
# vendored virtual environments | ||
.venv | ||
|
||
# gradio work directory | ||
.gradio |
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
49 changes: 49 additions & 0 deletions
49
python/instrumentation/openinference-instrumentation-smolagents/examples/README.md
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,49 @@ | ||
# OpenInference smolagents Examples | ||
|
||
This directory contains numerous examples that show how to use OpenInference to instrument smolagents applications. | ||
Specifically, this uses the [openinference-instrumentation-smolagents](..) package from source in the parent directory. | ||
|
||
## Installation | ||
|
||
```shell | ||
pip install -r requirements.txt | ||
``` | ||
|
||
Start Phoenix in the background as a collector, which listens on `http://localhost:6006` and default gRPC port 4317. | ||
Note that Phoenix does not send data over the internet. It only operates locally on your machine. | ||
|
||
```shell | ||
python -m phoenix.server.main serve | ||
``` | ||
|
||
## Running | ||
|
||
Copy [env.example](env.example) to `.env` and update variables your example uses, such as `OPENAI_API_KEY`. | ||
|
||
Then, run an example like this: | ||
|
||
```shell | ||
dotenv run -- opentelemetry-instrument python managed_agent.py | ||
``` | ||
|
||
Finally, browse for your trace in Phoenix at `http://localhost:6006`! | ||
|
||
## Manual instrumentation | ||
|
||
`opentelemetry-instrument` is the [Zero-code instrumentation](https://opentelemetry.io/docs/zero-code/python) approach | ||
for Python. It avoids explicitly importing and configuring OpenTelemetry code in your main source. Alternatively, you | ||
can copy-paste the following into your main source and run it without `opentelemetry-instrument`. | ||
|
||
```python | ||
from opentelemetry.sdk.trace import TracerProvider | ||
|
||
from openinference.instrumentation.smolagents import SmolagentsInstrumentor | ||
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter | ||
from opentelemetry.sdk.trace.export import SimpleSpanProcessor | ||
|
||
otlp_exporter = OTLPSpanExporter(endpoint="http://localhost:4317", insecure=True) | ||
trace_provider = TracerProvider() | ||
trace_provider.add_span_processor(SimpleSpanProcessor(otlp_exporter)) | ||
|
||
SmolagentsInstrumentor().instrument(tracer_provider=trace_provider) | ||
``` |
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
12 changes: 12 additions & 0 deletions
12
python/instrumentation/openinference-instrumentation-smolagents/examples/env.example
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,12 @@ | ||
# API Key from https://platform.openai.com/api-keys | ||
OPENAI_API_KEY=sk-YOUR_API_KEY | ||
# API Key from https://e2b.dev/docs/legacy/getting-started/api-key | ||
E2B_API_KEY=e2b_YOUR_API_KEY | ||
|
||
# Phoenix listens on the default gRPC port 4317, so you don't need to change | ||
# exporter settings. If you prefer to export via HTTP, uncomment this: | ||
# OTEL_EXPORTER_OTLP_ENDPOINT=http://0.0.0.0:6006 | ||
# OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf | ||
|
||
# Export traces every second instead of every 5 seconds | ||
OTEL_BSP_SCHEDULE_DELAY=1000 |
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
21 changes: 2 additions & 19 deletions
21
python/instrumentation/openinference-instrumentation-smolagents/examples/openai_model.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 |
---|---|---|
@@ -1,22 +1,5 @@ | ||
import os | ||
|
||
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter | ||
from opentelemetry.sdk.trace import TracerProvider | ||
from opentelemetry.sdk.trace.export import ( | ||
SimpleSpanProcessor, | ||
) | ||
from smolagents import OpenAIServerModel | ||
|
||
from openinference.instrumentation.smolagents import SmolagentsInstrumentor | ||
|
||
endpoint = "http://0.0.0.0:6006/v1/traces" | ||
trace_provider = TracerProvider() | ||
trace_provider.add_span_processor(SimpleSpanProcessor(OTLPSpanExporter(endpoint))) | ||
|
||
SmolagentsInstrumentor().instrument(tracer_provider=trace_provider, skip_dep_check=True) | ||
|
||
model = OpenAIServerModel( | ||
model_id="gpt-4o", api_key=os.environ["OPENAI_API_KEY"], api_base="https://api.openai.com/v1" | ||
) | ||
model = OpenAIServerModel(model_id="gpt-4o") | ||
output = model(messages=[{"role": "user", "content": "hello world"}]) | ||
print(output) | ||
print(output.content) |
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
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
22 changes: 16 additions & 6 deletions
22
python/instrumentation/openinference-instrumentation-smolagents/examples/requirements.txt
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,9 +1,19 @@ | ||
# Main dependencies of the examples in this directory: | ||
smolagents[e2b,gradio,litellm,openai] | ||
datasets | ||
langchain | ||
langchain-community | ||
opentelemetry-exporter-otlp | ||
opentelemetry-exporter-otlp-proto-http | ||
opentelemetry-sdk | ||
langchain_community | ||
rank_bm25 | ||
requests | ||
sqlalchemy | ||
|
||
# Install `opentelemetry-instrument` for zero code instrumentation. This | ||
# defaults to export traces to localhost 4317, which Phoenix listens on. | ||
opentelemetry-sdk | ||
opentelemetry-exporter-otlp-proto-grpc | ||
opentelemetry-distro | ||
# Source of openinference-instrumentation-smolagents | ||
-e ../ | ||
|
||
|
||
# Both `opentelemetry-instrument` and main need .env variables. Run like this: | ||
# dotenv run -- opentelemetry-instrument python e2b_example.py | ||
python-dotenv[cli] |
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.