from typing import Optional from typing import ( Annotated, Sequence, TypedDict, ) import urllib.parse import requests import json from operator import add import os import gradio as gr from langchain_openai import OpenAI from langchain_openai import ChatOpenAI from langchain_core.prompts import ChatPromptTemplate, PromptTemplate, MessagesPlaceholder, SystemMessagePromptTemplate from langchain_core.messages import ToolMessage from langchain_core.output_parsers import PydanticOutputParser from langchain_core.messages import HumanMessage, SystemMessage from langchain_core.tools import tool, BaseTool from langchain_core.messages import BaseMessage from langgraph.graph import MessagesState from langgraph.prebuilt.chat_agent_executor import AgentState from langgraph.graph import MessagesState, StateGraph, START, END from langgraph.prebuilt import create_react_agent from pydantic import BaseModel, Field from typing import Annotated, Literal, TypedDict from langchain_core.runnables.config import RunnableConfig from langgraph.graph.message import add_messages import chromadb #Setup API os.environ["OPENAI_API_KEY"] = os.getenv('OPEN_AI_KEY') #Setup Model llm = ChatOpenAI(model="o3-mini", reasoning_effort="medium") #Setup Tools @tool def search_law_provision(query: str) -> str: """ Returns Malaysian legal provision based on a query. Query needs to be as specific as possible. Example: "What is the definition of money laundering under Malaysian law?" "What are the critiria of evidence of money laundering to ensure it is admissable in the court?" """ params = { "query_text": query } result = collection.query(query_texts=query, n_results=5) answer = "" for i in range(len(result["documents"][0])): act_name = result["metadatas"][0][i]['act'] part_title = result["metadatas"][0][i]['part_title'] section_title = "SECTION " + result["metadatas"][0][i]['section_title'] section_content = result["documents"][0][i] section_content = act_name + "\n\n" + part_title + "\n" + section_title + "\n\n" + section_content answer = answer + section_content + "\n\n\n" return answer ## Agent System Prompt system_prompt = ( "You are a critical plaintiff specialized on Malaysian legal framework." "For every legal task, support your argument with Malaysian legal provision." "You are provided with a tool to search for Malaysian legal provision." "Think STEP-BY-STEP." "Once you have figure out the steps, use use the tool step-by-step to execute the actions." "Do not assume anything. ALWAYS use the tool." "You are encouraged to use the tool multiple times to search for any law privision to strengthen your argument.\n" "For example:\n" "1.First you search the legal definition of the crime.\n" "2.Then you search for the requirement to be charged for a crime\n" "3.Then you search for the evidence that is required.\n" "Continue as necessary." ) # Plaintiff Agent (for _plaintiff_step) plaintiff_raw_prompt_template = ( "Based on the following case facts, accuse the accused of a certain crime\n" "Don't assume anything. Always the tools provided to strengthen your argument. Search only for Malaysian legal frameworks and cases." "Don't reveal any weakness to your argument that the defence may use against you." "Provide detailed argument for your case. Each argument MUST be supported by the legal privision that you have retrieved. And quote the specific part the legal provision is referring to.\n" "CASE FACTS:\n" ) plaintiff_prompt = ChatPromptTemplate.from_messages([ ("system", system_prompt), ("human", plaintiff_raw_prompt_template), ("human", "{case}"), ]) llm_with_tools = llm.bind_tools(tools=[search_law_provision]) chain = plaintiff_prompt | llm_with_tools def invoke_llm(state: MessagesState): chain = plaintiff_prompt | llm_with_tools return { "messages": [chain.invoke({"case": state["messages"]})] } def call_tools(state: MessagesState): last_message = state["messages"][-1] tool_calls = last_message.tool_calls new_messages = [] for tool_call in tool_calls: print("Plaintiff searching for: ", tool_call["args"]["query"]) # run the tool result = search_law_provision.invoke(tool_call["args"]) # send result back to LLM so it continues new_messages.append(ToolMessage(content=str(result), tool_call_id=tool_call["id"])) return {"messages": new_messages} def should_run_tools(state: MessagesState): last_message = state["messages"][-1] if last_message.tool_calls: return "call_tools" return END builder = StateGraph(MessagesState) builder.add_node("invoke_llm", invoke_llm) builder.add_node("call_tools", call_tools) builder.add_edge(START, "invoke_llm") builder.add_conditional_edges("invoke_llm", should_run_tools) builder.add_edge("call_tools", "invoke_llm") graph = builder.compile() ## Defence Agent defence_system_prompt = ( "You are a defence attorney specializing in the defence of Malaysian Crime cases.\n" "You are provided with a tool to search for Malaysian legal provision.\n" "Think STEP-BY-STEP.\n" "Once you have figure out the steps, use the tool step-by-step to execute the actions.\n" "You can also search for laws regarding the admissability of the evidence.\n" "Look and heaviliy scrutinize any reasoning flaw in the argument presented by the defendant.\n" "Only argue against the plaintiff if there are no reasoning flaw by the plaintiff." "Utilize the tool to cross check all the legal article referenced by the plaintiff.\n" "Ensure that the evidence presented is admissable under malaysian law.\n" "Query EVIDENCE ACT 1950 (REVISED - 1971) ACT 56 to find specific law for the evidence act.\n" "Ensure that the CRIMINAL PROCEDURE CODE (REVISED 1999) ACT 593 is followed by the plaintiff.\n" ) defence_raw_prompt_template = ( "ALWAYS use the legal search tool before trying to make a legal argument." "The plaintiff is presented with case facts:\n {case} .\n" "And predicted that {answer} \n" "Remember to use the legal search tool. Then, cite and quote the specific legal article that you have used to construct your argument" ) defence_prompt = ChatPromptTemplate.from_messages( [ ("system", defence_system_prompt), ("user", defence_raw_prompt_template), ] ) template = "Format the response by the defence \n\n {format_instructions} \n\n Defence Response: \n\n {defence_response}. \n Only fill in the legal_prediction if there are no reasoning flaws." defence_llm_with_tools = llm.bind_tools([search_law_provision]) class DefenceAgentState(TypedDict): case: str plaintiff_answer: str messages: Annotated[Sequence[BaseMessage], add_messages] def defence_invoke_llm(state: DefenceAgentState): defence_message = defence_prompt.format_prompt( case=state["case"], answer=state["plaintiff_answer"] ).to_messages() return { "messages": defence_llm_with_tools.invoke(defence_message + state["messages"]) } def defence_call_tools(state: DefenceAgentState): last_message = state["messages"][-1] tool_calls = last_message.tool_calls # print(last_message) new_messages = [] for tool_call in tool_calls: print("Defence is Searching For: ", tool_call["args"]["query"]) # run the tool result = search_law_provision.invoke(tool_call["args"]) # send result back to LLM so it continues new_messages.append(ToolMessage(content=str(result), tool_call_id=tool_call["id"])) return {"messages": new_messages} def defence_should_run_tools(state: DefenceAgentState) -> Literal["defence_call_tools", END]: last_message = state["messages"][-1] if last_message.tool_calls: # print(last_message.tool_calls) return "defence_call_tools" return END defence_builder = StateGraph(DefenceAgentState) defence_builder.add_node("defence_invoke_llm", defence_invoke_llm) defence_builder.add_node("defence_call_tools", defence_call_tools) defence_builder.add_edge(START, "defence_invoke_llm") defence_builder.add_conditional_edges("defence_invoke_llm", defence_should_run_tools) defence_builder.add_edge("defence_call_tools", "defence_invoke_llm") defence_graph = defence_builder.compile() class Response(BaseModel): legal_prediction: Optional[str] = Field(description="The legal prediction. Leave it empty if there are reasoning flaws by the plaintiff", default=None) counter_argument: Optional[str] = Field(description="The full argument by the defendent. Do not summarize", default=None) ##Plaintiff Reply Agent raw_plaintiff_prompt_with_argument_by_defence = ( "You are an argumentative plaintiff lawyer and have received a response from the defence lawyer. " "Improve your argument to support your initial answer or accept that the defence lawyer is correct and change your answer." "Think STEP-BY-STEP.\n" "Once you have figure out the steps, use the tool step-by-step to execute the actions.\n" "Rebut each of the counter argument made by the defence. Don't ignore any of the derence argument.\n" "Then, create new arguments to further challenge the defence. Don't be repetitive in the argument.\n" "ALWAYS use the tools to search for the argument made by the defence. Do not assume ANYTHING.\n" "Use the tools to cross-check the argument made by the defence.\n" "\nCASE: {case}\n\n" "Current Answer:\n\n{current_answer}\n\n" "Defence Response:\n\n{defence_response}\n\n" ) plaintiff_prompt_with_critique = ChatPromptTemplate.from_messages( [ ("system", system_prompt), ("user", raw_plaintiff_prompt_with_argument_by_defence), ] ) class PlaintiffAgentState(TypedDict): case: str current_answer:str defence_response: str messages: Annotated[Sequence[BaseMessage], add_messages] plaintiff_llm_with_tools = llm.bind_tools([search_law_provision]) def plaintiff_invoke_llm(state: PlaintiffAgentState): plaintiff_message = plaintiff_prompt.format_prompt( case=state["case"], current_answer=state["current_answer"], defence_response=state["defence_response"] ).to_messages() return { "messages": plaintiff_llm_with_tools.invoke(plaintiff_message + state["messages"]) } def plaintiff_call_tools(state: PlaintiffAgentState): last_message = state["messages"][-1] tool_calls = last_message.tool_calls # print(last_message) new_messages = [] for tool_call in tool_calls: print("Plaintiff is searching for: ", tool_call["args"]["query"]) # run the tool result = search_law_provision.invoke(tool_call["args"]) # send result back to LLM so it continues new_messages.append(ToolMessage(content=str(result), tool_call_id=tool_call["id"])) return {"messages": new_messages} def plaintiff_should_run_tools(state: PlaintiffAgentState) -> Literal["plaintiff_call_tools", END]: last_message = state["messages"][-1] if last_message.tool_calls: # print(last_message.tool_calls) return "plaintiff_call_tools" return END plaintiff_builder = StateGraph(PlaintiffAgentState) plaintiff_builder.add_node("plaintiff_invoke_llm", plaintiff_invoke_llm) plaintiff_builder.add_node("plaintiff_call_tools", plaintiff_call_tools) plaintiff_builder.add_edge(START, "plaintiff_invoke_llm") plaintiff_builder.add_conditional_edges("plaintiff_invoke_llm", plaintiff_should_run_tools) plaintiff_builder.add_edge("plaintiff_call_tools", "plaintiff_invoke_llm") plaintiff_graph = plaintiff_builder.compile() #Multi-Agent LLM Simulator class PlaintiffReflectionAgentState(TypedDict): case: str plaintiff_answer: str steps: Annotated[int, add] defence_response: str response: Response def _plaintiff_start(state: PlaintiffReflectionAgentState): """ Provide the initial argument to start the argument between the plaintiff and the defence. """ print(state["case"]) answer = graph.invoke({"messages": [HumanMessage(content=state["case"])]}) return {"plaintiff_answer": answer["messages"][-1].content} def _defence_step(state: PlaintiffReflectionAgentState): """ Argues the legal prediction by the plaintiff. If disagree, then provide critique. Othwerwise, add answer to the Response pydantic instance. """ result = defence_graph.invoke({"case": state["case"], "plaintiff_answer": state["plaintiff_answer"]}) defence_response = result["messages"][-1].content parser = PydanticOutputParser(pydantic_object=Response) system_message_prompt = SystemMessagePromptTemplate.from_template(template) formatting_prompt = ChatPromptTemplate.from_messages([system_message_prompt]) messages = formatting_prompt.format_prompt( format_instructions=parser.get_format_instructions(), defence_response=defence_response ).to_messages() format_result = llm.invoke(messages) response = parser.parse(format_result.content) return {"response": response, "defence_response": defence_response, "steps": 1} def _should_end(state: PlaintiffReflectionAgentState, config: RunnableConfig) -> Literal["plaintiff_reply", END]: """ Checks if the answer have been given. And if it exceeds the maximum number of argument cycles. If answer have been given (defence accept the plaintiff argument) end the process. Otherwise, plaintiff is allowed to reply. """ max_argument_steps = config["configurable"].get("max_argument_steps", 10) if state.get("response") and state["response"].legal_prediction: return END if state.get("steps", 1) > max_argument_steps: return END return "plaintiff_reply" def _plaintiff_reply(state: PlaintiffReflectionAgentState): result = plaintiff_graph.invoke({"case": state["case"], "current_answer": state["plaintiff_answer"], "defence_response": state["defence_response"]}) plaintiff_reply = result["messages"][-1].content return {"plaintiff_answer": plaintiff_reply} multi_agent_builder = StateGraph(PlaintiffReflectionAgentState) multi_agent_builder.add_node("plaintiff_start", _plaintiff_start) multi_agent_builder.add_node("plaintiff_reply", _plaintiff_reply) multi_agent_builder.add_node("defence", _defence_step) multi_agent_builder.add_edge(START, "plaintiff_start") multi_agent_builder.add_edge("plaintiff_start", "defence") multi_agent_builder.add_edge("plaintiff_reply", "defence") multi_agent_builder.add_conditional_edges("defence", _should_end) multi_agent_graph = multi_agent_builder.compile() def simulate_case(case): argument = "" for _, event in multi_agent_graph.stream({"case": case}, stream_mode=["updates"]): if "plaintiff_start" in event: argument += """\n\n\n ====================Plantiff Initial Argument [BEGINS]==========================\n\n """+event["plaintiff_start"]["plaintiff_answer"]+""" ====================Plantiff Initial Argument [ENDS]============================\n\n """ elif "defence" in event: argument += """\n\n\n ==========================Defence Response [BEGINS]==============================\n\n """+event["defence"]["defence_response"]+""" ==========================Defence Response [ENDS]================================\n\n """ elif "plaintiff_reply" in event: argument += """\n\n\n ===========================Plantiff Reply [BEGINS]===============================\n\n """+event["plaintiff_reply"]["plaintiff_answer"]+""" ===========================Plantiff Reply [ENDS]===============================\n\n """ else: continue yield argument # print(event) if __name__ == "__main__": client = chromadb.PersistentClient() try: collection = client.create_collection( name="section_collection", ) except Exception as e: print(e) collection = client.get_collection("section_collection") # collection = client.get_collection("section_collection") # case = """ # Case Summary # # Mr. Ahmad, a small business owner, is accused of laundering money through his retail shop. Authorities allege that suspicious deposits into his company account indicate proceeds of crime. # # Alleged Facts # # Suspicious Cash Deposits # # Over a period of three months, Mr. Ahmad deposited RM50,000 in cash into his business account. # # Authorities argue that such deposits are inconsistent with his “expected” daily sales. # # Foreign Transfers # # Mr. Ahmad sent two wire transfers of RM10,000 each to a supplier in Thailand. # # Investigators claim these transfers have “no clear business justification.” # # Lifestyle Indicators # # Mr. Ahmad was seen driving a luxury car registered under his cousin’s name. # # Authorities suggest this demonstrates hidden wealth from illegal sources. # # Lack of Documentation # # Investigators argue that Mr. Ahmad could not provide “complete documentation” for his imports. # # This is taken as evidence of attempts to conceal illicit proceeds. # """ demo = gr.Interface( fn=simulate_case, inputs=gr.Textbox(placeholder="Enter case to simulate"), outputs=gr.Textbox(), title="Court Room Simulator", description="Simulate how your case will be argued in a court room" ) # for event in simulate_case(case): # print(event) demo.launch()