monocle-apptrace 0.3.1b1__py3-none-any.whl → 0.4.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of monocle-apptrace might be problematic. Click here for more details.

Files changed (46) hide show
  1. monocle_apptrace/exporters/aws/s3_exporter.py +3 -1
  2. monocle_apptrace/exporters/azure/blob_exporter.py +2 -2
  3. monocle_apptrace/exporters/base_exporter.py +10 -4
  4. monocle_apptrace/exporters/file_exporter.py +19 -4
  5. monocle_apptrace/exporters/monocle_exporters.py +8 -5
  6. monocle_apptrace/exporters/okahu/okahu_exporter.py +5 -2
  7. monocle_apptrace/instrumentation/common/__init__.py +1 -1
  8. monocle_apptrace/instrumentation/common/constants.py +12 -5
  9. monocle_apptrace/instrumentation/common/instrumentor.py +44 -22
  10. monocle_apptrace/instrumentation/common/span_handler.py +102 -50
  11. monocle_apptrace/instrumentation/common/tracing.md +68 -0
  12. monocle_apptrace/instrumentation/common/utils.py +114 -63
  13. monocle_apptrace/instrumentation/common/wrapper.py +202 -47
  14. monocle_apptrace/instrumentation/common/wrapper_method.py +15 -7
  15. monocle_apptrace/instrumentation/metamodel/aiohttp/__init__.py +0 -0
  16. monocle_apptrace/instrumentation/metamodel/aiohttp/_helper.py +66 -0
  17. monocle_apptrace/instrumentation/metamodel/aiohttp/entities/http.py +51 -0
  18. monocle_apptrace/instrumentation/metamodel/aiohttp/methods.py +13 -0
  19. monocle_apptrace/instrumentation/metamodel/anthropic/methods.py +4 -2
  20. monocle_apptrace/instrumentation/metamodel/flask/_helper.py +50 -3
  21. monocle_apptrace/instrumentation/metamodel/flask/entities/http.py +48 -0
  22. monocle_apptrace/instrumentation/metamodel/flask/methods.py +10 -1
  23. monocle_apptrace/instrumentation/metamodel/haystack/_helper.py +17 -4
  24. monocle_apptrace/instrumentation/metamodel/haystack/entities/inference.py +5 -2
  25. monocle_apptrace/instrumentation/metamodel/haystack/methods.py +8 -4
  26. monocle_apptrace/instrumentation/metamodel/langchain/_helper.py +12 -4
  27. monocle_apptrace/instrumentation/metamodel/langchain/entities/inference.py +1 -1
  28. monocle_apptrace/instrumentation/metamodel/langchain/methods.py +6 -14
  29. monocle_apptrace/instrumentation/metamodel/llamaindex/_helper.py +13 -9
  30. monocle_apptrace/instrumentation/metamodel/llamaindex/entities/inference.py +1 -1
  31. monocle_apptrace/instrumentation/metamodel/llamaindex/methods.py +16 -15
  32. monocle_apptrace/instrumentation/metamodel/openai/_helper.py +26 -5
  33. monocle_apptrace/instrumentation/metamodel/openai/entities/inference.py +184 -26
  34. monocle_apptrace/instrumentation/metamodel/openai/methods.py +6 -8
  35. monocle_apptrace/instrumentation/metamodel/requests/_helper.py +31 -0
  36. monocle_apptrace/instrumentation/metamodel/requests/entities/http.py +51 -0
  37. monocle_apptrace/instrumentation/metamodel/requests/methods.py +2 -1
  38. monocle_apptrace/instrumentation/metamodel/teamsai/_helper.py +55 -5
  39. monocle_apptrace/instrumentation/metamodel/teamsai/entities/inference/actionplanner_output_processor.py +13 -33
  40. monocle_apptrace/instrumentation/metamodel/teamsai/entities/inference/teamsai_output_processor.py +24 -20
  41. monocle_apptrace/instrumentation/metamodel/teamsai/methods.py +54 -8
  42. {monocle_apptrace-0.3.1b1.dist-info → monocle_apptrace-0.4.0.dist-info}/METADATA +22 -18
  43. {monocle_apptrace-0.3.1b1.dist-info → monocle_apptrace-0.4.0.dist-info}/RECORD +46 -39
  44. {monocle_apptrace-0.3.1b1.dist-info → monocle_apptrace-0.4.0.dist-info}/WHEEL +0 -0
  45. {monocle_apptrace-0.3.1b1.dist-info → monocle_apptrace-0.4.0.dist-info}/licenses/LICENSE +0 -0
  46. {monocle_apptrace-0.3.1b1.dist-info → monocle_apptrace-0.4.0.dist-info}/licenses/NOTICE +0 -0
@@ -1,4 +1,5 @@
1
1
  import logging
2
+
2
3
  from monocle_apptrace.instrumentation.common.utils import (
3
4
  Option,
4
5
  get_keys_as_tuple,
@@ -11,13 +12,19 @@ logger = logging.getLogger(__name__)
11
12
  def extract_messages(kwargs):
12
13
  try:
13
14
  messages = []
15
+ system_message, user_message = None,None
14
16
  if isinstance(kwargs, dict):
15
17
  if 'system_prompt' in kwargs and kwargs['system_prompt']:
16
18
  system_message = kwargs['system_prompt']
17
- messages.append({"system" : system_message})
18
19
  if 'prompt' in kwargs and kwargs['prompt']:
19
20
  user_message = extract_question_from_prompt(kwargs['prompt'])
21
+ if 'messages' in kwargs and len(kwargs['messages'])>1:
22
+ system_message = kwargs['messages'][0].text
23
+ user_message = kwargs['messages'][1].text
24
+ if system_message and user_message:
25
+ messages.append({"system": system_message})
20
26
  messages.append({"user": user_message})
27
+
21
28
  return [str(message) for message in messages]
22
29
  except Exception as e:
23
30
  logger.warning("Warning: Error occurred in extract_messages: %s", str(e))
@@ -52,6 +59,8 @@ def extract_assistant_message(response):
52
59
  reply = response["replies"][0]
53
60
  if hasattr(reply, 'content'):
54
61
  return [reply.content]
62
+ if hasattr(reply, 'text'):
63
+ return [reply.text]
55
64
  return [reply]
56
65
  except Exception as e:
57
66
  logger.warning("Warning: Error occurred in extract_assistant_message: %s", str(e))
@@ -108,15 +117,19 @@ def extract_embeding_model(instance):
108
117
 
109
118
  def update_span_from_llm_response(response, instance):
110
119
  meta_dict = {}
111
- if response is not None and isinstance(response, dict) and "meta" in response:
112
- token_usage = response["meta"][0]["usage"]
120
+ token_usage = None
121
+ if response is not None and isinstance(response, dict):
122
+ if "meta" in response:
123
+ token_usage = response["meta"][0]["usage"]
124
+ elif "replies" in response: # and "meta" in response["replies"][0]:
125
+ token_usage = response["replies"][0].meta["usage"]
113
126
  if token_usage is not None:
114
127
  temperature = instance.__dict__.get("temperature", None)
115
128
  meta_dict.update({"temperature": temperature})
116
129
  meta_dict.update(
117
130
  {"completion_tokens": token_usage.get("completion_tokens") or token_usage.get("output_tokens")})
118
131
  meta_dict.update({"prompt_tokens": token_usage.get("prompt_tokens") or token_usage.get("input_tokens")})
119
- meta_dict.update({"total_tokens": token_usage.get("total_tokens")})
132
+ meta_dict.update({"total_tokens": token_usage.get("total_tokens") or token_usage.get("completion_tokens")+token_usage.get("prompt_tokens")})
120
133
  return meta_dict
121
134
 
122
135
 
@@ -1,15 +1,18 @@
1
1
  from monocle_apptrace.instrumentation.metamodel.haystack import (
2
2
  _helper,
3
3
  )
4
+ from monocle_apptrace.instrumentation.common.utils import get_llm_type
4
5
 
5
6
  INFERENCE = {
6
- "type": "inference",
7
+ "type": "inference.framework",
7
8
  "attributes": [
8
9
  [
9
10
  {
10
11
  "_comment": "provider type ,name , deployment , inference_endpoint",
11
12
  "attribute": "type",
12
- "accessor": lambda arguments: 'inference.azure_openai'
13
+ # "accessor": lambda arguments: 'inference.azure_openai'
14
+ "accessor": lambda arguments: 'inference.' + (get_llm_type(arguments['instance']) or 'generic')
15
+
13
16
  },
14
17
  {
15
18
  "attribute": "provider_name",
@@ -7,7 +7,6 @@ HAYSTACK_METHODS = [
7
7
  "package": "haystack.components.retrievers.in_memory",
8
8
  "object": "InMemoryEmbeddingRetriever",
9
9
  "method": "run",
10
- "span_name": "haystack.retriever",
11
10
  "wrapper_method": task_wrapper,
12
11
  "output_processor": RETRIEVAL
13
12
  },
@@ -15,7 +14,6 @@ HAYSTACK_METHODS = [
15
14
  "package": "haystack_integrations.components.retrievers.opensearch",
16
15
  "object": "OpenSearchEmbeddingRetriever",
17
16
  "method": "run",
18
- "span_name": "haystack.retriever",
19
17
  "wrapper_method": task_wrapper,
20
18
  "output_processor": RETRIEVAL
21
19
  },
@@ -37,7 +35,13 @@ HAYSTACK_METHODS = [
37
35
  "package": "haystack.core.pipeline.pipeline",
38
36
  "object": "Pipeline",
39
37
  "method": "run",
38
+ "wrapper_method": task_wrapper
39
+ },
40
+ {
41
+ "package": "haystack_integrations.components.generators.anthropic",
42
+ "object": "AnthropicChatGenerator",
43
+ "method": "run",
40
44
  "wrapper_method": task_wrapper,
41
- "span_type": "workflow"
42
- }
45
+ "output_processor": INFERENCE
46
+ },
43
47
  ]
@@ -50,14 +50,22 @@ def extract_assistant_message(response):
50
50
 
51
51
 
52
52
  def extract_provider_name(instance):
53
- provider_url: Option[str] = try_option(getattr, instance.client._client.base_url, 'host')
53
+ provider_url: Option[str] = None
54
+ if hasattr(instance,'client'):
55
+ provider_url: Option[str] = try_option(getattr, instance.client._client.base_url, 'host')
56
+ if hasattr(instance, '_client'):
57
+ provider_url = try_option(getattr, instance._client.base_url, 'host')
54
58
  return provider_url.unwrap_or(None)
55
59
 
56
60
 
57
61
  def extract_inference_endpoint(instance):
58
- inference_endpoint: Option[str] = try_option(getattr, instance.client._client, 'base_url').map(str)
59
- if inference_endpoint.is_none() and "meta" in instance.client.__dict__:
60
- inference_endpoint = try_option(getattr, instance.client.meta, 'endpoint_url').map(str)
62
+ inference_endpoint: Option[str] = None
63
+ if hasattr(instance,'client'):
64
+ inference_endpoint: Option[str] = try_option(getattr, instance.client._client, 'base_url').map(str)
65
+ if inference_endpoint.is_none() and "meta" in instance.client.__dict__:
66
+ inference_endpoint = try_option(getattr, instance.client.meta, 'endpoint_url').map(str)
67
+ if hasattr(instance,'_client'):
68
+ inference_endpoint = try_option(getattr, instance._client, 'base_url').map(str)
61
69
 
62
70
  return inference_endpoint.unwrap_or(extract_provider_name(instance))
63
71
 
@@ -4,7 +4,7 @@ from monocle_apptrace.instrumentation.metamodel.langchain import (
4
4
  from monocle_apptrace.instrumentation.common.utils import resolve_from_alias, get_llm_type
5
5
 
6
6
  INFERENCE = {
7
- "type": "inference",
7
+ "type": "inference.framework",
8
8
  "attributes": [
9
9
  [
10
10
  {
@@ -11,15 +11,13 @@ LANGCHAIN_METHODS = [
11
11
  "package": "langchain.prompts.base",
12
12
  "object": "BasePromptTemplate",
13
13
  "method": "invoke",
14
- "wrapper_method": task_wrapper,
15
- "span_type": "workflow"
14
+ "wrapper_method": task_wrapper
16
15
  },
17
16
  {
18
17
  "package": "langchain.prompts.base",
19
18
  "object": "BasePromptTemplate",
20
19
  "method": "ainvoke",
21
- "wrapper_method": atask_wrapper,
22
- "span_type": "workflow"
20
+ "wrapper_method": atask_wrapper
23
21
  },
24
22
  {
25
23
  "package": "langchain.chat_models.base",
@@ -82,30 +80,24 @@ LANGCHAIN_METHODS = [
82
80
  "package": "langchain.schema",
83
81
  "object": "BaseOutputParser",
84
82
  "method": "invoke",
85
- "wrapper_method": task_wrapper,
86
- "span_type": "workflow"
83
+ "wrapper_method": task_wrapper
87
84
  },
88
85
  {
89
86
  "package": "langchain.schema",
90
87
  "object": "BaseOutputParser",
91
88
  "method": "ainvoke",
92
- "wrapper_method": atask_wrapper,
93
- "span_type": "workflow"
89
+ "wrapper_method": atask_wrapper
94
90
  },
95
91
  {
96
92
  "package": "langchain.schema.runnable",
97
93
  "object": "RunnableSequence",
98
94
  "method": "invoke",
99
- "span_name": "langchain.workflow",
100
- "wrapper_method": task_wrapper,
101
- "span_type": "workflow"
95
+ "wrapper_method": task_wrapper
102
96
  },
103
97
  {
104
98
  "package": "langchain.schema.runnable",
105
99
  "object": "RunnableSequence",
106
100
  "method": "ainvoke",
107
- "span_name": "langchain.workflow",
108
- "wrapper_method": atask_wrapper,
109
- "span_type": "workflow"
101
+ "wrapper_method": atask_wrapper
110
102
  }
111
103
  ]
@@ -96,12 +96,19 @@ def extract_query_from_content(content):
96
96
 
97
97
 
98
98
  def extract_provider_name(instance):
99
- provider_url = try_option(getattr, instance, 'api_base').and_then(lambda url: urlparse(url).hostname)
100
- return provider_url
99
+ if hasattr(instance,'api_base'):
100
+ provider_url: Option[str]= try_option(getattr, instance, 'api_base').and_then(lambda url: urlparse(url).hostname)
101
+ if hasattr(instance,'_client'):
102
+ provider_url:Option[str] = try_option(getattr, instance._client.base_url,'host')
103
+ return provider_url.unwrap_or(None)
101
104
 
102
105
 
103
106
  def extract_inference_endpoint(instance):
104
- inference_endpoint = try_option(getattr, instance._client.sdk_configuration, 'server_url').map(str)
107
+ if hasattr(instance,'_client'):
108
+ if hasattr(instance._client,'sdk_configuration'):
109
+ inference_endpoint: Option[str] = try_option(getattr, instance._client.sdk_configuration, 'server_url').map(str)
110
+ if hasattr(instance._client,'base_url'):
111
+ inference_endpoint: Option[str] = try_option(getattr, instance._client, 'base_url').map(str)
105
112
  return inference_endpoint.unwrap_or(extract_provider_name(instance))
106
113
 
107
114
 
@@ -163,10 +170,7 @@ def update_span_from_llm_response(response, instance):
163
170
  if token_usage is not None:
164
171
  temperature = instance.__dict__.get("temperature", None)
165
172
  meta_dict.update({"temperature": temperature})
166
- if getattr(token_usage, "completion_tokens", None):
167
- meta_dict.update({"completion_tokens": getattr(token_usage, "completion_tokens")})
168
- if getattr(token_usage, "prompt_tokens", None):
169
- meta_dict.update({"prompt_tokens": getattr(token_usage, "prompt_tokens")})
170
- if getattr(token_usage, "total_tokens", None):
171
- meta_dict.update({"total_tokens": getattr(token_usage, "total_tokens")})
173
+ meta_dict.update({"completion_tokens": getattr(token_usage, "completion_tokens",None) or getattr(token_usage,"output_tokens",None)})
174
+ meta_dict.update({"prompt_tokens": getattr(token_usage, "prompt_tokens",None) or getattr(token_usage,"input_tokens",None)})
175
+ meta_dict.update({"total_tokens": getattr(token_usage, "total_tokens",None) or getattr(token_usage,"output_tokens",None)+getattr(token_usage,"input_tokens",None)})
172
176
  return meta_dict
@@ -4,7 +4,7 @@ from monocle_apptrace.instrumentation.metamodel.llamaindex import (
4
4
  from monocle_apptrace.instrumentation.common.utils import resolve_from_alias, get_llm_type
5
5
 
6
6
  INFERENCE = {
7
- "type": "inference",
7
+ "type": "inference.framework",
8
8
  "attributes": [
9
9
  [
10
10
  {
@@ -13,7 +13,6 @@ LLAMAINDEX_METHODS = [
13
13
  "package": "llama_index.core.indices.base_retriever",
14
14
  "object": "BaseRetriever",
15
15
  "method": "retrieve",
16
- "span_name": "llamaindex.retrieve",
17
16
  "wrapper_method": task_wrapper,
18
17
  "output_processor": RETRIEVAL
19
18
  },
@@ -21,7 +20,6 @@ LLAMAINDEX_METHODS = [
21
20
  "package": "llama_index.core.indices.base_retriever",
22
21
  "object": "BaseRetriever",
23
22
  "method": "aretrieve",
24
- "span_name": "llamaindex.retrieve",
25
23
  "wrapper_method": atask_wrapper,
26
24
  "output_processor": RETRIEVAL
27
25
  },
@@ -29,23 +27,18 @@ LLAMAINDEX_METHODS = [
29
27
  "package": "llama_index.core.base.base_query_engine",
30
28
  "object": "BaseQueryEngine",
31
29
  "method": "query",
32
- "span_name": "llamaindex.query",
33
- "wrapper_method": task_wrapper,
34
- "span_type": "workflow"
30
+ "wrapper_method": task_wrapper
35
31
  },
36
32
  {
37
33
  "package": "llama_index.core.base.base_query_engine",
38
34
  "object": "BaseQueryEngine",
39
35
  "method": "aquery",
40
- "span_name": "llamaindex.query",
41
- "wrapper_method": atask_wrapper,
42
- "span_type": "workflow"
36
+ "wrapper_method": atask_wrapper
43
37
  },
44
38
  {
45
39
  "package": "llama_index.core.llms.custom",
46
40
  "object": "CustomLLM",
47
41
  "method": "chat",
48
- "span_name": "llamaindex.llmchat",
49
42
  "wrapper_method": task_wrapper,
50
43
  "output_processor": INFERENCE
51
44
  },
@@ -53,7 +46,6 @@ LLAMAINDEX_METHODS = [
53
46
  "package": "llama_index.core.llms.custom",
54
47
  "object": "CustomLLM",
55
48
  "method": "achat",
56
- "span_name": "llamaindex.llmchat",
57
49
  "wrapper_method": atask_wrapper,
58
50
  "output_processor": INFERENCE,
59
51
 
@@ -62,7 +54,6 @@ LLAMAINDEX_METHODS = [
62
54
  "package": "llama_index.llms.openai.base",
63
55
  "object": "OpenAI",
64
56
  "method": "chat",
65
- "span_name": "llamaindex.openai",
66
57
  "wrapper_method": task_wrapper,
67
58
  "output_processor": INFERENCE
68
59
  },
@@ -70,7 +61,6 @@ LLAMAINDEX_METHODS = [
70
61
  "package": "llama_index.llms.openai.base",
71
62
  "object": "OpenAI",
72
63
  "method": "achat",
73
- "span_name": "llamaindex.openai",
74
64
  "wrapper_method": atask_wrapper,
75
65
  "output_processor": INFERENCE
76
66
  },
@@ -78,7 +68,6 @@ LLAMAINDEX_METHODS = [
78
68
  "package": "llama_index.llms.mistralai.base",
79
69
  "object": "MistralAI",
80
70
  "method": "chat",
81
- "span_name": "llamaindex.mistralai",
82
71
  "wrapper_method": task_wrapper,
83
72
  "output_processor": INFERENCE
84
73
  },
@@ -86,7 +75,6 @@ LLAMAINDEX_METHODS = [
86
75
  "package": "llama_index.llms.mistralai.base",
87
76
  "object": "MistralAI",
88
77
  "method": "achat",
89
- "span_name": "llamaindex.mistralai",
90
78
  "wrapper_method": atask_wrapper,
91
79
  "output_processor": INFERENCE
92
80
  },
@@ -94,8 +82,21 @@ LLAMAINDEX_METHODS = [
94
82
  "package": "llama_index.core.agent",
95
83
  "object": "ReActAgent",
96
84
  "method": "chat",
97
- "span_name": "react.agent",
98
85
  "wrapper_method": task_wrapper,
99
86
  "output_processor": AGENT
87
+ },
88
+ {
89
+ "package": "llama_index.llms.anthropic",
90
+ "object": "Anthropic",
91
+ "method": "chat",
92
+ "wrapper_method": task_wrapper,
93
+ "output_processor": INFERENCE
94
+ },
95
+ {
96
+ "package": "llama_index.llms.anthropic",
97
+ "object": "Anthropic",
98
+ "method": "achat",
99
+ "wrapper_method": atask_wrapper,
100
+ "output_processor": INFERENCE
100
101
  }
101
102
  ]
@@ -6,11 +6,11 @@ and assistant messages from various input formats.
6
6
  import logging
7
7
  from monocle_apptrace.instrumentation.common.utils import (
8
8
  Option,
9
- get_keys_as_tuple,
10
- get_nested_value,
11
9
  try_option,
10
+ get_exception_message,
11
+ get_parent_span
12
12
  )
13
-
13
+ from monocle_apptrace.instrumentation.common.span_handler import NonFrameworkSpanHandler, WORKFLOW_TYPE_MAP
14
14
 
15
15
  logger = logging.getLogger(__name__)
16
16
 
@@ -34,8 +34,11 @@ def extract_messages(kwargs):
34
34
  return []
35
35
 
36
36
 
37
- def extract_assistant_message(response):
37
+ def extract_assistant_message(arguments):
38
38
  try:
39
+ if arguments["exception"] is not None:
40
+ return get_exception_message(arguments)
41
+ response = arguments["result"]
39
42
  if hasattr(response,"output_text") and len(response.output_text):
40
43
  return response.output_text
41
44
  if response is not None and hasattr(response,"choices") and len(response.choices) >0:
@@ -114,4 +117,22 @@ def get_inference_type(instance):
114
117
  if inference_type.unwrap_or(None):
115
118
  return 'azure_openai'
116
119
  else:
117
- return 'openai'
120
+ return 'openai'
121
+
122
+ class OpenAISpanHandler(NonFrameworkSpanHandler):
123
+ def is_teams_span_in_progress(self) -> bool:
124
+ return self.is_framework_span_in_progess() and self.get_workflow_name_in_progress() == WORKFLOW_TYPE_MAP["teams.ai"]
125
+
126
+ # If openAI is being called by Teams AI SDK, then retain the metadata part of the span events
127
+ def skip_processor(self, to_wrap, wrapped, instance, span, args, kwargs) -> list[str]:
128
+ if self.is_teams_span_in_progress():
129
+ return ["attributes", "events.data.input", "events.data.output"]
130
+ else:
131
+ return super().skip_processor(to_wrap, wrapped, instance, span, args, kwargs)
132
+
133
+ def hydrate_events(self, to_wrap, wrapped, instance, args, kwargs, ret_result, span, parent_span=None, ex:Exception=None) -> bool:
134
+ # If openAI is being called by Teams AI SDK, then copy parent
135
+ if self.is_teams_span_in_progress() and ex is None:
136
+ return super().hydrate_events(to_wrap, wrapped, instance, args, kwargs, ret_result, span=parent_span, parent_span=None, ex=ex)
137
+
138
+ return super().hydrate_events(to_wrap, wrapped, instance, args, kwargs, ret_result, span, parent_span=parent_span, ex=ex)
@@ -1,71 +1,229 @@
1
+ import logging
2
+ import random
3
+ import time
4
+ from types import SimpleNamespace
1
5
  from monocle_apptrace.instrumentation.metamodel.openai import (
2
6
  _helper,
3
7
  )
4
- from monocle_apptrace.instrumentation.common.utils import resolve_from_alias
8
+ from monocle_apptrace.instrumentation.common.utils import (
9
+ patch_instance_method,
10
+ resolve_from_alias,
11
+ get_status,
12
+ get_exception_status_code
13
+ )
14
+
15
+ logger = logging.getLogger(__name__)
16
+
17
+
18
+ def process_stream(to_wrap, response, span_processor):
19
+ waiting_for_first_token = True
20
+ stream_start_time = time.time_ns()
21
+ first_token_time = stream_start_time
22
+ stream_closed_time = None
23
+ accumulated_response = ""
24
+ token_usage = None
25
+ accumulated_temp_list = []
26
+
27
+ if to_wrap and hasattr(response, "__iter__"):
28
+ original_iter = response.__iter__
29
+
30
+ def new_iter(self):
31
+ nonlocal waiting_for_first_token, first_token_time, stream_closed_time, accumulated_response, token_usage
32
+
33
+ for item in original_iter():
34
+ try:
35
+ if (
36
+ item.choices
37
+ and item.choices[0].delta
38
+ and item.choices[0].delta.content
39
+ ):
40
+ if waiting_for_first_token:
41
+ waiting_for_first_token = False
42
+ first_token_time = time.time_ns()
43
+
44
+ accumulated_response += item.choices[0].delta.content
45
+ # token_usage = item.usage
46
+ elif item.object == "chat.completion.chunk" and item.usage:
47
+ # Handle the case where the response is a chunk
48
+ token_usage = item.usage
49
+ stream_closed_time = time.time_ns()
50
+
51
+ except Exception as e:
52
+ logger.warning(
53
+ "Warning: Error occurred while processing item in new_iter: %s",
54
+ str(e),
55
+ )
56
+ finally:
57
+ accumulated_temp_list.append(item)
58
+ yield item
59
+
60
+ if span_processor:
61
+ ret_val = SimpleNamespace(
62
+ type="stream",
63
+ timestamps={
64
+ "data.input": int(stream_start_time),
65
+ "data.output": int(first_token_time),
66
+ "metadata": int(stream_closed_time or time.time_ns()),
67
+ },
68
+ output_text=accumulated_response,
69
+ usage=token_usage,
70
+ )
71
+ span_processor(ret_val)
72
+
73
+ patch_instance_method(response, "__iter__", new_iter)
74
+
75
+ if to_wrap and hasattr(response, "__aiter__"):
76
+ original_iter = response.__aiter__
77
+
78
+ async def new_aiter(self):
79
+ nonlocal waiting_for_first_token, first_token_time, stream_closed_time, accumulated_response, token_usage
80
+
81
+ async for item in original_iter():
82
+ try:
83
+ if (
84
+ item.choices
85
+ and item.choices[0].delta
86
+ and item.choices[0].delta.content
87
+ ):
88
+ if waiting_for_first_token:
89
+ waiting_for_first_token = False
90
+ first_token_time = time.time_ns()
91
+
92
+ accumulated_response += item.choices[0].delta.content
93
+ # token_usage = item.usage
94
+ elif item.object == "chat.completion.chunk" and item.usage:
95
+ # Handle the case where the response is a chunk
96
+ token_usage = item.usage
97
+ stream_closed_time = time.time_ns()
98
+
99
+ except Exception as e:
100
+ logger.warning(
101
+ "Warning: Error occurred while processing item in new_aiter: %s",
102
+ str(e),
103
+ )
104
+ finally:
105
+ accumulated_temp_list.append(item)
106
+ yield item
107
+
108
+ if span_processor:
109
+ ret_val = SimpleNamespace(
110
+ type="stream",
111
+ timestamps={
112
+ "data.input": int(stream_start_time),
113
+ "data.output": int(first_token_time),
114
+ "metadata": int(stream_closed_time or time.time_ns()),
115
+ },
116
+ output_text=accumulated_response,
117
+ usage=token_usage,
118
+ )
119
+ span_processor(ret_val)
120
+
121
+ patch_instance_method(response, "__aiter__", new_aiter)
122
+
5
123
 
6
124
  INFERENCE = {
7
125
  "type": "inference",
126
+ "is_auto_close": lambda kwargs: kwargs.get("stream", False) is False,
127
+ "response_processor": process_stream,
8
128
  "attributes": [
9
129
  [
10
130
  {
11
131
  "_comment": "provider type ,name , deployment , inference_endpoint",
12
132
  "attribute": "type",
13
- "accessor": lambda arguments: 'inference.' + (_helper.get_inference_type(arguments['instance'])) or 'openai'
133
+ "accessor": lambda arguments: "inference."
134
+ + (_helper.get_inference_type(arguments["instance"]))
135
+ or "openai",
14
136
  },
15
137
  {
16
138
  "attribute": "provider_name",
17
- "accessor": lambda arguments: _helper.extract_provider_name(arguments['instance'])
139
+ "accessor": lambda arguments: _helper.extract_provider_name(
140
+ arguments["instance"]
141
+ ),
18
142
  },
19
143
  {
20
144
  "attribute": "deployment",
21
- "accessor": lambda arguments: resolve_from_alias(arguments['instance'].__dict__, ['engine', 'azure_deployment', 'deployment_name', 'deployment_id', 'deployment'])
145
+ "accessor": lambda arguments: resolve_from_alias(
146
+ arguments["instance"].__dict__,
147
+ [
148
+ "engine",
149
+ "azure_deployment",
150
+ "deployment_name",
151
+ "deployment_id",
152
+ "deployment",
153
+ ],
154
+ ),
22
155
  },
23
156
  {
24
157
  "attribute": "inference_endpoint",
25
- "accessor": lambda arguments: resolve_from_alias(arguments['instance'].__dict__, ['azure_endpoint', 'api_base', 'endpoint']) or _helper.extract_inference_endpoint(arguments['instance'])
26
- }
158
+ "accessor": lambda arguments: resolve_from_alias(
159
+ arguments["instance"].__dict__,
160
+ ["azure_endpoint", "api_base", "endpoint"],
161
+ )
162
+ or _helper.extract_inference_endpoint(arguments["instance"]),
163
+ },
27
164
  ],
28
165
  [
29
166
  {
30
167
  "_comment": "LLM Model",
31
168
  "attribute": "name",
32
- "accessor": lambda arguments: resolve_from_alias(arguments['kwargs'], ['model', 'model_name', 'endpoint_name', 'deployment_name'])
169
+ "accessor": lambda arguments: resolve_from_alias(
170
+ arguments["kwargs"],
171
+ ["model", "model_name", "endpoint_name", "deployment_name"],
172
+ ),
33
173
  },
34
174
  {
35
175
  "attribute": "type",
36
- "accessor": lambda arguments: 'model.llm.' + resolve_from_alias(arguments['kwargs'], ['model', 'model_name', 'endpoint_name', 'deployment_name'])
37
- }
38
- ]
176
+ "accessor": lambda arguments: "model.llm."
177
+ + resolve_from_alias(
178
+ arguments["kwargs"],
179
+ ["model", "model_name", "endpoint_name", "deployment_name"],
180
+ ),
181
+ },
182
+ ],
39
183
  ],
40
184
  "events": [
41
- {"name": "data.input",
42
- "attributes": [
43
-
44
- {
45
- "_comment": "this is instruction and user query to LLM",
46
- "attribute": "input",
47
- "accessor": lambda arguments: _helper.extract_messages(arguments['kwargs'])
48
- }
49
- ]
50
- },
185
+ {
186
+ "name": "data.input",
187
+ "attributes": [
188
+ {
189
+ "_comment": "this is instruction and user query to LLM",
190
+ "attribute": "input",
191
+ "accessor": lambda arguments: _helper.extract_messages(
192
+ arguments["kwargs"]
193
+ ),
194
+ }
195
+ ],
196
+ },
51
197
  {
52
198
  "name": "data.output",
53
199
  "attributes": [
54
200
  {
55
201
  "_comment": "this is result from LLM",
56
202
  "attribute": "response",
57
- "accessor": lambda arguments: _helper.extract_assistant_message(arguments['result'])
203
+ "accessor": lambda arguments: _helper.extract_assistant_message(
204
+ arguments,
205
+ ),
206
+ },
207
+ {
208
+ "attribute": "status",
209
+ "accessor": lambda arguments: get_status(arguments)
210
+ },
211
+ {
212
+ "attribute": "status_code",
213
+ "accessor": lambda arguments: get_exception_status_code(arguments)
58
214
  }
59
- ]
215
+ ],
60
216
  },
61
217
  {
62
218
  "name": "metadata",
63
219
  "attributes": [
64
220
  {
65
221
  "_comment": "this is metadata usage from LLM",
66
- "accessor": lambda arguments: _helper.update_span_from_llm_response(arguments['result'])
222
+ "accessor": lambda arguments: _helper.update_span_from_llm_response(
223
+ arguments["result"]
224
+ ),
67
225
  }
68
- ]
69
- }
70
- ]
226
+ ],
227
+ },
228
+ ],
71
229
  }