-
Notifications
You must be signed in to change notification settings - Fork 7
instrument cua, fixes and tests to litellm responses #187
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c3339c6
instrument cua, fixes and tests to litellm responses
dinmukhamedm 0afebae
Update tests/test_litellm_openai.py
dinmukhamedm 221c2e6
Update src/lmnr/opentelemetry_lib/opentelemetry/instrumentation/cua_a…
dinmukhamedm c2fb191
Update src/lmnr/opentelemetry_lib/opentelemetry/instrumentation/cua_c…
dinmukhamedm 96293e9
add ruff config python 310
dinmukhamedm b25406d
tool span, dont record screenshot, rename step span, skip empty step …
dinmukhamedm 5dee2d5
fix type hint
dinmukhamedm 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
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
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
92 changes: 92 additions & 0 deletions
92
src/lmnr/opentelemetry_lib/opentelemetry/instrumentation/cua_agent/__init__.py
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,92 @@ | ||
"""OpenTelemetry Groq instrumentation""" | ||
|
||
import logging | ||
from typing import Any, AsyncGenerator, Collection | ||
|
||
from lmnr.opentelemetry_lib.decorators import json_dumps | ||
from lmnr import Laminar | ||
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor | ||
from opentelemetry.instrumentation.utils import unwrap | ||
|
||
from opentelemetry.trace import Span | ||
from opentelemetry.trace.status import Status, StatusCode | ||
from wrapt import wrap_function_wrapper | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
_instruments = ("cua-agent >= 0.4.0",) | ||
|
||
|
||
def _wrap_run( | ||
wrapped, | ||
instance, | ||
args, | ||
kwargs, | ||
): | ||
parent_span = Laminar.start_span("agent.run") | ||
instance._lmnr_parent_span = parent_span | ||
|
||
try: | ||
result: AsyncGenerator[dict[str, Any], None] = wrapped(*args, **kwargs) | ||
return _abuild_from_streaming_response(parent_span, result) | ||
except Exception as e: | ||
if parent_span.is_recording(): | ||
parent_span.set_status(Status(StatusCode.ERROR)) | ||
parent_span.record_exception(e) | ||
parent_span.end() | ||
raise | ||
|
||
|
||
async def _abuild_from_streaming_response( | ||
parent_span: Span, response: AsyncGenerator[dict[str, Any], None] | ||
) -> AsyncGenerator[dict[str, Any], None]: | ||
with Laminar.use_span(parent_span, end_on_exit=True): | ||
response_iter = aiter(response) | ||
while True: | ||
step = None | ||
step_span = Laminar.start_span("agent.step") | ||
with Laminar.use_span(step_span): | ||
try: | ||
step = await anext(response_iter) | ||
step_span.set_attribute("lmnr.span.output", json_dumps(step)) | ||
if step_span.is_recording(): | ||
step_span.end() | ||
except StopAsyncIteration: | ||
# don't end on purpose, there is no iteration step here. | ||
break | ||
|
||
if step is not None: | ||
yield step | ||
|
||
|
||
class CuaAgentInstrumentor(BaseInstrumentor): | ||
def __init__(self): | ||
super().__init__() | ||
|
||
def instrumentation_dependencies(self) -> Collection[str]: | ||
return _instruments | ||
|
||
def _instrument(self, **kwargs): | ||
wrap_package = "agent.agent" | ||
wrap_object = "ComputerAgent" | ||
wrap_method = "run" | ||
try: | ||
wrap_function_wrapper( | ||
wrap_package, | ||
f"{wrap_object}.{wrap_method}", | ||
_wrap_run, | ||
) | ||
except ModuleNotFoundError: | ||
pass # that's ok, we don't want to fail if some methods do not exist | ||
|
||
def _uninstrument(self, **kwargs): | ||
wrap_package = "agent.agent" | ||
wrap_object = "ComputerAgent" | ||
wrap_method = "run" | ||
try: | ||
unwrap( | ||
f"{wrap_package}.{wrap_object}", | ||
wrap_method, | ||
) | ||
except ModuleNotFoundError: | ||
pass # that's ok, we don't want to fail if some methods do not exist |
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.
Uh oh!
There was an error while loading. Please reload this page.