Skip to content
This repository has been archived by the owner on Sep 12, 2024. It is now read-only.

Commit

Permalink
add create_web_app
Browse files Browse the repository at this point in the history
  • Loading branch information
SeeknnDestroy committed Oct 16, 2023
1 parent 65c7c8e commit 950984f
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 74 deletions.
Empty file removed autollm/app/api/__init__.py
Empty file.
43 changes: 0 additions & 43 deletions autollm/app/api/ask_question.py

This file was deleted.

16 changes: 0 additions & 16 deletions autollm/app/api/health_check.py

This file was deleted.

51 changes: 36 additions & 15 deletions autollm/app/main.py
Original file line number Diff line number Diff line change
@@ -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")
25 changes: 25 additions & 0 deletions autollm/app/utils.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 950984f

Please sign in to comment.