zhangjf commited on
Commit
a269fbf
1 Parent(s): 4199bb2

update existing qas.json

Browse files
Files changed (1) hide show
  1. app.py +7 -1
app.py CHANGED
@@ -24,10 +24,15 @@ def num_tokens_from_messages(messages, model="gpt-3.5-turbo"):
24
  encoding = tiktoken.get_encoding("cl100k_base")
25
  if model == "gpt-3.5-turbo": # note: future models may deviate from this
26
  num_tokens = 0
 
27
  for message in messages:
28
  num_tokens += 4 # every message follows <im_start>{role/name}\n{content}<im_end>\n
29
  for key, value in message.items():
30
- num_tokens += len(encoding.encode(value))
 
 
 
 
31
  if key == "name": # if there's a name, the role is omitted
32
  num_tokens += -1 # role is always required and always 1 token
33
  num_tokens += 2 # every reply is primed with <im_start>assistant
@@ -95,6 +100,7 @@ def ask(query, max_attempt_times=3):
95
  idx = openai.api_keys.index(openai.api_key)
96
  idx = (idx + 1) % len(openai.api_keys)
97
  openai.api_key = openai.api_keys[idx]
 
98
  print(f"switch api_key")
99
  print(f"retry in {attempt_times*10} seconds...")
100
  time.sleep(attempt_times*10)
 
24
  encoding = tiktoken.get_encoding("cl100k_base")
25
  if model == "gpt-3.5-turbo": # note: future models may deviate from this
26
  num_tokens = 0
27
+ len_values = 0
28
  for message in messages:
29
  num_tokens += 4 # every message follows <im_start>{role/name}\n{content}<im_end>\n
30
  for key, value in message.items():
31
+ try:
32
+ num_tokens += len(encoding.encode(value))
33
+ except:
34
+ num_tokens += int(num_tokens/len_values*len(value)) # linear estimation
35
+ len_values += len(value)
36
  if key == "name": # if there's a name, the role is omitted
37
  num_tokens += -1 # role is always required and always 1 token
38
  num_tokens += 2 # every reply is primed with <im_start>assistant
 
100
  idx = openai.api_keys.index(openai.api_key)
101
  idx = (idx + 1) % len(openai.api_keys)
102
  openai.api_key = openai.api_keys[idx]
103
+ attempt_times -= 0.7
104
  print(f"switch api_key")
105
  print(f"retry in {attempt_times*10} seconds...")
106
  time.sleep(attempt_times*10)