-
Notifications
You must be signed in to change notification settings - Fork 40
doc: API endpoints for sotopia #242
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
decacd9
api doc
XuhuiZhou b62a554
add PUT
XuhuiZhou 1942a14
add an temp example for websocket
XuhuiZhou a5e0cc2
websocket
XuhuiZhou 1cddc10
update readme
XuhuiZhou 4cd747f
Update README.md
ProKil 2342dc5
update websocket live simulation api doc
bugsz 4977993
[autofix.ci] apply automated fixes
autofix-ci[bot] 215cf8c
update websocket doc
bugsz ea2f92c
Merge branch 'feature/sotopia-ui-doc' of https://github.com/sotopia-lβ¦
bugsz ae7f7a8
add api server with websocket as well as a client
bugsz f6aeecf
fix mypy errors
bugsz ead08b4
support stopping the chat
bugsz ed6e437
add 404 to the status code
bugsz c345711
fix mypy issue
bugsz cdc19ec
remove files
XuhuiZhou 1cb3759
update requirement
XuhuiZhou 644249b
update return message from the server
bugsz 859eda2
add restful error code
bugsz 2cc44cc
Update README.md
bugsz 80063cc
remove paks
XuhuiZhou File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| # Sotopia UI | ||
XuhuiZhou marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| ## FastAPI Server | ||
|
|
||
| The API server is a FastAPI application that is used to connect the Sotopia UI to the Sotopia backend. | ||
| This could also help with other projects that need to connect to the Sotopia backend through HTTP requests. | ||
|
|
||
| Here are some initial design of the API server: | ||
|
|
||
| ### Getting Data from the API Server | ||
|
|
||
| #### GET /get/scenarios/{scenario_id} | ||
|
|
||
| Get scenarios by scenario_id. | ||
| parameters: | ||
| - scenario_id: str | ||
|
|
||
| returns: | ||
| - scenarios: EnvironmentProfile | ||
|
|
||
|
|
||
| #### GET /get/scenarios | ||
|
|
||
| Get all scenarios. | ||
|
|
||
| returns: | ||
| - scenarios: list[EnvironmentProfile] | ||
|
|
||
| #### GET /get/scenarios/{sceanrio_tag} | ||
XuhuiZhou marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| Get scenarios by scenario_tag. | ||
| parameters: | ||
| - scenario_tag: str | ||
| (This scenario tag could be a keyword; so people can search for scenarios by keywords) | ||
|
|
||
| returns: | ||
| - scenarios: list[EnvironmentProfile] | ||
|
|
||
| #### GET /get/agents | ||
|
|
||
| Get all agents. | ||
|
|
||
| returns: | ||
| - agents: list[AgentProfile] | ||
|
|
||
| #### GET /get/agents/{agent_id} | ||
|
|
||
| Get agent by agent_id. | ||
| parameters: | ||
| - agent_id: str | ||
|
|
||
| returns: | ||
| - agent: AgentProfile | ||
|
|
||
| #### GET /get/agents/{agent_gender} | ||
|
|
||
| Get agents by agent_gender. | ||
XuhuiZhou marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| parameters: | ||
| - agent_gender: Literal["male", "female"] | ||
|
|
||
| returns: | ||
| - agents: list[AgentProfile] | ||
|
|
||
| #### GET /get/agents/{agent_occupation} | ||
|
|
||
| Get agents by agent_occupation. | ||
| parameters: | ||
| - agent_occupation: str | ||
|
|
||
| returns: | ||
| - agents: list[AgentProfile] | ||
|
|
||
|
|
||
| #### GET /get/episodes | ||
|
|
||
| Get all episodes. | ||
|
|
||
| returns: | ||
| - episodes: list[Episode] | ||
|
|
||
| #### GET /get/episodes/{episode_tag} | ||
|
|
||
| Get episode by episode_tag. | ||
XuhuiZhou marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| parameters: | ||
| - episode_tag: str | ||
|
|
||
| returns: | ||
| - episode: list[Episode] | ||
|
|
||
| #### GET /get/episodes/{episode_id} | ||
|
|
||
| Get episode by episode_id. | ||
| parameters: | ||
| - episode_id: str | ||
|
|
||
| returns: | ||
| - episode: Episode | ||
|
|
||
|
|
||
| ### Sending Data to the API Server | ||
|
|
||
| w#### POST /post/agents/ | ||
|
|
||
| Send agent profile to the API server. | ||
| Request Body: | ||
| AgentProfile | ||
|
|
||
| returns: | ||
| - agent_id: str | ||
|
|
||
| #### POST /post/scenarios/ | ||
|
|
||
| Send scenario profile to the API server. | ||
| Request Body: | ||
| EnvironmentProfile | ||
|
|
||
| returns: | ||
| - scenario_id: str | ||
|
|
||
| ### Updating Data in the API Server | ||
|
|
||
| #### PUT /put/agents/{agent_id} | ||
|
|
||
| Update agent profile in the API server. | ||
| Request Body: | ||
| AgentProfile | ||
|
|
||
| returns: | ||
| - agent_id: str | ||
|
|
||
|
|
||
| #### PUT /put/scenarios/{scenario_id} | ||
|
|
||
| Update scenario profile in the API server. | ||
| Request Body: | ||
| EnvironmentProfile | ||
|
|
||
| returns: | ||
| - scenario_id: str | ||
|
|
||
| ### Initiating a new non-streaming simulation episode | ||
|
|
||
| #### POST /post/episodes/ | ||
|
|
||
| ```python | ||
| class SimulationEpisodeInitiation(BaseModel): | ||
| scenario_id: str | ||
| agent_ids: list[str] | ||
| episode_tag: str | ||
| models: list[str] | ||
| ``` | ||
|
|
||
| Send episode profile to the API server. | ||
| Request Body: | ||
| SimulationEpisodeInitiation | ||
|
|
||
| returns: | ||
| - episode_id: str (This is the id of the episode that will be used to get the episode data, saved in the redis database) | ||
|
|
||
| ### Initiating a new interactive streaming simulation episode (this operation will open a websocket connection) | ||
|
|
||
| We use the websocket connection to send the simulation step-by-step results to the UI. | ||
ProKil marked this conversation as resolved.
Show resolved
Hide resolved
|
||
XuhuiZhou marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| from fastapi import FastAPI, WebSocket, WebSocketDisconnect | ||
| from fastapi.responses import HTMLResponse | ||
|
|
||
| app = FastAPI() | ||
|
|
||
| html = """ | ||
| <!DOCTYPE html> | ||
| <html> | ||
| <head> | ||
| <title>Chat</title> | ||
| </head> | ||
| <body> | ||
| <h1>WebSocket Chat</h1> | ||
| <h2>Your ID: <span id="ws-id"></span></h2> | ||
| <form action="" onsubmit="sendMessage(event)"> | ||
| <input type="text" id="messageText" autocomplete="off"/> | ||
| <button>Send</button> | ||
| </form> | ||
| <ul id='messages'> | ||
| </ul> | ||
| <script> | ||
| var client_id = Date.now() | ||
| document.querySelector("#ws-id").textContent = client_id; | ||
| var ws = new WebSocket(`ws://localhost:8000/ws/${client_id}`); | ||
| ws.onmessage = function(event) { | ||
| var messages = document.getElementById('messages') | ||
| var message = document.createElement('li') | ||
| var content = document.createTextNode(event.data) | ||
| message.appendChild(content) | ||
| messages.appendChild(message) | ||
| }; | ||
| function sendMessage(event) { | ||
| var input = document.getElementById("messageText") | ||
| ws.send(input.value) | ||
| input.value = '' | ||
| event.preventDefault() | ||
| } | ||
| </script> | ||
| </body> | ||
| </html> | ||
| """ | ||
|
|
||
|
|
||
| class ConnectionManager: | ||
| def __init__(self) -> None: | ||
| self.active_connections: list[WebSocket] = [] | ||
|
|
||
| async def connect(self, websocket: WebSocket) -> None: | ||
| await websocket.accept() | ||
| self.active_connections.append(websocket) | ||
|
|
||
| def disconnect(self, websocket: WebSocket) -> None: | ||
| self.active_connections.remove(websocket) | ||
|
|
||
| async def send_personal_message(self, message: str, websocket: WebSocket) -> None: | ||
| await websocket.send_text(message) | ||
|
|
||
| async def broadcast(self, message: str) -> None: | ||
| for connection in self.active_connections: | ||
| await connection.send_text(message) | ||
|
|
||
|
|
||
| manager = ConnectionManager() | ||
|
|
||
|
|
||
| @app.get("/") | ||
| async def get() -> HTMLResponse: | ||
| return HTMLResponse(html) | ||
|
|
||
|
|
||
| @app.websocket("/ws/{client_id}") | ||
| async def websocket_endpoint(websocket: WebSocket, client_id: int) -> None: | ||
| await manager.connect(websocket) | ||
| try: | ||
| while True: | ||
| data = await websocket.receive_text() | ||
| await manager.send_personal_message(f"You wrote: {data}", websocket) | ||
| await manager.broadcast(f"Client #{client_id} says: {data}") | ||
| except WebSocketDisconnect: | ||
| manager.disconnect(websocket) | ||
| await manager.broadcast(f"Client #{client_id} left the chat") |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is websockets needed? Can we use aiohttp?