Skip to content
7 changes: 6 additions & 1 deletion agentops/instrumentation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,16 @@ def get_instance(self) -> BaseInstrumentor:
class_name="GoogleGenerativeAIInstrumentor",
provider_import_name="google.genai",
),
InstrumentorLoader(
module_name="agentops.instrumentation.ibm_watsonx_ai",
class_name="IBMWatsonXInstrumentor",
provider_import_name="ibm_watsonx_ai",
),
InstrumentorLoader(
module_name="agentops.instrumentation.ag2",
class_name="AG2Instrumentor",
provider_import_name="autogen",
),
)
]


Expand Down
32 changes: 32 additions & 0 deletions agentops/instrumentation/ibm_watsonx_ai/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""IBM WatsonX AI instrumentation for AgentOps.

This package provides instrumentation for IBM's WatsonX AI foundation models,
capturing telemetry for model interactions including completions, chat, and streaming responses.
"""

import logging
from typing import Collection

Check warning on line 8 in agentops/instrumentation/ibm_watsonx_ai/__init__.py

View check run for this annotation

Codecov / codecov/patch

agentops/instrumentation/ibm_watsonx_ai/__init__.py#L7-L8

Added lines #L7 - L8 were not covered by tests

logger = logging.getLogger(__name__)

Check warning on line 10 in agentops/instrumentation/ibm_watsonx_ai/__init__.py

View check run for this annotation

Codecov / codecov/patch

agentops/instrumentation/ibm_watsonx_ai/__init__.py#L10

Added line #L10 was not covered by tests

def get_version() -> str:

Check warning on line 12 in agentops/instrumentation/ibm_watsonx_ai/__init__.py

View check run for this annotation

Codecov / codecov/patch

agentops/instrumentation/ibm_watsonx_ai/__init__.py#L12

Added line #L12 was not covered by tests
"""Get the version of the IBM watsonx.ai SDK, or 'unknown' if not found."""
try:
from importlib.metadata import version
return version("ibm-watsonx-ai")
except ImportError:
logger.debug("Could not find IBM WatsonX AI SDK version")
return "1.3.11" # Default to known supported version if not found

Check warning on line 19 in agentops/instrumentation/ibm_watsonx_ai/__init__.py

View check run for this annotation

Codecov / codecov/patch

agentops/instrumentation/ibm_watsonx_ai/__init__.py#L14-L19

Added lines #L14 - L19 were not covered by tests

# Library identification for instrumentation
LIBRARY_NAME = "ibm_watsonx_ai"
LIBRARY_VERSION = get_version()

Check warning on line 23 in agentops/instrumentation/ibm_watsonx_ai/__init__.py

View check run for this annotation

Codecov / codecov/patch

agentops/instrumentation/ibm_watsonx_ai/__init__.py#L22-L23

Added lines #L22 - L23 were not covered by tests

# Import after defining constants to avoid circular imports
from agentops.instrumentation.ibm_watsonx_ai.instrumentor import IBMWatsonXInstrumentor # noqa: E402

Check warning on line 26 in agentops/instrumentation/ibm_watsonx_ai/__init__.py

View check run for this annotation

Codecov / codecov/patch

agentops/instrumentation/ibm_watsonx_ai/__init__.py#L26

Added line #L26 was not covered by tests

__all__ = [

Check warning on line 28 in agentops/instrumentation/ibm_watsonx_ai/__init__.py

View check run for this annotation

Codecov / codecov/patch

agentops/instrumentation/ibm_watsonx_ai/__init__.py#L28

Added line #L28 was not covered by tests
"LIBRARY_NAME",
"LIBRARY_VERSION",
"IBMWatsonXInstrumentor",
]
27 changes: 27 additions & 0 deletions agentops/instrumentation/ibm_watsonx_ai/attributes/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""Attribute extraction utilities for IBM watsonx.ai instrumentation."""

from agentops.instrumentation.ibm_watsonx_ai.attributes.attributes import (

Check warning on line 3 in agentops/instrumentation/ibm_watsonx_ai/attributes/__init__.py

View check run for this annotation

Codecov / codecov/patch

agentops/instrumentation/ibm_watsonx_ai/attributes/__init__.py#L3

Added line #L3 was not covered by tests
get_generate_attributes,
get_chat_attributes,
get_tokenize_attributes,
get_model_details_attributes
)
from agentops.instrumentation.ibm_watsonx_ai.attributes.common import (

Check warning on line 9 in agentops/instrumentation/ibm_watsonx_ai/attributes/__init__.py

View check run for this annotation

Codecov / codecov/patch

agentops/instrumentation/ibm_watsonx_ai/attributes/__init__.py#L9

Added line #L9 was not covered by tests
extract_params_attributes,
convert_params_to_dict,
extract_prompt_from_args,
extract_messages_from_args,
extract_params_from_args
)

__all__ = [

Check warning on line 17 in agentops/instrumentation/ibm_watsonx_ai/attributes/__init__.py

View check run for this annotation

Codecov / codecov/patch

agentops/instrumentation/ibm_watsonx_ai/attributes/__init__.py#L17

Added line #L17 was not covered by tests
"get_generate_attributes",
"get_chat_attributes",
"get_tokenize_attributes",
"get_model_details_attributes",
"extract_params_attributes",
"convert_params_to_dict",
"extract_prompt_from_args",
"extract_messages_from_args",
"extract_params_from_args"
]
244 changes: 244 additions & 0 deletions agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,244 @@
"""Attributes for IBM watsonx.ai model instrumentation.

This module provides attribute extraction functions for IBM watsonx.ai model operations.
"""
from typing import Any, Dict, Optional, Tuple
from agentops.instrumentation.common.attributes import AttributeMap
from agentops.semconv import SpanAttributes, MessageAttributes
from agentops.instrumentation.ibm_watsonx_ai.attributes.common import (

Check warning on line 8 in agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py

View check run for this annotation

Codecov / codecov/patch

agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py#L5-L8

Added lines #L5 - L8 were not covered by tests
extract_params_attributes,
convert_params_to_dict,
extract_prompt_from_args,
extract_messages_from_args,
extract_params_from_args
)
from ibm_watsonx_ai.foundation_models.schema import TextGenParameters, TextChatParameters

Check warning on line 15 in agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py

View check run for this annotation

Codecov / codecov/patch

agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py#L15

Added line #L15 was not covered by tests

def get_generate_attributes(args: Optional[Tuple] = None, kwargs: Optional[Dict] = None, return_value: Optional[Any] = None) -> AttributeMap:

Check warning on line 17 in agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py

View check run for this annotation

Codecov / codecov/patch

agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py#L17

Added line #L17 was not covered by tests
"""Extract token usage attributes from generate method calls."""
attributes = {}

Check warning on line 19 in agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py

View check run for this annotation

Codecov / codecov/patch

agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py#L19

Added line #L19 was not covered by tests

# Extract prompt using helper function
prompt = extract_prompt_from_args(args, kwargs)
if prompt:
attributes[MessageAttributes.PROMPT_ROLE.format(i=0)] = "user"
attributes[MessageAttributes.PROMPT_CONTENT.format(i=0)] = prompt
attributes[MessageAttributes.PROMPT_TYPE.format(i=0)] = "text"

Check warning on line 26 in agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py

View check run for this annotation

Codecov / codecov/patch

agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py#L22-L26

Added lines #L22 - L26 were not covered by tests

# Extract parameters using helper functions
params = extract_params_from_args(args, kwargs)
if params:
params_dict = convert_params_to_dict(params)
if params_dict:
attributes.update(extract_params_attributes(params_dict))

Check warning on line 33 in agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py

View check run for this annotation

Codecov / codecov/patch

agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py#L29-L33

Added lines #L29 - L33 were not covered by tests

# Extract response information
if return_value:
if isinstance(return_value, dict):

Check warning on line 37 in agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py

View check run for this annotation

Codecov / codecov/patch

agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py#L36-L37

Added lines #L36 - L37 were not covered by tests
# Extract model information
if 'model_id' in return_value:
attributes[SpanAttributes.LLM_REQUEST_MODEL] = return_value['model_id']

Check warning on line 40 in agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py

View check run for this annotation

Codecov / codecov/patch

agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py#L39-L40

Added lines #L39 - L40 were not covered by tests

# Handle results
if 'results' in return_value:
for idx, result in enumerate(return_value['results']):

Check warning on line 44 in agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py

View check run for this annotation

Codecov / codecov/patch

agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py#L43-L44

Added lines #L43 - L44 were not covered by tests
# Extract completion
if 'generated_text' in result:
attributes[MessageAttributes.COMPLETION_CONTENT.format(i=idx)] = result['generated_text']
attributes[MessageAttributes.COMPLETION_ROLE.format(i=idx)] = "assistant"
attributes[MessageAttributes.COMPLETION_TYPE.format(i=idx)] = "text"

Check warning on line 49 in agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py

View check run for this annotation

Codecov / codecov/patch

agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py#L46-L49

Added lines #L46 - L49 were not covered by tests

# Extract token usage
if 'input_token_count' in result:
attributes[SpanAttributes.LLM_USAGE_PROMPT_TOKENS] = result['input_token_count']
if 'generated_token_count' in result:
attributes[SpanAttributes.LLM_USAGE_COMPLETION_TOKENS] = result['generated_token_count']
if 'input_token_count' in result and 'generated_token_count' in result:
attributes[SpanAttributes.LLM_USAGE_TOTAL_TOKENS] = result['input_token_count'] + result['generated_token_count']

Check warning on line 57 in agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py

View check run for this annotation

Codecov / codecov/patch

agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py#L52-L57

Added lines #L52 - L57 were not covered by tests

if 'stop_reason' in result:
attributes[SpanAttributes.LLM_RESPONSE_STOP_REASON] = result['stop_reason']

Check warning on line 60 in agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py

View check run for this annotation

Codecov / codecov/patch

agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py#L59-L60

Added lines #L59 - L60 were not covered by tests

return attributes

Check warning on line 62 in agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py

View check run for this annotation

Codecov / codecov/patch

agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py#L62

Added line #L62 was not covered by tests

def get_tokenize_attributes(args: Optional[Tuple] = None, kwargs: Optional[Dict] = None, return_value: Optional[Any] = None) -> AttributeMap:

Check warning on line 64 in agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py

View check run for this annotation

Codecov / codecov/patch

agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py#L64

Added line #L64 was not covered by tests
"""Extract attributes from tokenize method calls."""
attributes = {}

Check warning on line 66 in agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py

View check run for this annotation

Codecov / codecov/patch

agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py#L66

Added line #L66 was not covered by tests

# Extract input from args or kwargs using helper function
prompt = extract_prompt_from_args(args, kwargs)
if prompt:
attributes[MessageAttributes.PROMPT_ROLE.format(i=0)] = "user"
attributes[MessageAttributes.PROMPT_CONTENT.format(i=0)] = prompt
attributes[MessageAttributes.PROMPT_TYPE.format(i=0)] = "text"

Check warning on line 73 in agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py

View check run for this annotation

Codecov / codecov/patch

agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py#L69-L73

Added lines #L69 - L73 were not covered by tests

# Extract response information
if return_value and isinstance(return_value, dict):
if "model_id" in return_value:
attributes[SpanAttributes.LLM_REQUEST_MODEL] = return_value["model_id"]
if "result" in return_value:
attributes["ibm.watsonx.tokenize.result"] = str(return_value["result"])
if "token_count" in return_value["result"]:
attributes[SpanAttributes.LLM_USAGE_PROMPT_TOKENS] = return_value["result"]["token_count"]

Check warning on line 82 in agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py

View check run for this annotation

Codecov / codecov/patch

agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py#L76-L82

Added lines #L76 - L82 were not covered by tests

return attributes

Check warning on line 84 in agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py

View check run for this annotation

Codecov / codecov/patch

agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py#L84

Added line #L84 was not covered by tests

def get_model_details_attributes(args: Optional[Tuple] = None, kwargs: Optional[Dict] = None, return_value: Optional[Any] = None) -> AttributeMap:

Check warning on line 86 in agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py

View check run for this annotation

Codecov / codecov/patch

agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py#L86

Added line #L86 was not covered by tests
"""Extract attributes from get_details method calls."""
if not isinstance(return_value, dict):
return {}

Check warning on line 89 in agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py

View check run for this annotation

Codecov / codecov/patch

agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py#L88-L89

Added lines #L88 - L89 were not covered by tests

# Basic model information
attributes = {

Check warning on line 92 in agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py

View check run for this annotation

Codecov / codecov/patch

agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py#L92

Added line #L92 was not covered by tests
f"ibm.watsonx.model.{key}": value
for key, value in return_value.items()
if key in ["model_id", "label", "provider", "source", "short_description", "long_description",
"number_params", "input_tier", "output_tier"]
}

# Model functions
if "functions" in return_value:
attributes["ibm.watsonx.model.functions"] = str([func["id"] for func in return_value["functions"]])

Check warning on line 101 in agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py

View check run for this annotation

Codecov / codecov/patch

agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py#L100-L101

Added lines #L100 - L101 were not covered by tests

# Model tasks
if "tasks" in return_value:
task_info = [

Check warning on line 105 in agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py

View check run for this annotation

Codecov / codecov/patch

agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py#L104-L105

Added lines #L104 - L105 were not covered by tests
{k: v for k, v in task.items() if k in ["id", "ratings", "tags"]}
for task in return_value["tasks"]
]
attributes["ibm.watsonx.model.tasks"] = str(task_info)

Check warning on line 109 in agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py

View check run for this annotation

Codecov / codecov/patch

agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py#L109

Added line #L109 was not covered by tests

# Model limits
if "model_limits" in return_value:
limits = return_value["model_limits"]
attributes.update({

Check warning on line 114 in agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py

View check run for this annotation

Codecov / codecov/patch

agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py#L112-L114

Added lines #L112 - L114 were not covered by tests
f"ibm.watsonx.model.{key}": value
for key, value in limits.items()
if key in ["max_sequence_length", "max_output_tokens", "training_data_max_records"]
})

# Service tier limits
if "limits" in return_value:
for tier, tier_limits in return_value["limits"].items():
attributes.update({

Check warning on line 123 in agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py

View check run for this annotation

Codecov / codecov/patch

agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py#L121-L123

Added lines #L121 - L123 were not covered by tests
f"ibm.watsonx.model.limits.{tier}.{key}": value
for key, value in tier_limits.items()
if key in ["call_time", "max_output_tokens"]
})

# Model lifecycle
if "lifecycle" in return_value:
attributes.update({

Check warning on line 131 in agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py

View check run for this annotation

Codecov / codecov/patch

agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py#L130-L131

Added lines #L130 - L131 were not covered by tests
f"ibm.watsonx.model.lifecycle.{stage['id']}": stage["start_date"]
for stage in return_value["lifecycle"]
if "id" in stage and "start_date" in stage
})

# Training parameters
if "training_parameters" in return_value:
attributes.update({

Check warning on line 139 in agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py

View check run for this annotation

Codecov / codecov/patch

agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py#L138-L139

Added lines #L138 - L139 were not covered by tests
f"ibm.watsonx.model.training.{key}": str(value) if isinstance(value, dict) else value
for key, value in return_value["training_parameters"].items()
})

return attributes

Check warning on line 144 in agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py

View check run for this annotation

Codecov / codecov/patch

agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py#L144

Added line #L144 was not covered by tests

def get_chat_attributes(args: Optional[Tuple] = None, kwargs: Optional[Dict] = None, return_value: Optional[Any] = None) -> AttributeMap:

Check warning on line 146 in agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py

View check run for this annotation

Codecov / codecov/patch

agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py#L146

Added line #L146 was not covered by tests
"""Extract attributes from chat method calls."""
attributes = {}

Check warning on line 148 in agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py

View check run for this annotation

Codecov / codecov/patch

agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py#L148

Added line #L148 was not covered by tests

# Extract messages using helper function
messages = extract_messages_from_args(args, kwargs)
if messages:

Check warning on line 152 in agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py

View check run for this annotation

Codecov / codecov/patch

agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py#L151-L152

Added lines #L151 - L152 were not covered by tests
# Process each message in the conversation
for i, message in enumerate(messages):
if not isinstance(message, dict):
continue

Check warning on line 156 in agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py

View check run for this annotation

Codecov / codecov/patch

agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py#L154-L156

Added lines #L154 - L156 were not covered by tests

# Extract role and content
role = message.get('role', '')
content = message.get('content', [])

Check warning on line 160 in agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py

View check run for this annotation

Codecov / codecov/patch

agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py#L159-L160

Added lines #L159 - L160 were not covered by tests

# Handle content which can be a list of different types (text, image_url)
if isinstance(content, list):

Check warning on line 163 in agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py

View check run for this annotation

Codecov / codecov/patch

agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py#L163

Added line #L163 was not covered by tests
# Combine all text content
text_content = []
image_urls = []

Check warning on line 166 in agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py

View check run for this annotation

Codecov / codecov/patch

agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py#L165-L166

Added lines #L165 - L166 were not covered by tests

for content_item in content:
if isinstance(content_item, dict):
if content_item.get('type') == 'text':
text_content.append(content_item.get('text', ''))
elif content_item.get('type') == 'image_url':
image_url = content_item.get('image_url', {})
if isinstance(image_url, dict) and 'url' in image_url:
url = image_url['url']

Check warning on line 175 in agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py

View check run for this annotation

Codecov / codecov/patch

agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py#L168-L175

Added lines #L168 - L175 were not covered by tests
# Only store URLs that start with http, otherwise use placeholder
if url and isinstance(url, str) and url.startswith(('http://', 'https://')):
image_urls.append(url)

Check warning on line 178 in agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py

View check run for this annotation

Codecov / codecov/patch

agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py#L177-L178

Added lines #L177 - L178 were not covered by tests
else:
image_urls.append("[IMAGE_PLACEHOLDER]")

Check warning on line 180 in agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py

View check run for this annotation

Codecov / codecov/patch

agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py#L180

Added line #L180 was not covered by tests

# Set text content if any
if text_content:
attributes[MessageAttributes.PROMPT_CONTENT.format(i=i)] = ' '.join(text_content)
attributes[MessageAttributes.PROMPT_TYPE.format(i=i)] = "text"
attributes[MessageAttributes.PROMPT_ROLE.format(i=i)] = role

Check warning on line 186 in agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py

View check run for this annotation

Codecov / codecov/patch

agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py#L183-L186

Added lines #L183 - L186 were not covered by tests

# Set image URLs if any
if image_urls:
attributes[f"ibm.watsonx.chat.message.{i}.images"] = str(image_urls)

Check warning on line 190 in agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py

View check run for this annotation

Codecov / codecov/patch

agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py#L189-L190

Added lines #L189 - L190 were not covered by tests
else:
# Handle string content
attributes[MessageAttributes.PROMPT_CONTENT.format(i=i)] = str(content)
attributes[MessageAttributes.PROMPT_TYPE.format(i=i)] = "text"
attributes[MessageAttributes.PROMPT_ROLE.format(i=i)] = role

Check warning on line 195 in agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py

View check run for this annotation

Codecov / codecov/patch

agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py#L193-L195

Added lines #L193 - L195 were not covered by tests

# Extract parameters using helper functions
params = extract_params_from_args(args, kwargs)
if params:
params_dict = convert_params_to_dict(params)
if params_dict:
attributes.update(extract_params_attributes(params_dict))

Check warning on line 202 in agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py

View check run for this annotation

Codecov / codecov/patch

agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py#L198-L202

Added lines #L198 - L202 were not covered by tests

# Extract response information
if return_value and isinstance(return_value, dict):

Check warning on line 205 in agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py

View check run for this annotation

Codecov / codecov/patch

agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py#L205

Added line #L205 was not covered by tests
# Extract model information
if 'model_id' in return_value:
attributes[SpanAttributes.LLM_REQUEST_MODEL] = return_value['model_id']
elif 'model' in return_value:
attributes[SpanAttributes.LLM_REQUEST_MODEL] = return_value['model']

Check warning on line 210 in agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py

View check run for this annotation

Codecov / codecov/patch

agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py#L207-L210

Added lines #L207 - L210 were not covered by tests

# Extract completion from choices
if 'choices' in return_value:
for idx, choice in enumerate(return_value['choices']):
if isinstance(choice, dict) and 'message' in choice:
message = choice['message']
if isinstance(message, dict):
if 'content' in message:
attributes[MessageAttributes.COMPLETION_CONTENT.format(i=idx)] = message['content']
attributes[MessageAttributes.COMPLETION_ROLE.format(i=idx)] = message.get('role', 'assistant')
attributes[MessageAttributes.COMPLETION_TYPE.format(i=idx)] = "text"
if 'finish_reason' in choice:
attributes[SpanAttributes.LLM_RESPONSE_STOP_REASON] = choice['finish_reason']

Check warning on line 223 in agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py

View check run for this annotation

Codecov / codecov/patch

agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py#L213-L223

Added lines #L213 - L223 were not covered by tests

# Extract token usage
if 'usage' in return_value:
usage = return_value['usage']
if isinstance(usage, dict):
if 'prompt_tokens' in usage:
attributes[SpanAttributes.LLM_USAGE_PROMPT_TOKENS] = usage['prompt_tokens']
if 'completion_tokens' in usage:
attributes[SpanAttributes.LLM_USAGE_COMPLETION_TOKENS] = usage['completion_tokens']
if 'total_tokens' in usage:
attributes[SpanAttributes.LLM_USAGE_TOTAL_TOKENS] = usage['total_tokens']

Check warning on line 234 in agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py

View check run for this annotation

Codecov / codecov/patch

agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py#L226-L234

Added lines #L226 - L234 were not covered by tests

# Extract additional metadata
if 'id' in return_value:
attributes['ibm.watsonx.chat.id'] = return_value['id']
if 'model_version' in return_value:
attributes['ibm.watsonx.model.version'] = return_value['model_version']
if 'created_at' in return_value:
attributes['ibm.watsonx.chat.created_at'] = return_value['created_at']

Check warning on line 242 in agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py

View check run for this annotation

Codecov / codecov/patch

agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py#L237-L242

Added lines #L237 - L242 were not covered by tests

return attributes

Check warning on line 244 in agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py

View check run for this annotation

Codecov / codecov/patch

agentops/instrumentation/ibm_watsonx_ai/attributes/attributes.py#L244

Added line #L244 was not covered by tests
Loading
Loading