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

refactor: context structure for simple python template

parent 81cf59de
No related branches found
No related tags found
No related merge requests found
from typing import List from typing import List
from app.utils.index import get_index
from fastapi import APIRouter, Depends, HTTPException, status from fastapi import APIRouter, Depends, HTTPException, 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 MessageRole, ChatMessage
from pydantic import BaseModel from pydantic import BaseModel
from app.context import get_index
chat_router = r = APIRouter() chat_router = r = APIRouter()
......
import logging
import os import os
import logging
from llama_index import ( from llama_index import (
SimpleDirectoryReader, SimpleDirectoryReader,
...@@ -10,16 +10,17 @@ from llama_index import ( ...@@ -10,16 +10,17 @@ from llama_index import (
) )
from llama_index.llms import OpenAI from llama_index.llms import OpenAI
STORAGE_DIR = "./storage" # directory to cache the generated index STORAGE_DIR = "./storage" # directory to cache the generated index
DATA_DIR = "./data" # directory containing the documents to index DATA_DIR = "./data" # directory containing the documents to index
service_context = ServiceContext.from_defaults( def create_base_context():
llm=OpenAI(model="gpt-3.5-turbo") model = os.getenv("MODEL", "gpt-3.5-turbo")
) return ServiceContext.from_defaults(
llm=OpenAI(model=model),
)
def get_index(): def get_index():
service_context = create_base_context()
logger = logging.getLogger("uvicorn") logger = logging.getLogger("uvicorn")
# check if storage already exists # check if storage already exists
if not os.path.exists(STORAGE_DIR): if not os.path.exists(STORAGE_DIR):
......
...@@ -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