aslan-ng commited on
Commit
77c1869
·
verified ·
1 Parent(s): 9f31560

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -11
app.py CHANGED
@@ -758,23 +758,37 @@ with gr.Blocks() as demo:
758
  )
759
 
760
  def respond(message, history):
761
- # `history` here is a list of {"role", "content"} dicts (messages format)
762
  if history is None:
763
  history = []
764
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
765
  try:
766
- out = str(agent.run(message))
767
  except Exception as e:
768
- out = f"[Error] {e}"
769
-
770
- # APPEND MESSAGES AS DICTS, NOT LISTS/TUPLES
771
- history = history + [
772
  {"role": "user", "content": message},
773
- {"role": "assistant", "content": out},
774
  ]
775
-
776
- # Clear input, update chat
777
- return "", history
778
 
779
  gr.Examples(
780
  examples=[
 
758
  )
759
 
760
  def respond(message, history):
761
+ # Initialize history if needed
762
  if history is None:
763
  history = []
764
+
765
+ # Build smolagents message list
766
+ smol_messages = []
767
+
768
+ # 1. Add system prompt FIRST
769
+ smol_messages.append({"role": "system", "content": system_prompt})
770
+
771
+ # 2. Add full chat history (each entry already {"role", "content"})
772
+ for turn in history:
773
+ smol_messages.append({"role": turn["role"], "content": turn["content"]})
774
+
775
+ # 3. Add the new user message
776
+ smol_messages.append({"role": "user", "content": message})
777
+
778
+ # Run the agent with the FULL conversation
779
  try:
780
+ response_text = str(agent.run(smol_messages))
781
  except Exception as e:
782
+ response_text = f"[Error] {e}"
783
+
784
+ # Add latest exchange to UI history
785
+ updated_history = history + [
786
  {"role": "user", "content": message},
787
+ {"role": "assistant", "content": response_text},
788
  ]
789
+
790
+ # Clear input + update chat
791
+ return "", updated_history
792
 
793
  gr.Examples(
794
  examples=[