-
Notifications
You must be signed in to change notification settings - Fork 240
/
Copy pathserver.py
executable file
·50 lines (32 loc) · 1.31 KB
/
server.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
#!/usr/bin/env python
"""An example that shows how to use path dependencies for authentication.
The path dependencies are applied to all the routes added by the `add_routes`.
To keep this example brief, we're providing a placeholder verify_token function
that shows how to use path dependencies.
To implement proper auth, please see the FastAPI docs:
* https://fastapi.tiangolo.com/tutorial/dependencies/dependencies-in-path-operation-decorators/
* https://fastapi.tiangolo.com/tutorial/dependencies/
* https://fastapi.tiangolo.com/tutorial/security/
""" # noqa: E501
from fastapi import Depends, FastAPI, Header, HTTPException
from langchain_core.runnables import RunnableLambda
from typing_extensions import Annotated
from langserve import add_routes
async def verify_token(x_token: Annotated[str, Header()]) -> None:
"""Verify the token is valid."""
# Replace this with your actual authentication logic
if x_token != "secret-token":
raise HTTPException(status_code=400, detail="X-Token header invalid")
app = FastAPI()
def add_one(x: int) -> int:
"""Add one to an integer."""
return x + 1
chain = RunnableLambda(add_one)
add_routes(
app,
chain,
dependencies=[Depends(verify_token)],
)
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="localhost", port=8000)