Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -96,6 +96,8 @@ from langchain_core.runnables import RunnableLambda
|
|
| 96 |
from langchain.chains import LLMChain
|
| 97 |
from langchain.chains.combine_documents.stuff import StuffDocumentsChain
|
| 98 |
from sentence_transformers import SentenceTransformer
|
|
|
|
|
|
|
| 99 |
|
| 100 |
|
| 101 |
|
|
@@ -1252,6 +1254,28 @@ INTENT_LABELS = {
|
|
| 1252 |
"General": ["who are you", "tell me something", "what can you do", "fun fact"],
|
| 1253 |
}
|
| 1254 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1255 |
def detect_intent_embedding(query, file_names=[]):
|
| 1256 |
query_emb = embedding_model.encode(query, normalize_embeddings=True)
|
| 1257 |
best_label = None
|
|
@@ -1470,6 +1494,10 @@ def langgraph_tab6_main(query: str, file=None):
|
|
| 1470 |
state = {"query": query, "file_names": file_names}
|
| 1471 |
if retriever is not None:
|
| 1472 |
state["retriever"] = retriever
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1473 |
result = graph.invoke(state)
|
| 1474 |
if "answer" in result:
|
| 1475 |
return result["answer"]
|
|
|
|
| 96 |
from langchain.chains import LLMChain
|
| 97 |
from langchain.chains.combine_documents.stuff import StuffDocumentsChain
|
| 98 |
from sentence_transformers import SentenceTransformer
|
| 99 |
+
# === AutoGen for multi-intent collaboration ===
|
| 100 |
+
from autogen import AssistantAgent, UserProxyAgent, GroupChat, GroupChatManager
|
| 101 |
|
| 102 |
|
| 103 |
|
|
|
|
| 1254 |
"General": ["who are you", "tell me something", "what can you do", "fun fact"],
|
| 1255 |
}
|
| 1256 |
|
| 1257 |
+
# === AutoGen 多代理人協作邏輯 ===
|
| 1258 |
+
def autogen_multi_intent_agent(query: str, docs: list) -> str:
|
| 1259 |
+
try:
|
| 1260 |
+
context = "\n\n".join(d.page_content for d in docs[:10])
|
| 1261 |
+
system_prompt = f"""You are a helpful assistant. Your task is to answer the following user question using two strategies:
|
| 1262 |
+
1. Use context-based question answering based on the document below.
|
| 1263 |
+
2. Also generate a short summary of the document, in case that helps interpret the question.
|
| 1264 |
+
|
| 1265 |
+
Document Context:
|
| 1266 |
+
{context}
|
| 1267 |
+
"""
|
| 1268 |
+
user_proxy = UserProxyAgent(name="User", is_termination_msg=lambda x: True, human_input_mode="NEVER")
|
| 1269 |
+
qa_agent = AssistantAgent(name="QA_Agent", system_message="You are great at document-based QA.")
|
| 1270 |
+
sum_agent = AssistantAgent(name="Summary_Agent", system_message="You are great at summarising text.")
|
| 1271 |
+
group_chat = GroupChat(agents=[user_proxy, qa_agent, sum_agent], messages=[], max_round=3)
|
| 1272 |
+
manager = GroupChatManager(groupchat=group_chat, llm_config={"config_list": [{"model": "gpt-4o", "api_key": openai_api_key}]})
|
| 1273 |
+
|
| 1274 |
+
user_proxy.initiate_chat(manager, message=query)
|
| 1275 |
+
return user_proxy.last_message()["content"]
|
| 1276 |
+
except Exception as e:
|
| 1277 |
+
return f"[AutoGen Error] {e}"
|
| 1278 |
+
|
| 1279 |
def detect_intent_embedding(query, file_names=[]):
|
| 1280 |
query_emb = embedding_model.encode(query, normalize_embeddings=True)
|
| 1281 |
best_label = None
|
|
|
|
| 1494 |
state = {"query": query, "file_names": file_names}
|
| 1495 |
if retriever is not None:
|
| 1496 |
state["retriever"] = retriever
|
| 1497 |
+
# 判斷是否為模糊查詢(summarise + QA)
|
| 1498 |
+
if any(kw in query.lower() for kw in ["same project", "same paper", "are they the same", "is this document", "summarise and answer", "is it related to"]):
|
| 1499 |
+
autogen_response = autogen_multi_intent_agent(query, all_docs)
|
| 1500 |
+
return autogen_response
|
| 1501 |
result = graph.invoke(state)
|
| 1502 |
if "answer" in result:
|
| 1503 |
return result["answer"]
|