Skip to content

Commit 98646f5

Browse files
committed
fix(chat-ui): avoid double LLM call and stale history on errors
fix
1 parent 369d7d4 commit 98646f5

2 files changed

Lines changed: 10 additions & 7 deletions

File tree

nemoguardrails/server/api.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def __init__(self, *args, **kwargs):
6767
# Initialize custom attributes
6868
self.default_config_id: Optional[str] = None
6969
self.rails_config_path: str = ""
70-
self.disable_chat_ui: bool = False
70+
self.disable_chat_ui: bool = os.getenv("NEMO_GUARDRAILS_DISABLE_CHAT_UI", "false").lower() == "true"
7171
self.auto_reload: bool = False
7272
self.stop_signal: bool = False
7373
self.single_config_mode: bool = False
@@ -186,8 +186,6 @@ async def lifespan(app: GuardrailsApp):
186186
app.single_config_mode = False
187187
app.single_config_id = None
188188

189-
app.disable_chat_ui = os.getenv("NEMO_GUARDRAILS_DISABLE_CHAT_UI", "false").lower() == "true"
190-
191189

192190
@app.get(
193191
"/v1/rails/configs",
@@ -725,7 +723,7 @@ class GuardrailsConfigurationError(Exception):
725723

726724
@app.get("/")
727725
async def root_redirect():
728-
return RedirectResponse(url="/chat")
726+
return RedirectResponse(url="chat")
729727

730728
else:
731729
if not app.disable_chat_ui and mount_chainlit is None:

nemoguardrails/server/app.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,25 +115,28 @@ async def on_message(message: cl.Message):
115115

116116
try:
117117
llm_rails = await _get_rails([config_id])
118-
except ValueError:
118+
except Exception:
119119
log.exception("Failed to load rails config '%s'", config_id)
120+
messages.pop()
121+
cl.user_session.set("messages", messages)
120122
await cl.Message(content=f"Error loading guardrails configuration '{config_id}'. Check server logs.").send()
121123
return
122124

123125
response_msg = cl.Message(content="")
124126
await response_msg.send()
125127

126128
full_response = ""
129+
streaming_unsupported = False
127130
try:
128131
try:
129132
async for chunk in llm_rails.stream_async(messages=messages):
130133
if isinstance(chunk, str) and chunk:
131134
full_response += chunk
132135
await response_msg.stream_token(chunk)
133136
except StreamingNotSupportedError:
134-
full_response = ""
137+
streaming_unsupported = True
135138

136-
if not full_response:
139+
if streaming_unsupported:
137140
result = await llm_rails.generate_async(messages=messages)
138141
full_response = result.get("content", str(result)) if isinstance(result, dict) else str(result)
139142
response_msg.content = full_response
@@ -144,5 +147,7 @@ async def on_message(message: cl.Message):
144147

145148
except Exception:
146149
log.exception("Error generating response for config '%s'", config_id)
150+
messages.pop()
151+
cl.user_session.set("messages", messages)
147152
response_msg.content = f"An error occurred for configuration '{config_id}'. Check server logs."
148153
await response_msg.update()

0 commit comments

Comments
 (0)