Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1296,102 +1296,131 @@ def parse_query(query: str) -> dict:
|
|
| 1296 |
|
| 1297 |
def autogen_multi_document_analysis(query: str, docs: list, file_names: list) -> str:
|
| 1298 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1299 |
# 準備文件上下文
|
| 1300 |
context = "\n\n".join(
|
| 1301 |
f"Document {name}:\n{doc[:2000]}..."
|
| 1302 |
for name, doc in zip(file_names, docs)
|
| 1303 |
)
|
| 1304 |
|
| 1305 |
-
# 配置 LLM
|
| 1306 |
config_list = [{
|
| 1307 |
"model": "gpt-4",
|
| 1308 |
"api_key": openai_api_key
|
| 1309 |
}]
|
| 1310 |
|
| 1311 |
-
#
|
| 1312 |
llm_config = {
|
| 1313 |
"config_list": config_list,
|
| 1314 |
"temperature": 0
|
| 1315 |
-
# 移除所有緩存相關參數
|
| 1316 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1317 |
|
| 1318 |
-
# 定義用戶代理
|
| 1319 |
-
user_proxy = UserProxyAgent(
|
| 1320 |
-
name="User",
|
| 1321 |
-
system_message="A user seeking information from multiple documents.",
|
| 1322 |
-
human_input_mode="NEVER",
|
| 1323 |
-
code_execution_config={"use_docker": False},
|
| 1324 |
-
llm_config=llm_config
|
| 1325 |
-
)
|
| 1326 |
-
|
| 1327 |
-
# 定義文檔分析專家
|
| 1328 |
-
doc_analyzer = AssistantAgent(
|
| 1329 |
-
name="DocumentAnalyzer",
|
| 1330 |
-
system_message="""You are an expert at analyzing and comparing documents. Focus on:
|
| 1331 |
-
1. Key similarities and differences
|
| 1332 |
-
2. Main themes and topics
|
| 1333 |
-
3. Relationships between documents
|
| 1334 |
-
4. Evidence-based analysis""",
|
| 1335 |
-
llm_config=llm_config
|
| 1336 |
-
)
|
| 1337 |
-
|
| 1338 |
-
# 定義問答專家
|
| 1339 |
-
qa_expert = AssistantAgent(
|
| 1340 |
-
name="QAExpert",
|
| 1341 |
-
system_message="""You are an expert at extracting specific information. Focus on:
|
| 1342 |
-
1. Finding relevant details
|
| 1343 |
-
2. Answering specific questions
|
| 1344 |
-
3. Cross-referencing information
|
| 1345 |
-
4. Providing evidence""",
|
| 1346 |
-
llm_config=llm_config
|
| 1347 |
-
)
|
| 1348 |
-
|
| 1349 |
-
# 定義總結專家
|
| 1350 |
-
summarizer = AssistantAgent(
|
| 1351 |
-
name="Summarizer",
|
| 1352 |
-
system_message="""You are an expert at summarizing content. Focus on:
|
| 1353 |
-
1. Key points and findings
|
| 1354 |
-
2. Important relationships
|
| 1355 |
-
3. Critical conclusions
|
| 1356 |
-
4. Comprehensive overview""",
|
| 1357 |
-
llm_config=llm_config
|
| 1358 |
-
)
|
| 1359 |
|
| 1360 |
-
# 創建群組聊天
|
| 1361 |
-
groupchat = GroupChat(
|
| 1362 |
-
agents=[user_proxy, doc_analyzer, qa_expert, summarizer],
|
| 1363 |
-
messages=[],
|
| 1364 |
-
max_round=5
|
| 1365 |
-
)
|
| 1366 |
|
| 1367 |
-
|
| 1368 |
-
|
| 1369 |
-
|
| 1370 |
-
|
| 1371 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1372 |
|
| 1373 |
-
|
| 1374 |
-
|
| 1375 |
-
|
| 1376 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1377 |
|
| 1378 |
-
|
| 1379 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1380 |
|
| 1381 |
-
|
| 1382 |
-
|
| 1383 |
-
|
| 1384 |
-
|
| 1385 |
-
|
| 1386 |
-
|
| 1387 |
-
6. If looking for specific content, search thoroughly
|
| 1388 |
-
7. If analyzing relationships, consider all connections
|
| 1389 |
|
| 1390 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1391 |
|
| 1392 |
-
|
| 1393 |
-
|
| 1394 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1395 |
|
| 1396 |
except Exception as e:
|
| 1397 |
print(f"ERROR in AutoGen processing: {str(e)}")
|
|
|
|
| 1296 |
|
| 1297 |
def autogen_multi_document_analysis(query: str, docs: list, file_names: list) -> str:
|
| 1298 |
try:
|
| 1299 |
+
# 建立絕對路徑的暫存目錄,並確保它存在
|
| 1300 |
+
import tempfile
|
| 1301 |
+
import os
|
| 1302 |
+
|
| 1303 |
+
# 建立一個臨時工作目錄
|
| 1304 |
+
temp_dir = tempfile.mkdtemp(dir="/tmp")
|
| 1305 |
+
os.environ["OPENAI_CACHE_DIR"] = temp_dir
|
| 1306 |
+
|
| 1307 |
+
# 設置 AutoGen 的工作目錄
|
| 1308 |
+
os.environ["AUTOGEN_CACHE_PATH"] = temp_dir
|
| 1309 |
+
os.environ["AUTOGEN_CACHEDIR"] = temp_dir
|
| 1310 |
+
os.environ["OPENAI_CACHE_PATH"] = temp_dir
|
| 1311 |
+
|
| 1312 |
+
# 強制 AutoGen 使用我們的臨時目錄而不是 ./.cache
|
| 1313 |
+
import autogen
|
| 1314 |
+
if hasattr(autogen, "set_cache_dir"):
|
| 1315 |
+
autogen.set_cache_dir(temp_dir)
|
| 1316 |
+
|
| 1317 |
# 準備文件上下文
|
| 1318 |
context = "\n\n".join(
|
| 1319 |
f"Document {name}:\n{doc[:2000]}..."
|
| 1320 |
for name, doc in zip(file_names, docs)
|
| 1321 |
)
|
| 1322 |
|
| 1323 |
+
# 配置 LLM
|
| 1324 |
config_list = [{
|
| 1325 |
"model": "gpt-4",
|
| 1326 |
"api_key": openai_api_key
|
| 1327 |
}]
|
| 1328 |
|
| 1329 |
+
# 基礎配置 - 不包含任何緩存相關參數
|
| 1330 |
llm_config = {
|
| 1331 |
"config_list": config_list,
|
| 1332 |
"temperature": 0
|
|
|
|
| 1333 |
}
|
| 1334 |
+
|
| 1335 |
+
# 在進行 AutoGen 處理前,切換到臨時目錄
|
| 1336 |
+
original_dir = os.getcwd()
|
| 1337 |
+
os.chdir(temp_dir)
|
| 1338 |
+
|
| 1339 |
+
try:
|
| 1340 |
+
# 以下是您的 AutoGen 處理代碼
|
| 1341 |
+
user_proxy = UserProxyAgent(
|
| 1342 |
+
name="User",
|
| 1343 |
+
system_message="A user seeking information from multiple documents.",
|
| 1344 |
+
human_input_mode="NEVER",
|
| 1345 |
+
code_execution_config={"use_docker": False},
|
| 1346 |
+
llm_config=llm_config
|
| 1347 |
+
)
|
| 1348 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1349 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1350 |
|
| 1351 |
+
# 定義文檔分析專家
|
| 1352 |
+
doc_analyzer = AssistantAgent(
|
| 1353 |
+
name="DocumentAnalyzer",
|
| 1354 |
+
system_message="""You are an expert at analyzing and comparing documents. Focus on:
|
| 1355 |
+
1. Key similarities and differences
|
| 1356 |
+
2. Main themes and topics
|
| 1357 |
+
3. Relationships between documents
|
| 1358 |
+
4. Evidence-based analysis""",
|
| 1359 |
+
llm_config=llm_config
|
| 1360 |
+
)
|
| 1361 |
|
| 1362 |
+
# 定義問答專家
|
| 1363 |
+
qa_expert = AssistantAgent(
|
| 1364 |
+
name="QAExpert",
|
| 1365 |
+
system_message="""You are an expert at extracting specific information. Focus on:
|
| 1366 |
+
1. Finding relevant details
|
| 1367 |
+
2. Answering specific questions
|
| 1368 |
+
3. Cross-referencing information
|
| 1369 |
+
4. Providing evidence""",
|
| 1370 |
+
llm_config=llm_config
|
| 1371 |
+
)
|
| 1372 |
|
| 1373 |
+
# 定義總結專家
|
| 1374 |
+
summarizer = AssistantAgent(
|
| 1375 |
+
name="Summarizer",
|
| 1376 |
+
system_message="""You are an expert at summarizing content. Focus on:
|
| 1377 |
+
1. Key points and findings
|
| 1378 |
+
2. Important relationships
|
| 1379 |
+
3. Critical conclusions
|
| 1380 |
+
4. Comprehensive overview""",
|
| 1381 |
+
llm_config=llm_config
|
| 1382 |
+
)
|
| 1383 |
|
| 1384 |
+
# 創建群組聊天
|
| 1385 |
+
groupchat = GroupChat(
|
| 1386 |
+
agents=[user_proxy, doc_analyzer, qa_expert, summarizer],
|
| 1387 |
+
messages=[],
|
| 1388 |
+
max_round=5
|
| 1389 |
+
)
|
|
|
|
|
|
|
| 1390 |
|
| 1391 |
+
# 創建管理器
|
| 1392 |
+
manager = GroupChatManager(
|
| 1393 |
+
groupchat=groupchat,
|
| 1394 |
+
llm_config=llm_config
|
| 1395 |
+
)
|
| 1396 |
|
| 1397 |
+
# 準備任務提示
|
| 1398 |
+
task_prompt = f"""Analyze these documents and answer the query:
|
| 1399 |
+
|
| 1400 |
+
Query: {query}
|
| 1401 |
+
|
| 1402 |
+
Documents Context:
|
| 1403 |
+
{context}
|
| 1404 |
+
|
| 1405 |
+
Requirements:
|
| 1406 |
+
1. Provide a direct and clear answer
|
| 1407 |
+
2. Support all claims with evidence from the documents
|
| 1408 |
+
3. Consider relationships between all documents
|
| 1409 |
+
4. If comparing, analyze all relevant aspects
|
| 1410 |
+
5. If summarizing, cover all important points
|
| 1411 |
+
6. If looking for specific content, search thoroughly
|
| 1412 |
+
7. If analyzing relationships, consider all connections
|
| 1413 |
+
|
| 1414 |
+
Please provide a comprehensive and well-structured answer."""
|
| 1415 |
+
|
| 1416 |
+
# 執行群組討論
|
| 1417 |
+
user_proxy.initiate_chat(manager, message=task_prompt)
|
| 1418 |
+
return user_proxy.last_message()["content"]
|
| 1419 |
+
finally:
|
| 1420 |
+
# 完成後,切回原始目錄
|
| 1421 |
+
os.chdir(original_dir)
|
| 1422 |
+
|
| 1423 |
+
return result
|
| 1424 |
|
| 1425 |
except Exception as e:
|
| 1426 |
print(f"ERROR in AutoGen processing: {str(e)}")
|