This repository has been archived by the owner on Sep 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
65c7c8e
commit 950984f
Showing
5 changed files
with
61 additions
and
74 deletions.
There are no files selected for viewing
Empty file.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |