From db34efaa927ad652ebdc59a8f0b3efc088261a2e Mon Sep 17 00:00:00 2001
From: Marcus Schiesser <mail@marcusschiesser.de>
Date: Thu, 21 Dec 2023 14:31:53 +0700
Subject: [PATCH] fix[cl-fastapi]: use json for request content-type (and
 update llama-index)

---
 .../streaming/fastapi/app/api/routers/chat.py |  8 +++----
 .../types/streaming/fastapi/app/utils/json.py | 22 -------------------
 .../types/streaming/fastapi/pyproject.toml    |  2 +-
 3 files changed, 4 insertions(+), 28 deletions(-)
 delete mode 100644 templates/types/streaming/fastapi/app/utils/json.py

diff --git a/templates/types/streaming/fastapi/app/api/routers/chat.py b/templates/types/streaming/fastapi/app/api/routers/chat.py
index 36b618e2..c55b3bbe 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 d9a847f5..00000000
--- 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 f5b75b3c..3a99fabd 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"
 
-- 
GitLab