-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
73 lines (56 loc) · 1.92 KB
/
app.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from mangum import Mangum
from langchain.schema import OutputParserException
class UserMessage(BaseModel):
message: str
roomSlug: str
username: str
ismod: bool
app = FastAPI()
handler = Mangum(app)
origins = [
"http://localhost:3000",
"https://ruben30.com",
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=False,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/hello")
def hello_world():
return {'message': 'Hello World'}
@app.get("/")
def root():
return {'message': 'This is the root mate'}
from treasurehunt import DynamoStateMachineMessageHistory, StateMachineBufferMemory, create_treasure_hunt_bot, get_riddles_str
@app.post("/huntgpt")
def hello(message: UserMessage):
print('got message', message)
try:
riddles_str = get_riddles_str(message.roomSlug)
message_history = DynamoStateMachineMessageHistory(table_name="SessionTable-dev-local", session_id=message.roomSlug)
message_memory = StateMachineBufferMemory(chat_memory=message_history)
bot = create_treasure_hunt_bot(riddles_str=riddles_str, memory=message_memory)
params = {
'input': message.message,
'user_role': 'Moderator' if message.ismod else 'Player'
}
# Retry 3 times
for i in range(3):
try:
out = bot(params)
reply = out['reply']
return {'reply': reply}
except OutputParserException as e:
continue
except Exception as e:
print(e)
return {'reply': "Error getting bot response –" + str(e)[:200]}
except Exception as e:
print(e)
return {'reply': "Error setting up bot –" + str(e)[:200]}