|
| 1 | +from langtrace_python_sdk.utils.llm import ( |
| 2 | + get_langtrace_attributes, |
| 3 | + get_extra_attributes, |
| 4 | + get_span_name, |
| 5 | + set_span_attributes, |
| 6 | + get_llm_request_attributes, |
| 7 | + set_event_completion, |
| 8 | + set_usage_attributes, |
| 9 | +) |
| 10 | +from langtrace_python_sdk.constants.instrumentation.common import SERVICE_PROVIDERS |
| 11 | +from langtrace.trace_attributes import FrameworkSpanAttributes |
| 12 | +from opentelemetry.trace.status import Status, StatusCode |
| 13 | +from langtrace.trace_attributes import SpanAttributes |
| 14 | +from opentelemetry.trace import Tracer, SpanKind |
| 15 | + |
| 16 | +from langtrace_python_sdk.utils import deduce_args_and_kwargs, set_span_attribute |
| 17 | +import json |
| 18 | + |
| 19 | + |
| 20 | +def patch_initiate_chat(name, version, tracer: Tracer): |
| 21 | + def traced_method(wrapped, instance, args, kwargs): |
| 22 | + all_params = deduce_args_and_kwargs(wrapped, *args, **kwargs) |
| 23 | + all_params["recipient"] = json.dumps(parse_agent(all_params.get("recipient"))) |
| 24 | + span_attributes = { |
| 25 | + **get_langtrace_attributes( |
| 26 | + service_provider=SERVICE_PROVIDERS["AUTOGEN"], |
| 27 | + version=version, |
| 28 | + vendor_type="framework", |
| 29 | + ), |
| 30 | + "sender": json.dumps(parse_agent(instance)), |
| 31 | + **all_params, |
| 32 | + } |
| 33 | + attributes = FrameworkSpanAttributes(**span_attributes) |
| 34 | + |
| 35 | + with tracer.start_as_current_span( |
| 36 | + name=get_span_name(name), kind=SpanKind.CLIENT |
| 37 | + ) as span: |
| 38 | + try: |
| 39 | + set_span_attributes(span, attributes) |
| 40 | + result = wrapped(*args, **kwargs) |
| 41 | + # set_response_attributes(span, result) |
| 42 | + return result |
| 43 | + except Exception as err: |
| 44 | + # Record the exception in the span |
| 45 | + span.record_exception(err) |
| 46 | + |
| 47 | + # Set the span status to indicate an error |
| 48 | + span.set_status(Status(StatusCode.ERROR, str(err))) |
| 49 | + |
| 50 | + # Reraise the exception to ensure it's not swallowed |
| 51 | + raise |
| 52 | + |
| 53 | + return traced_method |
| 54 | + |
| 55 | + |
| 56 | +def patch_generate_reply(name, version, tracer: Tracer): |
| 57 | + |
| 58 | + def traced_method(wrapped, instance, args, kwargs): |
| 59 | + |
| 60 | + llm_config = instance.llm_config |
| 61 | + kwargs = { |
| 62 | + **kwargs, |
| 63 | + **llm_config.get("config_list")[0], |
| 64 | + } |
| 65 | + service_provider = SERVICE_PROVIDERS["AUTOGEN"] |
| 66 | + |
| 67 | + span_attributes = { |
| 68 | + **get_langtrace_attributes( |
| 69 | + version=version, |
| 70 | + service_provider=service_provider, |
| 71 | + vendor_type="framework", |
| 72 | + ), |
| 73 | + **get_llm_request_attributes( |
| 74 | + kwargs, |
| 75 | + prompts=kwargs.get("messages"), |
| 76 | + ), |
| 77 | + **get_extra_attributes(), |
| 78 | + } |
| 79 | + attributes = FrameworkSpanAttributes(**span_attributes) |
| 80 | + |
| 81 | + with tracer.start_as_current_span( |
| 82 | + name=get_span_name(name), kind=SpanKind.CLIENT |
| 83 | + ) as span: |
| 84 | + try: |
| 85 | + |
| 86 | + result = wrapped(*args, **kwargs) |
| 87 | + |
| 88 | + # if caching is disabled, return result as langtrace will instrument the rest. |
| 89 | + if "cache_seed" in llm_config and llm_config.get("cache_seed") is None: |
| 90 | + return result |
| 91 | + |
| 92 | + set_span_attributes(span, attributes) |
| 93 | + set_event_completion(span, [{"role": "assistant", "content": result}]) |
| 94 | + total_cost, response_model = list(instance.get_total_usage().keys()) |
| 95 | + set_span_attribute( |
| 96 | + span, SpanAttributes.LLM_RESPONSE_MODEL, response_model |
| 97 | + ) |
| 98 | + set_usage_attributes( |
| 99 | + span, instance.get_total_usage().get(response_model) |
| 100 | + ) |
| 101 | + |
| 102 | + return result |
| 103 | + |
| 104 | + except Exception as err: |
| 105 | + # Record the exception in the span |
| 106 | + span.record_exception(err) |
| 107 | + |
| 108 | + # Set the span status to indicate an error |
| 109 | + span.set_status(Status(StatusCode.ERROR, str(err))) |
| 110 | + |
| 111 | + # Reraise the exception to ensure it's not swallowed |
| 112 | + raise |
| 113 | + |
| 114 | + return traced_method |
| 115 | + |
| 116 | + |
| 117 | +def set_response_attributes(span, result): |
| 118 | + summary = getattr(result, "summary", None) |
| 119 | + if summary: |
| 120 | + set_span_attribute(span, "autogen.chat.summary", summary) |
| 121 | + |
| 122 | + |
| 123 | +def parse_agent(agent): |
| 124 | + |
| 125 | + return { |
| 126 | + "name": getattr(agent, "name", None), |
| 127 | + "description": getattr(agent, "description", None), |
| 128 | + "system_message": str(getattr(agent, "system_message", None)), |
| 129 | + "silent": getattr(agent, "silent", None), |
| 130 | + "llm_config": str(getattr(agent, "llm_config", None)), |
| 131 | + "human_input_mode": getattr(agent, "human_input_mode", None), |
| 132 | + } |
0 commit comments