ankban commited on
Commit
8e9e651
·
verified ·
1 Parent(s): ed07202

Update file_utils.py

Browse files
Files changed (1) hide show
  1. file_utils.py +8 -1
file_utils.py CHANGED
@@ -5,9 +5,16 @@ import os
5
 
6
  # === GPT-4 Client Setup ===
7
  client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
 
8
 
9
  # === Centralized LLM Caller ===
10
  def call_llm(prompt, system_message="You are a helpful assistant.", temperature=0.7):
 
 
 
 
 
 
11
  response = client.chat.completions.create(
12
  model="gpt-4",
13
  messages=[
@@ -102,4 +109,4 @@ Content:
102
  Question:
103
  {question}
104
  """
105
- return call_llm(prompt, system_message="You are a helpful and accurate tutor that stays grounded in provided content.")
 
5
 
6
  # === GPT-4 Client Setup ===
7
  client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
8
+ MAX_TOKENS = 7000 # Safety buffer below GPT-4's 8192 limit
9
 
10
  # === Centralized LLM Caller ===
11
  def call_llm(prompt, system_message="You are a helpful assistant.", temperature=0.7):
12
+ # Rough token approximation: 1 token ≈ 4 characters (English average)
13
+ approx_tokens = len(prompt) // 4
14
+ if approx_tokens > MAX_TOKENS:
15
+ prompt = prompt[:MAX_TOKENS * 4] # truncate by character length
16
+ prompt += "\n[NOTE: Truncated due to length limit.]"
17
+
18
  response = client.chat.completions.create(
19
  model="gpt-4",
20
  messages=[
 
109
  Question:
110
  {question}
111
  """
112
+ return call_llm(prompt, system_message="You are a helpful and accurate tutor that stays grounded in provided content.")