-
Notifications
You must be signed in to change notification settings - Fork 990
GenAI Utils | Add _BaseAgent base class and agent creation lifecycle #4217
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
Closed
etserend
wants to merge
13
commits into
open-telemetry:main
from
etserend:genai-utils/agent-base-and-creation
Closed
Changes from 10 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
34ec310
GenAI Utils | Agent Base Type and Creation Span
etserend 19448c5
use semconv attrs directly
etserend fc8cfe1
fix lint and add _BaseAgent to sphinx nitpick exceptions
etserend 0c23956
update span utils
etserend 4568e84
Merge remote-tracking branch 'upstream/main' into genai-utils/agent-b…
etserend 2648615
refactoring
etserend 46b1c2c
Merge remote-tracking branch 'upstream/main' into genai-utils/agent-b…
etserend b840edc
add _BaseAgent to nitpick-exceptions
etserend f0c1e6f
Merge remote-tracking branch 'upstream/main' into genai-utils/agent-b…
etserend a53bcfe
resolve merge conflict
etserend 43d0864
Merge remote-tracking branch 'upstream/main' into genai-utils/agent-b…
etserend edb9688
apply general invoke methods and resolved merge conflict
etserend 685ac57
feat(genai-utils): add AgentCreation invocation type with create_agen…
etserend 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -80,11 +80,16 @@ | |
| ) | ||
| from opentelemetry.util.genai.metrics import InvocationMetricsRecorder | ||
| from opentelemetry.util.genai.span_utils import ( | ||
| _apply_creation_finish_attributes, | ||
| _apply_error_attributes, | ||
| _apply_llm_finish_attributes, | ||
| _maybe_emit_llm_event, | ||
| ) | ||
| from opentelemetry.util.genai.types import Error, LLMInvocation | ||
| from opentelemetry.util.genai.types import ( | ||
| AgentCreation, | ||
| Error, | ||
| LLMInvocation, | ||
| ) | ||
| from opentelemetry.util.genai.version import __version__ | ||
|
|
||
|
|
||
|
|
@@ -211,6 +216,70 @@ def llm( | |
| raise | ||
| self.stop_llm(invocation) | ||
|
|
||
| # ---- Agent lifecycle ---- | ||
|
|
||
| def start_agent( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this goes first will need refactoring.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will refactor as needed |
||
| self, | ||
| agent: AgentCreation, | ||
| ) -> AgentCreation: | ||
| """Start an agent operation (create or invoke) and create a pending span entry.""" | ||
| span_name = f"{agent.operation_name} {agent.name}".strip() | ||
|
etserend marked this conversation as resolved.
Outdated
|
||
| span = self._tracer.start_span( | ||
| name=span_name, | ||
| kind=SpanKind.CLIENT, | ||
| ) | ||
| agent.monotonic_start_s = timeit.default_timer() | ||
|
etserend marked this conversation as resolved.
Outdated
|
||
| agent.span = span | ||
| agent.context_token = otel_context.attach(set_span_in_context(span)) | ||
| return agent | ||
|
|
||
| def stop_agent(self, agent: AgentCreation) -> AgentCreation: # pylint: disable=no-self-use | ||
| """Finalize an agent operation successfully and end its span.""" | ||
| if agent.context_token is None or agent.span is None: | ||
| return agent | ||
|
|
||
| span = agent.span | ||
| _apply_creation_finish_attributes(span, agent) | ||
| otel_context.detach(agent.context_token) | ||
| span.end() | ||
| return agent | ||
|
|
||
| def fail_agent( # pylint: disable=no-self-use | ||
| self, agent: AgentCreation, error: Error | ||
| ) -> AgentCreation: | ||
| """Fail an agent operation and end its span with error status.""" | ||
| if agent.context_token is None or agent.span is None: | ||
| return agent | ||
|
|
||
| span = agent.span | ||
| _apply_creation_finish_attributes(span, agent) | ||
| _apply_error_attributes(span, error) | ||
| otel_context.detach(agent.context_token) | ||
| span.end() | ||
| return agent | ||
|
|
||
| @contextmanager | ||
| def create_agent( | ||
| self, creation: AgentCreation | None = None | ||
| ) -> Iterator[AgentCreation]: | ||
| """Context manager for agent creation. | ||
|
|
||
| Only set data attributes on the creation object, do not modify the span or context. | ||
|
|
||
| Starts the span on entry. On normal exit, finalizes the creation and ends the span. | ||
| If an exception occurs inside the context, marks the span as error, ends it, and | ||
| re-raises the original exception. | ||
| """ | ||
| if creation is None: | ||
| creation = AgentCreation() | ||
| self.start_agent(creation) | ||
| try: | ||
| yield creation | ||
| except Exception as exc: | ||
| self.fail_agent(creation, Error(message=str(exc), type=type(exc))) | ||
| raise | ||
| self.stop_agent(creation) | ||
|
|
||
|
|
||
| def get_telemetry_handler( | ||
| tracer_provider: TracerProvider | None = None, | ||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.