Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,6 +13,9 @@ from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
| 13 |
from transformers.models.llama.configuration_llama import LlamaConfig
|
| 14 |
from huggingface_hub import hf_hub_download
|
| 15 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
# Solve permission issues
|
| 18 |
os.environ["MPLCONFIGDIR"] = "/tmp/matplotlib"
|
|
@@ -29,7 +32,9 @@ os.environ["HF_HUB_DOWNLOAD_TIMEOUT"] = "60"
|
|
| 29 |
os.environ["LANGCHAIN_TRACING_V2"] = "true"
|
| 30 |
os.environ["LANGCHAIN_API_KEY"] = os.getenv("LANGCHAIN_API_KEY")
|
| 31 |
os.environ["LANGCHAIN_PROJECT"] = os.getenv("LANGCHAIN_PROJECT")
|
| 32 |
-
|
|
|
|
|
|
|
| 33 |
|
| 34 |
|
| 35 |
|
|
@@ -1285,13 +1290,6 @@ def parse_query(query: str) -> dict:
|
|
| 1285 |
|
| 1286 |
def autogen_multi_document_analysis(query: str, docs: list, file_names: list) -> str:
|
| 1287 |
try:
|
| 1288 |
-
import os
|
| 1289 |
-
import tempfile
|
| 1290 |
-
|
| 1291 |
-
# 創建臨時目錄作為緩存目錄
|
| 1292 |
-
cache_dir = tempfile.mkdtemp()
|
| 1293 |
-
os.environ['AUTOGEN_CACHE_DIR'] = cache_dir
|
| 1294 |
-
|
| 1295 |
# 準備文件上下文
|
| 1296 |
context = "\n\n".join(
|
| 1297 |
f"Document {name}:\n{doc[:2000]}..."
|
|
@@ -1304,70 +1302,76 @@ def autogen_multi_document_analysis(query: str, docs: list, file_names: list) ->
|
|
| 1304 |
"api_key": openai_api_key
|
| 1305 |
}]
|
| 1306 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1307 |
# 基礎配置
|
| 1308 |
llm_config = {
|
| 1309 |
"config_list": config_list,
|
| 1310 |
"temperature": 0,
|
| 1311 |
"request_timeout": 120,
|
| 1312 |
-
"
|
| 1313 |
-
"cache_dir": cache_dir # 使用臨時目錄作為緩存
|
| 1314 |
}
|
| 1315 |
|
| 1316 |
-
#
|
| 1317 |
-
|
| 1318 |
"use_docker": False,
|
| 1319 |
-
"
|
| 1320 |
"timeout": 60,
|
| 1321 |
-
"
|
| 1322 |
}
|
| 1323 |
|
| 1324 |
-
# 定義代理
|
| 1325 |
user_proxy = UserProxyAgent(
|
| 1326 |
name="User",
|
| 1327 |
system_message="A user seeking information from multiple documents.",
|
| 1328 |
human_input_mode="NEVER",
|
| 1329 |
-
code_execution_config=
|
|
|
|
| 1330 |
)
|
| 1331 |
|
| 1332 |
-
# 定義專家
|
| 1333 |
-
|
| 1334 |
-
|
| 1335 |
-
|
| 1336 |
-
|
| 1337 |
-
|
| 1338 |
-
|
| 1339 |
-
|
| 1340 |
-
|
| 1341 |
-
|
| 1342 |
-
|
| 1343 |
-
|
| 1344 |
-
|
| 1345 |
-
|
| 1346 |
-
|
| 1347 |
-
|
| 1348 |
-
|
| 1349 |
-
|
| 1350 |
-
|
| 1351 |
-
|
| 1352 |
-
|
| 1353 |
-
|
| 1354 |
-
|
| 1355 |
-
|
| 1356 |
-
|
| 1357 |
-
|
| 1358 |
-
|
| 1359 |
-
|
| 1360 |
-
|
| 1361 |
-
|
| 1362 |
-
|
|
|
|
| 1363 |
|
| 1364 |
# 創建群組聊天
|
| 1365 |
groupchat = GroupChat(
|
| 1366 |
-
agents=[user_proxy
|
| 1367 |
messages=[],
|
| 1368 |
max_round=5
|
| 1369 |
)
|
| 1370 |
|
|
|
|
| 1371 |
manager = GroupChatManager(
|
| 1372 |
groupchat=groupchat,
|
| 1373 |
llm_config=llm_config
|
|
@@ -1382,31 +1386,27 @@ def autogen_multi_document_analysis(query: str, docs: list, file_names: list) ->
|
|
| 1382 |
{context}
|
| 1383 |
|
| 1384 |
Requirements:
|
| 1385 |
-
1. Provide direct
|
| 1386 |
-
2. Support
|
| 1387 |
3. Consider relationships between all documents
|
| 1388 |
-
4. If comparing, analyze all relevant
|
| 1389 |
-
5. If summarizing,
|
| 1390 |
-
6. If looking for specific content, search
|
| 1391 |
-
7. If analyzing
|
| 1392 |
|
| 1393 |
-
Please
|
| 1394 |
|
| 1395 |
# 執行群組討論
|
| 1396 |
user_proxy.initiate_chat(manager, message=task_prompt)
|
| 1397 |
result = user_proxy.last_message()["content"]
|
| 1398 |
-
|
| 1399 |
-
# 清理臨時目錄
|
| 1400 |
-
import shutil
|
| 1401 |
-
try:
|
| 1402 |
-
shutil.rmtree(cache_dir)
|
| 1403 |
-
except:
|
| 1404 |
-
pass
|
| 1405 |
-
|
| 1406 |
return result
|
| 1407 |
|
| 1408 |
except Exception as e:
|
| 1409 |
print(f"ERROR in AutoGen processing: {str(e)}")
|
|
|
|
|
|
|
|
|
|
| 1410 |
return f"Error during multi-agent processing: {str(e)}"
|
| 1411 |
|
| 1412 |
|
|
|
|
| 13 |
from transformers.models.llama.configuration_llama import LlamaConfig
|
| 14 |
from huggingface_hub import hf_hub_download
|
| 15 |
import gradio as gr
|
| 16 |
+
from pathlib import Path
|
| 17 |
+
|
| 18 |
+
|
| 19 |
|
| 20 |
# Solve permission issues
|
| 21 |
os.environ["MPLCONFIGDIR"] = "/tmp/matplotlib"
|
|
|
|
| 32 |
os.environ["LANGCHAIN_TRACING_V2"] = "true"
|
| 33 |
os.environ["LANGCHAIN_API_KEY"] = os.getenv("LANGCHAIN_API_KEY")
|
| 34 |
os.environ["LANGCHAIN_PROJECT"] = os.getenv("LANGCHAIN_PROJECT")
|
| 35 |
+
TEMP_DIR = tempfile.mkdtemp()
|
| 36 |
+
os.environ['AUTOGEN_CACHE_DIR'] = TEMP_DIR
|
| 37 |
+
os.environ['AUTOGEN_USE_DOCKER'] = "False"
|
| 38 |
|
| 39 |
|
| 40 |
|
|
|
|
| 1290 |
|
| 1291 |
def autogen_multi_document_analysis(query: str, docs: list, file_names: list) -> str:
|
| 1292 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1293 |
# 準備文件上下文
|
| 1294 |
context = "\n\n".join(
|
| 1295 |
f"Document {name}:\n{doc[:2000]}..."
|
|
|
|
| 1302 |
"api_key": openai_api_key
|
| 1303 |
}]
|
| 1304 |
|
| 1305 |
+
# 創建臨時工作目錄
|
| 1306 |
+
work_dir = os.path.join(TEMP_DIR, 'workdir')
|
| 1307 |
+
os.makedirs(work_dir, exist_ok=True)
|
| 1308 |
+
|
| 1309 |
# 基礎配置
|
| 1310 |
llm_config = {
|
| 1311 |
"config_list": config_list,
|
| 1312 |
"temperature": 0,
|
| 1313 |
"request_timeout": 120,
|
| 1314 |
+
"cache_seed": None # 禁用緩存
|
|
|
|
| 1315 |
}
|
| 1316 |
|
| 1317 |
+
# 配置執行設置
|
| 1318 |
+
execution_config = {
|
| 1319 |
"use_docker": False,
|
| 1320 |
+
"work_dir": work_dir,
|
| 1321 |
"timeout": 60,
|
| 1322 |
+
"last_n_messages": 3
|
| 1323 |
}
|
| 1324 |
|
| 1325 |
+
# 定義用戶代理
|
| 1326 |
user_proxy = UserProxyAgent(
|
| 1327 |
name="User",
|
| 1328 |
system_message="A user seeking information from multiple documents.",
|
| 1329 |
human_input_mode="NEVER",
|
| 1330 |
+
code_execution_config=execution_config,
|
| 1331 |
+
llm_config=llm_config
|
| 1332 |
)
|
| 1333 |
|
| 1334 |
+
# 定義文檔分析專家
|
| 1335 |
+
doc_analyzer = AssistantAgent(
|
| 1336 |
+
name="DocumentAnalyzer",
|
| 1337 |
+
system_message="""You are an expert at analyzing and comparing documents. Focus on:
|
| 1338 |
+
1. Key similarities and differences
|
| 1339 |
+
2. Main themes and topics
|
| 1340 |
+
3. Relationships between documents
|
| 1341 |
+
4. Evidence-based analysis""",
|
| 1342 |
+
llm_config=llm_config
|
| 1343 |
+
)
|
| 1344 |
+
|
| 1345 |
+
# 定義問答專家
|
| 1346 |
+
qa_expert = AssistantAgent(
|
| 1347 |
+
name="QAExpert",
|
| 1348 |
+
system_message="""You are an expert at extracting specific information. Focus on:
|
| 1349 |
+
1. Finding relevant details
|
| 1350 |
+
2. Answering specific questions
|
| 1351 |
+
3. Cross-referencing information
|
| 1352 |
+
4. Providing evidence""",
|
| 1353 |
+
llm_config=llm_config
|
| 1354 |
+
)
|
| 1355 |
+
|
| 1356 |
+
# 定義總結專家
|
| 1357 |
+
summarizer = AssistantAgent(
|
| 1358 |
+
name="Summarizer",
|
| 1359 |
+
system_message="""You are an expert at summarizing content. Focus on:
|
| 1360 |
+
1. Key points and findings
|
| 1361 |
+
2. Important relationships
|
| 1362 |
+
3. Critical conclusions
|
| 1363 |
+
4. Comprehensive overview""",
|
| 1364 |
+
llm_config=llm_config
|
| 1365 |
+
)
|
| 1366 |
|
| 1367 |
# 創建群組聊天
|
| 1368 |
groupchat = GroupChat(
|
| 1369 |
+
agents=[user_proxy, doc_analyzer, qa_expert, summarizer],
|
| 1370 |
messages=[],
|
| 1371 |
max_round=5
|
| 1372 |
)
|
| 1373 |
|
| 1374 |
+
# 創建管理器
|
| 1375 |
manager = GroupChatManager(
|
| 1376 |
groupchat=groupchat,
|
| 1377 |
llm_config=llm_config
|
|
|
|
| 1386 |
{context}
|
| 1387 |
|
| 1388 |
Requirements:
|
| 1389 |
+
1. Provide a direct and clear answer
|
| 1390 |
+
2. Support all claims with evidence from the documents
|
| 1391 |
3. Consider relationships between all documents
|
| 1392 |
+
4. If comparing, analyze all relevant aspects
|
| 1393 |
+
5. If summarizing, cover all important points
|
| 1394 |
+
6. If looking for specific content, search thoroughly
|
| 1395 |
+
7. If analyzing relationships, consider all connections
|
| 1396 |
|
| 1397 |
+
Please provide a comprehensive and well-structured answer."""
|
| 1398 |
|
| 1399 |
# 執行群組討論
|
| 1400 |
user_proxy.initiate_chat(manager, message=task_prompt)
|
| 1401 |
result = user_proxy.last_message()["content"]
|
| 1402 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1403 |
return result
|
| 1404 |
|
| 1405 |
except Exception as e:
|
| 1406 |
print(f"ERROR in AutoGen processing: {str(e)}")
|
| 1407 |
+
# 提供更有用的錯誤信息
|
| 1408 |
+
if "Cache directory" in str(e):
|
| 1409 |
+
return "Error: Unable to process the documents due to a system configuration issue. The analysis will be performed without caching."
|
| 1410 |
return f"Error during multi-agent processing: {str(e)}"
|
| 1411 |
|
| 1412 |
|