| 1 | import sys |
| 2 | import threading |
| 3 | from abc import ABC, abstractmethod |
| 4 | |
| 5 | from app.log import log_and_cprint |
| 6 | |
| 7 | # Variables for each process. Since models are singleton objects, their references are copied |
| 8 | # to each process, but they all point to the same objects. For safe updating costs per process, |
| 9 | # we define the accumulators here. |
| 10 | |
| 11 | thread_cost = threading.local() |
| 12 | thread_cost.process_cost = 0.0 |
| 13 | thread_cost.process_input_tokens = 0 |
| 14 | thread_cost.process_output_tokens = 0 |
| 15 | |
| 16 | |
| 17 | class Model(ABC): |
| 18 | def __init__( |
| 19 | self, |
| 20 | name: str, |
| 21 | cost_per_input: float, |
| 22 | cost_per_output: float, |
| 23 | parallel_tool_call: bool = False, |
| 24 | ): |
| 25 | self.name: str = name |
| 26 | # cost stats - zero for local models |
| 27 | self.cost_per_input: float = cost_per_input |
| 28 | self.cost_per_output: float = cost_per_output |
| 29 | # whether the model supports parallel tool call |
| 30 | self.parallel_tool_call: bool = parallel_tool_call |
| 31 | |
| 32 | @abstractmethod |
| 33 | def check_api_key(self) -> str: |
| 34 | raise NotImplementedError("abstract base class") |
| 35 | |
| 36 | @abstractmethod |
| 37 | def setup(self) -> None: |
| 38 | raise NotImplementedError("abstract base class") |
| 39 | |
| 40 | @abstractmethod |
| 41 | def call(self, messages: list[dict], **kwargs): |
| 42 | raise NotImplementedError("abstract base class") |
| 43 | |
| 44 | def calc_cost(self, input_tokens: int, output_tokens: int) -> float: |
| 45 | """ |
| 46 | Calculates the cost of a request based on the number of input/output tokens. |
| 47 | """ |
| 48 | input_cost = self.cost_per_input * input_tokens |
| 49 | output_cost = self.cost_per_output * output_tokens |
| 50 | cost = input_cost + output_cost |
| 51 | log_and_cprint( |
| 52 | f"Model API request cost info: " |
| 53 | f"input_tokens={input_tokens}, output_tokens={output_tokens}, cost={cost:.6f}", |
| 54 | style="yellow", |
| 55 | ) |
| 56 | return cost |
| 57 | |
| 58 | def get_overall_exec_stats(self): |
| 59 | return { |
| 60 | "model": self.name, |
| 61 | "input_cost_per_token": self.cost_per_input, |
| 62 | "output_cost_per_token": self.cost_per_output, |
| 63 | "total_input_tokens": thread_cost.process_input_tokens, |
| 64 | "total_output_tokens": thread_cost.process_output_tokens, |
| 65 | "total_tokens": thread_cost.process_input_tokens |
| 66 | + thread_cost.process_output_tokens, |
| 67 | "total_cost": thread_cost.process_cost, |
| 68 | } |
| 69 | |
| 70 | |
| 71 | MODEL_HUB = {} |
| 72 | |
| 73 | |
| 74 | def register_model(model: Model): |
| 75 | global MODEL_HUB |
| 76 | MODEL_HUB[model.name] = model |
| 77 | |
| 78 | |
| 79 | def get_all_model_names(): |
| 80 | return list(MODEL_HUB.keys()) |
| 81 | |
| 82 | |
| 83 | # To be set at runtime - the selected model for a run |
| 84 | SELECTED_MODEL: Model |
| 85 | |
| 86 | |
| 87 | def set_model(model_name: str): |
| 88 | global SELECTED_MODEL |
| 89 | if model_name not in MODEL_HUB: |
| 90 | print(f"Invalid model name: {model_name}") |
| 91 | sys.exit(1) |
| 92 | SELECTED_MODEL = MODEL_HUB[model_name] |
| 93 | SELECTED_MODEL.setup() |
| 94 | |
| 95 | |
| 96 | # the model temperature to use |
| 97 | # For OpenAI models: this value should be from 0 to 2 |
| 98 | MODEL_TEMP: float = 0.0 |