Black Goku commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -25,7 +25,6 @@ client = InferenceClient(
|
|
| 25 |
api_key=HF_TOKEN
|
| 26 |
)
|
| 27 |
|
| 28 |
-
# You can use Qwen/Qwen2.5-7B-Instruct or meta-llama/Llama-3.1-8B-Instruct
|
| 29 |
MODEL_NAME = "Qwen/Qwen2.5-7B-Instruct"
|
| 30 |
|
| 31 |
SYSTEM_PROMPT = (
|
|
@@ -39,7 +38,26 @@ SYSTEM_PROMPT = (
|
|
| 39 |
"concrete things actually available on THIS server based on the context you were given "
|
| 40 |
"(their job, balance, nearby ores or structures, ongoing events) — never invent features, "
|
| 41 |
"currencies, or events that weren't mentioned in the context. "
|
| 42 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
)
|
| 44 |
|
| 45 |
class ChatRequest(BaseModel):
|
|
@@ -68,7 +86,7 @@ async def chat(request: ChatRequest):
|
|
| 68 |
except Exception as e:
|
| 69 |
print("[Error in /chat]:", e)
|
| 70 |
traceback.print_exc()
|
| 71 |
-
|
| 72 |
error_msg = str(e)
|
| 73 |
if "503" in error_msg or "loading" in error_msg.lower():
|
| 74 |
return {"reply": "My Cloud Brain is currently waking up. Please send this message again in 10 seconds!"}
|
|
@@ -77,6 +95,20 @@ async def chat(request: ChatRequest):
|
|
| 77 |
|
| 78 |
@app.post("/chat/stream")
|
| 79 |
async def chat_stream(request: ChatRequest):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
def token_generator():
|
| 81 |
try:
|
| 82 |
stream = client.chat.completions.create(
|
|
@@ -99,4 +131,4 @@ async def chat_stream(request: ChatRequest):
|
|
| 99 |
print("[Error in /chat/stream]:", e)
|
| 100 |
yield json.dumps({"error": str(e)}) + "\n"
|
| 101 |
|
| 102 |
-
return StreamingResponse(token_generator(), media_type="application/x-ndjson")
|
|
|
|
| 25 |
api_key=HF_TOKEN
|
| 26 |
)
|
| 27 |
|
|
|
|
| 28 |
MODEL_NAME = "Qwen/Qwen2.5-7B-Instruct"
|
| 29 |
|
| 30 |
SYSTEM_PROMPT = (
|
|
|
|
| 38 |
"concrete things actually available on THIS server based on the context you were given "
|
| 39 |
"(their job, balance, nearby ores or structures, ongoing events) — never invent features, "
|
| 40 |
"currencies, or events that weren't mentioned in the context. "
|
| 41 |
+
"\n\n"
|
| 42 |
+
"ACTIONS: You can trigger real server actions by including a special tag anywhere in your "
|
| 43 |
+
"reply. The Minecraft plugin reading your reply will strip these tags before showing your "
|
| 44 |
+
"message to the player and will actually perform the action, so only include a tag when you "
|
| 45 |
+
"genuinely mean to do the thing right now. Never explain the tag syntax to players, never "
|
| 46 |
+
"show it to them in an example — just use it naturally when it's the right moment.\n"
|
| 47 |
+
"Available tags:\n"
|
| 48 |
+
" [ACTION:PAY:<playername>:<amount>] — pay another player from the asker's own balance.\n"
|
| 49 |
+
" [ACTION:GIVE_ITEM:<playername>:<MINECRAFT_MATERIAL_ID>:<amount>] — give an item from the "
|
| 50 |
+
"asker's own inventory to another player. Use real Minecraft material ids in caps with "
|
| 51 |
+
"underscores, e.g. DIAMOND, IRON_INGOT, GOLDEN_APPLE.\n"
|
| 52 |
+
" [ACTION:TELEPORT_SELF:<destination>] — teleport the asker themself somewhere, e.g. spawn or home.\n"
|
| 53 |
+
" [ACTION:BUY:<MINECRAFT_MATERIAL_ID>:<amount>] — buy an item for the asker, paid from their "
|
| 54 |
+
"own balance at the server's price.\n"
|
| 55 |
+
"Only ever use these when the player's message clearly asks for that specific thing — for "
|
| 56 |
+
"example 'vex pay Steve 1000' means use PAY, 'vex send me home' means use TELEPORT_SELF, "
|
| 57 |
+
"'vex buy me a potion' means use BUY. If a request is ambiguous or missing information (like "
|
| 58 |
+
"a missing amount or an unclear target), ask a clarifying question in plain text instead of "
|
| 59 |
+
"guessing and using a tag. Never use more than one action tag per reply unless the player "
|
| 60 |
+
"clearly asked for multiple distinct things."
|
| 61 |
)
|
| 62 |
|
| 63 |
class ChatRequest(BaseModel):
|
|
|
|
| 86 |
except Exception as e:
|
| 87 |
print("[Error in /chat]:", e)
|
| 88 |
traceback.print_exc()
|
| 89 |
+
|
| 90 |
error_msg = str(e)
|
| 91 |
if "503" in error_msg or "loading" in error_msg.lower():
|
| 92 |
return {"reply": "My Cloud Brain is currently waking up. Please send this message again in 10 seconds!"}
|
|
|
|
| 95 |
|
| 96 |
@app.post("/chat/stream")
|
| 97 |
async def chat_stream(request: ChatRequest):
|
| 98 |
+
"""
|
| 99 |
+
Streams the reply as newline-delimited JSON chunks:
|
| 100 |
+
{"token": "Hel"}
|
| 101 |
+
{"token": "lo"}
|
| 102 |
+
...
|
| 103 |
+
{"done": true}
|
| 104 |
+
On error, a single line is sent: {"error": "<message>"}
|
| 105 |
+
A plain HTTP client can just read the response body line by line.
|
| 106 |
+
|
| 107 |
+
NOTE: action tags may be split across streamed token chunks. If your
|
| 108 |
+
client needs to react to actions from the streaming endpoint, buffer
|
| 109 |
+
the full text and run the same [ACTION:...] parsing after streaming
|
| 110 |
+
completes — don't try to detect tags token-by-token.
|
| 111 |
+
"""
|
| 112 |
def token_generator():
|
| 113 |
try:
|
| 114 |
stream = client.chat.completions.create(
|
|
|
|
| 131 |
print("[Error in /chat/stream]:", e)
|
| 132 |
yield json.dumps({"error": str(e)}) + "\n"
|
| 133 |
|
| 134 |
+
return StreamingResponse(token_generator(), media_type="application/x-ndjson")
|