| 80 | |
| 81 | |
| 82 | class Resource: |
| 83 | def __init__( |
| 84 | self, |
| 85 | api_key: str, |
| 86 | base_url: str, |
| 87 | organization: Optional[str] = None, |
| 88 | project: Optional[str] = None, |
| 89 | max_retries: int = 2, |
| 90 | timeout: Optional[float] = None, |
| 91 | ) -> None: |
| 92 | # # client args |
| 93 | self.client_args: ClientArgs = { |
| 94 | "api_key": api_key, |
| 95 | "base_url": self._enforce_trailing_slash(base_url), |
| 96 | "organization": organization, |
| 97 | "project": project, |
| 98 | "max_retries": max_retries, |
| 99 | "timeout": timeout, |
| 100 | } |
| 101 | |
| 102 | # priority items |
| 103 | self._last_used_time: float = time.time() |
| 104 | self._request_count: int = 0 |
| 105 | self._prompt_tokens: int = 0 |
| 106 | self._completion_tokens: int = 0 |
| 107 | self._total_tokens: int = 0 |
| 108 | # self._client = OpenAI(api_key=self.api_key, base_url=self.base_url) |
| 109 | |
| 110 | def __lt__(self, other: "Resource") -> bool: |
| 111 | """Compare two resources based on request rate, token usage and price |
| 112 | Args: |
| 113 | other (Resource): The other resource to compare with |
| 114 | Returns: |
| 115 | bool: True if this resource has a lower request rate and token usage |
| 116 | than the other resource, False otherwise |
| 117 | """ |
| 118 | if abs(self._last_used_time - other._last_used_time) > A_MINITE_IN_SECONDS: |
| 119 | return self._last_used_time < other._last_used_time |
| 120 | elif abs(self._request_count - other._request_count) > RPM_INTERVAL: |
| 121 | return self._request_count < other._request_count |
| 122 | else: |
| 123 | return ( |
| 124 | self._prompt_tokens * INPUT_PRICE_PER_TOKEN |
| 125 | + self._completion_tokens * OUTPUT_PRICE_PER_TOKEN |
| 126 | ) < ( |
| 127 | other._prompt_tokens * INPUT_PRICE_PER_TOKEN |
| 128 | + other._completion_tokens * OUTPUT_PRICE_PER_TOKEN |
| 129 | ) |
| 130 | |
| 131 | def __gt__(self, other: "Resource") -> bool: |
| 132 | """Compare two resources based on request rate and token usage |
| 133 | Args: |
| 134 | other (Resource): The other resource to compare with |
| 135 | Returns: |
| 136 | bool: True if this resource has a higher request rate and token usage |
| 137 | than the other resource, False otherwise |
| 138 | """ |
| 139 | return not self.__lt__(other) |