MCPcopy
hub / github.com/pathwaycom/pathway / _prep_message_log

Function _prep_message_log

python/pathway/xpacks/llm/llms.py:66–92  ·  view source on GitHub ↗

Prepare logs from OpenAI messages. If `verbose` is `True`, shorten OpenAI chat logs by redacting images. Otherwise, truncate the logs. Returns: str: Prepared log.

(messages: list[dict], verbose: bool)

Source from the content-addressed store, hash-verified

64
65
66def _prep_message_log(messages: list[dict], verbose: bool) -> str:
67 """
68 Prepare logs from OpenAI messages.
69
70 If `verbose` is `True`, shorten OpenAI chat logs by redacting images.
71 Otherwise, truncate the logs.
72
73 Returns:
74 str: Prepared log.
75 """
76 if verbose:
77 log_messages = copy.deepcopy(messages)
78 for message in log_messages:
79 msg_content = message["content"]
80 if isinstance(msg_content, list):
81 for c_msg in msg_content:
82 if c_msg.get("type", "text") == "image_url":
83 img_bytes = c_msg["image_url"]["url"]
84 c_msg["image_url"]["url"] = (
85 img_bytes[: img_bytes.index(",") + 25]
86 + "...REDACTED_B64_IMAGE"
87 )
88 logs = str(log_messages)
89 else:
90 str_msg = str(messages)
91 logs = str_msg[:100] + "..."
92 return logs
93
94
95class OpenAIChat(BaseChat):

Callers 4

__wrapped__Method · 0.90
__wrapped__Method · 0.85
__wrapped__Method · 0.85
__wrapped__Method · 0.85

Calls 2

indexMethod · 0.80
getMethod · 0.45

Tested by

no test coverage detected