From a9608c92033be3778696a80572003d61673f384a Mon Sep 17 00:00:00 2001
From: thucpn <thucsh2@gmail.com>
Date: Fri, 22 Dec 2023 17:01:00 +0700
Subject: [PATCH] refactor: context structure for simple python template

---
 .../types/simple/fastapi/app/api/routers/chat.py    |  2 +-
 .../fastapi/app/{utils/index.py => context.py}      | 13 +++++++------
 .../types/simple/fastapi/app/utils/__init__.py      |  0
 templates/types/simple/fastapi/pyproject.toml       |  2 +-
 4 files changed, 9 insertions(+), 8 deletions(-)
 rename templates/types/simple/fastapi/app/{utils/index.py => context.py} (86%)
 delete mode 100644 templates/types/simple/fastapi/app/utils/__init__.py

diff --git a/templates/types/simple/fastapi/app/api/routers/chat.py b/templates/types/simple/fastapi/app/api/routers/chat.py
index 81f602ed..e728db0d 100644
--- a/templates/types/simple/fastapi/app/api/routers/chat.py
+++ b/templates/types/simple/fastapi/app/api/routers/chat.py
@@ -1,10 +1,10 @@
 from typing import List
 
-from app.utils.index import get_index
 from fastapi import APIRouter, Depends, HTTPException, status
 from llama_index import VectorStoreIndex
 from llama_index.llms.base import MessageRole, ChatMessage
 from pydantic import BaseModel
+from app.context import get_index
 
 chat_router = r = APIRouter()
 
diff --git a/templates/types/simple/fastapi/app/utils/index.py b/templates/types/simple/fastapi/app/context.py
similarity index 86%
rename from templates/types/simple/fastapi/app/utils/index.py
rename to templates/types/simple/fastapi/app/context.py
index 530935b7..48ca79a9 100644
--- a/templates/types/simple/fastapi/app/utils/index.py
+++ b/templates/types/simple/fastapi/app/context.py
@@ -1,5 +1,5 @@
-import logging
 import os
+import logging
 
 from llama_index import (
     SimpleDirectoryReader,
@@ -10,16 +10,17 @@ from llama_index import (
 )
 from llama_index.llms import OpenAI
 
-
 STORAGE_DIR = "./storage"  # directory to cache the generated index
 DATA_DIR = "./data"  # directory containing the documents to index
 
-service_context = ServiceContext.from_defaults(
-    llm=OpenAI(model="gpt-3.5-turbo")
-)
-
+def create_base_context():
+    model = os.getenv("MODEL", "gpt-3.5-turbo")
+    return ServiceContext.from_defaults(
+        llm=OpenAI(model=model),
+    )
 
 def get_index():
+    service_context = create_base_context()
     logger = logging.getLogger("uvicorn")
     # check if storage already exists
     if not os.path.exists(STORAGE_DIR):
diff --git a/templates/types/simple/fastapi/app/utils/__init__.py b/templates/types/simple/fastapi/app/utils/__init__.py
deleted file mode 100644
index e69de29b..00000000
diff --git a/templates/types/simple/fastapi/pyproject.toml b/templates/types/simple/fastapi/pyproject.toml
index 59d182bb..10d1d609 100644
--- a/templates/types/simple/fastapi/pyproject.toml
+++ b/templates/types/simple/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