├── LICENSE ├── README.md ├── api ├── __init__.py ├── hello.py ├── random.py └── v1 │ ├── __init__.py │ └── groq.py ├── main.py ├── package.json ├── public ├── __init__.py ├── favicon.ico ├── usage.py └── vercel.png ├── requirements.txt └── vercel.json /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) [2024] [ultrasev] 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 | Deploy Python(+FastAPI) project on Vercel 3 |

Deploy Python(+FastAPI) project on Vercel

4 | 5 |

6 | 7 | Issues 8 | 9 | 10 | GitHub pull requests 11 | 12 |
13 | Report Bug 14 | Request Feature 15 |

16 | 17 | 18 | # Deploy with Vercel 19 | 20 | [![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https://github.com/ultrasev/vercel-python-fastapi/tree/master/vercel&demo-title=PythonDeployment&demo-description=Deploy&demo-url=https://llmproxy.vercel.app/&demo-image=https://vercel.com/button) 21 | 22 | # Local Development 23 | 24 | ```bash 25 | pip3 install -r requirements.txt 26 | pip3 install uvicorn 27 | uvicorn main:app --host 0.0.0.0 --port 8000 --reload 28 | ``` 29 | 30 | # Documentation 31 | - [FastAPI framework](https://fastapi.tiangolo.com/) 32 | - [Vercel: configure a build](https://dlj.one/ktkfme) 33 | - [Stackoverflow: How can you specify python runtime version in vercel?](https://dlj.one/itw3m0) 34 | - [Vercel Python hello world example](https://vercel.com/templates/python/python-hello-world) 35 | 36 | 37 | # License 38 | Copyright © 2024 [ultrasev](https://github.com/ultrasev).
39 | This project is [MIT](LICENSE) licensed. 40 | -------------------------------------------------------------------------------- /api/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ultrasev/vercel-python-fastapi/417c4b7c273a1f5b5bb55ab0cae6ab07eaa00f27/api/__init__.py -------------------------------------------------------------------------------- /api/hello.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | from fastapi.routing import APIRouter 3 | router = APIRouter() 4 | 5 | 6 | @router.get("/") 7 | def read_root(): 8 | return {"Hello": "World"} 9 | -------------------------------------------------------------------------------- /api/random.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | from fastapi.routing import APIRouter 3 | import random 4 | router = APIRouter() 5 | 6 | 7 | @router.get("/") 8 | def read_root(): 9 | return {"number": random.randint(1, 100)} 10 | 11 | 12 | -------------------------------------------------------------------------------- /api/v1/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ultrasev/vercel-python-fastapi/417c4b7c273a1f5b5bb55ab0cae6ab07eaa00f27/api/v1/__init__.py -------------------------------------------------------------------------------- /api/v1/groq.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import typing 3 | 4 | import pydantic 5 | from fastapi import Header 6 | from fastapi.routing import APIRouter 7 | from openai import AsyncClient 8 | 9 | router = APIRouter() 10 | 11 | 12 | class ChatArgs(pydantic.BaseModel): 13 | model: str 14 | messages: typing.List[typing.Dict[str, str]] 15 | 16 | 17 | @router.post("/chat/completions") 18 | async def groq_api(args: ChatArgs, authorization: str = Header(...)): 19 | api_key = authorization.split(" ")[1] 20 | client = AsyncClient(base_url="https://api.groq.com/openai/v1", 21 | api_key=api_key) 22 | return await client.chat.completions.create( 23 | model=args.model, 24 | messages=args.messages, 25 | ) 26 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | from public.usage import USAGE as html 3 | from api.hello import router as hello_router 4 | from api.random import router as random_router 5 | from api.v1.groq import router as groq_router 6 | from fastapi import FastAPI 7 | from fastapi.responses import Response 8 | app = FastAPI() 9 | 10 | app.include_router(hello_router, prefix="/hello") 11 | app.include_router(random_router, prefix="/random") 12 | app.include_router(groq_router, prefix="/v1/groq") 13 | 14 | 15 | @app.get("/") 16 | def _root(): 17 | return Response(content=html, media_type="text/html") 18 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "engines": { 3 | "node": "18.x" 4 | } 5 | } -------------------------------------------------------------------------------- /public/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ultrasev/vercel-python-fastapi/417c4b7c273a1f5b5bb55ab0cae6ab07eaa00f27/public/__init__.py -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ultrasev/vercel-python-fastapi/417c4b7c273a1f5b5bb55ab0cae6ab07eaa00f27/public/favicon.ico -------------------------------------------------------------------------------- /public/usage.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | USAGE = """ 4 | 5 | 6 | 7 | 8 | Usage 9 | 40 | 41 | 42 |
43 |

success

44 |

Usage

45 |

Visit Github doc for more information.

46 |
47 | 48 | 49 | 50 | """ 51 | -------------------------------------------------------------------------------- /public/vercel.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ultrasev/vercel-python-fastapi/417c4b7c273a1f5b5bb55ab0cae6ab07eaa00f27/public/vercel.png -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | fastapi==0.88.0 2 | pydantic~=1.10.4 3 | python-multipart==0.0.5 4 | expiringdict==1.2.2 5 | rich==13.4.2 6 | openai==1.6.1 7 | httpx==0.27.0 8 | -------------------------------------------------------------------------------- /vercel.json: -------------------------------------------------------------------------------- 1 | { 2 | "builds": [ 3 | { 4 | "src": "main.py", 5 | "use": "@vercel/python" 6 | } 7 | ], 8 | "routes": [ 9 | { 10 | "src": "/(.*)", 11 | "dest": "main.py" 12 | } 13 | ] 14 | } --------------------------------------------------------------------------------