peterpeter8585 commited on
Commit
a1a7ddf
Β·
verified Β·
1 Parent(s): 0b58527

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -7
app.py CHANGED
@@ -377,16 +377,40 @@ agent=initialize_agent(tools,llm,agent=AgentType.STRUCTURED_CHAT_ZERO_SHOT_REACT
377
  # ──────────────────────────────
378
  def chat(message, history):
379
  try:
380
- result = agent.invoke(message)
381
- # dict λŒ€μ‘ 처리
382
- if isinstance(result, dict):
383
- response = result.get("output") or result.get("answer") or str(result)
384
- else:
385
- response = str(result)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
386
  except Exception as e:
387
  response = f"⚠️ 였λ₯˜: {e}"
388
- history.append((message, response))
 
389
  return history, history
 
390
  with gr.Blocks(theme=gr.themes.Soft(), title="PIXAL Assistant (LangChain + GitHub LLM)") as demo:
391
  gr.Markdown("""
392
  ## πŸ€– PIXAL Assistant
 
377
  # ──────────────────────────────
378
  def chat(message, history):
379
  try:
380
+ response = agent.run(message)
381
+
382
+ # JSON ν˜•νƒœλ‘œ 좜λ ₯될 κ°€λŠ₯성이 μžˆλŠ” 경우 처리
383
+ if isinstance(response, dict):
384
+ if "action_input" in response:
385
+ response = response["action_input"]
386
+ elif "output" in response:
387
+ response = response["output"]
388
+ elif "text" in response:
389
+ response = response["text"]
390
+ else:
391
+ response = str(response)
392
+ elif isinstance(response, str):
393
+ # "Final Answer"κ°€ ν¬ν•¨λœ λ¬Έμžμ—΄μ΄λ©΄ κ·Έ λΆ€λΆ„λ§Œ μΆ”μΆœ
394
+ if '"action_input":' in response:
395
+ import re, json
396
+ match = re.search(r'["\']action_input["\']\s*:\s*["\'](.*?)["\']', response)
397
+ if match:
398
+ response = match.group(1)
399
+ elif "Final Answer" in response:
400
+ # {"action": "Final Answer", "action_input": "..."} ν˜•μ‹μΌ λ•Œ
401
+ try:
402
+ data = json.loads(response)
403
+ if isinstance(data, dict) and "action_input" in data:
404
+ response = data["action_input"]
405
+ except Exception:
406
+ response = response.replace("Final Answer", "").strip()
407
+
408
  except Exception as e:
409
  response = f"⚠️ 였λ₯˜: {e}"
410
+
411
+ history = history + [(message, response)]
412
  return history, history
413
+
414
  with gr.Blocks(theme=gr.themes.Soft(), title="PIXAL Assistant (LangChain + GitHub LLM)") as demo:
415
  gr.Markdown("""
416
  ## πŸ€– PIXAL Assistant