|
| 1 | +# Copyright The OpenTelemetry Authors |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +# Includes work from: |
| 16 | +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 17 | +# SPDX-License-Identifier: Apache-2.0 |
| 18 | + |
| 19 | +from __future__ import annotations |
| 20 | + |
| 21 | +from botocore.eventstream import EventStream |
| 22 | +from wrapt import ObjectProxy |
| 23 | + |
| 24 | + |
| 25 | +# pylint: disable=abstract-method |
| 26 | +class ConverseStreamWrapper(ObjectProxy): |
| 27 | + """Wrapper for botocore.eventstream.EventStream""" |
| 28 | + |
| 29 | + def __init__( |
| 30 | + self, |
| 31 | + stream: EventStream, |
| 32 | + stream_done_callback, |
| 33 | + ): |
| 34 | + super().__init__(stream) |
| 35 | + |
| 36 | + self._stream_done_callback = stream_done_callback |
| 37 | + # accumulating things in the same shape of non-streaming version |
| 38 | + # {"usage": {"inputTokens": 0, "outputTokens": 0}, "stopReason": "finish"} |
| 39 | + self._response = {} |
| 40 | + |
| 41 | + def __iter__(self): |
| 42 | + for event in self.__wrapped__: |
| 43 | + self._process_event(event) |
| 44 | + yield event |
| 45 | + |
| 46 | + def _process_event(self, event): |
| 47 | + if "messageStart" in event: |
| 48 | + # {'messageStart': {'role': 'assistant'}} |
| 49 | + pass |
| 50 | + |
| 51 | + if "contentBlockDelta" in event: |
| 52 | + # {'contentBlockDelta': {'delta': {'text': "Hello"}, 'contentBlockIndex': 0}} |
| 53 | + pass |
| 54 | + |
| 55 | + if "contentBlockStop" in event: |
| 56 | + # {'contentBlockStop': {'contentBlockIndex': 0}} |
| 57 | + pass |
| 58 | + |
| 59 | + if "messageStop" in event: |
| 60 | + # {'messageStop': {'stopReason': 'end_turn'}} |
| 61 | + if stop_reason := event["messageStop"].get("stopReason"): |
| 62 | + self._response["stopReason"] = stop_reason |
| 63 | + |
| 64 | + if "metadata" in event: |
| 65 | + # {'metadata': {'usage': {'inputTokens': 12, 'outputTokens': 15, 'totalTokens': 27}, 'metrics': {'latencyMs': 2980}}} |
| 66 | + if usage := event["metadata"].get("usage"): |
| 67 | + self._response["usage"] = {} |
| 68 | + if input_tokens := usage.get("inputTokens"): |
| 69 | + self._response["usage"]["inputTokens"] = input_tokens |
| 70 | + |
| 71 | + if output_tokens := usage.get("outputTokens"): |
| 72 | + self._response["usage"]["outputTokens"] = output_tokens |
| 73 | + |
| 74 | + self._stream_done_callback(self._response) |
0 commit comments