Spaces:
Running
Running
limit query length to 500
Browse files
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 |
-
|
24 |
-
|
25 |
-
if
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
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=
|
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:]
|