diff --git a/templates/types/streaming/fastapi/app/api/routers/chat.py b/templates/types/streaming/fastapi/app/api/routers/chat.py index 36b618e232a33fc882b3d4b42b885c29c64edd4e..c55b3bbed5cb4ddc10a65e78d20796fb2c936362 100644 --- a/templates/types/streaming/fastapi/app/api/routers/chat.py +++ b/templates/types/streaming/fastapi/app/api/routers/chat.py @@ -2,11 +2,11 @@ from typing import List from fastapi.responses import StreamingResponse -from app.utils.json import json_to_model from app.utils.index import get_index from fastapi import APIRouter, Depends, HTTPException, Request, status from llama_index import VectorStoreIndex -from llama_index.llms.base import MessageRole, ChatMessage +from llama_index.llms.base import ChatMessage +from llama_index.llms.types import MessageRole from pydantic import BaseModel chat_router = r = APIRouter() @@ -24,9 +24,7 @@ class _ChatData(BaseModel): @r.post("") async def chat( request: Request, - # Note: To support clients sending a JSON object using content-type "text/plain", - # we need to use Depends(json_to_model(_ChatData)) here - data: _ChatData = Depends(json_to_model(_ChatData)), + data: _ChatData, index: VectorStoreIndex = Depends(get_index), ): # check preconditions and get last message diff --git a/templates/types/streaming/fastapi/app/utils/json.py b/templates/types/streaming/fastapi/app/utils/json.py deleted file mode 100644 index d9a847f53e107f665389f11ec005795e0fb8c5b3..0000000000000000000000000000000000000000 --- a/templates/types/streaming/fastapi/app/utils/json.py +++ /dev/null @@ -1,22 +0,0 @@ -import json -from typing import TypeVar -from fastapi import HTTPException, Request - -from pydantic import BaseModel, ValidationError - - -T = TypeVar("T", bound=BaseModel) - - -def json_to_model(cls: T): - async def get_json(request: Request) -> T: - body = await request.body() - try: - data_dict = json.loads(body.decode("utf-8")) - return cls(**data_dict) - except (json.JSONDecodeError, ValidationError) as e: - raise HTTPException( - status_code=400, detail=f"Could not decode JSON: {str(e)}" - ) - - return get_json diff --git a/templates/types/streaming/fastapi/pyproject.toml b/templates/types/streaming/fastapi/pyproject.toml index f5b75b3cfdbe88001d4588faf27e07cec36212a9..3a99fabd774091ee11c5c7e2c1a20499a13a7260 100644 --- a/templates/types/streaming/fastapi/pyproject.toml +++ b/templates/types/streaming/fastapi/pyproject.toml @@ -9,7 +9,7 @@ readme = "README.md" python = "^3.11,<3.12" fastapi = "^0.104.1" uvicorn = { extras = ["standard"], version = "^0.23.2" } -llama-index = "^0.8.56" +llama-index = "^0.9.19" pypdf = "^3.17.0" python-dotenv = "^1.0.0"