forked from poe-platform/server-bot-quick-start
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathbot_RunTrinoQuery.py
87 lines (71 loc) · 2.4 KB
/
bot_RunTrinoQuery.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
"""
BOT_NAME="RunTrinoQuery"; modal deploy --name $BOT_NAME bot_${BOT_NAME}.py; curl -X POST https://api.poe.com/bot/fetch_settings/$BOT_NAME/$POE_ACCESS_KEY
Test message:
SELECT nationkey, name FROM tpch.sf1.nation
"""
import re
import os
import textwrap
from typing import AsyncIterable
import trino
from fastapi_poe import PoeBot, make_app
from fastapi_poe.types import QueryRequest, SettingsRequest, SettingsResponse
from modal import Image, Stub, asgi_app
from sse_starlette.sse import ServerSentEvent
from trino.exceptions import TrinoUserError
def format_output(columns, rows) -> str:
output = " | " + "|".join(column.name for column in columns) + " | "
output += "\n" + " | " + " | ".join("-" for _ in columns) + " | "
for row in rows:
output += "\n" + " | " + " | ".join(str(value) for value in row) + " | "
return output
def strip_code(code):
pattern = r"```sql([\s\S]*?)```"
matches = re.findall(pattern, code)
if matches:
return "\n\n".join(matches)
return code
conn = trino.dbapi.connect(
host=os.environ["TRINO_HOST_URL"],
port=443,
http_scheme="https",
auth=trino.auth.BasicAuthentication(
os.environ["TRINO_USERNAME"], os.environ["TRINO_PASSWORD"]
),
)
cur = conn.cursor()
def make_query(query):
try:
cur.execute(query)
except TrinoUserError as e:
return "```python\n" + str(e) + "\n```"
rows = cur.fetchall()
columns = cur.description
output = format_output(columns, rows)
return output
class RunTrinoQueryBot(PoeBot):
async def get_response(
self, request: QueryRequest
) -> AsyncIterable[ServerSentEvent]:
user_statement = request.query[-1].content
print("user_statement")
print(user_statement)
user_statement = strip_code(user_statement)
output = make_query(user_statement)
yield self.text_event(output)
async def get_settings(self, setting: SettingsRequest) -> SettingsResponse:
return SettingsResponse(
server_bot_dependencies={},
allow_attachments=False,
introduction_message=textwrap.dedent(
"""
Please send a Trino query, such as
````
```sql
SELECT nationkey, name FROM tpch.sf1.nation
```
````
Try copying the above, paste it, and reply.
"""
).strip(),
)