Skip to content
Snippets Groups Projects
Commit db34efaa authored by Marcus Schiesser's avatar Marcus Schiesser
Browse files

fix[cl-fastapi]: use json for request content-type (and update llama-index)

parent 93745f77
No related branches found
No related tags found
No related merge requests found
...@@ -2,11 +2,11 @@ from typing import List ...@@ -2,11 +2,11 @@ from typing import List
from fastapi.responses import StreamingResponse from fastapi.responses import StreamingResponse
from app.utils.json import json_to_model
from app.utils.index import get_index from app.utils.index import get_index
from fastapi import APIRouter, Depends, HTTPException, Request, status from fastapi import APIRouter, Depends, HTTPException, Request, status
from llama_index import VectorStoreIndex 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 from pydantic import BaseModel
chat_router = r = APIRouter() chat_router = r = APIRouter()
...@@ -24,9 +24,7 @@ class _ChatData(BaseModel): ...@@ -24,9 +24,7 @@ class _ChatData(BaseModel):
@r.post("") @r.post("")
async def chat( async def chat(
request: Request, request: Request,
# Note: To support clients sending a JSON object using content-type "text/plain", data: _ChatData,
# we need to use Depends(json_to_model(_ChatData)) here
data: _ChatData = Depends(json_to_model(_ChatData)),
index: VectorStoreIndex = Depends(get_index), index: VectorStoreIndex = Depends(get_index),
): ):
# check preconditions and get last message # check preconditions and get last message
......
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
...@@ -9,7 +9,7 @@ readme = "README.md" ...@@ -9,7 +9,7 @@ readme = "README.md"
python = "^3.11,<3.12" python = "^3.11,<3.12"
fastapi = "^0.104.1" fastapi = "^0.104.1"
uvicorn = { extras = ["standard"], version = "^0.23.2" } uvicorn = { extras = ["standard"], version = "^0.23.2" }
llama-index = "^0.8.56" llama-index = "^0.9.19"
pypdf = "^3.17.0" pypdf = "^3.17.0"
python-dotenv = "^1.0.0" python-dotenv = "^1.0.0"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment