|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from abc import ABC, abstractmethod |
| 4 | +from typing import Dict, TYPE_CHECKING, Union |
| 5 | +import sqlite3 |
| 6 | +import uuid |
| 7 | + |
| 8 | +from openai import OpenAI, AzureOpenAI |
| 9 | +from openai.types.chat import ChatCompletion |
| 10 | + |
| 11 | +if TYPE_CHECKING: |
| 12 | + from autogen import ConversableAgent, OpenAIWrapper |
| 13 | + |
| 14 | + |
| 15 | +class BaseLogger(ABC): |
| 16 | + @abstractmethod |
| 17 | + def start(self) -> str: |
| 18 | + """ |
| 19 | + Open a connection to the logging database, and start recording. |
| 20 | +
|
| 21 | + Returns: |
| 22 | + session_id (str): a unique id for the logging session |
| 23 | + """ |
| 24 | + ... |
| 25 | + |
| 26 | + @abstractmethod |
| 27 | + def log_chat_completion( |
| 28 | + invocation_id: uuid.UUID, |
| 29 | + client_id: int, |
| 30 | + wrapper_id: int, |
| 31 | + request: Dict, |
| 32 | + response: Union[str, ChatCompletion], |
| 33 | + is_cached: int, |
| 34 | + cost: float, |
| 35 | + start_time: str, |
| 36 | + ) -> None: |
| 37 | + """ |
| 38 | + Log a chat completion to database. |
| 39 | +
|
| 40 | + In AutoGen, chat completions are somewhat complicated because they are handled by the `autogen.oai.OpenAIWrapper` class. |
| 41 | + One invocation to `create` can lead to multiple underlying OpenAI calls, depending on the llm_config list used, and |
| 42 | + any errors or retries. |
| 43 | +
|
| 44 | + Args: |
| 45 | + invocation_id (uuid): A unique identifier for the invocation to the OpenAIWrapper.create method call |
| 46 | + client_id (int): A unique identifier for the underlying OpenAI client instance |
| 47 | + wrapper_id (int): A unique identifier for the OpenAIWrapper instance |
| 48 | + request (dict): A dictionary representing the the request or call to the OpenAI client endpoint |
| 49 | + response (str or ChatCompletion): The response from OpenAI |
| 50 | + is_chached (int): 1 if the response was a cache hit, 0 otherwise |
| 51 | + cost(float): The cost for OpenAI response |
| 52 | + start_time (str): A string representing the moment the request was initiated |
| 53 | + """ |
| 54 | + ... |
| 55 | + |
| 56 | + @abstractmethod |
| 57 | + def log_new_agent(agent: ConversableAgent, init_args: Dict) -> None: |
| 58 | + """ |
| 59 | + Log the birth of a new agent. |
| 60 | +
|
| 61 | + Args: |
| 62 | + agent (ConversableAgent): The agent to log. |
| 63 | + init_args (dict): The arguments passed to the construct the conversable agent |
| 64 | + """ |
| 65 | + ... |
| 66 | + |
| 67 | + @abstractmethod |
| 68 | + def log_new_wrapper(wrapper: OpenAIWrapper, init_args: Dict) -> None: |
| 69 | + """ |
| 70 | + Log the birth of a new OpenAIWrapper. |
| 71 | +
|
| 72 | + Args: |
| 73 | + wrapper (OpenAIWrapper): The wrapper to log. |
| 74 | + init_args (dict): The arguments passed to the construct the wrapper |
| 75 | + """ |
| 76 | + ... |
| 77 | + |
| 78 | + @abstractmethod |
| 79 | + def log_new_client(client: Union[AzureOpenAI, OpenAI], wrapper: OpenAIWrapper, init_args: Dict) -> None: |
| 80 | + """ |
| 81 | + Log the birth of a new OpenAIWrapper. |
| 82 | +
|
| 83 | + Args: |
| 84 | + wrapper (OpenAI): The OpenAI client to log. |
| 85 | + init_args (dict): The arguments passed to the construct the client |
| 86 | + """ |
| 87 | + ... |
| 88 | + |
| 89 | + @abstractmethod |
| 90 | + def stop() -> None: |
| 91 | + """ |
| 92 | + Close the connection to the logging database, and stop logging. |
| 93 | + """ |
| 94 | + ... |
| 95 | + |
| 96 | + @abstractmethod |
| 97 | + def get_connection() -> Union[sqlite3.Connection]: |
| 98 | + """ |
| 99 | + Return a connection to the logging database. |
| 100 | + """ |
| 101 | + ... |
0 commit comments