zhangjf commited on
Commit
df5f063
·
1 Parent(s): a58a224

limit query length to 500

Browse files
Files changed (1) hide show
  1. app.py +12 -12
app.py CHANGED
@@ -20,17 +20,17 @@ def ask(question, history, behavior):
20
  {"role":"user" if i%2==0 else "assistant", "content":content}
21
  for i,content in enumerate(history + [question])
22
  ]
23
- length_messages = num_tokens_from_messages(messages)
24
- time_penalty = (length_messages-1000)//10
25
- if time_penalty>0:
26
- print(f"sleep for {time_penalty:.2f}s for too long a quest: {length_messages}")
27
- time.sleep(time_penalty)
28
- response = openai.ChatCompletion.create(
29
- model="gpt-3.5-turbo",
30
- messages=forget_long_term(messages)
31
- )["choices"][0]["message"]["content"]
32
- while response.startswith("\n"):
33
- response = response[1:]
34
  except Exception as e:
35
  print(e)
36
  response = 'Timeout! Please wait a few minutes and retry'
@@ -57,7 +57,7 @@ def num_tokens_from_messages(messages, model="gpt-3.5-turbo"):
57
  raise NotImplementedError(f"""num_tokens_from_messages() is not presently implemented for model {model}.
58
  See https://github.com/openai/openai-python/blob/main/chatml.md for information on how messages are converted to tokens.""")
59
 
60
- def forget_long_term(messages, max_num_tokens=4000):
61
  while num_tokens_from_messages(messages)>max_num_tokens:
62
  if messages[0]["role"]=="system" and not len(messages[0]["content"]>=max_num_tokens):
63
  messages = messages[:1] + messages[2:]
 
20
  {"role":"user" if i%2==0 else "assistant", "content":content}
21
  for i,content in enumerate(history + [question])
22
  ]
23
+ raw_length = num_tokens_from_messages(messages)
24
+ messages=forget_long_term(messages)
25
+ if len(messages)==0:
26
+ response = 'Your query is too long and expensive: {raw_length}>500 tokens'
27
+ else:
28
+ response = openai.ChatCompletion.create(
29
+ model="gpt-3.5-turbo",
30
+ messages=messages
31
+ )["choices"][0]["message"]["content"]
32
+ while response.startswith("\n"):
33
+ response = response[1:]
34
  except Exception as e:
35
  print(e)
36
  response = 'Timeout! Please wait a few minutes and retry'
 
57
  raise NotImplementedError(f"""num_tokens_from_messages() is not presently implemented for model {model}.
58
  See https://github.com/openai/openai-python/blob/main/chatml.md for information on how messages are converted to tokens.""")
59
 
60
+ def forget_long_term(messages, max_num_tokens=500):
61
  while num_tokens_from_messages(messages)>max_num_tokens:
62
  if messages[0]["role"]=="system" and not len(messages[0]["content"]>=max_num_tokens):
63
  messages = messages[:1] + messages[2:]