Spaces:
Runtime error
Runtime error
Commit
·
0aec042
1
Parent(s):
4516f6c
implement message history management with trimming functionality
Browse files- app/app.py +15 -10
- app/utils.py +8 -1
app/app.py
CHANGED
|
@@ -11,7 +11,7 @@ from ulid import ulid
|
|
| 11 |
import asyncio
|
| 12 |
import time
|
| 13 |
|
| 14 |
-
from utils import MODELS_PROVIDERS_MAP, PROVIDERS_API_KEYS, REASONER_PROVIDERS_MAP, check_openai_api_key
|
| 15 |
from settings import PROFILES_SETTINGS
|
| 16 |
|
| 17 |
DEFAULT_REASONER_CONFIG = LlmConfig(
|
|
@@ -90,6 +90,7 @@ async def setup_agent(settings):
|
|
| 90 |
@cl.on_chat_start
|
| 91 |
async def start_chat():
|
| 92 |
user_profile = cl.user_session.get("chat_profile")
|
|
|
|
| 93 |
llm_config = DEFAULT_LLM_CONFIG.get(user_profile)
|
| 94 |
llm = Llm.from_config(llm_config)
|
| 95 |
llm.session_id = ulid()
|
|
@@ -100,6 +101,7 @@ async def start_chat():
|
|
| 100 |
settings = await cl.ChatSettings(
|
| 101 |
PROFILES_SETTINGS.get(user_profile)
|
| 102 |
).send()
|
|
|
|
| 103 |
|
| 104 |
async def run_concurrent_tasks(llm, message):
|
| 105 |
asyncio.create_task(llm.acomplete(message))
|
|
@@ -128,7 +130,11 @@ async def main(message: cl.Message):
|
|
| 128 |
|
| 129 |
start = time.time()
|
| 130 |
thinking=False
|
| 131 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
try:
|
| 133 |
if llm.reasoner is not None or llm.config.model in REASONER_PROVIDERS_MAP:
|
| 134 |
# Streaming the thinking
|
|
@@ -136,7 +142,7 @@ async def main(message: cl.Message):
|
|
| 136 |
msg = cl.Message(content="")
|
| 137 |
async for chunk in run_concurrent_tasks(
|
| 138 |
llm,
|
| 139 |
-
message=
|
| 140 |
):
|
| 141 |
if chunk == STREAM_START_TOKEN:
|
| 142 |
continue
|
|
@@ -150,6 +156,7 @@ async def main(message: cl.Message):
|
|
| 150 |
thinking_step.name = f"{llm.reasoner.config.model} to think for {thought_for}s"
|
| 151 |
await thinking_step.update()
|
| 152 |
chunk = f"```{llm.config.model}```\n"
|
|
|
|
| 153 |
|
| 154 |
if chunk == STREAM_END_TOKEN:
|
| 155 |
break
|
|
@@ -162,7 +169,7 @@ async def main(message: cl.Message):
|
|
| 162 |
msg = cl.Message(content="")
|
| 163 |
async for chunk in run_concurrent_tasks(
|
| 164 |
llm,
|
| 165 |
-
message=
|
| 166 |
):
|
| 167 |
if chunk == STREAM_START_TOKEN:
|
| 168 |
continue
|
|
@@ -171,14 +178,12 @@ async def main(message: cl.Message):
|
|
| 171 |
break
|
| 172 |
|
| 173 |
await msg.stream_token(chunk)
|
| 174 |
-
|
| 175 |
-
|
|
|
|
| 176 |
await msg.send()
|
| 177 |
|
| 178 |
except Exception as e:
|
| 179 |
await cl.ErrorMessage("Internal Server Error").send()
|
| 180 |
|
| 181 |
-
### TODO add support for
|
| 182 |
-
### Add suppor to deactivate history in settings
|
| 183 |
-
### TODO add future todos, include support for images and pdf upload for conversation
|
| 184 |
-
### TODO openrouter missing'
|
|
|
|
| 11 |
import asyncio
|
| 12 |
import time
|
| 13 |
|
| 14 |
+
from utils import MODELS_PROVIDERS_MAP, PROVIDERS_API_KEYS, REASONER_PROVIDERS_MAP, check_openai_api_key, trim_messages
|
| 15 |
from settings import PROFILES_SETTINGS
|
| 16 |
|
| 17 |
DEFAULT_REASONER_CONFIG = LlmConfig(
|
|
|
|
| 90 |
@cl.on_chat_start
|
| 91 |
async def start_chat():
|
| 92 |
user_profile = cl.user_session.get("chat_profile")
|
| 93 |
+
cl.user_session.set("history", [])
|
| 94 |
llm_config = DEFAULT_LLM_CONFIG.get(user_profile)
|
| 95 |
llm = Llm.from_config(llm_config)
|
| 96 |
llm.session_id = ulid()
|
|
|
|
| 101 |
settings = await cl.ChatSettings(
|
| 102 |
PROFILES_SETTINGS.get(user_profile)
|
| 103 |
).send()
|
| 104 |
+
|
| 105 |
|
| 106 |
async def run_concurrent_tasks(llm, message):
|
| 107 |
asyncio.create_task(llm.acomplete(message))
|
|
|
|
| 130 |
|
| 131 |
start = time.time()
|
| 132 |
thinking=False
|
| 133 |
+
|
| 134 |
+
history = cl.user_session.get("history")
|
| 135 |
+
history.append(message.content)
|
| 136 |
+
history = trim_messages(history, llm.tokenizer)
|
| 137 |
+
model_id = None
|
| 138 |
try:
|
| 139 |
if llm.reasoner is not None or llm.config.model in REASONER_PROVIDERS_MAP:
|
| 140 |
# Streaming the thinking
|
|
|
|
| 142 |
msg = cl.Message(content="")
|
| 143 |
async for chunk in run_concurrent_tasks(
|
| 144 |
llm,
|
| 145 |
+
message=history
|
| 146 |
):
|
| 147 |
if chunk == STREAM_START_TOKEN:
|
| 148 |
continue
|
|
|
|
| 156 |
thinking_step.name = f"{llm.reasoner.config.model} to think for {thought_for}s"
|
| 157 |
await thinking_step.update()
|
| 158 |
chunk = f"```{llm.config.model}```\n"
|
| 159 |
+
model_id = f"```{llm.config.model}```\n"
|
| 160 |
|
| 161 |
if chunk == STREAM_END_TOKEN:
|
| 162 |
break
|
|
|
|
| 169 |
msg = cl.Message(content="")
|
| 170 |
async for chunk in run_concurrent_tasks(
|
| 171 |
llm,
|
| 172 |
+
message=history
|
| 173 |
):
|
| 174 |
if chunk == STREAM_START_TOKEN:
|
| 175 |
continue
|
|
|
|
| 178 |
break
|
| 179 |
|
| 180 |
await msg.stream_token(chunk)
|
| 181 |
+
|
| 182 |
+
hst_msg = msg.content.replace(model_id, "") if model_id else msg.content
|
| 183 |
+
history.append(hst_msg)
|
| 184 |
await msg.send()
|
| 185 |
|
| 186 |
except Exception as e:
|
| 187 |
await cl.ErrorMessage("Internal Server Error").send()
|
| 188 |
|
| 189 |
+
### TODO add future todos, include support for images and pdf upload for conversation
|
|
|
|
|
|
|
|
|
app/utils.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
from pathlib import Path
|
|
|
|
| 2 |
import openai
|
| 3 |
import os
|
| 4 |
|
|
@@ -35,4 +36,10 @@ def check_openai_api_key(api_key, base_url=None):
|
|
| 35 |
except openai.AuthenticationError:
|
| 36 |
return False
|
| 37 |
else:
|
| 38 |
-
return True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from pathlib import Path
|
| 2 |
+
from typing import Optional
|
| 3 |
import openai
|
| 4 |
import os
|
| 5 |
|
|
|
|
| 36 |
except openai.AuthenticationError:
|
| 37 |
return False
|
| 38 |
else:
|
| 39 |
+
return True
|
| 40 |
+
|
| 41 |
+
def trim_messages(messages, tokenizer_fn, max_tokens :Optional[int]=None):
|
| 42 |
+
max_tokens = max_tokens or int(os.environ.get("MAX_HISTORY_TOKENS", 1028))
|
| 43 |
+
while messages and sum(len(tokenizer_fn(msg)) for msg in messages) > max_tokens:
|
| 44 |
+
messages.pop(0) # Remove from the beginning
|
| 45 |
+
return messages
|