-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoken_count.py
More file actions
39 lines (30 loc) · 1006 Bytes
/
token_count.py
File metadata and controls
39 lines (30 loc) · 1006 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import psycopg2
import tiktoken
# Connect to PostgreSQL
DB_PARAMS = {
"dbname": "chatbot_data",
"user": "als",
"password": "postgrespw",
"host": "localhost",
"port": "5432",
}
conn = psycopg2.connect(**DB_PARAMS)
cur = conn.cursor()
# Load OpenAI's tokenizer
tokenizer = tiktoken.get_encoding("cl100k_base") # Supports text-embedding-ada-002
# Fetch all scraped pages
cur.execute("SELECT url, content FROM scraped_pages;")
pages = cur.fetchall()
# Analyze token counts
long_pages = []
for url, text in pages:
token_count = len(tokenizer.encode(text))
if token_count > 8192:
long_pages.append((url, token_count)) # Track pages that exceed the limit
print(f"🔹 {url} → {token_count} tokens")
# Print summary of long pages
if long_pages:
print("\n🚨 Pages exceeding 8192 tokens:")
for url, count in sorted(long_pages, key=lambda x: x[1], reverse=True):
print(f"❌ {url} → {count} tokens")
print("\n✅ Token analysis complete!")