feat: inject today's date into system prompt and add extended thinking support
Fixes age validation errors caused by the LLM not knowing the current date.
Changes:
- prompts.py: inject date.today() at the top of both system prompts so the
LLM can accurately calculate a child's age from their date of birth
- llm.py: add optional thinking_budget parameter to complete(); when set,
passes thinking={"type": "enabled", "budget_tokens": N} to litellm and
raises max_tokens to thinking_budget + 4096 (Anthropic models only)
- config.py: add thinking_budget field, read from THINKING_BUDGET env var
- .env.example: document the THINKING_BUDGET option
- core.py: pass thinking_budget through to llm.complete()
- main.py: pass thinking_budget when constructing EmailAgent
- chat_app.py: switch from stream_complete to asyncio.to_thread(complete)
so extended thinking works and so only the reply field is shown to
the parent (not the raw JSON wrapper)
To enable extended thinking set THINKING_BUDGET=8000 in .env.
https://claude.ai/code/session_01SUWzMzFvSfWiHXA2p6rPg9
This commit is contained in:
+7
-9
@@ -123,17 +123,15 @@ async def on_message(message: cl.Message) -> None:
|
||||
# --- Build system prompt ---
|
||||
system = build_system_prompt(_kb, state)
|
||||
|
||||
# --- Call LLM natively async to keep the event loop free ---
|
||||
# litellm.acompletion() is a true coroutine — it does not block the event
|
||||
# loop and does not require asyncio.to_thread (which can drop Chainlit's
|
||||
# contextvars, causing the session to reset mid-conversation).
|
||||
# --- Call LLM natively async (supports extended thinking; no event-loop blocking) ---
|
||||
try:
|
||||
full_content = await llm.acomplete(_config.ai_model, system, state.messages)
|
||||
full_content = await llm.acomplete(
|
||||
_config.ai_model, system, state.messages, _config.thinking_budget
|
||||
)
|
||||
except Exception:
|
||||
logger.exception("LLM call failed for session %s", state.conversation_id)
|
||||
error_text = fallback_message(state.language)
|
||||
await cl.Message(content=error_text).send()
|
||||
# Don't update state — let parent retry
|
||||
return
|
||||
|
||||
# --- Parse and apply LLM response ---
|
||||
@@ -151,12 +149,12 @@ async def on_message(message: cl.Message) -> None:
|
||||
state.language = language
|
||||
state.updated_at = now
|
||||
|
||||
# Send the reply text to the parent (only the human-readable reply, not the JSON wrapper)
|
||||
await cl.Message(content=reply_text).send()
|
||||
|
||||
# Append assistant reply to history
|
||||
state.messages.append(ChatMessage(role="assistant", content=reply_text))
|
||||
|
||||
# Send the reply text to the user (parsed from the LLM's JSON response)
|
||||
await cl.Message(content=reply_text).send()
|
||||
|
||||
# --- Handle registration completion ---
|
||||
if is_complete and not state.completed:
|
||||
state.completed = True
|
||||
|
||||
Reference in New Issue
Block a user