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17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
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()