Align gen_ai.tool_definitions with JSON Schema and add it to completion hook #4181
Align gen_ai.tool_definitions with JSON Schema and add it to completion hook #4181wikaaaaa wants to merge 25 commits intoopen-telemetry:mainfrom
Conversation
util/opentelemetry-util-genai/src/opentelemetry/util/genai/_upload/completion_hook.py
Outdated
Show resolved
Hide resolved
util/opentelemetry-util-genai/src/opentelemetry/util/genai/_upload/completion_hook.py
Outdated
Show resolved
Hide resolved
c79def6 to
c6531c5
Compare
c6531c5 to
a7bc9de
Compare
| ## Unreleased | ||
| - Fix bug in how tokens are counted when using the streaming `generateContent` method. ([#4152](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4152)). | ||
| - Add `gen_ai.tool.definitions` attribute to `gen_ai.client.inference.operation.details` log event ([#4142](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4142)). | ||
| - Add `gen_ai.tool_definitions` to completion hook ([#4181](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4181)) |
There was a problem hiding this comment.
I think we have to bump the util-genai dependency in pyproject.toml: https://github.com/open-telemetry/opentelemetry-python-contrib/blob/main/instrumentation-genai/opentelemetry-instrumentation-google-genai/pyproject.toml#L43 -- but I guess we have to do a release of the util gen AI library first ? I'm a bit confused about what the process is supposed to be here @aabmass
| if isinstance(td, FunctionToolDefinition): | ||
| result.append( | ||
| FunctionToolDefinition( | ||
| name=td.name, |
There was a problem hiding this comment.
nit: this could just be 1 long dict/list comprehension
| **completion_details_attributes, | ||
| } | ||
| else: | ||
| tool_def_without_params_attr = _tool_def_without_parameters_attr( |
There was a problem hiding this comment.
nit: just inline this below instead of storing in a variable for 1 line (here and below)
|
|
||
| name: str | ||
| description: str | None | ||
| parameters: Any | None # or dict[str, Any] |
There was a problem hiding this comment.
I'm pretty sure Any includes None ?
|
|
||
| ## Unreleased | ||
|
|
||
| - Add `gen_ai.tool_definitions` to completion hook ([#4181](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4181)) |
There was a problem hiding this comment.
This is duplicated here and below
| usedforsecurity=False, | ||
| ).hexdigest() | ||
|
|
||
| tool_definitions_hash = None |
There was a problem hiding this comment.
nit: update hash_tool_definitions to accept an empty list and return None in that case in order to make this code a little cleaner..
| self._base_path, | ||
| f"{system_instruction_hash or uuid_str}_system_instruction.{self._format}", | ||
| ), | ||
| tool_definitions_ref=posixpath.join( |
There was a problem hiding this comment.
do we still want to stamp the ref / do an upload if tool definitions is None ?
| return None | ||
| return [] | ||
|
|
||
| tool_definitions = [] |
There was a problem hiding this comment.
nit I think it could be a list comprehension ? return [de for de in _to_tool_definition(tool) for tool in _config_to_tools(config) if de (maybe also below, but not sure with the await call..)
|
|
||
|
|
||
| def _tool_def_without_parameters_attr( | ||
| tool_def: list[ToolDefinition], |
There was a problem hiding this comment.
Do we just want to use FunctionToolDefinitiondirectly Instead of using ToolDefinition everywhere ? I don't think we want to use the GenericToolDefinition at all ?
| return { | ||
| "error": f"failed to serialize tool definition, tool type={type(tool).__name__}" | ||
| } | ||
| return [ |
There was a problem hiding this comment.
Ah I see we have a Generic tool definition here.. So do we really want to add something to the telemetry in case of a tool or params that we fail to serialize ? I feel like it makes more sense to leave it out and log an error/warning instead. Maybe worth bringing up that question in the sem conv PR. Maybe @aabmass has thoughts on this
Description
This PR aligns
gen_ai.tool_definitionswith JSON Schema proposed in open-telemetry/semantic-conventions#3378.This PR also adds
gen_ai.tool_definitionsto completion hook and configures exporting the attribute to GCS. Its configured the same way as system instructions, it's being hashed, since it might be large and often doesn't differ.Its a continuation of #4142, which added
gen_ai.tool_definitionstogen_ai.client.inference.operation.details.Type of change
New feature (non-breaking change which adds functionality)
How Has This Been Tested?
Does This PR Require a Core Repo Change?
Checklist:
See contributing.md for styleguide, changelog guidelines, and more.