-
Notifications
You must be signed in to change notification settings - Fork 504
fix(tracing): prevent mutation of user options when tracing is enabled #1273
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
Open
Pouyanpi
wants to merge
7
commits into
develop
Choose a base branch
from
fix/tracing-options-mutation
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5bde0b0
feat(tracing)!: migrate OpenTelemetry adapter to use otel API
Pouyanpi 0544cdc
chore: update deps
Pouyanpi b71ef1e
docs: update README and add working example
Pouyanpi afa7010
feat: improve OpenTelemetry config and warnings
Pouyanpi 6609cf2
feat: deprecate register_otel_exporter function
Pouyanpi 5b8a59c
refactor(tracing): simplify OpenTelemetry adapter API
Pouyanpi b85493c
fix(tracing): prevent mutation of user options when tracing is enabled
Pouyanpi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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,146 @@ | ||
#!/usr/bin/env python3 | ||
# SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
""" | ||
Complete working example of NeMo Guardrails with OpenTelemetry tracing. | ||
|
||
This example uses the ConsoleSpanExporter so you can see traces immediately | ||
without needing to set up any external infrastructure. | ||
|
||
Usage: | ||
pip install nemoguardrails[tracing] opentelemetry-sdk | ||
python working_example.py | ||
""" | ||
|
||
from opentelemetry import trace | ||
from opentelemetry.sdk.resources import Resource | ||
from opentelemetry.sdk.trace import TracerProvider | ||
from opentelemetry.sdk.trace.export import BatchSpanProcessor, ConsoleSpanExporter | ||
|
||
from nemoguardrails import LLMRails, RailsConfig | ||
|
||
|
||
def setup_opentelemetry(): | ||
"""Configure OpenTelemetry SDK with console output.""" | ||
|
||
print("Setting up OpenTelemetry...") | ||
|
||
# configure resource (metadata about your service) | ||
resource = Resource.create( | ||
{ | ||
"service.name": "nemo-guardrails-example", | ||
"service.version": "1.0.0", | ||
"deployment.environment": "development", | ||
}, | ||
schema_url="https://opentelemetry.io/schemas/1.26.0", | ||
) | ||
|
||
# set up the tracer provider | ||
tracer_provider = TracerProvider(resource=resource) | ||
trace.set_tracer_provider(tracer_provider) | ||
|
||
# configure console exporter (prints traces to stdout) | ||
console_exporter = ConsoleSpanExporter() | ||
span_processor = BatchSpanProcessor(console_exporter) | ||
tracer_provider.add_span_processor(span_processor) | ||
|
||
print(" OpenTelemetry configured with ConsoleSpanExporter") | ||
print(" Traces will be printed to the console below\n") | ||
|
||
|
||
def create_guardrails_config(): | ||
"""Create a simple guardrails configuration with tracing enabled.""" | ||
|
||
return RailsConfig.from_content( | ||
colang_content=""" | ||
define user express greeting | ||
"hello" | ||
"hi" | ||
"hey" | ||
|
||
define flow | ||
user express greeting | ||
bot express greeting | ||
|
||
define bot express greeting | ||
"Hello! I'm a guardrails-enabled assistant." | ||
"Hi there! How can I help you today?" | ||
""", | ||
config={ | ||
"models": [ | ||
{ | ||
"type": "main", | ||
"engine": "openai", | ||
"model": "gpt-4o", | ||
} | ||
], | ||
"tracing": {"enabled": True, "adapters": [{"name": "OpenTelemetry"}]}, | ||
# Note: The following old-style configuration is deprecated and will be ignored: | ||
# "tracing": { | ||
# "enabled": True, | ||
# "adapters": [{ | ||
# "name": "OpenTelemetry", | ||
# "service_name": "my-service", # DEPRECATED - configure in Resource | ||
# "exporter": "console", # DEPRECATED - configure SDK | ||
# "resource_attributes": { # DEPRECATED - configure in Resource | ||
# "env": "production" | ||
# } | ||
# }] | ||
# } | ||
}, | ||
) | ||
|
||
|
||
def main(): | ||
"""Main function demonstrating NeMo Guardrails with OpenTelemetry.""" | ||
print(" NeMo Guardrails + OpenTelemetry Example") | ||
print("=" * 50) | ||
|
||
# step 1: configure OpenTelemetry (APPLICATION'S RESPONSIBILITY) | ||
setup_opentelemetry() | ||
|
||
# step 2: create guardrails configuration | ||
print(" Creating guardrails configuration...") | ||
config = create_guardrails_config() | ||
rails = LLMRails(config) | ||
print(" Guardrails configured with tracing enabled\n") | ||
|
||
# step 3: test the guardrails with tracing | ||
print(" Testing guardrails (traces will appear below)...") | ||
print("-" * 50) | ||
|
||
# this will create spans that get exported to the console | ||
response = rails.generate( | ||
messages=[{"role": "user", "content": "What can you do?"}] | ||
) | ||
|
||
print(f"User: What can you do?") | ||
print(f"Bot: {response.response}") | ||
print("-" * 50) | ||
|
||
# force export any remaining spans | ||
print("\n Flushing remaining traces...") | ||
trace.get_tracer_provider().force_flush(1000) | ||
|
||
print("\n Example completed!") | ||
print("\n Tips:") | ||
print(" - Traces were printed above (look for JSON output)") | ||
print(" - In production, replace ConsoleSpanExporter with OTLP/Jaeger") | ||
print(" - The spans show the internal flow of guardrails processing") | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this still work if we pass
options
in as a Dict:Could you add a test to check this is still ok?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Would
options.model_copy(deep=True)
be clearer here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, the first one is indeed a bug.
Yes it is more readable. I'll make both of these changes 👍🏻