Remove unused branches in logging
This commit is contained in:
+50
-59
@@ -108,16 +108,16 @@ def log_request(req: Request) -> str:
|
||||
console.print(Panel.fit("Args / Form / JSON"))
|
||||
json_payload = details.get("json")
|
||||
cleaned_json = json_payload
|
||||
if isinstance(json_payload, dict):
|
||||
# Remove verbose fields to log them separately
|
||||
cleaned_json = {
|
||||
k: v
|
||||
for k, v in json_payload.items()
|
||||
if k
|
||||
not in {
|
||||
"tools",
|
||||
}
|
||||
|
||||
# Remove verbose fields to log them separately
|
||||
cleaned_json = {
|
||||
k: v
|
||||
for k, v in json_payload.items()
|
||||
if k
|
||||
not in {
|
||||
"tools",
|
||||
}
|
||||
}
|
||||
console.print_json(
|
||||
data={
|
||||
"query_args": details.get("query_args"),
|
||||
@@ -128,64 +128,55 @@ def log_request(req: Request) -> str:
|
||||
|
||||
# Separate section for chat messages (role + content)
|
||||
messages = []
|
||||
if isinstance(json_payload, dict):
|
||||
maybe_messages = json_payload.get("messages")
|
||||
if isinstance(maybe_messages, list):
|
||||
messages = maybe_messages
|
||||
messages = json_payload.get("messages")
|
||||
|
||||
if messages:
|
||||
console.rule(f"Messages ({len(messages)})")
|
||||
console.rule(f"Messages ({len(messages)})")
|
||||
|
||||
for idx, msg in enumerate(messages, start=1):
|
||||
role = ""
|
||||
content_val: Any = ""
|
||||
name = None
|
||||
if isinstance(msg, dict):
|
||||
role = str(msg.get("role", ""))
|
||||
content_val = msg.get("content", "")
|
||||
name = msg.get("name")
|
||||
tool_call_id = msg.get("tool_call_id")
|
||||
title = (
|
||||
f"Message {idx}: {role}"
|
||||
if not name
|
||||
else f"Message {idx}: {role} ({name}) - {tool_call_id}"
|
||||
for idx, msg in enumerate(messages, start=1):
|
||||
role = str(msg.get("role", ""))
|
||||
content_val = msg.get("content", "")
|
||||
name = msg.get("name")
|
||||
tool_call_id = msg.get("tool_call_id")
|
||||
title = (
|
||||
f"Message {idx}: {role}"
|
||||
if not name
|
||||
else f"Message {idx}: {role} ({name}) - {tool_call_id}"
|
||||
)
|
||||
console.rule(title)
|
||||
console.print(
|
||||
Padding(
|
||||
Markdown(
|
||||
content_val.replace("<", "\n`<")
|
||||
.replace(">", ">`\n")
|
||||
.replace(">`\n\n\n`<", ">`\n\n`<")
|
||||
),
|
||||
(1, 0),
|
||||
)
|
||||
console.rule(title)
|
||||
)
|
||||
tool_calls = msg.get("tool_calls", [])
|
||||
for tool_call in tool_calls:
|
||||
function = tool_call.get("function", {})
|
||||
arguments = function.get("arguments")
|
||||
console.print(
|
||||
Padding(
|
||||
Markdown(
|
||||
content_val.replace("<", "\n`<")
|
||||
.replace(">", ">`\n")
|
||||
.replace(">`\n\n\n`<", ">`\n\n`<")
|
||||
),
|
||||
(1, 0),
|
||||
Panel.fit(f"Tool call [italic]{tool_call.get('id')}[italic]"),
|
||||
(0, 4),
|
||||
)
|
||||
)
|
||||
tool_calls = msg.get("tool_calls", [])
|
||||
for tool_call in tool_calls:
|
||||
function = tool_call.get("function", {})
|
||||
arguments = function.get("arguments")
|
||||
console.print(
|
||||
Padding(
|
||||
Panel.fit(f"Tool call [italic]{tool_call.get('id')}[italic]"),
|
||||
(0, 4),
|
||||
)
|
||||
console.print(
|
||||
Padding(
|
||||
f"[bold][magenta]{function.get('name')}[/magenta] ([/bold]",
|
||||
(0, 4),
|
||||
)
|
||||
console.print(
|
||||
Padding(
|
||||
f"[bold][magenta]{function.get('name')}[/magenta] ([/bold]",
|
||||
(0, 4),
|
||||
)
|
||||
)
|
||||
if arguments:
|
||||
try:
|
||||
console.print(Padding(JSON(arguments), (0, 8)))
|
||||
except json.JSONDecodeError:
|
||||
console.print("[red]Invalid JSON generated by the model:[/red]")
|
||||
print(arguments)
|
||||
console.print(Padding("[bold])[/bold]", (0, 4)))
|
||||
if tool_calls:
|
||||
console.print()
|
||||
)
|
||||
try:
|
||||
console.print(Padding(JSON(arguments), (0, 8)))
|
||||
except json.JSONDecodeError:
|
||||
console.print("[red]Invalid JSON generated by the model:[/red]")
|
||||
print(arguments)
|
||||
console.print(Padding("[bold])[/bold]", (0, 4)))
|
||||
if tool_calls:
|
||||
console.print()
|
||||
|
||||
return request_id
|
||||
|
||||
|
||||
Reference in New Issue
Block a user