Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1107,7 +1107,40 @@ def multi_agent_with_langgraph(query: str, file=None) -> str:
|
|
| 1107 |
compiled_graph = graph.compile()
|
| 1108 |
result = graph.run(query)
|
| 1109 |
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1110 |
|
|
|
|
| 1111 |
|
| 1112 |
@traceable(name="Multi-Agent Chat")
|
| 1113 |
def multi_agent_chat_advanced(query: str, file=None) -> str:
|
|
@@ -1188,7 +1221,9 @@ def multi_agent_chat_advanced(query: str, file=None) -> str:
|
|
| 1188 |
# If using QA Chain is appropriate
|
| 1189 |
if use_file_chain:
|
| 1190 |
try:
|
| 1191 |
-
answer = session_qa_chain.run(query)
|
|
|
|
|
|
|
| 1192 |
|
| 1193 |
# ✅ DeepEval 評估僅在 Tab1 文件 QA 的情況下觸發
|
| 1194 |
if SHOW_EVAL:
|
|
@@ -1318,7 +1353,7 @@ Feel free to upload a document and ask related questions, or just type a questio
|
|
| 1318 |
demo = gr.TabbedInterface(
|
| 1319 |
interface_list=[
|
| 1320 |
gr.Interface(
|
| 1321 |
-
fn=
|
| 1322 |
inputs=[
|
| 1323 |
gr.Textbox(label="Enter your query"),
|
| 1324 |
gr.File(label="Upload file (CSV, PDF, TXT, DOCX)", file_count="single")
|
|
|
|
| 1107 |
compiled_graph = graph.compile()
|
| 1108 |
result = graph.run(query)
|
| 1109 |
return result
|
| 1110 |
+
from langgraph.graph import StateGraph
|
| 1111 |
+
from langchain_core.runnables import RunnableLambda
|
| 1112 |
+
from langchain_core.runnables import RunnableConfig
|
| 1113 |
+
|
| 1114 |
+
def build_langgraph_doc_qa_chain(llm, retriever, memory, prompt):
|
| 1115 |
+
def retrieve_step(state):
|
| 1116 |
+
docs = state['retriever'].get_relevant_documents(state['query'])
|
| 1117 |
+
return {"docs": docs, **state}
|
| 1118 |
+
|
| 1119 |
+
def answer_step(state):
|
| 1120 |
+
from langchain_core.prompts import PromptTemplate
|
| 1121 |
+
from langchain.chains.combine_documents.stuff import StuffDocumentsChain
|
| 1122 |
+
doc_prompt = PromptTemplate(input_variables=["page_content"], template="{page_content}")
|
| 1123 |
+
doc_chain = StuffDocumentsChain(llm=state['llm'], prompt=state['prompt'])
|
| 1124 |
+
answer = doc_chain.run(state['docs'])
|
| 1125 |
+
return {"answer": answer, **state}
|
| 1126 |
+
|
| 1127 |
+
builder = StateGraph()
|
| 1128 |
+
builder.add_node("Retrieve", retrieve_step)
|
| 1129 |
+
builder.add_node("Answer", answer_step)
|
| 1130 |
+
builder.set_entry_point("Retrieve")
|
| 1131 |
+
builder.add_edge("Retrieve", "Answer")
|
| 1132 |
+
builder.set_finish_point("Answer")
|
| 1133 |
+
compiled = builder.compile()
|
| 1134 |
+
|
| 1135 |
+
def run(query):
|
| 1136 |
+
return compiled.invoke({
|
| 1137 |
+
"query": query,
|
| 1138 |
+
"retriever": retriever,
|
| 1139 |
+
"llm": llm,
|
| 1140 |
+
"prompt": prompt
|
| 1141 |
+
})
|
| 1142 |
|
| 1143 |
+
return run
|
| 1144 |
|
| 1145 |
@traceable(name="Multi-Agent Chat")
|
| 1146 |
def multi_agent_chat_advanced(query: str, file=None) -> str:
|
|
|
|
| 1221 |
# If using QA Chain is appropriate
|
| 1222 |
if use_file_chain:
|
| 1223 |
try:
|
| 1224 |
+
#answer = session_qa_chain.run(query)
|
| 1225 |
+
session_graph_chain = build_langgraph_doc_qa_chain(llm_gpt4, session_retriever, memory, custom_prompt)
|
| 1226 |
+
answer = session_graph_chain(query)["answer"]
|
| 1227 |
|
| 1228 |
# ✅ DeepEval 評估僅在 Tab1 文件 QA 的情況下觸發
|
| 1229 |
if SHOW_EVAL:
|
|
|
|
| 1353 |
demo = gr.TabbedInterface(
|
| 1354 |
interface_list=[
|
| 1355 |
gr.Interface(
|
| 1356 |
+
fn=multi_agent_chat_advanced,
|
| 1357 |
inputs=[
|
| 1358 |
gr.Textbox(label="Enter your query"),
|
| 1359 |
gr.File(label="Upload file (CSV, PDF, TXT, DOCX)", file_count="single")
|