From a48078d46eb23891259c37b4e8d21b8115180b86 Mon Sep 17 00:00:00 2001 From: Geovanny Risco Date: Fri, 14 Jun 2024 09:04:24 +0200 Subject: [PATCH] reorder folder structure --- src/.funcignore => .funcignore | 0 .github/workflows/main_laia-backend.yml | 6 +- .vscode/settings.json | 2 +- .vscode/tasks.json | 2 +- function_app.py | 29 +++++++++ src/host.json => host.json | 0 laia/__init__.py | 0 {src => laia}/llama3.py | 0 {src => laia}/llamaindex.py | 0 src/function_app.py | 79 ------------------------- 10 files changed, 33 insertions(+), 85 deletions(-) rename src/.funcignore => .funcignore (100%) create mode 100644 function_app.py rename src/host.json => host.json (100%) create mode 100644 laia/__init__.py rename {src => laia}/llama3.py (100%) rename {src => laia}/llamaindex.py (100%) delete mode 100644 src/function_app.py diff --git a/src/.funcignore b/.funcignore similarity index 100% rename from src/.funcignore rename to .funcignore diff --git a/.github/workflows/main_laia-backend.yml b/.github/workflows/main_laia-backend.yml index 25328a2..320b972 100644 --- a/.github/workflows/main_laia-backend.yml +++ b/.github/workflows/main_laia-backend.yml @@ -11,7 +11,7 @@ on: workflow_dispatch: env: - AZURE_FUNCTIONAPP_PACKAGE_PATH: 'src' # set this to the path to your web app project, defaults to the repository root + AZURE_FUNCTIONAPP_PACKAGE_PATH: '.' # set this to the path to your web app project, defaults to the repository root PYTHON_VERSION: '3.11' # set this to the python version to use (supports 3.6, 3.7, 3.8) jobs: @@ -37,9 +37,7 @@ jobs: # Optional: Add step to run tests here - name: Zip artifact for deployment - run: | - cp requirements.txt src/ - zip release.zip ./data/* ./index/* ./prompts/* ./src/* -r + run: zip release.zip ./* -r -x .git/\* -x .github/\* -x venv/\* - name: Upload artifact for deployment job uses: actions/upload-artifact@v3 diff --git a/.vscode/settings.json b/.vscode/settings.json index 865b986..c10c746 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,5 +1,5 @@ { - "azureFunctions.deploySubpath": "src", + "azureFunctions.deploySubpath": "", "azureFunctions.scmDoBuildDuringDeployment": true, "azureFunctions.projectLanguage": "Python", "azureFunctions.projectRuntime": "~4", diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 9db7143..2747b2c 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -8,7 +8,7 @@ "problemMatcher": "$func-python-watch", "isBackground": true, "options": { - "cwd": "${workspaceFolder}/src" + "cwd": "${workspaceFolder}" } } ] diff --git a/function_app.py b/function_app.py new file mode 100644 index 0000000..bf5ee1e --- /dev/null +++ b/function_app.py @@ -0,0 +1,29 @@ +import logging + +import azure.functions as func +from llama_index.core.llms import ChatMessage + +from laia.llamaindex import generate_response + +logger = logging.getLogger(__name__) + + +app = func.FunctionApp() + + +@app.function_name(name="generate") +@app.route(route="generate", auth_level=func.AuthLevel.ANONYMOUS) +def test_function(req: func.HttpRequest) -> func.HttpResponse: + logger.info(f"Python HTTP trigger function processed a request: {req}") + req_body = req.get_json() + logger.info(f"Request body: {req_body}") + try: + messages = [ + ChatMessage(role=item["role"], content=item["content"]) + for item in req_body["messages"] + ] + response = generate_response(messages) + return func.HttpResponse(str(response), status_code=200) + except Exception as e: + logger.error(e) + return func.HttpResponse("Internal Server Error", status_code=500) diff --git a/src/host.json b/host.json similarity index 100% rename from src/host.json rename to host.json diff --git a/laia/__init__.py b/laia/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/llama3.py b/laia/llama3.py similarity index 100% rename from src/llama3.py rename to laia/llama3.py diff --git a/src/llamaindex.py b/laia/llamaindex.py similarity index 100% rename from src/llamaindex.py rename to laia/llamaindex.py diff --git a/src/function_app.py b/src/function_app.py deleted file mode 100644 index 01ffea8..0000000 --- a/src/function_app.py +++ /dev/null @@ -1,79 +0,0 @@ -import logging -import pathlib -from os import getenv - -import azure.functions as func -from dotenv import load_dotenv -from llama_index.core import ( - KeywordTableIndex, - SimpleDirectoryReader, - StorageContext, - load_index_from_storage, -) -from llama_index.core.chat_engine.types import ChatMode -from llama_index.core.llms import ChatMessage, MessageRole -from llama_index.core.memory import ChatMemoryBuffer -from llama_index.llms.databricks import Databricks - -logger = logging.getLogger(__name__) - -load_dotenv() -app = func.FunctionApp() - -# Define static data paths -INDEX_PERSIST_DIR = "../index" -DOCS_DATA_DIR = "../data" -PROMPTS_DIR = "../prompts" - -# define LLM -llm = Databricks( - model="databricks-meta-llama-3-70b-instruct", - api_key=getenv("DATABRICKS_TOKEN"), - api_base="https://adb-2978037251816793.13.azuredatabricks.net/serving-endpoints", - max_tokens=256, -) -# build or load index -if pathlib.Path(INDEX_PERSIST_DIR).exists(): - storage_context = StorageContext.from_defaults(persist_dir=INDEX_PERSIST_DIR) - index = load_index_from_storage(storage_context, llm=llm) -else: - documents = SimpleDirectoryReader(DOCS_DATA_DIR).load_data() - index = KeywordTableIndex.from_documents(documents, llm=llm) - # Save the index for later use - index.storage_context.persist(INDEX_PERSIST_DIR) - -# Read system prompt -with open(pathlib.Path(PROMPTS_DIR) / "system.txt") as f: - system_prompt = f.read() - - -# Create chat engine -def generate_response(messages: list[ChatMessage]): - chat_engine = index.as_chat_engine( - llm=llm, - memory=ChatMemoryBuffer.from_defaults(token_limit=10500), - chat_mode=ChatMode.CONTEXT, - system_prompt=system_prompt, - ) - current_message = messages[-1] - chat_history = messages[:-1] - response = chat_engine.chat(str(current_message.content), chat_history=chat_history) - return response - - -@app.function_name(name="generate") -@app.route(route="generate", auth_level=func.AuthLevel.ANONYMOUS, methods=["POST"]) -def test_function(req: func.HttpRequest) -> func.HttpResponse: - logger.info(f"Python HTTP trigger function processed a request: {req}") - req_body = req.get_json() - logger.info(f"Request body: {req_body}") - try: - messages = [ - ChatMessage(role=item["role"], content=item["content"]) - for item in req_body["messages"] - ] - response = generate_response(messages) - return func.HttpResponse(str(response), status_code=200) - except Exception as e: - logger.error(e) - return func.HttpResponse("Internal Server Error", status_code=500)