|
12 | 12 | # See the License for the specific language governing permissions and |
13 | 13 | # limitations under the License. |
14 | 14 |
|
| 15 | +import itertools |
| 16 | +import logging |
| 17 | + |
| 18 | +from newrelic.api.transaction import current_transaction |
| 19 | +from newrelic.common.object_wrapper import ObjectProxy |
| 20 | + |
| 21 | +_logger = logging.getLogger(__name__) |
| 22 | + |
15 | 23 |
|
16 | 24 | def _get_llm_metadata(transaction): |
17 | | - # Grab LLM-related custom attributes off of the transaction to store as metadata on LLM events |
18 | | - custom_attrs_dict = transaction._custom_params |
19 | | - llm_metadata_dict = {key: value for key, value in custom_attrs_dict.items() if key.startswith("llm.")} |
20 | | - llm_context_attrs = getattr(transaction, "_llm_context_attrs", None) |
21 | | - if llm_context_attrs: |
22 | | - llm_metadata_dict.update(llm_context_attrs) |
| 25 | + if not transaction: |
| 26 | + return {} |
| 27 | + try: |
| 28 | + # Grab LLM-related custom attributes off of the transaction to store as metadata on LLM events |
| 29 | + custom_attrs_dict = getattr(transaction, "_custom_params", {}) |
| 30 | + llm_metadata_dict = {key: value for key, value in custom_attrs_dict.items() if key.startswith("llm.")} |
| 31 | + llm_context_attrs = getattr(transaction, "_llm_context_attrs", None) |
| 32 | + if llm_context_attrs: |
| 33 | + llm_metadata_dict.update(llm_context_attrs) |
| 34 | + except Exception: |
| 35 | + _logger.warning("Unable to capture custom metadata attributes to record on LLM events.") |
| 36 | + return {} |
23 | 37 |
|
24 | 38 | return llm_metadata_dict |
| 39 | + |
| 40 | + |
| 41 | +class GeneratorProxy(ObjectProxy): |
| 42 | + def __init__(self, wrapped, on_stop_iteration, on_error): |
| 43 | + super().__init__(wrapped) |
| 44 | + self._nr_on_stop_iteration = on_stop_iteration |
| 45 | + self._nr_on_error = on_error |
| 46 | + |
| 47 | + def __iter__(self): |
| 48 | + self._nr_wrapped_iter = self.__wrapped__.__iter__() |
| 49 | + return self |
| 50 | + |
| 51 | + def __next__(self): |
| 52 | + transaction = current_transaction() |
| 53 | + if not transaction: |
| 54 | + return self._nr_wrapped_iter.__next__() |
| 55 | + |
| 56 | + return_val = None |
| 57 | + try: |
| 58 | + return_val = self._nr_wrapped_iter.__next__() |
| 59 | + except StopIteration: |
| 60 | + self._nr_on_stop_iteration(self, transaction) |
| 61 | + raise |
| 62 | + except Exception: |
| 63 | + self._nr_on_error(self, transaction) |
| 64 | + raise |
| 65 | + return return_val |
| 66 | + |
| 67 | + def close(self): |
| 68 | + return self.__wrapped__.close() |
| 69 | + |
| 70 | + def __copy__(self): |
| 71 | + # Required to properly interface with itertool.tee, which can be called by LangChain on generators |
| 72 | + self.__wrapped__, copy = itertools.tee(self.__wrapped__, 2) |
| 73 | + return GeneratorProxy(copy, self._nr_on_stop_iteration, self._nr_on_error) |
| 74 | + |
| 75 | + |
| 76 | +class AsyncGeneratorProxy(ObjectProxy): |
| 77 | + def __init__(self, wrapped, on_stop_iteration, on_error): |
| 78 | + super().__init__(wrapped) |
| 79 | + self._nr_on_stop_iteration = on_stop_iteration |
| 80 | + self._nr_on_error = on_error |
| 81 | + |
| 82 | + def __aiter__(self): |
| 83 | + self._nr_wrapped_iter = self.__wrapped__.__aiter__() |
| 84 | + return self |
| 85 | + |
| 86 | + async def __anext__(self): |
| 87 | + transaction = current_transaction() |
| 88 | + if not transaction: |
| 89 | + return await self._nr_wrapped_iter.__anext__() |
| 90 | + |
| 91 | + return_val = None |
| 92 | + try: |
| 93 | + return_val = await self._nr_wrapped_iter.__anext__() |
| 94 | + except StopAsyncIteration: |
| 95 | + self._nr_on_stop_iteration(self, transaction) |
| 96 | + raise |
| 97 | + except Exception: |
| 98 | + self._nr_on_error(self, transaction) |
| 99 | + raise |
| 100 | + return return_val |
| 101 | + |
| 102 | + async def aclose(self): |
| 103 | + return await self.__wrapped__.aclose() |
| 104 | + |
| 105 | + def __copy__(self): |
| 106 | + # Required to properly interface with itertool.tee, which can be called by LangChain on generators |
| 107 | + self.__wrapped__, copy = itertools.tee(self.__wrapped__, n=2) |
| 108 | + return AsyncGeneratorProxy(copy, self._nr_on_stop_iteration, self._nr_on_error) |
0 commit comments