Datangtang commited on
Commit
ee73df4
·
verified ·
1 Parent(s): 77cf31a

修复 chat()

Browse files
Files changed (1) hide show
  1. app.py +33 -19
app.py CHANGED
@@ -31,32 +31,46 @@ print("Model loaded successfully!")
31
 
32
  def chat(message, history):
33
  """Handle chat interactions"""
34
- # Build conversation (keep it short)
35
- conversation = ""
36
-
37
- # Only use last 3 turns of history to keep context short
38
  recent_history = history[-3:] if len(history) > 3 else history
39
-
40
- for human, assistant in recent_history:
41
- conversation += f"User: {human}\n"
42
- conversation += f"Assistant: {assistant}\n"
43
-
44
- conversation += f"User: {message}\n"
45
- conversation += "Assistant:"
46
-
47
- # Generate with optimized settings
 
 
 
 
 
 
 
 
 
 
 
 
 
48
  response = llm(
49
  conversation,
50
- max_tokens=128, # Reduced from 256 (faster)
51
  temperature=0.7,
52
  top_p=0.9,
53
- top_k=40, # Added: limit sampling
54
  repeat_penalty=1.1,
55
- stop=["User:", "\n\n"],
56
- echo=False,
57
  )
58
-
59
- return response['choices'][0]['text'].strip()
 
60
 
61
  # Create interface WITHOUT example caching
62
  demo = gr.ChatInterface(
 
31
 
32
  def chat(message, history):
33
  """Handle chat interactions"""
34
+
35
+ conversation = "System: You are a helpful assistant.\n"
36
+
37
+ # Only use last 3 turns
38
  recent_history = history[-3:] if len(history) > 3 else history
39
+
40
+ for msg in recent_history:
41
+ # ChatInterface passes dict messages
42
+ if isinstance(msg, dict):
43
+ role = msg.get("role")
44
+ content = msg.get("content", "")
45
+
46
+ if role == "user":
47
+ conversation += f"User: {content}\n"
48
+ elif role == "assistant":
49
+ conversation += f"Assistant: {content}\n"
50
+
51
+ # fallback: old tuple/list history
52
+ else:
53
+ human, assistant = msg
54
+ conversation += f"User: {human}\n"
55
+ conversation += f"Assistant: {assistant or ''}\n"
56
+
57
+ # Add current user message
58
+ conversation += f"User: {message}\nAssistant:"
59
+
60
+ # LLM inference
61
  response = llm(
62
  conversation,
63
+ max_tokens=128,
64
  temperature=0.7,
65
  top_p=0.9,
66
+ top_k=40,
67
  repeat_penalty=1.1,
68
+ stop=["User:", "Assistant:"],
69
+ echo=False
70
  )
71
+
72
+ return response["choices"][0]["text"].strip()
73
+
74
 
75
  # Create interface WITHOUT example caching
76
  demo = gr.ChatInterface(