diff --git a/autollm/app/api/__init__.py b/autollm/app/api/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/autollm/app/api/ask_question.py b/autollm/app/api/ask_question.py deleted file mode 100644 index ea9bdf6d..00000000 --- a/autollm/app/api/ask_question.py +++ /dev/null @@ -1,43 +0,0 @@ -import logging - -from fastapi import APIRouter -from utils import llm_utils -from utils.constants import DEFAULT_INDEX_NAME, DEFAULT_VECTORE_STORE_TYPE -from vectorstores import auto - -router = APIRouter() -logger = logging.getLogger(__name__) - -# TODO: create qa_pipeline 13-25 lines -token_counter, callback_manager = llm_utils.initialize_token_counting() - -# Initialize the service context -llm_utils.initialize_service_context(callback_manager=callback_manager) - -# Create the text QA template for the query engine -text_qa_template = llm_utils.create_text_qa_template() - -# Initialize the query engine -vector_store = auto.AutoVectorStore().create(DEFAULT_VECTORE_STORE_TYPE, index_name=DEFAULT_INDEX_NAME) -vector_store.connect_vectorstore() -query_engine = vector_store.vectorindex.as_query_engine(text_qa_template=text_qa_template) - - -@router.get("/ask_question") -async def ask_question(user_query: str): - """ - Perform Text-Based Queries on Document Store. - - This endpoint receives a natural language query from the user and returns the most relevant answer from the document store. - - Args: - user_query (str): The natural language query from the user. - - Returns: - response (str): The response containing the answer to the user's query. - """ - # Query the engine - response = query_engine.query(user_query) - llm_utils.log_total_cost(token_counter=token_counter) - token_counter.reset_counts() - return response.response # extracts the response text diff --git a/autollm/app/api/health_check.py b/autollm/app/api/health_check.py deleted file mode 100644 index fbc959aa..00000000 --- a/autollm/app/api/health_check.py +++ /dev/null @@ -1,16 +0,0 @@ -import logging - -from fastapi import APIRouter - -router = APIRouter() -logger = logging.getLogger(__name__) - - -@router.get("/health", tags=["health"]) -async def health_check(): - """Checks the health of the API.""" - try: - return {"status": "healthy"} - except Exception as e: - logger.error(f"Health check failed: {e}") - return {"status": "unhealthy"} diff --git a/autollm/app/main.py b/autollm/app/main.py index 42645cd3..c3a8cb8f 100644 --- a/autollm/app/main.py +++ b/autollm/app/main.py @@ -1,21 +1,42 @@ import logging -from fastapi import FastAPI +from fastapi import FastAPI, HTTPException, Query -from .api import ask_question, health_check -from .docs import description, openapi_url, tags_metadata, terms_of_service, title, version +from autollm.app.docs import description, openapi_url, tags_metadata, terms_of_service, title, version +from autollm.app.utils import load_config_and_initialize_engines logging.basicConfig(level=logging.INFO) -app = FastAPI( - title=title, - description=description, - version=version, - openapi_url=openapi_url, - terms_of_service=terms_of_service, - openapi_tags=tags_metadata, -) - -# Include the API routes -app.include_router(ask_question.router, tags=["ask"]) -app.include_router(health_check.router, tags=["health"]) + +# Function to create the FastAPI web app +def create_web_app(config_file_path: str, env_file_path: str = None): + app = FastAPI( + title=title, + description=description, + version=version, + openapi_url=openapi_url, + terms_of_service=terms_of_service, + openapi_tags=tags_metadata, + ) + + query_engines = load_config_and_initialize_engines(config_file_path, env_file_path) + + @app.post("/query") + async def query( + task: str = Query(..., description="Task to execute"), + user_query: str = Query(..., description="User's query")): + if task not in query_engines: + raise HTTPException(status_code=400, detail="Invalid task name") + + # Use the appropriate query engine for the task + query_engine = query_engines[task] + response = query_engine.query(user_query) + + return response + + return app + + +# For demonstration, let's assume we have a config.yaml with task configurations and an optional .env file +# This function call would typically be in your main application file +# app = create_web_app("path/to/config.yaml", "path/to/.env") diff --git a/autollm/app/utils.py b/autollm/app/utils.py new file mode 100644 index 00000000..3b6bd121 --- /dev/null +++ b/autollm/app/utils.py @@ -0,0 +1,25 @@ +from typing import Dict + +import yaml +from dotenv import load_dotenv + +from autollm.auto.query_engine import AutoQueryEngine + + +# Function to load the configuration for tasks and initialize query engines +def load_config_and_initialize_engines(config_file_path: str, + env_file_path: str = None) -> Dict[str, AutoQueryEngine]: + # Optionally load environment variables from a .env file + if env_file_path: + load_dotenv(dotenv_path=env_file_path) + + # Load the YAML configuration file + with open(config_file_path) as f: + config = yaml.safe_load(f) + + # Initialize query engines based on the config + query_engines = {} + for task, params in config.items(): + query_engines[task] = AutoQueryEngine.from_parameters(**params) + + return query_engines