diff --git a/packages/create-llama/templates/types/simple/fastapi/app/utils/index.py b/packages/create-llama/templates/types/simple/fastapi/app/utils/index.py index 0ee09b19d0526dca77420f7519d25c71f2f04670..530935b7ec84c93bafee5e4d37265f4e4411fd65 100644 --- a/packages/create-llama/templates/types/simple/fastapi/app/utils/index.py +++ b/packages/create-llama/templates/types/simple/fastapi/app/utils/index.py @@ -15,9 +15,10 @@ 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("gpt-3.5-turbo") + llm=OpenAI(model="gpt-3.5-turbo") ) + def get_index(): logger = logging.getLogger("uvicorn") # check if storage already exists diff --git a/packages/create-llama/templates/types/simple/fastapi_auth/README-template.md b/packages/create-llama/templates/types/simple/fastapi_auth/README-template.md new file mode 100644 index 0000000000000000000000000000000000000000..f0b92bdfce648a374bd2723fee4ceec69605db69 --- /dev/null +++ b/packages/create-llama/templates/types/simple/fastapi_auth/README-template.md @@ -0,0 +1,42 @@ +This is a [LlamaIndex](https://www.llamaindex.ai/) project using [FastAPI](https://fastapi.tiangolo.com/) bootstrapped with [`create-llama`](https://github.com/run-llama/LlamaIndexTS/tree/main/packages/create-llama). + +## Getting Started + +First, setup the environment: + +``` +poetry install +poetry shell +``` + +Second, run the development server: + +``` +python main.py +``` + +Then call the API endpoint `/api/chat` to see the result: + +``` +curl --location 'localhost:8000/api/chat' \ +--header 'Content-Type: application/json' \ +--data '{ "messages": [{ "role": "user", "content": "Hello" }] }' +``` + +You can start editing the API by modifying `app/api/routers/chat.py`. The endpoint auto-updates as you save the file. + +Open [http://localhost:8000/docs](http://localhost:8000/docs) with your browser to see the Swagger UI of the API. + +The API allows CORS for all origins to simplify development. You can change this behavior by setting the `ENVIRONMENT` environment variable to `prod`: + +``` +ENVIRONMENT=prod uvicorn main:app +``` + +## Learn More + +To learn more about LlamaIndex, take a look at the following resources: + +- [LlamaIndex Documentation](https://docs.llamaindex.ai) - learn about LlamaIndex. + +You can check out [the LlamaIndex GitHub repository](https://github.com/run-llama/llama_index) - your feedback and contributions are welcome! diff --git a/packages/create-llama/templates/types/simple/fastapi_auth/app/__init__.py b/packages/create-llama/templates/types/simple/fastapi_auth/app/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/packages/create-llama/templates/types/simple/fastapi_auth/app/api/__init__.py b/packages/create-llama/templates/types/simple/fastapi_auth/app/api/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/packages/create-llama/templates/types/simple/fastapi_auth/app/api/routers/__init__.py b/packages/create-llama/templates/types/simple/fastapi_auth/app/api/routers/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/packages/create-llama/templates/types/simple/fastapi_auth/app/api/routers/chat.py b/packages/create-llama/templates/types/simple/fastapi_auth/app/api/routers/chat.py new file mode 100644 index 0000000000000000000000000000000000000000..81f602edbeae66c5850b30a6183c009ab4b1e014 --- /dev/null +++ b/packages/create-llama/templates/types/simple/fastapi_auth/app/api/routers/chat.py @@ -0,0 +1,56 @@ +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 + +chat_router = r = APIRouter() + + +class _Message(BaseModel): + role: MessageRole + content: str + + +class _ChatData(BaseModel): + messages: List[_Message] + + +class _Result(BaseModel): + result: _Message + + +@r.post("") +async def chat( + data: _ChatData, + index: VectorStoreIndex = Depends(get_index), +) -> _Result: + # check preconditions and get last message + if len(data.messages) == 0: + raise HTTPException( + status_code=status.HTTP_400_BAD_REQUEST, + detail="No messages provided", + ) + lastMessage = data.messages.pop() + if lastMessage.role != MessageRole.USER: + raise HTTPException( + status_code=status.HTTP_400_BAD_REQUEST, + detail="Last message must be from user", + ) + # convert messages coming from the request to type ChatMessage + messages = [ + ChatMessage( + role=m.role, + content=m.content, + ) + for m in data.messages + ] + + # query chat engine + chat_engine = index.as_chat_engine() + response = chat_engine.chat(lastMessage.content, messages) + return _Result( + result=_Message(role=MessageRole.ASSISTANT, content=response.response) + ) diff --git a/packages/create-llama/templates/types/simple/fastapi_auth/app/utils/__init__.py b/packages/create-llama/templates/types/simple/fastapi_auth/app/utils/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/packages/create-llama/templates/types/simple/fastapi_auth/app/utils/index.py b/packages/create-llama/templates/types/simple/fastapi_auth/app/utils/index.py new file mode 100644 index 0000000000000000000000000000000000000000..43472724877e48853c6bd5df0c19ca9a0aa9371d --- /dev/null +++ b/packages/create-llama/templates/types/simple/fastapi_auth/app/utils/index.py @@ -0,0 +1,38 @@ +import logging +import os + +from llama_index import ( + SimpleDirectoryReader, + ServiceContext, + StorageContext, + VectorStoreIndex, + load_index_from_storage, +) + + +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 get_index(): + logger = logging.getLogger("uvicorn") + # check if storage already exists + if not os.path.exists(STORAGE_DIR): + logger.info("Creating new index") + # load the documents and create the index + documents = SimpleDirectoryReader(DATA_DIR).load_data() + index = VectorStoreIndex.from_documents(documents, service_context=service_context) + # store it for later + index.storage_context.persist(STORAGE_DIR) + logger.info(f"Finished creating new index. Stored in {STORAGE_DIR}") + else: + # load the existing index + logger.info(f"Loading index from {STORAGE_DIR}...") + storage_context = StorageContext.from_defaults(persist_dir=STORAGE_DIR) + index = load_index_from_storage(storage_context, service_context=service_context) + logger.info(f"Finished loading index from {STORAGE_DIR}") + return index diff --git a/packages/create-llama/templates/types/simple/fastapi_auth/data/101.pdf b/packages/create-llama/templates/types/simple/fastapi_auth/data/101.pdf new file mode 100644 index 0000000000000000000000000000000000000000..ae5acffd5398b7c59e2df9e6dead2d99128b719c Binary files /dev/null and b/packages/create-llama/templates/types/simple/fastapi_auth/data/101.pdf differ diff --git a/packages/create-llama/templates/types/simple/fastapi_auth/gitignore b/packages/create-llama/templates/types/simple/fastapi_auth/gitignore new file mode 100644 index 0000000000000000000000000000000000000000..069fcb4020566da83dbc398c06dce5bbe92aface --- /dev/null +++ b/packages/create-llama/templates/types/simple/fastapi_auth/gitignore @@ -0,0 +1,2 @@ +__pycache__ +storage diff --git a/packages/create-llama/templates/types/simple/fastapi_auth/main.py b/packages/create-llama/templates/types/simple/fastapi_auth/main.py new file mode 100644 index 0000000000000000000000000000000000000000..9dc1a0afb6634f319538e8545bdd647693dffbfb --- /dev/null +++ b/packages/create-llama/templates/types/simple/fastapi_auth/main.py @@ -0,0 +1,31 @@ +import logging +import os +import uvicorn +from app.api.routers.chat import chat_router +from fastapi import FastAPI +from fastapi.middleware.cors import CORSMiddleware +from dotenv import load_dotenv + +load_dotenv() + +app = FastAPI() + +environment = os.getenv("ENVIRONMENT", "dev") # Default to 'development' if not set + + +if environment == "dev": + logger = logging.getLogger("uvicorn") + logger.warning("Running in development mode - allowing CORS for all origins") + app.add_middleware( + CORSMiddleware, + allow_origins=["*"], + allow_credentials=True, + allow_methods=["*"], + allow_headers=["*"], + ) + +app.include_router(chat_router, prefix="/api/chat") + + +if __name__ == "__main__": + uvicorn.run(app="main:app", host="0.0.0.0", reload=True) diff --git a/packages/create-llama/templates/types/simple/fastapi_auth/pyproject.toml b/packages/create-llama/templates/types/simple/fastapi_auth/pyproject.toml new file mode 100644 index 0000000000000000000000000000000000000000..59d182bbb47a8d0ee06de3550f2c7fbf954a3901 --- /dev/null +++ b/packages/create-llama/templates/types/simple/fastapi_auth/pyproject.toml @@ -0,0 +1,19 @@ +[tool.poetry] +name = "llamaindex-fastapi" +version = "0.1.0" +description = "" +authors = ["Marcus Schiesser <mail@marcusschiesser.de>"] +readme = "README.md" + +[tool.poetry.dependencies] +python = "^3.11,<3.12" +fastapi = "^0.104.1" +uvicorn = { extras = ["standard"], version = "^0.23.2" } +llama-index = "^0.8.56" +pypdf = "^3.17.0" +python-dotenv = "^1.0.0" + + +[build-system] +requires = ["poetry-core"] +build-backend = "poetry.core.masonry.api" diff --git a/packages/create-llama/templates/types/simple/fastapi_auth/tests/__init__.py b/packages/create-llama/templates/types/simple/fastapi_auth/tests/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/packages/create-llama/templates/types/streaming/fastapi/app/utils/index.py b/packages/create-llama/templates/types/streaming/fastapi/app/utils/index.py index 0ee09b19d0526dca77420f7519d25c71f2f04670..cb16cdba37897fc203bdd0358c5f088628375a3f 100644 --- a/packages/create-llama/templates/types/streaming/fastapi/app/utils/index.py +++ b/packages/create-llama/templates/types/streaming/fastapi/app/utils/index.py @@ -15,7 +15,7 @@ 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("gpt-3.5-turbo") + llm=OpenAI(model="gpt-3.5-turbo") ) def get_index():