Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -290,8 +290,6 @@ extracted_info = extracted_info or st.session_state.get("last_extracted_info", N
|
|
| 290 |
|
| 291 |
# --------- Classic ReAct AGENT ---------
|
| 292 |
def po_match_tool_func(input_text):
|
| 293 |
-
# This tool receives a string "query" (not structured context!) so it must access globals or session
|
| 294 |
-
# We'll use the extracted_info and po_df from session
|
| 295 |
invoice = st.session_state.get("last_extracted_info")
|
| 296 |
po_df = st.session_state.get("po_df")
|
| 297 |
|
|
@@ -320,13 +318,11 @@ def po_match_tool_func(input_text):
|
|
| 320 |
return f"PO matched: {matched_po.to_dict()}"
|
| 321 |
return "No matching PO found."
|
| 322 |
|
| 323 |
-
# Save PO df to session for tool access
|
| 324 |
if po_df is not None:
|
| 325 |
st.session_state["po_df"] = po_df
|
| 326 |
|
| 327 |
if extracted_info is not None and po_df is not None:
|
| 328 |
if st.button("Make a decision (AI Agent)"):
|
| 329 |
-
# Define the tool for the agent (takes input string, outputs string)
|
| 330 |
tools = [
|
| 331 |
Tool(
|
| 332 |
name="po_match_tool",
|
|
@@ -334,7 +330,6 @@ if extracted_info is not None and po_df is not None:
|
|
| 334 |
description="Use this tool to check if the invoice matches any PO in the current PO list.",
|
| 335 |
)
|
| 336 |
]
|
| 337 |
-
# Use session state for the LLM agent
|
| 338 |
openai_api_key = os.getenv("OPENAI_API_KEY")
|
| 339 |
llm = ChatOpenAI(
|
| 340 |
openai_api_key=openai_api_key,
|
|
@@ -348,16 +343,22 @@ if extracted_info is not None and po_df is not None:
|
|
| 348 |
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
|
| 349 |
verbose=True,
|
| 350 |
)
|
| 351 |
-
# Build the prompt
|
| 352 |
prompt = (
|
| 353 |
f"Below is an extracted invoice in JSON and a list of active POs is loaded in the system. "
|
| 354 |
-
f"Use po_match_tool to check if the invoice matches
|
| 355 |
f"Step by step, reason whether the invoice matches an active PO and can be approved. "
|
| 356 |
f"If there is a match, state the matched PO, otherwise explain why not. "
|
| 357 |
-
f"
|
|
|
|
| 358 |
f"Invoice JSON:\n{json.dumps(extracted_info, indent=2)}"
|
| 359 |
)
|
| 360 |
with st.spinner("AI is reasoning and making a decision..."):
|
| 361 |
result = agent.run(prompt)
|
| 362 |
-
|
| 363 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 290 |
|
| 291 |
# --------- Classic ReAct AGENT ---------
|
| 292 |
def po_match_tool_func(input_text):
|
|
|
|
|
|
|
| 293 |
invoice = st.session_state.get("last_extracted_info")
|
| 294 |
po_df = st.session_state.get("po_df")
|
| 295 |
|
|
|
|
| 318 |
return f"PO matched: {matched_po.to_dict()}"
|
| 319 |
return "No matching PO found."
|
| 320 |
|
|
|
|
| 321 |
if po_df is not None:
|
| 322 |
st.session_state["po_df"] = po_df
|
| 323 |
|
| 324 |
if extracted_info is not None and po_df is not None:
|
| 325 |
if st.button("Make a decision (AI Agent)"):
|
|
|
|
| 326 |
tools = [
|
| 327 |
Tool(
|
| 328 |
name="po_match_tool",
|
|
|
|
| 330 |
description="Use this tool to check if the invoice matches any PO in the current PO list.",
|
| 331 |
)
|
| 332 |
]
|
|
|
|
| 333 |
openai_api_key = os.getenv("OPENAI_API_KEY")
|
| 334 |
llm = ChatOpenAI(
|
| 335 |
openai_api_key=openai_api_key,
|
|
|
|
| 343 |
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
|
| 344 |
verbose=True,
|
| 345 |
)
|
|
|
|
| 346 |
prompt = (
|
| 347 |
f"Below is an extracted invoice in JSON and a list of active POs is loaded in the system. "
|
| 348 |
+
f"Use po_match_tool to check if the invoice matches any PO in the current PO list. "
|
| 349 |
f"Step by step, reason whether the invoice matches an active PO and can be approved. "
|
| 350 |
f"If there is a match, state the matched PO, otherwise explain why not. "
|
| 351 |
+
f"At the end, respond with a JSON like this: "
|
| 352 |
+
f'{{"decision": "APPROVED or REJECTED", "reason": "<short explanation>"}}.\n'
|
| 353 |
f"Invoice JSON:\n{json.dumps(extracted_info, indent=2)}"
|
| 354 |
)
|
| 355 |
with st.spinner("AI is reasoning and making a decision..."):
|
| 356 |
result = agent.run(prompt)
|
| 357 |
+
try:
|
| 358 |
+
result_json = json.loads(result)
|
| 359 |
+
st.subheader("AI Decision")
|
| 360 |
+
st.write(f"**Decision:** {result_json.get('decision', 'N/A')}")
|
| 361 |
+
st.write(f"**Reason:** {result_json.get('reason', 'N/A')}")
|
| 362 |
+
except Exception:
|
| 363 |
+
st.subheader("AI Decision & Reason")
|
| 364 |
+
st.write(result)
|