-
Notifications
You must be signed in to change notification settings - Fork 678
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
fix(llamaindex): instrument agents & tools #533
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f1032bf
fix(llamaindex): instrument agents & tools
nirga 97d37ed
chore: tests
nirga 4a89415
Merge branch 'main' into llamaindex-agents
nirga 721df38
chore(sample-app): regenerated poetry.lock
nirga af477a8
fix(sample-app): pydantic issue
nirga 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 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
72 changes: 72 additions & 0 deletions
72
...umentation-llamaindex/opentelemetry/instrumentation/llamaindex/base_agent_instrumentor.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 |
---|---|---|
@@ -0,0 +1,72 @@ | ||
from importlib.metadata import version as package_version, PackageNotFoundError | ||
|
||
from wrapt import wrap_function_wrapper | ||
|
||
from opentelemetry.instrumentation.llamaindex.utils import ( | ||
_with_tracer_wrapper, | ||
start_as_current_span_async, | ||
) | ||
from opentelemetry.semconv.ai import SpanAttributes, TraceloopSpanKindValues | ||
|
||
|
||
TO_INSTRUMENT = [ | ||
{ | ||
"class": "AgentRunner", | ||
"v9_module": "llama_index.agent.runner.base", | ||
"v10_module": "llama_index.core.agent.runner.base", | ||
"v10_legacy_module": "llama_index.legacy.agent.runner.base", | ||
}, | ||
{ | ||
"class": "OpenAIAssistantAgent", | ||
"v9_module": "llama_index.agent.openai_assistant_agent", | ||
"v10_module": "llama_index.agent.openai.openai_assistant_agent", | ||
"v10_legacy_module": "llama_index.legacy.agent.openai_assistant_agent", | ||
}, | ||
] | ||
|
||
|
||
class BaseAgentInstrumentor: | ||
def __init__(self, tracer): | ||
self._tracer = tracer | ||
|
||
def instrument(self): | ||
for module in TO_INSTRUMENT: | ||
try: | ||
package_version("llama-index-core") | ||
self._instrument_module(module["v10_module"], module["class"]) | ||
self._instrument_module(module["v10_legacy_module"], module["class"]) | ||
|
||
except PackageNotFoundError: | ||
self._instrument_module(module["v9_module"], module["class"]) | ||
|
||
def _instrument_module(self, module_name, class_name): | ||
wrap_function_wrapper( | ||
module_name, f"{class_name}.chat", query_wrapper(self._tracer) | ||
) | ||
wrap_function_wrapper( | ||
module_name, f"{class_name}.achat", aquery_wrapper(self._tracer) | ||
) | ||
|
||
|
||
@_with_tracer_wrapper | ||
def query_wrapper(tracer, wrapped, instance, args, kwargs): | ||
with tracer.start_as_current_span(f"{instance.__class__.__name__}.agent") as span: | ||
span.set_attribute( | ||
SpanAttributes.TRACELOOP_SPAN_KIND, | ||
TraceloopSpanKindValues.AGENT.value, | ||
) | ||
|
||
return wrapped(*args, **kwargs) | ||
|
||
|
||
@_with_tracer_wrapper | ||
async def aquery_wrapper(tracer, wrapped, instance, args, kwargs): | ||
async with start_as_current_span_async( | ||
tracer=tracer, name=f"{instance.__class__.__name__}.agent" | ||
) as span: | ||
span.set_attribute( | ||
SpanAttributes.TRACELOOP_SPAN_KIND, | ||
TraceloopSpanKindValues.AGENT.value, | ||
) | ||
|
||
return await wrapped(*args, **kwargs) |
72 changes: 72 additions & 0 deletions
72
...rumentation-llamaindex/opentelemetry/instrumentation/llamaindex/base_tool_instrumentor.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 |
---|---|---|
@@ -0,0 +1,72 @@ | ||
from importlib.metadata import version as package_version, PackageNotFoundError | ||
|
||
from wrapt import wrap_function_wrapper | ||
|
||
from opentelemetry.instrumentation.llamaindex.utils import ( | ||
_with_tracer_wrapper, | ||
start_as_current_span_async, | ||
) | ||
from opentelemetry.semconv.ai import SpanAttributes, TraceloopSpanKindValues | ||
|
||
|
||
TO_INSTRUMENT = [ | ||
{ | ||
"class": "FunctionTool", | ||
"v9_module": "llama_index.tools.function_tool", | ||
"v10_module": "llama_index.core.tools.function_tool", | ||
"v10_legacy_module": "llama_index.legacy.tools.function_tool", | ||
}, | ||
{ | ||
"class": "QueryEngineTool", | ||
"v9_module": "llama_index.tools.query_engine", | ||
"v10_module": "llama_index.core.tools.query_engine", | ||
"v10_legacy_module": "llama_index.legacy.tools.query_engine", | ||
}, | ||
] | ||
|
||
|
||
class BaseToolInstrumentor: | ||
def __init__(self, tracer): | ||
self._tracer = tracer | ||
|
||
def instrument(self): | ||
for module in TO_INSTRUMENT: | ||
try: | ||
package_version("llama-index-core") | ||
self._instrument_module(module["v10_module"], module["class"]) | ||
self._instrument_module(module["v10_legacy_module"], module["class"]) | ||
|
||
except PackageNotFoundError: | ||
self._instrument_module(module["v9_module"], module["class"]) | ||
|
||
def _instrument_module(self, module_name, class_name): | ||
wrap_function_wrapper( | ||
module_name, f"{class_name}.call", query_wrapper(self._tracer) | ||
) | ||
wrap_function_wrapper( | ||
module_name, f"{class_name}.acall", aquery_wrapper(self._tracer) | ||
) | ||
|
||
|
||
@_with_tracer_wrapper | ||
def query_wrapper(tracer, wrapped, instance, args, kwargs): | ||
with tracer.start_as_current_span(f"{instance.__class__.__name__}.tool") as span: | ||
span.set_attribute( | ||
SpanAttributes.TRACELOOP_SPAN_KIND, | ||
TraceloopSpanKindValues.TOOL.value, | ||
) | ||
|
||
return wrapped(*args, **kwargs) | ||
|
||
|
||
@_with_tracer_wrapper | ||
async def aquery_wrapper(tracer, wrapped, instance, args, kwargs): | ||
async with start_as_current_span_async( | ||
tracer=tracer, name=f"{instance.__class__.__name__}.tool" | ||
) as span: | ||
span.set_attribute( | ||
SpanAttributes.TRACELOOP_SPAN_KIND, | ||
TraceloopSpanKindValues.TOOL.value, | ||
) | ||
|
||
return await wrapped(*args, **kwargs) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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.
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.
🙃 (NIT) There is a lot of duplication here. Maybe it could be refactored sometime.
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.
Yeah I know :/ I need to refactor this