Skip to content

Commit d27e623

Browse files
authored
Merge branch 'TablewareBox:main' into wangcx
2 parents ef1e1d2 + 5cd4d3e commit d27e623

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

evals/completion_fns/gemini.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import os
2+
3+
import PIL
24
import numpy as np
35
from typing import Any, Optional, Union
46

@@ -56,7 +58,9 @@ def truncate_multimodal_prompt(prompt_list, max_images=16, max_size_bytes=4 * 10
5658
# The image data is a string representation of bytes; calculate its length accordingly.
5759
item_size = len(item['data']) # Approximation of size in bytes
5860
image_count += 1
59-
else:
61+
elif isinstance(item, PIL.Image.Image): # It's an image
62+
item_size = len(item.tobytes()) # Approximation of size in bytes
63+
image_count += 1
6064
continue # Skip any item that doesn't fit expected structure
6165

6266
# Check if adding this item would exceed limits
@@ -159,6 +163,7 @@ def __call__(
159163
generation_config=generation_config,
160164
safety_settings=safety_settings)
161165
# response = request_with_timeout(model.generate_content, contents=[openai_create_prompt] + attached_file_content)
166+
print("Num tokens:", model.count_tokens(contents=contents))
162167
response = model.generate_content(
163168
contents=contents,
164169
)

evals/utils/doc_utils.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
import io
12
import os
23
from pathlib import Path
4+
from typing import Union
5+
6+
import PIL
37
import fitz # PyMuPDF
48
import PyPDF2
59
import tiktoken
@@ -109,7 +113,8 @@ def extract_text_and_fill_in_images(pdf_path, save_img_path=None, add_page_num:
109113
all_contents.append(text)
110114
for i, block in enumerate(blocks_from_pymupdf[page_num]):
111115
if block[1] == "image":
112-
img_cookie = {'mime_type': 'image/png', 'data': block[2]}
116+
# img_cookie = {'mime_type': 'image/png', 'data': block[2]}
117+
img_cookie = PIL.Image.open(io.BytesIO(block[2]))
113118
all_contents.append(img_cookie)
114119
# if i != len(blocks_from_pymupdf) - 1:
115120
# fig_caption = " ".join(block[i+1].split()[:3])
@@ -124,3 +129,15 @@ def num_tokens_from_string(string: str, encoding_name: str) -> int:
124129
encoding = tiktoken.get_encoding(encoding_name)
125130
num_tokens = len(encoding.encode(string))
126131
return num_tokens
132+
133+
134+
def prompt_cutoff(prompt: Union[str, list], model: str = "gpt-3.5-turbo"):
135+
model_max_tokens = {
136+
"gpt-3.5-turbo": 15800,
137+
"claude-2.1": 10000,
138+
"gemini-1.0-pro": 25000,
139+
"gemini-1.0-pro-vision": 25000,
140+
}
141+
142+
if type(prompt) == str:
143+
pass

0 commit comments

Comments
 (0)