-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
77 lines (63 loc) · 2.15 KB
/
server.py
File metadata and controls
77 lines (63 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
from fastapi import FastAPI, Request
from fastapi.responses import RedirectResponse
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
from pydantic import BaseModel
from prompt import RetrievalChain
from embedding import load_vector_store
from fastapi.templating import Jinja2Templates
from models import Message
import starlette.status as status
import json
import meta
import os
import uvicorn
# App environment
environment = os.environ.get("ENV", "development")
manifest = {}
# vite manifest
if environment != "development":
with open("public/.vite/manifest.json") as f:
manifest = json.load(f)
chain = RetrievalChain(load_vector_store(), history_aware=True)
app = FastAPI(
title=f"{meta.title} API",
version=meta.version,
description=f"API for the {meta.title} chatbot",
)
templates = Jinja2Templates(directory="templates")
if environment == "development":
app.mount("/public", StaticFiles(directory="frontend/public", html=True), name="public")
app.mount("/src", StaticFiles(directory="frontend/src", html=True), name="frontend/src")
else:
app.mount("/public", StaticFiles(directory="public", html=True), name="public")
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
)
class InvokeChainInput(BaseModel):
input: str
chat_history: list[Message] | None = None
n_results: int = 10
class InvokeChainRequest(BaseModel):
config: dict
input: InvokeChainInput
kwargs: dict
@app.get("/")
async def index(request: Request):
return templates.TemplateResponse(request, "index.html", context={
"title": meta.title,
"description": meta.description,
"env": environment,
"manifest": manifest,
})
@app.get("/{full_path}")
async def catch_all(request: Request, full_path: str):
return RedirectResponse(url="/public/" + full_path, status_code=status.HTTP_302_FOUND)
@app.post("/invoke")
async def invoke_chain(request: InvokeChainRequest):
result = chain.invoke(dict(request.input), request.config)
return result
def run_server(host = "localhost", port = 8000):
uvicorn.run(app, host=host, port=port)