Skip to content

Same agent Smaller bill. The tools and infrastructure to get more work from your AI. Fewer tokens. Less compute. Lower cost.

[Feat]: Use OpenTelemetry Events for AWS Bedrock Instrumentation (#664)

Patcher1c15725182f0
diff --git a/sdk/python/pyproject.toml b/sdk/python/pyproject.toml
index e9ddd19d..7fd7ffcf 100644
--- a/sdk/python/pyproject.toml
+++ b/sdk/python/pyproject.toml
@@ -1,6 +1,6 @@
[tool.poetry]
name = "openlit"
-version = "1.33.18"
+version = "1.33.19"
description = "OpenTelemetry-native Auto instrumentation library for monitoring LLM Applications and GPUs, facilitating the integration of observability into your GenAI-driven projects"
authors = ["OpenLIT"]
license = "Apache-2.0"
diff --git a/sdk/python/src/openlit/__helpers.py b/sdk/python/src/openlit/__helpers.py
index 7579eecd..539aa1bd 100644
--- a/sdk/python/src/openlit/__helpers.py
+++ b/sdk/python/src/openlit/__helpers.py
@@ -237,27 +237,21 @@ def extract_and_format_input(messages):
them into fixed roles like 'user', 'assistant', 'system', 'tool'.
"""
- fixed_roles = ['user', 'assistant', 'system', 'tool'] # Ensure these are your fixed keys
- # Initialize the dictionary with fixed keys and empty structures
+ fixed_roles = ['user', 'assistant', 'system', 'tool', 'developer']
formatted_messages = {role_key: {'role': '', 'content': ''} for role_key in fixed_roles}
for message in messages:
- # Normalize the message structure
message = response_as_dict(message)
- # Extract role and content
role = message.get('role')
if role not in fixed_roles:
- continue # Skip any role not in our predefined roles
+ continue
content = message.get('content', '')
- # Prepare content as a string
+ # Prepare content as a string, handling both list and str
if isinstance(content, list):
- content_str = ", ".join(
- f'{item.get("type", "text")}: {extract_text_from_item(item)}'
- for item in content
- )
+ content_str = ", ".join(str(item) for item in content)
else:
content_str = content
@@ -272,30 +266,6 @@ def extract_and_format_input(messages):
return formatted_messages
-def extract_text_from_item(item):
- """
- Extract text from inpit message
- """
-
- #pylint: disable=no-else-return
- if item.get('type') == 'text':
- return item.get('text', '')
- elif item.get('type') == 'image':
- # Handle image content specifically checking for 'url' or 'base64'
- source = item.get('source', {})
- if isinstance(source, dict):
- if source.get('type') == 'base64':
- # Return the actual base64 data if present
- return source.get('data', '[Missing base64 data]')
- elif source.get('type') == 'url':
- return source.get('url', '[Missing URL]')
- elif item.get('type') == 'image_url':
- # New format: Handle the 'image_url' type
- image_url = item.get('image_url', {})
- if isinstance(image_url, dict):
- return image_url.get('url', '[Missing image URL]')
- return ''
-
# To be removed one the change to log events (from span events) is complete
def concatenate_all_contents(formatted_messages):
"""
diff --git a/sdk/python/src/openlit/instrumentation/azure_ai_inference/async_azure_ai_inference.py b/sdk/python/src/openlit/instrumentation/azure_ai_inference/async_azure_ai_inference.py
index 522595f1..c72bd7e5 100644
--- a/sdk/python/src/openlit/instrumentation/azure_ai_inference/async_azure_ai_inference.py
+++ b/sdk/python/src/openlit/instrumentation/azure_ai_inference/async_azure_ai_inference.py
@@ -43,10 +43,10 @@ def async_complete(version, environment, application_name,
self.__wrapped__ = wrapped
self._span = span
self._span_name = span_name
- self._llmresponse = ""
- self._response_id = ""
- self._response_model = ""
- self._finish_reason = ""
+ self._llmresponse = ''
+ self._response_id = ''
+ self._response_model = ''
+ self._finish_reason = ''
self._input_tokens = 0
self._output_tokens = 0
@@ -96,7 +96,7 @@ def async_complete(version, environment, application_name,
except Exception as e:
handle_exception(self._span, e)
- logger.error("Error in trace creation: %s", e)
+ logger.error('Error in trace creation: %s', e)
raise
async def wrapper(wrapped, instance, args, kwargs):
@@ -104,11 +104,11 @@ def async_complete(version, environment, application_name,
Wraps the GenAI function call.
"""
- streaming = kwargs.get("stream", False)
- server_address, server_port = set_server_address_and_port(instance, "models.github.ai", 443)
- request_model = kwargs.get("model", "gpt-4o")
+ streaming = kwargs.get('stream', False)
+ server_address, server_port = set_server_address_and_port(instance, 'models.github.ai', 443)
+ request_model = kwargs.get('model', 'gpt-4o')
- span_name = f"{SemanticConvetion.GEN_AI_OPERATION_TYPE_CHAT} {request_model}"
+ span_name = f'{SemanticConvetion.GEN_AI_OPERATION_TYPE_CHAT} {request_model}'
# pylint: disable=no-else-return
if streaming:
diff --git a/sdk/python/src/openlit/instrumentation/azure_ai_inference/azure_ai_inference.py b/sdk/python/src/openlit/instrumentation/azure_ai_inference/azure_ai_inference.py
index 81470d7b..f415af09 100644
--- a/sdk/python/src/openlit/instrumentation/azure_ai_inference/azure_ai_inference.py
+++ b/sdk/python/src/openlit/instrumentation/azure_ai_inference/azure_ai_inference.py
@@ -43,10 +43,10 @@ def complete(version, environment, application_name,
self.__wrapped__ = wrapped
self._span = span
self._span_name = span_name
- self._llmresponse = ""
- self._response_id = ""
- self._response_model = ""
- self._finish_reason = ""
+ self._llmresponse = ''
+ self._response_id = ''
+ self._response_model = ''
+ self._finish_reason = ''
self._input_tokens = 0
self._output_tokens = 0
@@ -96,7 +96,7 @@ def complete(version, environment, application_name,
except Exception as e:
handle_exception(self._span, e)
- logger.error("Error in trace creation: %s", e)
+ logger.error('Error in trace creation: %s', e)
raise
def wrapper(wrapped, instance, args, kwargs):
@@ -104,11 +104,11 @@ def complete(version, environment, application_name,
Wraps the GenAI function call.
"""
- streaming = kwargs.get("stream", False)
- server_address, server_port = set_server_address_and_port(instance, "models.github.ai", 443)
- request_model = kwargs.get("model", "gpt-4o")
+ streaming = kwargs.get('stream', False)
+ server_address, server_port = set_server_address_and_port(instance, 'models.github.ai', 443)
+ request_model = kwargs.get('model', 'gpt-4o')
- span_name = f"{SemanticConvetion.GEN_AI_OPERATION_TYPE_CHAT} {request_model}"
+ span_name = f'{SemanticConvetion.GEN_AI_OPERATION_TYPE_CHAT} {request_model}'
# pylint: disable=no-else-return
if streaming:
diff --git a/sdk/python/src/openlit/instrumentation/azure_ai_inference/utils.py b/sdk/python/src/openlit/instrumentation/azure_ai_inference/utils.py
index 5fc07078..229768e9 100644
--- a/sdk/python/src/openlit/instrumentation/azure_ai_inference/utils.py
+++ b/sdk/python/src/openlit/instrumentation/azure_ai_inference/utils.py
@@ -76,9 +76,9 @@ def common_chat_logic(scope, pricing_info, environment, application_name, metric
scope._span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_TOP_K, scope._kwargs.get('top_k', 1.0))
scope._span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_TOP_P, scope._kwargs.get('top_p', 1.0))
scope._span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_FREQUENCY_PENALTY,
- scope._kwargs.get("frequency_penalty", 0.0))
+ scope._kwargs.get('frequency_penalty', 0.0))
scope._span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_PRESENCE_PENALTY,
- scope._kwargs.get("presence_penalty", 0.0))
+ scope._kwargs.get('presence_penalty', 0.0))
scope._span.set_attribute(SemanticConvetion.GEN_AI_RESPONSE_FINISH_REASON, [scope._finish_reason])
scope._span.set_attribute(SemanticConvetion.GEN_AI_RESPONSE_ID, scope._response_id)
scope._span.set_attribute(SemanticConvetion.GEN_AI_RESPONSE_MODEL, scope._response_model)
diff --git a/sdk/python/src/openlit/instrumentation/bedrock/__init__.py b/sdk/python/src/openlit/instrumentation/bedrock/__init__.py
index d0df6f7f..89a32efc 100644
--- a/sdk/python/src/openlit/instrumentation/bedrock/__init__.py
+++ b/sdk/python/src/openlit/instrumentation/bedrock/__init__.py
@@ -22,6 +22,7 @@ class BedrockInstrumentor(BaseInstrumentor):
application_name = kwargs.get("application_name", "default_application")
environment = kwargs.get("environment", "default_environment")
tracer = kwargs.get("tracer")
+ event_provider = kwargs.get('event_provider')
metrics = kwargs.get("metrics_dict")
pricing_info = kwargs.get("pricing_info", {})
capture_message_content = kwargs.get("capture_message_content", False)
@@ -33,7 +34,7 @@ class BedrockInstrumentor(BaseInstrumentor):
"botocore.client",
"ClientCreator.create_client",
converse(version, environment, application_name,
- tracer, pricing_info, capture_message_content, metrics, disable_metrics),
+ tracer, event_provider, pricing_info, capture_message_content, metrics, disable_metrics),
)
def _uninstrument(self, **kwargs):
diff --git a/sdk/python/src/openlit/instrumentation/bedrock/bedrock.py b/sdk/python/src/openlit/instrumentation/bedrock/bedrock.py
index 69f488b4..1c7ebed2 100644
--- a/sdk/python/src/openlit/instrumentation/bedrock/bedrock.py
+++ b/sdk/python/src/openlit/instrumentation/bedrock/bedrock.py
@@ -4,96 +4,33 @@ Module for monitoring Amazon Bedrock API calls.
import logging
import time
-from botocore.response import StreamingBody
-from botocore.exceptions import ReadTimeoutError, ResponseStreamingError
-from urllib3.exceptions import ProtocolError as URLLib3ProtocolError
-from urllib3.exceptions import ReadTimeoutError as URLLib3ReadTimeoutError
-from opentelemetry.trace import SpanKind, Status, StatusCode
-from opentelemetry.sdk.resources import SERVICE_NAME, TELEMETRY_SDK_NAME, DEPLOYMENT_ENVIRONMENT
+from opentelemetry.trace import SpanKind
from openlit.__helpers import (
- get_chat_model_cost,
- handle_exception,
- response_as_dict,
- create_metrics_attributes,
set_server_address_and_port
)
+from openlit.instrumentation.bedrock.utils import (
+ process_chat_response,
+)
from openlit.semcov import SemanticConvetion
# Initialize logger for logging potential issues and operations
logger = logging.getLogger(__name__)
-class CustomStreamWrapper(StreamingBody):
- """Handle streaming responses with the ability to read multiple times."""
-
- def __init__(self, stream_source, length):
- super().__init__(stream_source, length)
- self._stream_data = None
- self._read_position = 0
-
- def read(self, amt=None):
- if self._stream_data is None:
- try:
- self._stream_data = self._raw_stream.read()
- except URLLib3ReadTimeoutError as error:
- raise ReadTimeoutError(endpoint_url=error.url, error=error) from error
- except URLLib3ProtocolError as error:
- raise ResponseStreamingError(error=error) from error
-
- self._amount_read += len(self._stream_data)
- if amt is None or (not self._stream_data and amt > 0):
- self._verify_content_length()
-
- if amt is None:
- data_chunk = self._stream_data[self._read_position:]
- else:
- data_start = self._read_position
- self._read_position += amt
- data_chunk = self._stream_data[data_start:self._read_position]
-
- return data_chunk
-
-def converse(version, environment, application_name, tracer,
+def converse(version, environment, application_name, tracer, event_provider,
pricing_info, capture_message_content, metrics, disable_metrics):
"""
- Generates a telemetry wrapper for messages to collect metrics.
-
- Args:
- gen_ai_endpoint: Endpoint identifier for logging and tracing.
- version: The monitoring package version.
- environment: Deployment environment (e.g. production, staging).
- application_name: Name of the application using the Bedrock API.
- tracer: OpenTelemetry tracer for creating spans.
- pricing_info: Information for calculating Bedrock usage cost.
- capture_message_content: Whether to trace the actual content.
- metrics: Metrics collector.
- disable_metrics: Flag to toggle metrics collection.
- Returns:
- A function that wraps the chat method to add telemetry.
+ Generates a telemetry wrapper for GenAI function call
"""
def wrapper(wrapped, instance, args, kwargs):
"""
- Wraps an API call to add telemetry.
-
- Args:
- wrapped: Original method.
- instance: Instance of the class.
- args: Positional arguments of the 'messages' method.
- kwargs: Keyword arguments of the 'messages' method.
- Returns:
- Response from the original method.
+ Wraps the GenAI function call.
"""
def converse_wrapper(original_method, *method_args, **method_kwargs):
- """
- Adds instrumentation to the invoke model call.
- Args:
- original_method: The original invoke model method.
- *method_args: Positional arguments for the method.
- **method_kwargs: Keyword arguments for the method.
- Returns:
- The modified response with telemetry.
+ """
+ Wraps the GenAI function call.
"""
server_address, server_port = set_server_address_and_port(instance, 'aws.amazon.com', 443)
@@ -104,146 +41,27 @@ def converse(version, environment, application_name, tracer,
with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
start_time = time.time()
response = original_method(*method_args, **method_kwargs)
- end_time = time.time()
-
- response_dict = response_as_dict(response)
-
- try:
- message_prompt = method_kwargs.get('messages', '')
- formatted_messages = []
- for message in message_prompt:
- role = message['role']
- content = message['content']
-
- if isinstance(content, list):
- content_str = ", ".join(f'text: {item["text"]}' for item in content if "text" in item)
- formatted_messages.append(f'{role}: {content_str}')
- else:
- formatted_messages.append(f'{role}: {content}')
- prompt = '\n'.join(formatted_messages)
-
- input_tokens = response_dict.get('usage').get('inputTokens')
- output_tokens = response_dict.get('usage').get('outputTokens')
-
- # Calculate cost of the operation
- cost = get_chat_model_cost(request_model, pricing_info,
- input_tokens, output_tokens)
-
- llm_response = response_dict.get('output').get('message').get('content')[0].get('text')
-
- # Set base span attribues (OTel Semconv)
- span.set_attribute(TELEMETRY_SDK_NAME, 'openlit')
- span.set_attribute(SemanticConvetion.GEN_AI_OPERATION,
- SemanticConvetion.GEN_AI_OPERATION_TYPE_CHAT)
- span.set_attribute(SemanticConvetion.GEN_AI_SYSTEM,
- SemanticConvetion.GEN_AI_SYSTEM_AWS_BEDROCK)
- span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_MODEL,
- request_model)
- span.set_attribute(SemanticConvetion.SERVER_PORT,
- server_port)
-
- inference_config = method_kwargs.get('inferenceConfig', {})
-
- # List of attributes and their config keys
- attributes = [
- (SemanticConvetion.GEN_AI_REQUEST_FREQUENCY_PENALTY, 'frequencyPenalty'),
- (SemanticConvetion.GEN_AI_REQUEST_MAX_TOKENS, 'maxTokens'),
- (SemanticConvetion.GEN_AI_REQUEST_PRESENCE_PENALTY, 'presencePenalty'),
- (SemanticConvetion.GEN_AI_REQUEST_STOP_SEQUENCES, 'stopSequences'),
- (SemanticConvetion.GEN_AI_REQUEST_TEMPERATURE, 'temperature'),
- (SemanticConvetion.GEN_AI_REQUEST_TOP_P, 'topP'),
- (SemanticConvetion.GEN_AI_REQUEST_TOP_K, 'topK'),
- ]
-
- # Set each attribute if the corresponding value exists and is not None
- for attribute, key in attributes:
- value = inference_config.get(key)
- if value is not None:
- span.set_attribute(attribute, value)
-
- span.set_attribute(SemanticConvetion.GEN_AI_RESPONSE_ID,
- response_dict.get('ResponseMetadata').get('RequestId'))
- span.set_attribute(SemanticConvetion.GEN_AI_RESPONSE_MODEL,
- request_model)
- span.set_attribute(SemanticConvetion.GEN_AI_USAGE_INPUT_TOKENS,
- input_tokens)
- span.set_attribute(SemanticConvetion.GEN_AI_USAGE_OUTPUT_TOKENS,
- output_tokens)
- span.set_attribute(SemanticConvetion.SERVER_ADDRESS,
- server_address)
- if isinstance(llm_response, str):
- span.set_attribute(SemanticConvetion.GEN_AI_OUTPUT_TYPE,
- 'text')
- else:
- span.set_attribute(SemanticConvetion.GEN_AI_OUTPUT_TYPE,
- 'json')
-
- # Set base span attribues (Extras)
- span.set_attribute(DEPLOYMENT_ENVIRONMENT,
- environment)
- span.set_attribute(SERVICE_NAME,
- application_name)
- span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_IS_STREAM,
- False)
- span.set_attribute(SemanticConvetion.GEN_AI_USAGE_TOTAL_TOKENS,
- input_tokens + output_tokens)
- span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COST,
- cost)
- span.set_attribute(SemanticConvetion.GEN_AI_SERVER_TTFT,
- end_time - start_time)
- span.set_attribute(SemanticConvetion.GEN_AI_SDK_VERSION,
- version)
-
- if capture_message_content:
- span.add_event(
- name=SemanticConvetion.GEN_AI_CONTENT_PROMPT_EVENT,
- attributes={
- SemanticConvetion.GEN_AI_CONTENT_PROMPT: prompt,
- },
- )
- span.add_event(
- name=SemanticConvetion.GEN_AI_CONTENT_COMPLETION_EVENT,
- attributes={
- SemanticConvetion.GEN_AI_CONTENT_COMPLETION: llm_response,
- },
- )
-
- span.set_status(Status(StatusCode.OK))
-
- if disable_metrics is False:
- attributes = create_metrics_attributes(
- service_name=application_name,
- deployment_environment=environment,
- operation=SemanticConvetion.GEN_AI_OPERATION_TYPE_CHAT,
- system=SemanticConvetion.GEN_AI_SYSTEM_AWS_BEDROCK,
- request_model=request_model,
- server_address=server_address,
- server_port=server_port,
- response_model=request_model,
- )
-
- metrics['genai_client_usage_tokens'].record(
- input_tokens + output_tokens, attributes
- )
- metrics['genai_client_operation_duration'].record(
- end_time - start_time, attributes
- )
- metrics['genai_server_ttft'].record(
- end_time - start_time, attributes
- )
- metrics['genai_requests'].add(1, attributes)
- metrics['genai_completion_tokens'].add(output_tokens, attributes)
- metrics['genai_prompt_tokens'].add(input_tokens, attributes)
- metrics['genai_cost'].record(cost, attributes)
-
- return response
-
- except Exception as e:
- handle_exception(span, e)
- logger.error('Error in trace creation: %s', e)
-
- # Return original response
- return response
+ llm_config = method_kwargs.get('inferenceConfig', {})
+ response = process_chat_response(
+ response=response,
+ request_model=request_model,
+ pricing_info=pricing_info,
+ server_port=server_port,
+ server_address=server_address,
+ environment=environment,
+ application_name=application_name,
+ metrics=metrics,
+ event_provider=event_provider,
+ start_time=start_time,
+ span=span,
+ capture_message_content=capture_message_content,
+ disable_metrics=disable_metrics,
+ version=version,
+ llm_config=llm_config,
+ **method_kwargs
+ )
+
+ return response
# Get the original client instance from the wrapper
client = wrapped(*args, **kwargs)
diff --git a/sdk/python/src/openlit/instrumentation/bedrock/utils.py b/sdk/python/src/openlit/instrumentation/bedrock/utils.py
new file mode 100644
index 00000000..ef655448
--- /dev/null
+++ b/sdk/python/src/openlit/instrumentation/bedrock/utils.py
@@ -0,0 +1,252 @@
+"""
+AWS Bedrock OpenTelemetry instrumentation utility functions
+"""
+import time
+
+from opentelemetry.sdk.resources import SERVICE_NAME, TELEMETRY_SDK_NAME, DEPLOYMENT_ENVIRONMENT
+from opentelemetry.trace import Status, StatusCode
+
+from openlit.__helpers import (
+ calculate_ttft,
+ response_as_dict,
+ calculate_tbt,
+ extract_and_format_input,
+ get_chat_model_cost,
+ create_metrics_attributes,
+ otel_event,
+ concatenate_all_contents
+)
+from openlit.semcov import SemanticConvetion
+
+def process_chunk(self, chunk):
+ """
+ Process a chunk of response data and update state.
+ """
+
+ end_time = time.time()
+ # Record the timestamp for the current chunk
+ self._timestamps.append(end_time)
+
+ if len(self._timestamps) == 1:
+ # Calculate time to first chunk
+ self._ttft = calculate_ttft(self._timestamps, self._start_time)
+
+ chunked = response_as_dict(chunk)
+
+ # Collect message IDs and input token from events
+ if chunked.get('type') == 'message_start':
+ self._response_id = chunked.get('message').get('id')
+ self._input_tokens = chunked.get('message').get('usage').get('input_tokens')
+ self._response_model = chunked.get('message').get('model')
+ self._response_role = chunked.get('message').get('role')
+
+ # Collect message IDs and aggregated response from events
+ if chunked.get('type') == 'content_block_delta':
+ if chunked.get('delta').get('text'):
+ self._llmresponse += chunked.get('delta').get('text')
+ elif chunked.get('delta').get('partial_json'):
+ self._tool_arguments += chunked.get('delta').get('partial_json')
+
+ if chunked.get('type') == 'content_block_start':
+ if chunked.get('content_block').get('id'):
+ self._tool_id = chunked.get('content_block').get('id')
+ if chunked.get('content_block').get('name'):
+ self._tool_name = chunked.get('content_block').get('name')
+
+ # Collect output tokens and stop reason from events
+ if chunked.get('type') == 'message_delta':
+ self._output_tokens = chunked.get('usage').get('output_tokens')
+ self._finish_reason = chunked.get('delta').get('stop_reason')
+
+def common_chat_logic(scope, pricing_info, environment, application_name, metrics,
+ event_provider, capture_message_content, disable_metrics, version, llm_config, is_stream):
+ """
+ Process chat request and generate Telemetry
+ """
+
+ scope._end_time = time.time()
+ if len(scope._timestamps) > 1:
+ scope._tbt = calculate_tbt(scope._timestamps)
+
+ formatted_messages = extract_and_format_input(scope._kwargs.get('messages', ''))
+ print(formatted_messages)
+ request_model = scope._kwargs.get('model', 'claude-3-opus-20240229')
+
+ cost = get_chat_model_cost(request_model, pricing_info, scope._input_tokens, scope._output_tokens)
+
+ # Set Span attributes (OTel Semconv)
+ scope._span.set_attribute(TELEMETRY_SDK_NAME, 'openlit')
+ scope._span.set_attribute(SemanticConvetion.GEN_AI_OPERATION, SemanticConvetion.GEN_AI_OPERATION_TYPE_CHAT)
+ scope._span.set_attribute(SemanticConvetion.GEN_AI_SYSTEM, SemanticConvetion.GEN_AI_SYSTEM_AWS_BEDROCK)
+ scope._span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_MODEL, request_model)
+ scope._span.set_attribute(SemanticConvetion.SERVER_PORT, scope._server_port)
+
+ # List of attributes and their config keys
+ attributes = [
+ (SemanticConvetion.GEN_AI_REQUEST_FREQUENCY_PENALTY, 'frequencyPenalty'),
+ (SemanticConvetion.GEN_AI_REQUEST_MAX_TOKENS, 'maxTokens'),
+ (SemanticConvetion.GEN_AI_REQUEST_PRESENCE_PENALTY, 'presencePenalty'),
+ (SemanticConvetion.GEN_AI_REQUEST_STOP_SEQUENCES, 'stopSequences'),
+ (SemanticConvetion.GEN_AI_REQUEST_TEMPERATURE, 'temperature'),
+ (SemanticConvetion.GEN_AI_REQUEST_TOP_P, 'topP'),
+ (SemanticConvetion.GEN_AI_REQUEST_TOP_K, 'topK'),
+ ]
+
+ # Set each attribute if the corresponding value exists and is not None
+ for attribute, key in attributes:
+ value = llm_config.get(key)
+ if value is not None:
+ scope._span.set_attribute(attribute, value)
+
+ scope._span.set_attribute(SemanticConvetion.GEN_AI_RESPONSE_FINISH_REASON, [scope._finish_reason])
+ scope._span.set_attribute(SemanticConvetion.GEN_AI_RESPONSE_ID, scope._response_id)
+ scope._span.set_attribute(SemanticConvetion.GEN_AI_RESPONSE_MODEL, scope._response_model)
+ scope._span.set_attribute(SemanticConvetion.GEN_AI_USAGE_INPUT_TOKENS, scope._input_tokens)
+ scope._span.set_attribute(SemanticConvetion.GEN_AI_USAGE_OUTPUT_TOKENS, scope._output_tokens)
+ scope._span.set_attribute(SemanticConvetion.SERVER_ADDRESS, scope._server_address)
+
+ scope._span.set_attribute(SemanticConvetion.GEN_AI_OUTPUT_TYPE,
+ 'text' if isinstance(scope._llmresponse, str) else 'json')
+
+ scope._span.set_attribute(DEPLOYMENT_ENVIRONMENT, environment)
+ scope._span.set_attribute(SERVICE_NAME, application_name)
+ scope._span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_IS_STREAM, is_stream)
+ scope._span.set_attribute(SemanticConvetion.GEN_AI_CLIENT_TOKEN_USAGE, scope._input_tokens + scope._output_tokens)
+ scope._span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COST, cost)
+ scope._span.set_attribute(SemanticConvetion.GEN_AI_SERVER_TBT, scope._tbt)
+ scope._span.set_attribute(SemanticConvetion.GEN_AI_SERVER_TTFT, scope._ttft)
+ scope._span.set_attribute(SemanticConvetion.GEN_AI_SDK_VERSION, version)
+
+ # To be removed one the change to log events (from span events) is complete
+ prompt = concatenate_all_contents(formatted_messages)
+ if capture_message_content:
+ scope._span.add_event(
+ name=SemanticConvetion.GEN_AI_CONTENT_PROMPT_EVENT,
+ attributes={
+ SemanticConvetion.GEN_AI_CONTENT_PROMPT: prompt,
+ },
+ )
+ scope._span.add_event(
+ name=SemanticConvetion.GEN_AI_CONTENT_COMPLETION_EVENT,
+ attributes={
+ SemanticConvetion.GEN_AI_CONTENT_COMPLETION: scope._llmresponse,
+ },
+ )
+
+ choice_event_body = {
+ 'finish_reason': scope._finish_reason,
+ 'index': 0,
+ 'message': {
+ **({'content': scope._llmresponse} if capture_message_content else {}),
+ 'role': scope._response_role
+ }
+ }
+
+ # Emit events
+ for role in ['user', 'system', 'assistant', 'tool']:
+ if formatted_messages.get(role, {}).get('content', ''):
+ event = otel_event(
+ name=getattr(SemanticConvetion, f'GEN_AI_{role.upper()}_MESSAGE'),
+ attributes={
+ SemanticConvetion.GEN_AI_SYSTEM: SemanticConvetion.GEN_AI_SYSTEM_AWS_BEDROCK
+ },
+ body = {
+ # pylint: disable=line-too-long
+ **({'content': formatted_messages.get(role, {}).get('content', '')} if capture_message_content else {}),
+ 'role': formatted_messages.get(role, {}).get('role', []),
+ **({
+ 'tool_calls': {
+ 'function': {
+ # pylint: disable=line-too-long
+ 'name': (scope._tool_calls[0].get('function', {}).get('name', '') if scope._tool_calls else ''),
+ 'arguments': (scope._tool_calls[0].get('function', {}).get('arguments', '') if scope._tool_calls else '')
+ },
+ 'id': (scope._tool_calls[0].get('id', '') if scope._tool_calls else ''),
+ 'type': 'function'
+ }
+ } if role == 'assistant' else {}),
+ **({
+ 'id': (scope._tool_calls[0].get('id', '') if scope._tool_calls else '')
+ } if role == 'tool' else {})
+ }
+ )
+ event_provider.emit(event)
+
+ choice_event = otel_event(
+ name=SemanticConvetion.GEN_AI_CHOICE,
+ attributes={
+ SemanticConvetion.GEN_AI_SYSTEM: SemanticConvetion.GEN_AI_SYSTEM_AWS_BEDROCK
+ },
+ body=choice_event_body
+ )
+ event_provider.emit(choice_event)
+
+ scope._span.set_status(Status(StatusCode.OK))
+
+ if not disable_metrics:
+ metrics_attributes = create_metrics_attributes(
+ service_name=application_name,
+ deployment_environment=environment,
+ operation=SemanticConvetion.GEN_AI_OPERATION_TYPE_CHAT,
+ system=SemanticConvetion.GEN_AI_SYSTEM_AWS_BEDROCK,
+ request_model=request_model,
+ server_address=scope._server_address,
+ server_port=scope._server_port,
+ response_model=scope._response_model,
+ )
+
+ metrics['genai_client_usage_tokens'].record(scope._input_tokens + scope._output_tokens, metrics_attributes)
+ metrics['genai_client_operation_duration'].record(scope._end_time - scope._start_time, metrics_attributes)
+ metrics['genai_server_tbt'].record(scope._tbt, metrics_attributes)
+ metrics['genai_server_ttft'].record(scope._ttft, metrics_attributes)
+ metrics['genai_requests'].add(1, metrics_attributes)
+ metrics['genai_completion_tokens'].add(scope._output_tokens, metrics_attributes)
+ metrics['genai_prompt_tokens'].add(scope._input_tokens, metrics_attributes)
+ metrics['genai_cost'].record(cost, metrics_attributes)
+
+def process_streaming_chat_response(self, pricing_info, environment, application_name, metrics,
+ event_provider, capture_message_content=False, disable_metrics=False, version='', llm_config=''):
+
+ """
+ Process chat request and generate Telemetry
+ """
+ if self._tool_id != '':
+ self._tool_calls = {
+ 'id': self._tool_id,
+ 'name': self._tool_name,
+ 'input': self._tool_arguments
+ }
+
+ common_chat_logic(self, pricing_info, environment, application_name, metrics,
+ event_provider, capture_message_content, disable_metrics, version, llm_config, is_stream=True)
+
+def process_chat_response(response, request_model, pricing_info, server_port, server_address, environment,
+ application_name, metrics, event_provider, start_time, span, capture_message_content=False,
+ disable_metrics=False, version='1.0.0', llm_config='', **kwargs):
+
+ """
+ Process chat request and generate Telemetry
+ """
+
+ self = type('GenericScope', (), {})()
+ response_dict = response_as_dict(response)
+
+ # pylint: disable = no-member
+ self._start_time = start_time
+ self._end_time = time.time()
+ self._span = span
+ self._llmresponse = response_dict.get('output').get('message').get('content')[0].get('text')
+ self._response_role = 'assistant'
+ self._input_tokens = response_dict.get('usage').get('inputTokens')
+ self._output_tokens = response_dict.get('usage').get('outputTokens')
+ self._response_model = request_model
+ self._finish_reason = response_dict.get('stopReason', '')
+ self._response_id = response_dict.get('ResponseMetadata').get('RequestId')
+ self._timestamps = []
+ self._ttft, self._tbt = self._end_time - self._start_time, 0
+ self._server_address, self._server_port = server_address, server_port
+ self._kwargs = kwargs
+ common_chat_logic(self, pricing_info, environment, application_name, metrics,
+ event_provider, capture_message_content, disable_metrics, version, llm_config, is_stream=False)
+
+ return response