Commit
·
8b27505
1
Parent(s):
ebec74a
bug fixes
Browse files- app/api_helpers.py +0 -1
- app/message_processing.py +1 -35
- app/requirements.txt +1 -1
app/api_helpers.py
CHANGED
@@ -18,7 +18,6 @@ from message_processing import (
|
|
18 |
convert_to_openai_format,
|
19 |
convert_chunk_to_openai,
|
20 |
create_final_chunk,
|
21 |
-
split_text_by_completion_tokens,
|
22 |
parse_gemini_response_for_reasoning_and_content, # Added import
|
23 |
extract_reasoning_by_tags # Added for new OpenAI direct reasoning logic
|
24 |
)
|
|
|
18 |
convert_to_openai_format,
|
19 |
convert_chunk_to_openai,
|
20 |
create_final_chunk,
|
|
|
21 |
parse_gemini_response_for_reasoning_and_content, # Added import
|
22 |
extract_reasoning_by_tags # Added for new OpenAI direct reasoning logic
|
23 |
)
|
app/message_processing.py
CHANGED
@@ -6,8 +6,6 @@ import urllib.parse
|
|
6 |
from typing import List, Dict, Any, Union, Literal, Tuple # Added Tuple
|
7 |
|
8 |
from google.genai import types
|
9 |
-
from google.genai.types import HttpOptions as GenAIHttpOptions
|
10 |
-
from google import genai as google_genai_client
|
11 |
from models import OpenAIMessage, ContentPartText, ContentPartImage
|
12 |
|
13 |
SUPPORTED_ROLES = ["user", "model"]
|
@@ -322,36 +320,4 @@ def convert_chunk_to_openai(chunk: Any, model: str, response_id: str, candidate_
|
|
322 |
def create_final_chunk(model: str, response_id: str, candidate_count: int = 1) -> str:
|
323 |
choices = [{"index": i, "delta": {}, "finish_reason": "stop"} for i in range(candidate_count)]
|
324 |
final_chunk_data = {"id": response_id, "object": "chat.completion.chunk", "created": int(time.time()), "model": model, "choices": choices}
|
325 |
-
return f"data: {json.dumps(final_chunk_data)}\n\n"
|
326 |
-
|
327 |
-
def split_text_by_completion_tokens(
|
328 |
-
gcp_creds: Any, gcp_proj_id: str, gcp_loc: str, model_id_for_tokenizer: str,
|
329 |
-
full_text_to_tokenize: str, num_completion_tokens_from_usage: int
|
330 |
-
) -> tuple[str, str, List[str]]:
|
331 |
-
if not full_text_to_tokenize: return "", "", []
|
332 |
-
try:
|
333 |
-
sync_tokenizer_client = google_genai_client.Client(
|
334 |
-
vertexai=True, credentials=gcp_creds, project=gcp_proj_id, location=gcp_loc,
|
335 |
-
http_options=GenAIHttpOptions(api_version="v1")
|
336 |
-
)
|
337 |
-
token_compute_response = sync_tokenizer_client.models.compute_tokens(model=model_id_for_tokenizer, contents=full_text_to_tokenize)
|
338 |
-
all_final_token_strings = []
|
339 |
-
if token_compute_response.tokens_info:
|
340 |
-
for token_info_item in token_compute_response.tokens_info:
|
341 |
-
for api_token_bytes in token_info_item.tokens:
|
342 |
-
intermediate_str = api_token_bytes.decode('utf-8', errors='replace') if isinstance(api_token_bytes, bytes) else api_token_bytes
|
343 |
-
final_token_text = ""
|
344 |
-
try:
|
345 |
-
b64_decoded_bytes = base64.b64decode(intermediate_str)
|
346 |
-
final_token_text = b64_decoded_bytes.decode('utf-8', errors='replace')
|
347 |
-
except Exception: final_token_text = intermediate_str
|
348 |
-
all_final_token_strings.append(final_token_text)
|
349 |
-
if not all_final_token_strings: return "", full_text_to_tokenize, []
|
350 |
-
if not (0 < num_completion_tokens_from_usage <= len(all_final_token_strings)):
|
351 |
-
return "", "".join(all_final_token_strings), all_final_token_strings
|
352 |
-
completion_part_tokens = all_final_token_strings[-num_completion_tokens_from_usage:]
|
353 |
-
reasoning_part_tokens = all_final_token_strings[:-num_completion_tokens_from_usage]
|
354 |
-
return "".join(reasoning_part_tokens), "".join(completion_part_tokens), all_final_token_strings
|
355 |
-
except Exception as e_tok:
|
356 |
-
print(f"ERROR: Tokenizer failed in split_text_by_completion_tokens: {e_tok}")
|
357 |
-
return "", full_text_to_tokenize, []
|
|
|
6 |
from typing import List, Dict, Any, Union, Literal, Tuple # Added Tuple
|
7 |
|
8 |
from google.genai import types
|
|
|
|
|
9 |
from models import OpenAIMessage, ContentPartText, ContentPartImage
|
10 |
|
11 |
SUPPORTED_ROLES = ["user", "model"]
|
|
|
320 |
def create_final_chunk(model: str, response_id: str, candidate_count: int = 1) -> str:
|
321 |
choices = [{"index": i, "delta": {}, "finish_reason": "stop"} for i in range(candidate_count)]
|
322 |
final_chunk_data = {"id": response_id, "object": "chat.completion.chunk", "created": int(time.time()), "model": model, "choices": choices}
|
323 |
+
return f"data: {json.dumps(final_chunk_data)}\n\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app/requirements.txt
CHANGED
@@ -3,7 +3,7 @@ uvicorn==0.27.1
|
|
3 |
google-auth==2.38.0
|
4 |
google-cloud-aiplatform==1.86.0
|
5 |
pydantic==2.6.1
|
6 |
-
google-genai==1.
|
7 |
httpx>=0.25.0
|
8 |
openai
|
9 |
google-auth-oauthlib
|
|
|
3 |
google-auth==2.38.0
|
4 |
google-cloud-aiplatform==1.86.0
|
5 |
pydantic==2.6.1
|
6 |
+
google-genai==1.17.0
|
7 |
httpx>=0.25.0
|
8 |
openai
|
9 |
google-auth-oauthlib
|