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.
* add create_web_app * update readme * update fastapi docs * fix styling * some fixes, add tests for web app, update readme * update requirements * update httpx versioning * add examples * minor fixes, llama_index version update * major refactor * minor fix * another minor fix * bugfix * fix tests --------- Co-authored-by: fatih <34196005+fcakyon@users.noreply.github.com> Co-authored-by: fcakyon <fcakyon@gmail.com>
- Loading branch information
1 parent
f2a3d7f
commit 7ea85ba
Showing
22 changed files
with
254 additions
and
147 deletions.
There are no files selected for viewing
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
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
Empty file.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
File renamed without changes.
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,16 @@ | ||
# Purpose: FastAPI documentation configuration. | ||
# Metadata | ||
title = "AutoLLM Query Engine" | ||
description = """ | ||
This is a FastAPI service for AutoLLM's natural language query engine. | ||
It's designed to query multiple and big documents and get the most relevant results. | ||
""" | ||
version = "0.0.1" | ||
openapi_url = "/api/v1/openapi.json" | ||
terms_of_service = "Local Deployment, All Rights Reserved." | ||
tags_metadata = [ | ||
{ | ||
"name": "query", | ||
"description": "Operations related to querying the header-documents." | ||
}, | ||
] |
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,72 @@ | ||
import logging | ||
from typing import Dict | ||
|
||
import yaml | ||
from dotenv import load_dotenv | ||
from fastapi import FastAPI, HTTPException | ||
from pydantic import BaseModel, Field | ||
|
||
from autollm.auto.query_engine import AutoQueryEngine | ||
from autollm.serve.docs import description, openapi_url, tags_metadata, terms_of_service, title, version | ||
|
||
logging.basicConfig(level=logging.INFO) | ||
|
||
|
||
# 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['tasks']: | ||
task_name = task_params.pop('name') | ||
query_engines[task_name] = AutoQueryEngine.from_parameters(**task_params) | ||
|
||
return query_engines | ||
|
||
|
||
class QueryPayload(BaseModel): | ||
task: str = Field(..., description="Task to execute") | ||
user_query: str = Field(..., description="User's query") | ||
|
||
|
||
# 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(payload: QueryPayload): | ||
task = payload.task | ||
user_query = payload.user_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,2 @@ | ||
# .env.example | ||
OPENAI_API_KEY=your_openai_api_key_here |
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 @@ | ||
# config.example.yaml | ||
version: '1.0' # Version of this configuration file | ||
tasks: | ||
- name: "summarize" | ||
system_prompt: "Please summarize the following text:" # System prompt for this task | ||
vector_store_params: | ||
vector_store_type: "VectorStoreIndex" | ||
llm_params: | ||
model: "gpt-3.5-turbo" | ||
service_context_params: | ||
chunk_size: 1024 | ||
query_engine_params: | ||
similarity_top_k: 5 | ||
enable_cost_calculator: true | ||
- name: "qa" | ||
system_prompt: "Question:" # System prompt for this task | ||
vector_store_params: | ||
vector_store_type: "VectorStoreIndex" | ||
llm_params: | ||
model: "gpt-3.5-turbo" | ||
service_context_params: | ||
chunk_size: 1024 | ||
query_engine_params: | ||
similarity_top_k: 3 | ||
enable_cost_calculator: false |
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,6 +1,7 @@ | ||
llama-index==0.8.41 | ||
llama-index==0.8.46 | ||
litellm==0.8.4 | ||
gitpython==3.1.37 | ||
uvicorn==0.23.2 | ||
fastapi==0.103.2 | ||
python-dotenv | ||
httpx |
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
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,24 @@ | ||
# config.example.yaml | ||
tasks: | ||
- name: "summarize" | ||
system_prompt: "Please summarize the following text:" # System prompt for this task | ||
vector_store_params: | ||
vector_store_type: "VectorStoreIndex" | ||
llm_params: | ||
model: "gpt-3.5-turbo" | ||
service_context_params: | ||
chunk_size: 1024 | ||
query_engine_params: | ||
similarity_top_k: 5 | ||
enable_cost_calculator: true | ||
- name: "qa" | ||
system_prompt: "Question:" # System prompt for this task | ||
vector_store_params: | ||
vector_store_type: "VectorStoreIndex" | ||
llm_params: | ||
model: "gpt-3.5-turbo" | ||
service_context_params: | ||
chunk_size: 1024 | ||
query_engine_params: | ||
similarity_top_k: 3 | ||
enable_cost_calculator: false |
Oops, something went wrong.