Black Goku commited on
Update app.py
Browse files24 unmodified lines252627282930317 unmodified lines3940414243444522 unmodified lines686970717273742 unmodified lines77787980818216 unmodified lines9910010110224 unmodified linesapi_key=HF_TOKEN)# You can use Qwen/Qwen2.5-7B-Instruct or meta-llama/Llama-3.1-8B-InstructMODEL_NAME = "Qwen/Qwen2.5-7B-Instruct"SYSTEM_PROMPT = (7 unmodified lines"concrete things actually available on THIS server based on the context you were given ""(their job, balance, nearby ores or structures, ongoing events) β never invent features, ""currencies, or events that weren't mentioned in the context. ""Keep replies short and chat-friendly, since they're shown in Minecraft chat.")class ChatRequest(BaseModel):22 unmodified linesexcept Exception as e:print("[Error in /chat]:", e)traceback.print_exc()error_msg = str(e)if "503" in error_msg or "loading" in error_msg.lower():return {"reply": "My Cloud Brain is currently waking up. Please send this message again in 10 seconds!"}2 unmodified lines@app.post("/chat/stream")async def chat_stream(request: ChatRequest):def token_generator():try:stream = client.chat.completions.create(16 unmodified linesprint("[Error in /chat/stream]:", e)yield json.dumps({"error": str(e)}) + "\n"return StreamingResponse(token_generator(), media_type="application/x-ndjson")24 unmodified lines2526272829307 unmodified lines383940414243444546474849505152535455565758596061626322 unmodified lines868788899091922 unmodified lines959697989910010110210310410510610710810911011111211311416 unmodified lines13113213313424 unmodified linesapi_key=HF_TOKEN)MODEL_NAME = "Qwen/Qwen2.5-7B-Instruct"SYSTEM_PROMPT = (7 unmodified lines"concrete things actually available on THIS server based on the context you were given ""(their job, balance, nearby ores or structures, ongoing events) β never invent features, ""currencies, or events that weren't mentioned in the context. ""\n\n""ACTIONS: You can trigger real server actions by including a special tag anywhere in your ""reply. The Minecraft plugin reading your reply will strip these tags before showing your ""message to the player and will actually perform the action, so only include a tag when you ""genuinely mean to do the thing right now. Never explain the tag syntax to players, never ""show it to them in an example β just use it naturally when it's the right moment.\n""Available tags:\n"" [ACTION:PAY:<playername>:<amount>] β pay another player from the asker's own balance.\n"" [ACTION:GIVE_ITEM:<playername>:<MINECRAFT_MATERIAL_ID>:<amount>] β give an item from the ""asker's own inventory to another player. Use real Minecraft material ids in caps with ""underscores, e.g. DIAMOND, IRON_INGOT, GOLDEN_APPLE.\n"" [ACTION:TELEPORT_SELF:<destination>] β teleport the asker themself somewhere, e.g. spawn or home.\n"" [ACTION:BUY:<MINECRAFT_MATERIAL_ID>:<amount>] β buy an item for the asker, paid from their ""own balance at the server's price.\n""Only ever use these when the player's message clearly asks for that specific thing β for ""example 'vex pay Steve 1000' means use PAY, 'vex send me home' means use TELEPORT_SELF, ""'vex buy me a potion' means use BUY. If a request is ambiguous or missing information (like ""a missing amount or an unclear target), ask a clarifying question in plain text instead of ""guessing and using a tag. Never use more than one action tag per reply unless the player ""clearly asked for multiple distinct things.")class ChatRequest(BaseModel):22 unmodified linesexcept Exception as e:print("[Error in /chat]:", e)traceback.print_exc()error_msg = str(e)if "503" in error_msg or "loading" in error_msg.lower():return {"reply": "My Cloud Brain is currently waking up. Please send this message again in 10 seconds!"}2 unmodified lines@app.post("/chat/stream")async def chat_stream(request: ChatRequest):"""Streams the reply as newline-delimited JSON chunks:{"token": "Hel"}{"token": "lo"}...{"done": true}On error, a single line is sent: {"error": "<message>"}A plain HTTP client can just read the response body line by line.NOTE: action tags may be split across streamed token chunks. If yourclient needs to react to actions from the streaming endpoint, bufferthe full text and run the same [ACTION:...] parsing after streamingcompletes β don't try to detect tags token-by-token."""def token_generator():try:stream = client.chat.completions.create(16 unmodified linesprint("[Error in /chat/stream]:", e)yield json.dumps({"error": str(e)}) + "\n"return StreamingResponse(token_generator(), media_type="application/x-ndjson")
app.py+36-4