-
Notifications
You must be signed in to change notification settings - Fork 13
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
[Core] Add simple serving feature with Uvicorn #10
base: main
Are you sure you want to change the base?
Changes from 11 commits
f0cb664
d886cc4
a047ef2
be660d9
632374f
bb6dc68
ec492b0
8d41d94
8cbbddb
cb62eb2
0fca5dd
653ada7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
name: Linter | ||
|
||
on: | ||
pull_request: | ||
branches: | ||
- main | ||
push: | ||
branches: | ||
- main | ||
# To test workflow without event trigger | ||
workflow_dispatch: | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
strategy: | ||
matrix: | ||
python-version: ["3.9", "3.10", "3.11", "3.12"] | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- name: Set up Python ${{ matrix.python-version }} | ||
uses: actions/setup-python@v3 | ||
with: | ||
python-version: ${{ matrix.python-version }} | ||
- name: Install dependencies | ||
run: | | ||
python -m pip install --upgrade pip | ||
pip install ruff | ||
- name: Analysing the code with pylint | ||
run: | | ||
ruff check $(git ls-files '*.py') |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,9 @@ | ||
""" Gigax package. """ | ||
|
||
from gigax import scene | ||
from gigax import parse | ||
from gigax import step | ||
from gigax import prompt | ||
|
||
|
||
__all__ = ["scene", "parse", "step", "prompt"] |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,91 @@ | ||||||||
import logging | ||||||||
import os | ||||||||
import uvicorn | ||||||||
import sys | ||||||||
|
||||||||
from dotenv import load_dotenv | ||||||||
from fastapi.exceptions import RequestValidationError | ||||||||
from fastapi.responses import JSONResponse | ||||||||
from fastapi import FastAPI, Request, status | ||||||||
from fastapi.middleware.cors import CORSMiddleware | ||||||||
from fastapi.logger import logger as fastapi_logger | ||||||||
from pydantic import BaseModel | ||||||||
|
||||||||
from gigax.parse import CharacterAction, ProtagonistCharacter | ||||||||
from gigax.scene import ( | ||||||||
Character, | ||||||||
Item, | ||||||||
Location, | ||||||||
) | ||||||||
from gigax.step import NPCStepper | ||||||||
|
||||||||
load_dotenv() | ||||||||
|
||||||||
fastapi_logger.handlers = logging.getLogger("gunicorn.error").handlers | ||||||||
logging.basicConfig(level=logging.INFO, handlers=[logging.StreamHandler(sys.stdout)]) | ||||||||
|
||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
# FastAPI | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
app = FastAPI(root_path="/api") | ||||||||
origins = [ | ||||||||
"http://localhost", | ||||||||
] | ||||||||
app.add_middleware( | ||||||||
CORSMiddleware, | ||||||||
allow_origins=origins, | ||||||||
allow_credentials=True, | ||||||||
allow_methods=["*"], | ||||||||
allow_headers=["*"], | ||||||||
) | ||||||||
|
||||||||
|
||||||||
logger = logging.getLogger("uvicorn") | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
logging.basicConfig(level=logging.INFO) | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you sure that we need this line that is similar to the line 25? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
|
||||||||
|
||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
@app.exception_handler(RequestValidationError) | ||||||||
async def validation_exception_handler(request: Request, exc: RequestValidationError): | ||||||||
exc_str = f"{exc}".replace("\n", " ").replace(" ", " ") | ||||||||
logging.error(f"{request}: {exc_str}") | ||||||||
content = {"status_code": 10422, "message": exc_str, "data": None} | ||||||||
return JSONResponse( | ||||||||
content=content, status_code=status.HTTP_422_UNPROCESSABLE_ENTITY | ||||||||
) | ||||||||
|
||||||||
|
||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
class CharacterActionRequest(BaseModel): | ||||||||
context: str | ||||||||
locations: list[Location] | ||||||||
NPCs: list[Character] | ||||||||
protagonist: ProtagonistCharacter | ||||||||
items: list[Item] | ||||||||
events: list[CharacterAction] | ||||||||
|
||||||||
|
||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
@app.post( | ||||||||
"/step", | ||||||||
response_description="Step the character", | ||||||||
response_model=CharacterAction, | ||||||||
) | ||||||||
async def step( | ||||||||
request: CharacterActionRequest, | ||||||||
): | ||||||||
# Format the prompt | ||||||||
stepper = NPCStepper(model="llama_3_regex", api_key=os.getenv("API_KEY")) | ||||||||
|
||||||||
return await stepper.get_action( | ||||||||
context=request.context, | ||||||||
locations=request.locations, | ||||||||
NPCs=request.NPCs, | ||||||||
protagonist=request.protagonist, | ||||||||
items=request.items, | ||||||||
events=request.events, | ||||||||
) | ||||||||
|
||||||||
|
||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
@app.get("/health-check") | ||||||||
async def health_check(): | ||||||||
return {"status": "ok"} | ||||||||
|
||||||||
|
||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
if __name__ == "__main__": | ||||||||
uvicorn.run("__main__:app", host="0.0.0.0", port=5678, reload=True, workers=5) |
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.