|
2 | 2 | import json |
3 | 3 | import os |
4 | 4 | import time |
5 | | -from typing import TypeVar |
6 | 5 |
|
7 | 6 | import aiohttp |
8 | 7 | import litellm |
|
22 | 21 | from bespokelabs.curator.types.generic_request import GenericRequest |
23 | 22 | from bespokelabs.curator.types.generic_response import GenericResponse, _TokenUsage |
24 | 23 |
|
25 | | -T = TypeVar("T") |
26 | | - |
27 | 24 | _DEFAULT_OPENAI_URL: str = "https://api.openai.com/v1/chat/completions" |
28 | 25 |
|
29 | 26 | _OPENAI_ALLOWED_IMAGE_SIZE_MB = 20 |
30 | 27 |
|
31 | 28 |
|
32 | 29 | async def _handle_longlive_response(response: aiohttp.ClientResponse): |
33 | | - content_lines = [] |
34 | 30 | content_buffer = "" |
35 | 31 |
|
36 | 32 | async for line in response.content: |
37 | 33 | line_str = line.decode("utf-8").strip() |
| 34 | + if line_str: |
| 35 | + content_buffer += line_str |
38 | 36 |
|
39 | | - if not line_str: |
40 | | - continue |
41 | | - |
42 | | - content_buffer += line_str |
43 | | - content_lines.append(line_str) |
44 | | - |
45 | | - if not content_lines: |
| 37 | + if not content_buffer: |
46 | 38 | return { |
47 | 39 | "error": "Received an empty response from the API. Some providers, " |
48 | 40 | "such as DeepSeek, may disconnect after 30 minutes of inactivity, which can result in missing responses." |
49 | 41 | } |
50 | 42 |
|
51 | 43 | try: |
52 | 44 | return json.loads(content_buffer) |
53 | | - except json.JSONDecodeError: |
54 | | - try: |
55 | | - return json.loads("".join(content_lines)) |
56 | | - except json.JSONDecodeError as e: |
57 | | - raise ValueError(f"Failed to parse API response: {e}\nRaw response: {content_buffer}") from e |
| 45 | + except json.JSONDecodeError as e: |
| 46 | + raise ValueError(f"Failed to parse API response: {e}\nRaw response: {content_buffer}") from e |
58 | 47 |
|
59 | 48 |
|
60 | 49 | async def fetch_response(session, *, url, headers, payload, timeout, longlived_response=False): |
@@ -269,10 +258,7 @@ def _multimodal_prompt_supported(self) -> bool: |
269 | 258 | return True |
270 | 259 |
|
271 | 260 | def create_api_specific_request_online(self, generic_request: GenericRequest) -> dict: |
272 | | - """Create an OpenAI-specific request from a generic request. |
273 | | -
|
274 | | - Delegates to the mixin implementation. |
275 | | - """ |
| 261 | + """Create an OpenAI-specific request from a generic request.""" |
276 | 262 | return OpenAIRequestMixin.create_api_specific_request_online(self, generic_request) |
277 | 263 |
|
278 | 264 | async def call_single_request( |
@@ -345,13 +331,6 @@ async def call_single_request( |
345 | 331 | finish_reason=finish_reason, |
346 | 332 | ) |
347 | 333 |
|
348 | | - def get_token_encoding(self) -> str: |
349 | | - """Get the token encoding name for a given model.""" |
350 | | - if self.config.model.startswith("gpt-4"): |
351 | | - name = "cl100k_base" |
352 | | - elif self.config.model.startswith("gpt-3.5"): |
353 | | - name = "cl100k_base" |
354 | | - else: |
355 | | - name = "cl100k_base" # Default to cl100k_base |
356 | | - |
357 | | - return tiktoken.get_encoding(name) |
| 334 | + def get_token_encoding(self): |
| 335 | + """Get the token encoding for the model.""" |
| 336 | + return tiktoken.get_encoding("cl100k_base") |
0 commit comments