Spaces:
Sleeping
Sleeping
再次修改回复格式的问题
Browse files
app.py
CHANGED
|
@@ -30,34 +30,18 @@ llm = Llama(
|
|
| 30 |
print("Model loaded successfully!")
|
| 31 |
|
| 32 |
def chat(message, history):
|
| 33 |
-
|
| 34 |
|
| 35 |
conversation = "System: You are a helpful assistant.\n"
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 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 |
-
|
| 54 |
-
conversation += f"User: {human}\n"
|
| 55 |
-
conversation += f"Assistant: {assistant or ''}\n"
|
| 56 |
|
| 57 |
-
|
| 58 |
-
conversation += f"User: {message}\nAssistant:"
|
| 59 |
|
| 60 |
-
# LLM inference
|
| 61 |
response = llm(
|
| 62 |
conversation,
|
| 63 |
max_tokens=128,
|
|
@@ -65,11 +49,12 @@ def chat(message, history):
|
|
| 65 |
top_p=0.9,
|
| 66 |
top_k=40,
|
| 67 |
repeat_penalty=1.1,
|
| 68 |
-
stop=["User:", "Assistant:"]
|
| 69 |
-
echo=False
|
| 70 |
)
|
| 71 |
|
| 72 |
-
|
|
|
|
|
|
|
| 73 |
|
| 74 |
|
| 75 |
# Create interface WITHOUT example caching
|
|
|
|
| 30 |
print("Model loaded successfully!")
|
| 31 |
|
| 32 |
def chat(message, history):
|
| 33 |
+
llm_message = message["content"]
|
| 34 |
|
| 35 |
conversation = "System: You are a helpful assistant.\n"
|
| 36 |
|
| 37 |
+
for msg in history[-3:]:
|
| 38 |
+
if msg["role"] == "user":
|
| 39 |
+
conversation += f"User: {msg['content']}\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
else:
|
| 41 |
+
conversation += f"Assistant: {msg['content']}\n"
|
|
|
|
|
|
|
| 42 |
|
| 43 |
+
conversation += f"User: {llm_message}\nAssistant:"
|
|
|
|
| 44 |
|
|
|
|
| 45 |
response = llm(
|
| 46 |
conversation,
|
| 47 |
max_tokens=128,
|
|
|
|
| 49 |
top_p=0.9,
|
| 50 |
top_k=40,
|
| 51 |
repeat_penalty=1.1,
|
| 52 |
+
stop=["User:", "Assistant:"]
|
|
|
|
| 53 |
)
|
| 54 |
|
| 55 |
+
reply = response["choices"][0]["text"].strip()
|
| 56 |
+
|
| 57 |
+
return {"role": "assistant", "content": reply}
|
| 58 |
|
| 59 |
|
| 60 |
# Create interface WITHOUT example caching
|