|
14 | 14 | ) |
15 | 15 | from memos.types.openai_chat_completion_types import ChatCompletionToolMessageParam |
16 | 16 |
|
17 | | -from .base import BaseMessageParser |
| 17 | +from .base import BaseMessageParser, _add_lang_to_source |
| 18 | +from .utils import detect_lang |
18 | 19 |
|
19 | 20 |
|
20 | 21 | logger = get_logger(__name__) |
@@ -52,78 +53,92 @@ def create_source( |
52 | 53 | sources = [] |
53 | 54 |
|
54 | 55 | if isinstance(raw_content, list): |
55 | | - # Multimodal: create one SourceMessage per part |
| 56 | + text_contents = [] |
56 | 57 | for part in raw_content: |
57 | 58 | if isinstance(part, dict): |
58 | 59 | part_type = part.get("type", "") |
59 | 60 | if part_type == "text": |
60 | | - sources.append( |
61 | | - SourceMessage( |
62 | | - type="text", |
63 | | - role=role, |
64 | | - chat_time=chat_time, |
65 | | - message_id=message_id, |
66 | | - content=part.get("text", ""), |
67 | | - tool_call_id=tool_call_id, |
68 | | - ) |
| 61 | + text_contents.append(part.get("text", "")) |
| 62 | + |
| 63 | + # Detect overall language from all text content |
| 64 | + overall_lang = "en" |
| 65 | + if text_contents: |
| 66 | + combined_text = " ".join(text_contents) |
| 67 | + overall_lang = detect_lang(combined_text) |
| 68 | + |
| 69 | + # Create one SourceMessage per part, all with the same detected language |
| 70 | + for part in raw_content: |
| 71 | + if isinstance(part, dict): |
| 72 | + part_type = part.get("type", "") |
| 73 | + if part_type == "text": |
| 74 | + text_content = part.get("text", "") |
| 75 | + source = SourceMessage( |
| 76 | + type="text", |
| 77 | + role=role, |
| 78 | + chat_time=chat_time, |
| 79 | + message_id=message_id, |
| 80 | + content=text_content, |
| 81 | + tool_call_id=tool_call_id, |
69 | 82 | ) |
| 83 | + source.lang = overall_lang |
| 84 | + sources.append(source) |
70 | 85 | elif part_type == "file": |
71 | 86 | file_info = part.get("file", {}) |
72 | | - sources.append( |
73 | | - SourceMessage( |
74 | | - type="file", |
75 | | - role=role, |
76 | | - chat_time=chat_time, |
77 | | - message_id=message_id, |
78 | | - content=file_info.get("file_data", ""), |
79 | | - filename=file_info.get("filename", ""), |
80 | | - file_id=file_info.get("file_id", ""), |
81 | | - tool_call_id=tool_call_id, |
82 | | - file_info=file_info, |
83 | | - ) |
| 87 | + file_content = file_info.get("file_data", "") |
| 88 | + source = SourceMessage( |
| 89 | + type="file", |
| 90 | + role=role, |
| 91 | + chat_time=chat_time, |
| 92 | + message_id=message_id, |
| 93 | + content=file_content, |
| 94 | + filename=file_info.get("filename", ""), |
| 95 | + file_id=file_info.get("file_id", ""), |
| 96 | + tool_call_id=tool_call_id, |
| 97 | + file_info=file_info, |
84 | 98 | ) |
| 99 | + source.lang = overall_lang |
| 100 | + sources.append(source) |
85 | 101 | elif part_type == "image_url": |
86 | 102 | file_info = part.get("image_url", {}) |
87 | | - sources.append( |
88 | | - SourceMessage( |
89 | | - type="image_url", |
90 | | - role=role, |
91 | | - chat_time=chat_time, |
92 | | - message_id=message_id, |
93 | | - content=file_info.get("url", ""), |
94 | | - detail=file_info.get("detail", "auto"), |
95 | | - tool_call_id=tool_call_id, |
96 | | - ) |
| 103 | + source = SourceMessage( |
| 104 | + type="image_url", |
| 105 | + role=role, |
| 106 | + chat_time=chat_time, |
| 107 | + message_id=message_id, |
| 108 | + content=file_info.get("url", ""), |
| 109 | + detail=file_info.get("detail", "auto"), |
| 110 | + tool_call_id=tool_call_id, |
97 | 111 | ) |
| 112 | + source.lang = overall_lang |
| 113 | + sources.append(source) |
98 | 114 | elif part_type == "input_audio": |
99 | 115 | file_info = part.get("input_audio", {}) |
100 | | - sources.append( |
101 | | - SourceMessage( |
102 | | - type="input_audio", |
103 | | - role=role, |
104 | | - chat_time=chat_time, |
105 | | - message_id=message_id, |
106 | | - content=file_info.get("data", ""), |
107 | | - format=file_info.get("format", "wav"), |
108 | | - tool_call_id=tool_call_id, |
109 | | - ) |
| 116 | + source = SourceMessage( |
| 117 | + type="input_audio", |
| 118 | + role=role, |
| 119 | + chat_time=chat_time, |
| 120 | + message_id=message_id, |
| 121 | + content=file_info.get("data", ""), |
| 122 | + format=file_info.get("format", "wav"), |
| 123 | + tool_call_id=tool_call_id, |
110 | 124 | ) |
| 125 | + source.lang = overall_lang |
| 126 | + sources.append(source) |
111 | 127 | else: |
112 | 128 | logger.warning(f"[ToolParser] Unsupported part type: {part_type}") |
113 | 129 | continue |
114 | 130 | else: |
115 | 131 | # Simple string content message: single SourceMessage |
116 | 132 | if raw_content: |
117 | | - sources.append( |
118 | | - SourceMessage( |
119 | | - type="chat", |
120 | | - role=role, |
121 | | - chat_time=chat_time, |
122 | | - message_id=message_id, |
123 | | - content=raw_content, |
124 | | - tool_call_id=tool_call_id, |
125 | | - ) |
| 133 | + source = SourceMessage( |
| 134 | + type="chat", |
| 135 | + role=role, |
| 136 | + chat_time=chat_time, |
| 137 | + message_id=message_id, |
| 138 | + content=raw_content, |
| 139 | + tool_call_id=tool_call_id, |
126 | 140 | ) |
| 141 | + sources.append(_add_lang_to_source(source, raw_content)) |
127 | 142 |
|
128 | 143 | return sources |
129 | 144 |
|
|
0 commit comments