|
| 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 | +from __future__ import annotations |
| 16 | + |
| 17 | +import logging |
| 18 | +import queue |
| 19 | +import threading |
| 20 | +from dataclasses import dataclass |
| 21 | +from typing import Any |
| 22 | + |
| 23 | +from opentelemetry._logs import LogRecord |
| 24 | +from opentelemetry.trace import Span |
| 25 | +from opentelemetry.util._once import Once |
| 26 | +from opentelemetry.util.genai import types |
| 27 | +from opentelemetry.util.genai.upload_hook import UploadHook, _NoOpUploadHook |
| 28 | + |
| 29 | +_logger = logging.getLogger(__name__) |
| 30 | + |
| 31 | + |
| 32 | +# If the SDK is available, use the BatchProcessor shared class for queueing, otherwise the hook will be a |
| 33 | +# no-op. |
| 34 | +try: |
| 35 | + import fsspec |
| 36 | +except ImportError: |
| 37 | + fsspec = None |
| 38 | + |
| 39 | + |
| 40 | +@dataclass |
| 41 | +class Completion: |
| 42 | + inputs: list[types.InputMessage] |
| 43 | + outputs: list[types.OutputMessage] |
| 44 | + system_instruction: list[types.MessagePart] |
| 45 | + |
| 46 | + |
| 47 | +if fsspec is not None: |
| 48 | + |
| 49 | + class FsspecCompletionExporter: |
| 50 | + """Implements uploading GenAI completions to a generic backend using fsspec |
| 51 | +
|
| 52 | + This class is used by the `BatchUploadHook` to export completions to an external |
| 53 | + storage. |
| 54 | + """ |
| 55 | + |
| 56 | + def export(self, completion: Completion) -> None: |
| 57 | + """upload a completion with fsspec, may be called concurrently""" |
| 58 | + # TODO: implement fsspec upload |
| 59 | + |
| 60 | + class FsspecUploadHook(UploadHook): |
| 61 | + def __init__( |
| 62 | + self, exporter: FsspecCompletionExporter, maxsize: int = 20 |
| 63 | + ) -> None: |
| 64 | + self._exporter = exporter |
| 65 | + |
| 66 | + # Use opinionated defaults to start, we can add environment variables later |
| 67 | + self._queue: queue.Queue[Completion | None] = queue.Queue( |
| 68 | + maxsize=maxsize |
| 69 | + ) |
| 70 | + self._consumer_thread = threading.Thread( |
| 71 | + target=self._consumer, daemon=True |
| 72 | + ) |
| 73 | + self._consumer_thread.start() |
| 74 | + self._shutdown_once = Once() |
| 75 | + |
| 76 | + def _consumer(self) -> None: |
| 77 | + shutdown = False |
| 78 | + while not (shutdown and self._queue.empty()): |
| 79 | + completion = self._queue.get() |
| 80 | + if completion is None: |
| 81 | + shutdown = True |
| 82 | + self._queue.task_done() |
| 83 | + continue |
| 84 | + |
| 85 | + # TODO: submit to a thread pool |
| 86 | + self._exporter.export(completion) |
| 87 | + self._queue.task_done() |
| 88 | + |
| 89 | + def upload( |
| 90 | + self, |
| 91 | + *, |
| 92 | + inputs: list[types.InputMessage], |
| 93 | + outputs: list[types.OutputMessage], |
| 94 | + system_instruction: list[types.MessagePart], |
| 95 | + span: Span | None = None, |
| 96 | + log_record: LogRecord | None = None, |
| 97 | + **kwargs: Any, |
| 98 | + ) -> None: |
| 99 | + try: |
| 100 | + self._queue.put( |
| 101 | + Completion( |
| 102 | + inputs=inputs, |
| 103 | + outputs=outputs, |
| 104 | + system_instruction=system_instruction, |
| 105 | + ), |
| 106 | + block=False, |
| 107 | + ) |
| 108 | + except queue.Full: |
| 109 | + logging.warning( |
| 110 | + "gen ai fsspec upload queue is full, dropping completion" |
| 111 | + ) |
| 112 | + |
| 113 | + def shutdown(self, timeout_sec: float = 5) -> None: |
| 114 | + """Shuts down any worker threads""" |
| 115 | + |
| 116 | + def do_shutdown() -> None: |
| 117 | + # TODO: proper deadlines |
| 118 | + self._queue.put(None, timeout=timeout_sec) |
| 119 | + self._consumer_thread.join(timeout=timeout_sec) |
| 120 | + |
| 121 | + self._shutdown_once.do_once(do_shutdown) |
| 122 | + |
| 123 | + def fsspec_upload_hook() -> UploadHook: |
| 124 | + return FsspecUploadHook(FsspecCompletionExporter()) |
| 125 | +else: |
| 126 | + |
| 127 | + def fsspec_upload_hook() -> UploadHook: |
| 128 | + return _NoOpUploadHook() |
0 commit comments