-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.py
56 lines (39 loc) · 1.28 KB
/
main.py
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
import json
import os
import claude3_provider_common
from anthropic import AsyncAnthropic
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse, StreamingResponse
debug = os.environ.get("GPTSCRIPT_DEBUG", "false") == "true"
client = AsyncAnthropic()
app = FastAPI()
def log(*args):
if debug:
print(*args)
@app.middleware("http")
async def log_body(request: Request, call_next):
body = await request.body()
log("HTTP REQUEST BODY: ", body)
return await call_next(request)
@app.post("/")
async def post_root():
return 'ok'
@app.get("/")
async def get_root():
return 'ok'
@app.get("/v1/models")
async def list_models() -> JSONResponse:
return await claude3_provider_common.list_models(client)
@app.post("/v1/chat/completions")
async def completions(request: Request) -> StreamingResponse:
data = await request.body()
input = json.loads(data)
return await claude3_provider_common.completions(client, input)
if __name__ == "__main__":
import uvicorn
import asyncio
try:
uvicorn.run("main:app", host="127.0.0.1", port=int(os.environ.get("PORT", "8000")),
log_level="debug" if debug else "critical", access_log=debug)
except (KeyboardInterrupt, asyncio.CancelledError):
pass