unieai-mcp-accton-rfp 0.0.11__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,349 @@
1
+ import asyncio
2
+ import json
3
+ import logging
4
+ import os
5
+ import re
6
+ import tempfile
7
+ from typing import Any, Dict, Tuple, List, Optional
8
+ from datetime import datetime
9
+
10
+ import requests
11
+ from dotenv import load_dotenv
12
+ from fastmcp import FastMCP
13
+ from openpyxl import load_workbook
14
+ from openpyxl.worksheet.worksheet import Worksheet
15
+
16
+ # LangChain (1.x API)
17
+ from langchain_openai import ChatOpenAI
18
+ from langchain_core.messages import HumanMessage, SystemMessage
19
+
20
+ # ==============================
21
+ # 🎛 Environment & Logging
22
+ # ==============================
23
+
24
+ load_dotenv()
25
+
26
+ logging.basicConfig(level=logging.INFO)
27
+ logger = logging.getLogger("ExcelProcessor")
28
+
29
+ app = FastMCP("ExcelProcessor")
30
+ semaphore = asyncio.Semaphore(10)
31
+
32
+ # LLM 初始化 (LangChain 1.x)
33
+ llm = ChatOpenAI(
34
+ model=os.getenv("UNIEAI_MODEL"),
35
+ base_url=os.getenv("UNIEAI_API_URL"),
36
+ api_key=os.getenv("UNIEAI_API_KEY"),
37
+ temperature=0,
38
+ max_tokens=32768
39
+ )
40
+
41
+ # Appwrite ENV
42
+ APPWRITE_PROJECT_ID = os.getenv("APPWRITE_PROJECT_ID")
43
+ APPWRITE_API_KEY = os.getenv("APPWRITE_API_KEY")
44
+ APPWRITE_ENDPOINT = os.getenv("APPWRITE_ENDPOINT", "https://sgp.cloud.appwrite.io/v1")
45
+
46
+
47
+ # ==============================
48
+ # 🧩 Helper Functions
49
+ # ==============================
50
+
51
+ def _extract_json(text: str) -> Dict[str, Any]:
52
+ """擷取 JSON 區塊"""
53
+ match = re.search(r"\{[\s\S]*\}", text)
54
+ if match:
55
+ try:
56
+ return json.loads(match.group(0))
57
+ except Exception as e:
58
+ logger.warning(f"JSON 解析失敗: {e}")
59
+ return {"Result": "解析錯誤", "Reference": text.strip()}
60
+
61
+
62
+ def _parse_appwrite_url(url: str) -> Tuple[Optional[str], Optional[str]]:
63
+ pattern = r"/storage/buckets/([^/]+)/files/([^/]+)"
64
+ m = re.search(pattern, url)
65
+ if not m:
66
+ return None, None
67
+ return m.group(1), m.group(2)
68
+
69
+
70
+ def _generate_new_filename(original_name: str) -> str:
71
+ base, ext = os.path.splitext(original_name)
72
+ timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
73
+ return f"{base}_processed_{timestamp}{ext}"
74
+
75
+
76
+ # ==============================
77
+ # 🤖 LLM Logic(新增兩階段)
78
+ # ==============================
79
+
80
+ async def _call_llm_raw(prompt: str, user_message: str):
81
+ """返回 LLM 純文字內容"""
82
+ logger.info(f"🟢 _call_llm_raw : {prompt}, {user_message}")
83
+ try:
84
+ async with semaphore:
85
+ response = await llm.ainvoke([
86
+ SystemMessage(content=prompt),
87
+ HumanMessage(content=user_message)
88
+ ])
89
+ return (response.content or "").strip()
90
+ except Exception as e:
91
+ return f"LLM Error: {e}"
92
+
93
+
94
+ def _extract_result_json(text: str):
95
+ """解析第二階段 JSON"""
96
+ try:
97
+ return json.loads(re.search(r"\{[\s\S]*\}", text).group(0))
98
+ except:
99
+ return {"Result": "Error"}
100
+
101
+
102
+ # ==============================
103
+ # 📘 Prompt 建構(新:兩個 prompt)
104
+ # ==============================
105
+
106
+ def _build_reference_prompt() -> str:
107
+ return """
108
+ 你是一位嚴謹的產品經理助理,專門負責將內部產品規格(知識庫)與客戶的需求單(RFP)進行比對和符合性分析。
109
+
110
+ **任務指示:**
111
+ 1. 你將收到客戶的產品需求單 (RFP) 作為輸入。
112
+ 2. 你的知識庫已包含你公司產品的完整說明文件。
113
+ 3. 請仔細閱讀 RFP 中的每一條具體需求,並利用你的產品知識庫內容進行嚴格比對。
114
+
115
+ **比對規則:**
116
+ * **Conform (完全符合):** 公司的產品規格能**完整且無條件地**滿足 RFP 中的該項需求。
117
+ * **Half Conform (部分符合):** 公司的產品規格**只能滿足** RFP 中該項需求的**部分內容**,或者需要透過**變通、額外配置或未來規劃**才能滿足。
118
+ * **Not Conform (不符合):** 公司的產品規格**無法滿足** RFP 中的該項需求。
119
+
120
+ **輸出格式要求:**
121
+ 你必須以條列式清晰地輸出分析結果,**每一條結果必須包含**:
122
+ 1. RFP 中的**原始需求描述** (簡短摘錄或編號)。
123
+ 2. **符合程度** (只能是:Conform, Half Conform, Not Conform 三者之一)。
124
+ 3. **參考依據** (說明做出判斷的依據,需明確引用知識庫中**相關產品說明**的關鍵資訊或段落,例如:知識庫中「功能A」的描述支持此判斷)。
125
+
126
+ 請針對 RFP 中的每一條主要需求逐一進行分析。
127
+ """
128
+
129
+
130
+ def _build_result_prompt() -> str:
131
+ return """
132
+ 請依據以下 Reference 文本,判斷其符合性:
133
+ - Conform:完全符合
134
+ - Half Conform:部分符合
135
+ - Not Conform:不符合
136
+
137
+ 請僅輸出以下 JSON 格式:
138
+ {
139
+ "Result": "Conform / Half Conform / Not Conform"
140
+ }
141
+ """
142
+
143
+
144
+ def _build_user_message(a: str, b: str, c: str, d: str) -> str:
145
+ logger.info(f"🟢 _build_user_message : {a}, {b}, {c}, {d}")
146
+ return f"""
147
+ {a}, {b}, {c}, {d}
148
+ """
149
+
150
+
151
+ # ==============================
152
+ # 📊 Excel Processing Core
153
+ # ==============================
154
+
155
+ async def _process_excel_logic(url: str) -> Dict[str, Any]:
156
+ logger.info(f"🟢 開始處理 Excel:{url}")
157
+
158
+ # -------------------------
159
+ # Step 1: Download / Load
160
+ # -------------------------
161
+ source_type = ""
162
+ local_path = None
163
+ appwrite_info = (None, None)
164
+ bucket_id = None
165
+
166
+ if url.startswith("file:///"):
167
+ local_path = url.replace("file:///", "")
168
+ file_path = local_path
169
+ source_type = "local"
170
+
171
+ elif url.startswith("http"):
172
+ resp = requests.get(url)
173
+ resp.raise_for_status()
174
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".xlsx") as tmp:
175
+ tmp.write(resp.content)
176
+ file_path = tmp.name
177
+
178
+ bucket_id, file_id = _parse_appwrite_url(url)
179
+ if bucket_id:
180
+ source_type = "appwrite"
181
+ appwrite_info = (bucket_id, file_id)
182
+ else:
183
+ source_type = "remote_readonly"
184
+
185
+ else:
186
+ raise ValueError("❌ 不支援檔案來源")
187
+
188
+ # -------------------------
189
+ # Step 2: Open Excel
190
+ # -------------------------
191
+ wb = load_workbook(file_path)
192
+ ws = wb.active
193
+
194
+ header = {cell.value: idx for idx, cell in enumerate(ws[1], 1)}
195
+ for col in ["itemA", "itemB", "itemC", "itemD", "Result", "Reference"]:
196
+ if col not in header:
197
+ raise ValueError(f"❌ Excel 缺少欄位:{col}")
198
+
199
+ # -------------------------
200
+ # Step 3: Two-stage LLM (High Performance Version)
201
+ # -------------------------
202
+ rows_for_llm = []
203
+
204
+ for row in ws.iter_rows(min_row=2, values_only=False):
205
+ if any([cell.value for cell in row]):
206
+ rows_for_llm.append(row)
207
+
208
+ # Build input list for batch LLM calls
209
+ user_messages = []
210
+ for row in rows_for_llm:
211
+ a = row[header["itemA"] - 1].value or ""
212
+ b = row[header["itemB"] - 1].value or ""
213
+ c = row[header["itemC"] - 1].value or ""
214
+ d = row[header["itemD"] - 1].value or ""
215
+
216
+ user_messages.append(_build_user_message(str(a), str(b), str(c), str(d)))
217
+
218
+ reference_prompt = _build_reference_prompt()
219
+
220
+ # -------- Stage 1: Run all Reference LLM calls in parallel --------
221
+ ref_tasks = [
222
+ _call_llm_raw(reference_prompt, msg)
223
+ for msg in user_messages
224
+ ]
225
+
226
+ reference_results = await asyncio.gather(*ref_tasks)
227
+
228
+ # Write Reference to Excel
229
+ for row, ref_text in zip(rows_for_llm, reference_results):
230
+ r = row[0].row
231
+ ws.cell(r, header["Reference"], ref_text)
232
+
233
+ # -------- Stage 2: Run all Result LLM calls in parallel --------
234
+ result_prompt = _build_result_prompt()
235
+
236
+ result_tasks = [
237
+ _call_llm_raw(result_prompt, ref_text)
238
+ for ref_text in reference_results
239
+ ]
240
+
241
+ raw_result_outputs = await asyncio.gather(*result_tasks)
242
+
243
+ # Parse JSON + write Result to Excel
244
+ for row, raw_result in zip(rows_for_llm, raw_result_outputs):
245
+ r = row[0].row
246
+ parsed = _extract_result_json(raw_result)
247
+ ws.cell(r, header["Result"], parsed.get("Result", "Error"))
248
+
249
+
250
+ # -------------------------
251
+ # Step 4: Save local debug copy
252
+ # -------------------------
253
+ local_debug_dir = r"D:\TempExcelDebug"
254
+ os.makedirs(local_debug_dir, exist_ok=True)
255
+
256
+ local_debug_filename = _generate_new_filename("debug_output.xlsx")
257
+ local_debug_path = os.path.join(local_debug_dir, local_debug_filename)
258
+
259
+ wb.save(local_debug_path)
260
+ logger.info(f"📝 本機 debug 檔案已輸出:{local_debug_path}")
261
+
262
+ # -------------------------
263
+ # Step 5: Write back according to source
264
+ # -------------------------
265
+
266
+ # local
267
+ if source_type == "local":
268
+ wb.save(local_path)
269
+ return {
270
+ "status": "success",
271
+ "location_type": "local",
272
+ "output_path": local_path
273
+ }
274
+
275
+ # Appwrite
276
+ if source_type == "appwrite":
277
+ bucket_id, _ = appwrite_info
278
+
279
+ tmp_out_path = os.path.join(
280
+ tempfile.gettempdir(),
281
+ _generate_new_filename("upload.xlsx")
282
+ )
283
+ wb.save(tmp_out_path)
284
+
285
+ new_file_id = f"processed_{datetime.now().strftime('%Y%m%d_%H%M%S')}"
286
+ new_file_name = f"{new_file_id}.xlsx"
287
+
288
+ upload_url = f"{APPWRITE_ENDPOINT}/storage/buckets/{bucket_id}/files"
289
+
290
+ headers = {
291
+ "X-Appwrite-Project": APPWRITE_PROJECT_ID,
292
+ "X-Appwrite-Key": APPWRITE_API_KEY,
293
+ }
294
+
295
+ files = {
296
+ "file": (
297
+ new_file_name,
298
+ open(tmp_out_path, "rb"),
299
+ "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
300
+ )
301
+ }
302
+
303
+ data = { "fileId": new_file_id }
304
+
305
+ resp = requests.post(upload_url, headers=headers, files=files, data=data)
306
+ resp.raise_for_status()
307
+
308
+ return {
309
+ "status": "success",
310
+ "location_type": "appwrite_new_file",
311
+ "file_id": new_file_id,
312
+ "file_name": new_file_name,
313
+ "upload_response": resp.json(),
314
+ "download_url": f"{APPWRITE_ENDPOINT}/storage/buckets/{bucket_id}/files/{new_file_id}/view?project={APPWRITE_PROJECT_ID}"
315
+ }
316
+
317
+ # remote (can't write back)
318
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".xlsx") as tmp_out:
319
+ wb.save(tmp_out.name)
320
+ fallback = tmp_out.name
321
+
322
+ return {
323
+ "status": "success",
324
+ "location_type": "remote_readonly",
325
+ "output_path": fallback,
326
+ "message": "無法寫回遠端,只能輸出本機暫存檔"
327
+ }
328
+
329
+
330
+ # ==============================
331
+ # 🔧 MCP Tool
332
+ # ==============================
333
+
334
+ @app.tool()
335
+ async def process_excel(url: str):
336
+ return await _process_excel_logic(url)
337
+
338
+
339
+ # ==============================
340
+ # 🚀 CLI Test
341
+ # ==============================
342
+
343
+ if __name__ == "__main__":
344
+ test_url = (
345
+ "https://sgp.cloud.appwrite.io/v1/storage/buckets/6904374b00056677a970/files/6937a7fb00180f83ab67/view?project=6901b22e0036150b66d3&mode=admin"
346
+ )
347
+ print("🚀 測試開始...")
348
+ result = asyncio.run(_process_excel_logic(test_url))
349
+ print(json.dumps(result, ensure_ascii=False, indent=2))