forked from reasonmethis/docdocgo-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
415 lines (347 loc) · 14.7 KB
/
Copy pathapi.py
File metadata and controls
415 lines (347 loc) · 14.7 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
"""
The FastAPI server that enables API access to DocDocGo.
"""
import json
import os
import traceback
from typing import Annotated
from fastapi import Body, FastAPI, File, Form, HTTPException, UploadFile
from fastapi.middleware.cors import CORSMiddleware
from icecream import ic
from pydantic import BaseModel
from _prepare_env import is_env_loaded
from agents.dbmanager import (
get_access_role,
get_short_user_id,
get_user_facing_collection_name,
)
from components.chroma_ddg import get_vectorstore_using_openai_api_key
from docdocgo import get_bot_response, get_source_links
from utils.chat_state import AgentDataDict, ChatState, ScheduledQueries
from utils.helpers import DELIMITER
from utils.ingest import extract_text, format_ingest_failure
from utils.prepare import (
BYPASS_SETTINGS_RESTRICTIONS,
BYPASS_SETTINGS_RESTRICTIONS_PASSWORD,
DEFAULT_COLLECTION_NAME,
DEFAULT_OPENAI_API_KEY,
INCLUDE_ERROR_IN_USER_FACING_ERROR_MSG,
MAX_UPLOAD_BYTES,
get_logger,
)
from utils.query_parsing import parse_query
from utils.type_utils import (
INSTRUCT_AUTO_RUN_NEXT_QUERY,
AccessRole,
BotSettings,
ChatMode,
DDGError,
Instruction,
OperationMode,
PairwiseChatHistory,
)
logger = get_logger()
app = FastAPI()
# Allow all domains/origins
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["GET", "POST", "PUT", "DELETE", "OPTIONS"],
allow_headers=["*"],
)
is_env_loaded = is_env_loaded # see explanation at the end of docdocgo.py
# Define Pydantic models for request and response
RoleBasedChatMessage = dict[str, str] # {"role": "user" | "assistant", "content": str}
class ChatRequestData(BaseModel):
message: str
api_key: str
openai_api_key: str | None = None
chat_history: list[RoleBasedChatMessage] = []
collection_name: str | None = None
access_codes_cache: dict[str, str] | None = None # coll name -> access_code
agentic_flow_state_str: str | None = None # JSON string that frontend passes back
bot_settings: BotSettings | None = None
def parse_curr_state(self):
agentic_state: dict = json.loads(self.agentic_flow_state_str or "{}")
if (tmp := agentic_state.get("scheduled_queries")) is None:
scheduled_queries = ScheduledQueries()
else:
scheduled_queries = ScheduledQueries.model_validate_json(tmp)
agent_data: AgentDataDict = agentic_state.get("agent_data", {})
# TODO: validate agent_data
return scheduled_queries, agent_data
class ChatResponseData(BaseModel):
content: str
sources: list[str] | None = None
collection_name: str | None = None
user_facing_collection_name: str | None = None
instructions: list[Instruction] | None = None
agentic_flow_state_str: str | None = None # JSON string that frontend passes back
@staticmethod
def encode_agentic_state(
scheduled_queries: ScheduledQueries, agent_data: AgentDataDict
):
return json.dumps(
{
"scheduled_queries": scheduled_queries.model_dump_json(),
"agent_data": agent_data,
}
)
def convert_chat_history(
chat_history_in_role_format: list[RoleBasedChatMessage],
) -> PairwiseChatHistory:
"""Convert the chat history into a list of tuples of the form (user_message, bot_message)"""
chat_history = []
user_message = ""
for chat in chat_history_in_role_format:
if chat["role"] == "user":
if user_message:
chat_history.append((user_message, ""))
user_message = chat["content"]
elif chat["role"] == "assistant":
chat_history.append((user_message, chat["content"]))
user_message = ""
return chat_history
def decode_param(param: str | None) -> str | dict | list | None:
return None if param is None else json.loads(param)
async def handle_chat_or_ingest_request(
data: ChatRequestData, files: list[UploadFile] = []
):
try:
# Process the request data for constructing the chat state
message = data.message.strip()
api_key = data.api_key # DocDocGo API key
# If admin pwd is sent, treat it as if the default key was sent
if (
BYPASS_SETTINGS_RESTRICTIONS_PASSWORD
and data.openai_api_key
and data.openai_api_key.strip() == BYPASS_SETTINGS_RESTRICTIONS_PASSWORD
and DEFAULT_OPENAI_API_KEY # only do this if the default key is configured
):
data.openai_api_key = DEFAULT_OPENAI_API_KEY
# Same story if no key is sent but BYPASS_SETTINGS_RESTRICTIONS is set
elif not data.openai_api_key and BYPASS_SETTINGS_RESTRICTIONS:
data.openai_api_key = DEFAULT_OPENAI_API_KEY
# If no key is specified, use the default key (but set is_community_key to True)
openai_api_key: str = data.openai_api_key or DEFAULT_OPENAI_API_KEY
is_community_key = not data.openai_api_key
# User id is determined from the OpenAI API key (or None if community key)
user_id: str | None = get_short_user_id(data.openai_api_key)
# TODO: use full api key as user id (but show only the short version)
chat_history = convert_chat_history(data.chat_history)
data.collection_name = data.collection_name or DEFAULT_COLLECTION_NAME
collection_name = data.collection_name
access_codes_cache: dict[str, str] | None = data.access_codes_cache
scheduled_queries, session_data = data.parse_curr_state()
# Validate the user's API key
if api_key != os.getenv("DOCDOCGO_API_KEY"):
print(f"Invalid API key: {api_key}")
return ChatResponseData(content="Invalid API key.")
# Validate the provided bot settings
if data.bot_settings and is_community_key:
# Enforce default settings for community key
if data.bot_settings != BotSettings():
return ChatResponseData(
content="Apologies, you can customize your model settings (e.g. model name, "
"temperature) only when using your own OpenAI API key."
)
# Extract text from the files and convert to list of Document
docs, failed_files, unsupported_ext_files = extract_text(
files, allow_all_ext=True
) # returns quickly if no files
# Print and validate the user's message and successful upload
if files:
print(f"GOT {len(files)} FILES, {len(docs)} DOCUMENTS")
if failed_files or unsupported_ext_files:
return ChatResponseData(
content=format_ingest_failure(failed_files, unsupported_ext_files)
)
# Parse the query (or get the next scheduled query if message/docs are empty)
if message or docs:
# If docs uploaded with empty message, interpret as "/upload"
parsed_query = parse_query(message or "/upload")
else:
parsed_query = scheduled_queries.pop()
if not parsed_query:
return ChatResponseData(
content="Apologies, I received an empty message from you."
)
# If there are files but command is not ingest or summarize, postpone it till after ingestion
if docs and parsed_query.chat_mode not in (
ChatMode.INGEST_COMMAND_ID,
ChatMode.SUMMARIZE_COMMAND_ID,
):
scheduled_queries.add_to_front(parsed_query)
parsed_query = parse_query("/upload")
# Initialize vectorstore and chat state
try:
vectorstore = get_vectorstore_using_openai_api_key(
collection_name, openai_api_key=openai_api_key
)
except Exception as e:
return ChatResponseData(
content="Apologies, I could not load the vector database. This "
"could be due to a misconfiguration of the environment variables "
f"or missing files. The error reads: \n\n{e}"
)
access_code_by_coll_by_user_id = (
{user_id: access_codes_cache} if access_codes_cache else None
)
chat_state = ChatState(
operation_mode=OperationMode.FASTAPI,
vectorstore=vectorstore,
is_community_key=is_community_key,
chat_history=chat_history,
openai_api_key=openai_api_key,
user_id=user_id,
parsed_query=parsed_query,
scheduled_queries=scheduled_queries,
session_data=session_data,
access_code_by_coll_by_user_id=access_code_by_coll_by_user_id,
uploaded_docs=docs,
bot_settings=data.bot_settings,
)
# Validate (and cache, for this request) the user's access level
access_role = get_access_role(chat_state)
if access_role.value <= AccessRole.NONE.value:
return ChatResponseData(
content="Apologies, you do not have access to the collection."
)
# Get the bot's response
result = get_bot_response(chat_state)
except DDGError as e:
print(traceback.format_exc())
user_msg = (
e.user_facing_message_full
if INCLUDE_ERROR_IN_USER_FACING_ERROR_MSG
else e.user_facing_message
)
print("User message:", user_msg)
return ChatResponseData(content=user_msg) # NOTE: think about http status codes
except Exception:
print(traceback.format_exc())
return ChatResponseData(
content="Apologies, I encountered an error while trying to "
"compose a response to you."
)
# print("AI:", reply) - no need, we are streaming to stdout now
print(DELIMITER)
# Determine the current collection name
try:
collection_name = result["vectorstore"].name
except KeyError:
pass
# If result has instructions to auto-run a query, add to front of queue
if new_parsed_query := result.get("new_parsed_query"):
chat_state.scheduled_queries.add_to_front(new_parsed_query) # should move this
# If needed, add an explicit instruction to auto-run a query
instructions: list | None = result.get("instructions")
if chat_state.scheduled_queries:
instructions = instructions or []
instructions.append(Instruction(type=INSTRUCT_AUTO_RUN_NEXT_QUERY))
# NOTE: may want to move this to the main engine
# Prepare the response
rsp = ChatResponseData(
content=result["answer"],
sources=get_source_links(result) or None,
collection_name=collection_name,
user_facing_collection_name=get_user_facing_collection_name(
chat_state.user_id, collection_name
),
instructions=instructions,
agentic_flow_state_str=ChatResponseData.encode_agentic_state(
chat_state.scheduled_queries, chat_state.session_data
),
)
# Return the response
if rsp.sources:
print("Sources:" + "\n".join(rsp.sources) + "\n" + DELIMITER)
ic(rsp)
return rsp
@app.get("/")
async def root():
ic("/ endpoint hit: Hello from DocDocGo API!")
return {"message": "Hello from DocDocGo API!"}
@app.get("/health/")
async def health():
ic("/health/ endpoint hit: Hello from DocDocGo API!")
return {"message": "Hello from DocDocGo API!"}
@app.post("/ingest/", response_model=ChatResponseData)
async def ingest(
files: Annotated[list[UploadFile], File()],
message: Annotated[str, Form()],
api_key: Annotated[str, Form()],
openai_api_key: Annotated[str | None, Form()] = None,
chat_history: Annotated[str | None, Form()] = None, # JSON string
collection_name: Annotated[str | None, Form()] = None,
access_codes_cache: Annotated[str | None, Form()] = None, # JSON string
scheduled_queries_str: Annotated[str | None, Form()] = None,
agent_data: Annotated[str | None, Form()] = None, # JSON string
bot_settings: Annotated[BotSettings | None, Form()] = None,
):
"""
Handle a chat message from the user, which may include files, and return a
response from the bot.
"""
ic("Ingest endpoint hit")
# Validate the total size of the files
total_size = 0
for ufile in files:
# Move to the end of the file and get the position
ufile.file.seek(0, 2) # Seek to the end
total_size += ufile.file.tell() # Position = size
ufile.file.seek(0) # Reset for any further operations
if total_size > MAX_UPLOAD_BYTES:
raise HTTPException(
status_code=413,
detail=f"The total size of the files exceeds the permitted limit of {MAX_UPLOAD_BYTES} bytes.",
)
# Apparently if Upload is clicked in the browser with no file selected,
# then 'files' will NOT be empty, but will contain 1 file with no bytes.
if total_size == 0:
files = []
# We need to decode the parameters from the form data because they are JSON strings
# or None. In particular, I made it so that even string fields are expected to be encoded.
# One reason for this is that passing an empty string in a form field causes a 422 error.
# The problem is solved if we encode all strings - then an empty string becomes '""'.
try:
# Decode and validate request data
data = ChatRequestData(
message=decode_param(message),
api_key=decode_param(api_key),
openai_api_key=decode_param(openai_api_key),
chat_history=decode_param(chat_history),
collection_name=decode_param(collection_name),
access_codes_cache=decode_param(access_codes_cache),
agentic_flow_state_str=decode_param(scheduled_queries_str),
bot_settings=decode_param(bot_settings),
)
ic(data)
except Exception as e:
raise HTTPException(status_code=400, detail=f"Invalid request data: {e}")
return await handle_chat_or_ingest_request(data, files)
@app.post("/chat/", response_model=ChatResponseData)
async def chat(data: ChatRequestData = Body(...)):
"""Handle a chat message from the user and return a response from the bot"""
ic("Chat endpoint hit")
return await handle_chat_or_ingest_request(data)
if __name__ == "__main__":
print(
"Starting server... (you can instead start it using `uvicorn api:app --reload`)."
)
from pathlib import Path
import uvicorn
config = {
"host": "0.0.0.0",
"port": 8000,
"log_level": "info",
"reload": True,
"reload_dirs": ["./"],
"ws_ping_interval": 300,
"ws_ping_timeout": 300,
"timeout_keep_alive": 300,
"use_colors": True,
"workers": 1,
}
uvicorn.run(f"{Path(__file__).stem}:app", **config)