chore: format
This commit is contained in:
@@ -125,9 +125,9 @@ def upgrade() -> None:
|
||||
# - yield_per: fetches rows in chunks via cursor.fetchmany() (all backends)
|
||||
# - stream_results: enables server-side cursors on PostgreSQL (no-op on SQLite)
|
||||
result = conn.execute(
|
||||
sa.select(chat_table.c.id, chat_table.c.user_id, chat_table.c.chat).where(
|
||||
~chat_table.c.user_id.like("shared-%")
|
||||
).execution_options(yield_per=1000, stream_results=True)
|
||||
sa.select(chat_table.c.id, chat_table.c.user_id, chat_table.c.chat)
|
||||
.where(~chat_table.c.user_id.like("shared-%"))
|
||||
.execution_options(yield_per=1000, stream_results=True)
|
||||
)
|
||||
|
||||
now = int(time.time())
|
||||
@@ -175,33 +175,39 @@ def upgrade() -> None:
|
||||
if timestamp < 1577836800 or timestamp > now + 86400:
|
||||
timestamp = now
|
||||
|
||||
messages_batch.append({
|
||||
"id": f"{chat_id}-{message_id}",
|
||||
"chat_id": chat_id,
|
||||
"user_id": user_id,
|
||||
"role": role,
|
||||
"parent_id": message.get("parentId"),
|
||||
"content": message.get("content"),
|
||||
"output": message.get("output"),
|
||||
"model_id": message.get("model"),
|
||||
"files": message.get("files"),
|
||||
"sources": message.get("sources"),
|
||||
"embeds": message.get("embeds"),
|
||||
"done": message.get("done", True),
|
||||
"status_history": message.get("statusHistory"),
|
||||
"error": message.get("error"),
|
||||
"usage": message.get("usage"),
|
||||
"created_at": timestamp,
|
||||
"updated_at": timestamp,
|
||||
})
|
||||
messages_batch.append(
|
||||
{
|
||||
"id": f"{chat_id}-{message_id}",
|
||||
"chat_id": chat_id,
|
||||
"user_id": user_id,
|
||||
"role": role,
|
||||
"parent_id": message.get("parentId"),
|
||||
"content": message.get("content"),
|
||||
"output": message.get("output"),
|
||||
"model_id": message.get("model"),
|
||||
"files": message.get("files"),
|
||||
"sources": message.get("sources"),
|
||||
"embeds": message.get("embeds"),
|
||||
"done": message.get("done", True),
|
||||
"status_history": message.get("statusHistory"),
|
||||
"error": message.get("error"),
|
||||
"usage": message.get("usage"),
|
||||
"created_at": timestamp,
|
||||
"updated_at": timestamp,
|
||||
}
|
||||
)
|
||||
|
||||
# Flush batch when full
|
||||
if len(messages_batch) >= BATCH_SIZE:
|
||||
inserted, failed = _flush_batch(conn, chat_message_table, messages_batch)
|
||||
inserted, failed = _flush_batch(
|
||||
conn, chat_message_table, messages_batch
|
||||
)
|
||||
total_inserted += inserted
|
||||
total_failed += failed
|
||||
if total_inserted % 50000 < BATCH_SIZE:
|
||||
log.info(f"Migration progress: {total_inserted} messages inserted...")
|
||||
log.info(
|
||||
f"Migration progress: {total_inserted} messages inserted..."
|
||||
)
|
||||
messages_batch.clear()
|
||||
|
||||
# Flush remaining messages
|
||||
|
||||
@@ -296,9 +296,7 @@ class ChatMessageTable:
|
||||
# (prevents duplicates across pages when timestamps tie)
|
||||
chat_ids = (
|
||||
query.group_by(ChatMessage.chat_id)
|
||||
.order_by(
|
||||
func.max(ChatMessage.created_at).desc(), ChatMessage.chat_id
|
||||
)
|
||||
.order_by(func.max(ChatMessage.created_at).desc(), ChatMessage.chat_id)
|
||||
.offset(skip)
|
||||
.limit(limit)
|
||||
.all()
|
||||
|
||||
@@ -129,7 +129,9 @@ def create_folder(
|
||||
)
|
||||
|
||||
try:
|
||||
folder = Folders.insert_new_folder(user.id, form_data, form_data.parent_id, db=db)
|
||||
folder = Folders.insert_new_folder(
|
||||
user.id, form_data, form_data.parent_id, db=db
|
||||
)
|
||||
return folder
|
||||
except Exception as e:
|
||||
log.exception(e)
|
||||
@@ -317,7 +319,9 @@ async def delete_folder_by_id(
|
||||
folder = folders.pop()
|
||||
if folder:
|
||||
try:
|
||||
folder_ids = Folders.delete_folder_by_id_and_user_id(folder.id, user.id, db=db)
|
||||
folder_ids = Folders.delete_folder_by_id_and_user_id(
|
||||
folder.id, user.id, db=db
|
||||
)
|
||||
|
||||
for folder_id in folder_ids:
|
||||
if delete_contents:
|
||||
|
||||
@@ -2248,10 +2248,7 @@ async def process_chat_payload(request, form_data, user, metadata, model):
|
||||
folder.data["system_prompt"], form_data, metadata, user
|
||||
)
|
||||
if "files" in folder.data:
|
||||
if (
|
||||
metadata.get("params", {}).get("function_calling")
|
||||
!= "native"
|
||||
):
|
||||
if metadata.get("params", {}).get("function_calling") != "native":
|
||||
form_data["files"] = [
|
||||
*folder.data["files"],
|
||||
*form_data.get("files", []),
|
||||
@@ -2378,8 +2375,7 @@ async def process_chat_payload(request, form_data, user, metadata, model):
|
||||
if metadata.get("params", {}).get("function_calling") != "native":
|
||||
prompt = (
|
||||
request.app.state.config.CODE_INTERPRETER_PROMPT_TEMPLATE
|
||||
if request.app.state.config.CODE_INTERPRETER_PROMPT_TEMPLATE
|
||||
!= ""
|
||||
if request.app.state.config.CODE_INTERPRETER_PROMPT_TEMPLATE != ""
|
||||
else DEFAULT_CODE_INTERPRETER_PROMPT
|
||||
)
|
||||
|
||||
@@ -2755,9 +2751,7 @@ async def process_chat_payload(request, form_data, user, metadata, model):
|
||||
metadata["system_prompt"] = (
|
||||
get_content_from_message(system_message) if system_message else None
|
||||
)
|
||||
metadata["user_prompt"] = get_last_user_message(
|
||||
form_data["messages"]
|
||||
)
|
||||
metadata["user_prompt"] = get_last_user_message(form_data["messages"])
|
||||
metadata["sources"] = sources[:] if sources else []
|
||||
|
||||
# If context is not empty, insert it into the messages
|
||||
@@ -4200,11 +4194,7 @@ async def streaming_chat_response_handler(response, ctx):
|
||||
reasoning_item["status"] = "completed"
|
||||
|
||||
if response_tool_calls:
|
||||
tool_calls.append(
|
||||
_split_tool_calls(
|
||||
response_tool_calls
|
||||
)
|
||||
)
|
||||
tool_calls.append(_split_tool_calls(response_tool_calls))
|
||||
|
||||
if response.background:
|
||||
await response.background()
|
||||
@@ -4224,13 +4214,9 @@ async def streaming_chat_response_handler(response, ctx):
|
||||
# Use the pre-RAG system content captured before the
|
||||
# initial file-source injection in process_chat_payload.
|
||||
# This ensures restore truly undoes the RAG template.
|
||||
original_system_content = metadata.get(
|
||||
"system_prompt"
|
||||
)
|
||||
original_system_content = metadata.get("system_prompt")
|
||||
if original_system_content is None:
|
||||
original_system_message = get_system_message(
|
||||
form_data["messages"]
|
||||
)
|
||||
original_system_message = get_system_message(form_data["messages"])
|
||||
original_system_content = (
|
||||
get_content_from_message(original_system_message)
|
||||
if original_system_message
|
||||
@@ -4495,9 +4481,9 @@ async def streaming_chat_response_handler(response, ctx):
|
||||
if all_tool_call_sources and user_message:
|
||||
# Restore pre-RAG message state before re-applying
|
||||
# to prevent RAG template duplication.
|
||||
original_user_message = metadata.get(
|
||||
"user_prompt"
|
||||
) or user_message
|
||||
original_user_message = (
|
||||
metadata.get("user_prompt") or user_message
|
||||
)
|
||||
set_last_user_message_content(
|
||||
original_user_message,
|
||||
form_data["messages"],
|
||||
@@ -4509,14 +4495,11 @@ async def streaming_chat_response_handler(response, ctx):
|
||||
|
||||
# Combine file and tool sources into one RAG
|
||||
# template application.
|
||||
form_data["messages"] = (
|
||||
apply_source_context_to_messages(
|
||||
request,
|
||||
form_data["messages"],
|
||||
metadata.get("sources", [])
|
||||
+ all_tool_call_sources,
|
||||
user_message,
|
||||
)
|
||||
form_data["messages"] = apply_source_context_to_messages(
|
||||
request,
|
||||
form_data["messages"],
|
||||
metadata.get("sources", []) + all_tool_call_sources,
|
||||
user_message,
|
||||
)
|
||||
tool_call_sources.clear()
|
||||
|
||||
@@ -4588,8 +4571,7 @@ async def streaming_chat_response_handler(response, ctx):
|
||||
code = sanitize_code(code)
|
||||
|
||||
if CODE_INTERPRETER_BLOCKED_MODULES:
|
||||
blocking_code = textwrap.dedent(
|
||||
f"""
|
||||
blocking_code = textwrap.dedent(f"""
|
||||
import builtins
|
||||
|
||||
BLOCKED_MODULES = {CODE_INTERPRETER_BLOCKED_MODULES}
|
||||
@@ -4605,8 +4587,7 @@ async def streaming_chat_response_handler(response, ctx):
|
||||
return _real_import(name, globals, locals, fromlist, level)
|
||||
|
||||
builtins.__import__ = restricted_import
|
||||
"""
|
||||
)
|
||||
""")
|
||||
code = blocking_code + "\n" + code
|
||||
|
||||
if (
|
||||
|
||||
@@ -199,7 +199,9 @@ async def get_all_models(request, refresh: bool = False, user: UserModel = None)
|
||||
|
||||
base_model = base_model_lookup.get(custom_model.base_model_id)
|
||||
if base_model is None:
|
||||
base_model = base_model_lookup.get(custom_model.base_model_id.split(":")[0])
|
||||
base_model = base_model_lookup.get(
|
||||
custom_model.base_model_id.split(":")[0]
|
||||
)
|
||||
if base_model:
|
||||
owned_by = base_model.get("owned_by", "unknown")
|
||||
if "pipe" in base_model:
|
||||
|
||||
Reference in New Issue
Block a user