Spaces:
Sleeping
Sleeping
sleep for long quests
Browse files
app.py
CHANGED
@@ -12,17 +12,21 @@ def ask(question, history, behavior):
|
|
12 |
openai.request_times += 1
|
13 |
print(f"request times {openai.request_times}: {datetime.datetime.now()}: {question}")
|
14 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
response = openai.ChatCompletion.create(
|
16 |
model="gpt-3.5-turbo",
|
17 |
-
messages=forget_long_term(
|
18 |
-
[
|
19 |
-
{"role":"system", "content":content}
|
20 |
-
for content in behavior
|
21 |
-
] + [
|
22 |
-
{"role":"user" if i%2==0 else "assistant", "content":content}
|
23 |
-
for i,content in enumerate(history + [question])
|
24 |
-
]
|
25 |
-
)
|
26 |
)["choices"][0]["message"]["content"]
|
27 |
while response.startswith("\n"):
|
28 |
response = response[1:]
|
@@ -30,6 +34,7 @@ def ask(question, history, behavior):
|
|
30 |
print(e)
|
31 |
response = 'Timeout! Please wait a few minutes and retry'
|
32 |
history = history + [question, response]
|
|
|
33 |
return history
|
34 |
|
35 |
def num_tokens_from_messages(messages, model="gpt-3.5-turbo"):
|
@@ -162,4 +167,5 @@ with gr.Blocks() as demo:
|
|
162 |
button_gen.click(fn=predict, inputs=[txt, state, behavior], outputs=[txt, state, chatbot])
|
163 |
button_clr.click(fn=lambda :([],[]), inputs=None, outputs=[chatbot, state])
|
164 |
|
|
|
165 |
demo.launch()
|
|
|
12 |
openai.request_times += 1
|
13 |
print(f"request times {openai.request_times}: {datetime.datetime.now()}: {question}")
|
14 |
try:
|
15 |
+
messages = [
|
16 |
+
{"role":"system", "content":content}
|
17 |
+
for content in behavior
|
18 |
+
] + [
|
19 |
+
{"role":"user" if i%2==0 else "assistant", "content":content}
|
20 |
+
for i,content in enumerate(history + [question])
|
21 |
+
]
|
22 |
+
length_messages = num_tokens_from_messages(messages)
|
23 |
+
time_penalty = (length_messages-1000)//10
|
24 |
+
if time_penalty>0:
|
25 |
+
print(f"sleep for {time_penalty:.2f}s for too long a quest: {length_messages}")
|
26 |
+
time.sleep(time_penalty)
|
27 |
response = openai.ChatCompletion.create(
|
28 |
model="gpt-3.5-turbo",
|
29 |
+
messages=forget_long_term(messages)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
)["choices"][0]["message"]["content"]
|
31 |
while response.startswith("\n"):
|
32 |
response = response[1:]
|
|
|
34 |
print(e)
|
35 |
response = 'Timeout! Please wait a few minutes and retry'
|
36 |
history = history + [question, response]
|
37 |
+
record_dialogue(history)
|
38 |
return history
|
39 |
|
40 |
def num_tokens_from_messages(messages, model="gpt-3.5-turbo"):
|
|
|
167 |
button_gen.click(fn=predict, inputs=[txt, state, behavior], outputs=[txt, state, chatbot])
|
168 |
button_clr.click(fn=lambda :([],[]), inputs=None, outputs=[chatbot, state])
|
169 |
|
170 |
+
demo.queue(concurrency_count=3, max_size=10)
|
171 |
demo.launch()
|