Skip to content

Commit a9337d5

Browse files
committed
response
1 parent 4ba8ae7 commit a9337d5

File tree

3 files changed

+76
-19
lines changed
  • instrumentation-genai
    • opentelemetry-instrumentation-cohere-v2/src/opentelemetry/instrumentation/cohere_v2
    • opentelemetry-instrumentation-openai-v2/src/opentelemetry/instrumentation/openai_v2

3 files changed

+76
-19
lines changed

instrumentation-genai/opentelemetry-instrumentation-cohere-v2/src/opentelemetry/instrumentation/cohere_v2/patch.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from opentelemetry._events import Event, EventLogger
16-
from opentelemetry.trace import Span, SpanKind, Tracer
15+
from opentelemetry._events import EventLogger
16+
from opentelemetry.trace import SpanKind, Tracer
1717
from opentelemetry.instrumentation.genai_utils import (
1818
get_span_name,
1919
handle_span_exception
@@ -22,7 +22,8 @@
2222
from .utils import (
2323
get_llm_request_attributes,
2424
message_to_event,
25-
set_server_address_and_port
25+
set_response_attributes,
26+
set_server_address_and_port,
2627
)
2728

2829

@@ -52,11 +53,10 @@ def traced_method(wrapped, instance, args, kwargs):
5253

5354
try:
5455
result = wrapped(*args, **kwargs)
55-
# if span.is_recording():
56-
# _set_response_attributes(
57-
# span, result, event_logger, capture_content
58-
# )
59-
span.end()
56+
if span.is_recording():
57+
set_response_attributes(
58+
span, result, event_logger, capture_content
59+
)
6060
return result
6161

6262
except Exception as error:

instrumentation-genai/opentelemetry-instrumentation-cohere-v2/src/opentelemetry/instrumentation/cohere_v2/utils.py

Lines changed: 68 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,23 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
import cohere
1415

15-
from typing import Optional, Union
16+
from typing import List, Optional, Union
1617
from urllib.parse import urlparse
1718

18-
from opentelemetry._events import Event
19+
from opentelemetry._events import Event, EventLogger
1920
from opentelemetry.semconv._incubating.attributes import (
2021
gen_ai_attributes as GenAIAttributes,
2122
)
2223
from opentelemetry.semconv._incubating.attributes import (
2324
server_attributes as ServerAttributes,
2425
)
26+
from opentelemetry.trace import Span
2527

2628

27-
def extract_tool_calls(item, capture_content):
28-
tool_calls = get_property_value(item, "tool_calls")
29+
def extract_tool_calls(item: Union[cohere.types.ChatMessageV2, cohere.AssistantMessageResponse], capture_content: bool):
30+
tool_calls: Optional[List[cohere.ToolCallV2]] = get_property_value(item, "tool_calls")
2931
if tool_calls is None:
3032
return None
3133

@@ -65,7 +67,7 @@ def get_property_value(obj, property_name):
6567
return getattr(obj, property_name, None)
6668

6769

68-
def set_server_address_and_port(client_instance, attributes):
70+
def set_server_address_and_port(client_instance: cohere.client_v2.V2Client, attributes):
6971
base_client = getattr(client_instance, "_client_wrapper", None)
7072
base_url = getattr(base_client, "base_url", None)
7173
if not base_url:
@@ -82,7 +84,7 @@ def set_server_address_and_port(client_instance, attributes):
8284

8385
def get_llm_request_attributes(
8486
kwargs,
85-
client_instance,
87+
client_instance: cohere.client_v2.V2Client,
8688
operation_name=GenAIAttributes.GenAiOperationNameValues.CHAT.value,
8789
):
8890
attributes = {
@@ -116,7 +118,7 @@ def get_llm_request_attributes(
116118
return {k: v for k, v in attributes.items() if v is not None}
117119

118120

119-
def message_to_event(message, capture_content):
121+
def message_to_event(message: cohere.types.ChatMessageV2, capture_content: bool) -> Event:
120122
attributes = {
121123
GenAIAttributes.GEN_AI_SYSTEM: GenAIAttributes.GenAiSystemValues.COHERE.value
122124
}
@@ -140,3 +142,62 @@ def message_to_event(message, capture_content):
140142
attributes=attributes,
141143
body=body if body else None,
142144
)
145+
146+
147+
def set_response_attributes(
148+
span: Span, result: cohere.ChatResponse, event_logger: EventLogger, capture_content: bool
149+
):
150+
event_logger.emit(_response_to_event(result, capture_content))
151+
152+
span.set_attribute(
153+
GenAIAttributes.GEN_AI_RESPONSE_FINISH_REASONS,
154+
[result.finish_reason],
155+
)
156+
157+
if getattr(result, "id", None):
158+
span.set_attribute(GenAIAttributes.GEN_AI_RESPONSE_ID, result.id)
159+
160+
# Get the usage
161+
if getattr(result, "usage", None):
162+
if getattr(result.usage, "tokens"):
163+
span.set_attribute(
164+
GenAIAttributes.GEN_AI_USAGE_INPUT_TOKENS,
165+
result.usage.tokens.input_tokens,
166+
)
167+
span.set_attribute(
168+
GenAIAttributes.GEN_AI_USAGE_OUTPUT_TOKENS,
169+
result.usage.tokens.output_tokens,
170+
)
171+
172+
173+
def _response_to_event(response: cohere.ChatResponse, capture_content):
174+
attributes = {
175+
GenAIAttributes.GEN_AI_SYSTEM: GenAIAttributes.GenAiSystemValues.COHERE.value
176+
}
177+
178+
body = {
179+
"id": response.id,
180+
"finish_reason": response.finish_reason or "error",
181+
}
182+
183+
if response.message:
184+
message = {
185+
"role": (
186+
response.message.role
187+
if response.message.role and response.message.role != "assistant"
188+
else None
189+
)
190+
}
191+
tool_calls = extract_tool_calls(response.message, capture_content)
192+
if tool_calls:
193+
message["tool_calls"] = tool_calls
194+
content: List[cohere.AssistantMessageResponseContentItem] = get_property_value(response.message, "content")
195+
if capture_content and content:
196+
message["content"] = content
197+
body["message"] = message
198+
199+
return Event(
200+
name="gen_ai.choice",
201+
attributes=attributes,
202+
body=body,
203+
)

instrumentation-genai/opentelemetry-instrumentation-openai-v2/src/opentelemetry/instrumentation/openai_v2/utils.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,6 @@
2626
from opentelemetry.semconv._incubating.attributes import (
2727
server_attributes as ServerAttributes,
2828
)
29-
from opentelemetry.semconv.attributes import (
30-
error_attributes as ErrorAttributes,
31-
)
32-
from opentelemetry.trace.status import Status, StatusCode
3329

3430
OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT = (
3531
"OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT"

0 commit comments

Comments
 (0)