Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
DucNgn committed Jul 12, 2023
1 parent 52fd9d8 commit 92c7d0a
Show file tree
Hide file tree
Showing 11 changed files with 180 additions and 61 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions front_end/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
<meta charset="utf-8" />
<title>LLM Gateway</title>
<base href="/" />
<link rel="stylesheet" href="styles.css" />
<link rel="stylesheet" href="styles/styles.css" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="icon" type="image/x-icon" href="favicon.ico" />
<link rel="icon" type="image/x-icon" href="assets/favicon.ico" />
</head>

<body>
Expand Down
File renamed without changes.
4 changes: 4 additions & 0 deletions llm_gateway/_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# file generated by setuptools_scm
# don't change, don't track in version control
__version__ = version = "0.1.1.dev4"
__version_tuple__ = version_tuple = (0, 1, 1, "dev4")
97 changes: 75 additions & 22 deletions llm_gateway/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,37 +15,90 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import pathlib

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles

from llm_gateway.routers import cohere_api, openai_api

app = FastAPI()
app.title = "LLM Proxy"
app.description = "LLM Proxy Developed by Wealthsimple"

def create_bare_app() -> FastAPI:
app = FastAPI()
app.title = "LLM Proxy"
app.description = "LLM Proxy Developed by Wealthsimple"
return app


def attach_api_service(app: FastAPI) -> FastAPI:
api = FastAPI(openapi_prefix="/api")
api.include_router(openai_api.router, prefix="/openai")
api.include_router(cohere_api.router, prefix="/cohere")

@api.get("/healthcheck")
async def healthcheck():
"""
Endpoint to verify that the service is up and running
"""
return {"message": "llm-gateway is healthy"}

app.mount("/api", api, name="api")
return app


def attach_middleware(app: FastAPI) -> FastAPI:
# Allow Front-end Origin in local development
origins = ["http://localhost:3000"]

app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
return app


def mount_front_end(app: FastAPI) -> FastAPI:
FRONT_END_BUILD_DIRECTORY = (
pathlib.Path(__file__).parent.parent / "front_end" / "build"
)
app.mount(
"/assets",
StaticFiles(directory=FRONT_END_BUILD_DIRECTORY / "assets"),
name="assets",
)

app.mount(
"/static",
StaticFiles(directory=FRONT_END_BUILD_DIRECTORY / "static"),
name="static",
)

app.mount(
"/styles",
StaticFiles(directory=FRONT_END_BUILD_DIRECTORY / "styles"),
name="styles",
)

api = FastAPI(openapi_prefix="/api")
api.include_router(openai_api.router, prefix="/openai")
api.include_router(cohere_api.router, prefix="/cohere")
@app.get("/")
async def home() -> HTMLResponse:
with open(FRONT_END_BUILD_DIRECTORY / "index.html", "r", encoding="utf-8") as f:
return HTMLResponse(f.read())

app.mount("/api", api, name="api")
return app

# Allow Front-end Origin in local development
origins = ["http://localhost:3000"]

app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
def create_app() -> FastAPI:
app = create_bare_app()
app = attach_middleware(app)
app = mount_front_end(app)
app = attach_api_service(app)
print("Finished app initialization ")
return app


@api.get("/healthcheck")
async def healthcheck():
"""
Endpoint to verify that the service is up and running
"""
return {"message": "llm-gateway is healthy"}
app = create_app()
32 changes: 32 additions & 0 deletions llm_gateway/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import click


@click.group(no_args_is_help=True)
def cli():
pass


@cli.command("start")
@click.option(
"--host",
type=str,
help="Bind socket to this host. Default: 127.0.0.1",
)
@click.option(
"--port",
type=int,
help="Bind socket to this port. Default: 8000",
)
def start(
host=None,
port=None,
):
import uvicorn

host = host or "127.0.0.1"
port = 8000 if port is None else port
uvicorn.run("llm_gateway.app:app", host=host, port=port, log_level="info")


if __name__ == "__main__":
cli()
8 changes: 4 additions & 4 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 0 additions & 33 deletions pyproject.toml

This file was deleted.

63 changes: 63 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import os
from os.path import exists

from setuptools import find_packages, setup

description = open("README.md").read() if exists("README.md") else ""

setup(
name="llm_gateway",
description="",
long_description=description,
long_description_content_type="text/markdown",
url="https://github.com/wealthsimple/llm-gateway",
author="Wealthsimple Data Science & Engineering",
author_email="[email protected]",
license="Apache License 2.0",
packages=find_packages(include=["llm_gateway", "front_end"]),
package_data={"front_end": ["front_end/**"]},
entry_points={
"console_scripts": [
"llm_gateway = llm_gateway.cli:cli",
],
},
use_scm_version={
"write_to": "llm_gateway/_version.py",
"fallback_version": "0.0.0",
"local_scheme": "no-local-version",
},
setup_requires=["setuptools_scm"],
install_requires=[
"click",
"fastapi>=0.95.0",
"pydantic>=1.10.7",
"psycopg2-binary~=2.9.3",
"alembic~=1.8.0",
"sqlalchemy>=1.3.0",
"uvicorn[standard]>=0.21.1",
"openai[datalib]>=0.27.4",
"cohere>=4.6.1",
"click>=8.1.4",
],
extras_require={
"openai": [
"datalib",
],
"dev": [
"black==23.3.0",
"isort==5.12.0",
"flake8>=6.0.0",
"pre-commit>=3.2.2",
"pytest>=7.3.0",
"urllib3==1.26.15",
],
},
classifiers=[
"Intended Audience :: Developers",
"Intended Audience :: Science/Research",
"License :: OSI Approved :: Apache Software License",
"Operating System :: OS Independent",
"Programming Language :: Python",
"Programming Language :: Python :: 3 :: Only",
],
)

0 comments on commit 92c7d0a

Please sign in to comment.