Skip to content

Commit 41a76f3

Browse files
committed
Secure token count
1 parent 8abc4aa commit 41a76f3

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

src/ingest.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,12 @@ def create_tree_structure(query: dict, node: Dict, prefix: str = "", is_last: bo
153153

154154
def generate_token_string(context_string: str) -> str:
155155
formatted_tokens = ""
156-
total_gpt_tokens = count_string_tokens(prompt=context_string, model="gpt-4o")
156+
try:
157+
total_gpt_tokens = count_string_tokens(prompt=context_string, model="gpt-4o")
158+
except Exception as e:
159+
return None
157160
if total_gpt_tokens > 1000000:
158-
formatted_tokens = f"{total_gpt_tokens/1000000:.1f}M"
161+
formatted_tokens = f"{total_gpt_tokens/1000000:.1f}M"
159162
elif total_gpt_tokens > 1000:
160163
formatted_tokens = f"{total_gpt_tokens/1000:.1f}k"
161164
else:
@@ -203,7 +206,8 @@ def ingest_from_query(query: dict, ignore_patterns: List[str] = DEFAULT_IGNORE_P
203206

204207
print(files_content)
205208
formatted_tokens = generate_token_string(files_content)
206-
summary += f"\nEstimated tokens: {formatted_tokens}"
209+
if formatted_tokens:
210+
summary += f"\nEstimated tokens: {formatted_tokens}"
207211
return (summary, tree, files_content)
208212

209213
else:
@@ -215,7 +219,9 @@ def ingest_from_query(query: dict, ignore_patterns: List[str] = DEFAULT_IGNORE_P
215219

216220

217221
formatted_tokens = generate_token_string(tree + files_content)
218-
summary += f"\nEstimated tokens: {formatted_tokens}"
222+
if formatted_tokens:
223+
summary += f"\nEstimated tokens: {formatted_tokens}"
224+
219225

220226

221227

0 commit comments

Comments
 (0)