Spaces:
Build error
Build error
Commit
·
a0b9eb0
1
Parent(s):
8d1bc11
release v2.0
Browse files- pokemon_server.py +78 -0
pokemon_server.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
+
from pydantic import BaseModel, Field
|
| 4 |
+
from typing import Set, List, Dict
|
| 5 |
+
from agentverse.simulation import Simulation
|
| 6 |
+
from agentverse.message import Message
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
class UserRequest(BaseModel):
|
| 10 |
+
content: str = Field(default="")
|
| 11 |
+
sender: str = Field(default="Brendan")
|
| 12 |
+
receiver: str
|
| 13 |
+
receiver_id: int
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
class RoutineRequest(BaseModel):
|
| 17 |
+
agent_ids: List[int]
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
class UpdateRequest(BaseModel):
|
| 21 |
+
agent_locations: Dict[str, str]
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
app = FastAPI()
|
| 25 |
+
|
| 26 |
+
app.add_middleware(
|
| 27 |
+
CORSMiddleware,
|
| 28 |
+
allow_origins=["*"],
|
| 29 |
+
allow_credentials=True,
|
| 30 |
+
allow_methods=["*"],
|
| 31 |
+
allow_headers=["*"],
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
agent_verse = Simulation.from_task("pokemon")
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
@app.get("/")
|
| 38 |
+
def health_check():
|
| 39 |
+
return {"status": "ok"}
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
@app.post("/chat")
|
| 43 |
+
def chat(message: UserRequest):
|
| 44 |
+
content = message.content
|
| 45 |
+
receiver = message.receiver
|
| 46 |
+
receiver_id = message.receiver_id
|
| 47 |
+
response = agent_verse.next(
|
| 48 |
+
is_player=True,
|
| 49 |
+
player_content=content,
|
| 50 |
+
receiver=receiver,
|
| 51 |
+
receiver_id=receiver_id,
|
| 52 |
+
)
|
| 53 |
+
return response[0].dict()
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
@app.post("/make_decision")
|
| 57 |
+
def update(message: RoutineRequest):
|
| 58 |
+
response = agent_verse.next(is_player=False, agent_ids=message.agent_ids)
|
| 59 |
+
return [r.dict() for r in response]
|
| 60 |
+
# import json
|
| 61 |
+
|
| 62 |
+
# return [
|
| 63 |
+
# # {
|
| 64 |
+
# # "content": json.dumps(
|
| 65 |
+
# # {
|
| 66 |
+
# # "to": "Maxie",
|
| 67 |
+
# # "action": "Speak",
|
| 68 |
+
# # "text": "Hello Hello Hello Hello Hello Hello",
|
| 69 |
+
# # }
|
| 70 |
+
# # )
|
| 71 |
+
# # }
|
| 72 |
+
# {"content": json.dumps({"to": "Pokémon Center", "action": "MoveTo"})}
|
| 73 |
+
# ]
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
@app.post("/update_location")
|
| 77 |
+
def update_location(message: UpdateRequest):
|
| 78 |
+
agent_verse.update_state(message.agent_locations)
|