-
Notifications
You must be signed in to change notification settings - Fork 59
/
main.py
27 lines (20 loc) · 836 Bytes
/
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
import os
from fastapi import FastAPI, HTTPException, Request
from pydantic import BaseModel
from Bard import Chatbot
app = FastAPI()
class Message(BaseModel):
session_id: str
message: str
@app.post("/ask")
async def ask(request: Request, message: Message) -> dict:
# Get the user-defined auth key from the environment variables
user_auth_key = os.getenv('USER_AUTH_KEY')
# Check if the user has defined an auth key,
# If so, check if the auth key in the header matches it.
if user_auth_key and user_auth_key != request.headers.get('Authorization'):
raise HTTPException(status_code=401, detail='Invalid authorization key')
# Execute your code without authenticating the resource
chatbot = Chatbot(message.session_id)
response = chatbot.ask(message.message)
return response