Spaces:
Running
Running
import os | |
from openai import OpenAI | |
from openai import AzureOpenAI | |
from llama_index.llms.groq import Groq | |
from mistralai.client import MistralClient | |
from mistralai.models.chat_completion import ChatMessage | |
import time | |
import re | |
import mock_constants | |
import utils | |
from datetime import date | |
from prompts import uae_law_stage_prompts | |
import prompt_tracing | |
import comet_llm | |
import llm | |
from mock_constants_dir import uae_law_mock_constants | |
import azure_search | |
from dotenv import load_dotenv | |
load_dotenv() | |
class Uae_law(): | |
def __init__(self): | |
self.stage=1 | |
self.stage_1_chat_history = [] | |
self.stage_2_chat_history = [] | |
self.temp_bool = False | |
self.additional_message = "" | |
self.lawsuit_facts = "" | |
self.lawsuit_extracted_text = "" | |
self.claim_extracted_text = "" | |
self.defendant_extracted_text = "" | |
self.claimant_response_extracted_text = "" | |
self.defendant_response_extracted_text = "" | |
self.claimant_evidence_extracted_text = [] | |
self.claimant_evidence_facts = [] | |
self.defendant_evidence_extracted_text = [] | |
self.defendant_evidence_facts = [] | |
self.response_facts = "" | |
self.claimant_response_to_defendant_facts = "" | |
self.defendant_response_to_claimant_clarification_facts = "" | |
self.evidence_analysis = "" | |
self.evidence_analysis_conclusion = "" | |
self.case_fable = "" # stage 5 | |
self.stage_6_ideal_model = "" | |
utils.clear_or_create_empty_file("data//uae_logs.txt") # Clear log file | |
comet_llm.init( | |
api_key=os.environ.get("COMET_API_KEY"), | |
project="judgeai-v0", | |
) | |
def reset(self): | |
self.stage=1 | |
self.stage_1_chat_history = [] | |
self.stage_2_chat_history = [] | |
self.temp_bool = False | |
self.additional_message = "" | |
self.claimant_evidence_extracted_text = [] | |
self.claimant_evidence_facts = [] | |
self.defendant_evidence_extracted_text = [] | |
self.defendant_evidence_facts = [] | |
self.lawsuit_facts = "" | |
self.lawsuit_extracted_text = "" | |
self.claim_extracted_text = "" | |
self.defendant_extracted_text = "" | |
self.claimant_response_extracted_text = "" | |
self.defendant_response_extracted_text = "" | |
self.response_facts = "" | |
self.claimant_response_to_defendant_facts = "" | |
self.defendant_response_to_claimant_clarification_facts = "" | |
self.evidence_analysis = "" | |
self.evidence_analysis_conclusion = "" | |
self.case_fable = "" # stage 5 | |
self.stage_6_ideal_model = "" | |
utils.clear_or_create_empty_file("data//uae_logs.txt") # Clear log file | |
def stage_1_and_1_1_call(self, input_text): | |
''' | |
Function called to execute stage 1 and stage 1_1 | |
stage 1 and 1_1 - getting facts from lawsuit and analysing claimant evidence | |
''' | |
gpt_prompt = uae_law_stage_prompts.stage_1_and_1_1_prompt | |
self.claim_extracted_text = "\n\nText Extracted from Claim:\n" + input_text | |
prompt = input_text + "\n\nPrompt:" + gpt_prompt | |
response = llm.llm_function_call(prompt, "gpt-4-0125-preview") | |
# response = uae_law_mock_constants.stage_1_and_1_1_response | |
if llm.check_claim_validity(response.lower()): | |
self.temp_bool = False | |
self.stage = 2 | |
self.additional_message = "Defendant is directed to upload response to claim" | |
self.claimant_facts_and_evidence = "\nClaimant Facts and Evidence: \n" + response | |
# self.stage = 2 | |
else: | |
self.temp_bool = False | |
self.stage = 1 | |
self.additional_message = "For instructions on how to properly use JudgeAI, read [JudgeAI cheatsheet](https://drive.google.com/uc?export=download&id=10zj75HDpsn7H7k0Lk5HDCmfWECp0ZtY6)." | |
utils.write_string_to_file("data//uae_logs.txt", response) | |
return response | |
def stage_2_and_2_1_call(self, input_text): | |
''' | |
Function called to execute stage 2 and stage 2_1 | |
stage 2 and 2_1 - getting facts from response and analysing defendant evidence | |
''' | |
gpt_prompt = uae_law_stage_prompts.stage_2_and_2_1_prompt | |
self.defendant_extracted_text = "\n\nText Extracted from Response from defendant:\n" + input_text | |
prompt = input_text + self.claimant_facts_and_evidence + "\n\nPrompt:" + gpt_prompt | |
# print(prompt) | |
response = llm.llm_function_call(prompt, "gpt-4-0125-preview") | |
# response = uae_law_mock_constants.stage_2_and_2_2_response | |
if llm.check_prima_facie_rebutted(response.lower()): | |
self.temp_bool = False | |
self.stage = 3 | |
self.additional_message = "Claimant may provide a response" | |
self.defendant_facts_and_evidence = "\nDefendant Facts and Evidence: \n" + response | |
# self.stage = 2 | |
else: | |
self.temp_bool = False | |
self.stage = 4.9 | |
self.defendant_facts_and_evidence = "\nDefendant Facts and Evidence: \n" + response | |
self.additional_message = "Judge is now evaluating evidence submitted by all parties, generating case fables for each party, generating an ideal model and drafting final judgement..." | |
utils.write_string_to_file("data//uae_logs.txt", response) | |
return response | |
def stage_3_call(self, input_text): | |
''' | |
Function called to execute stage 3 | |
stage 3 - analysing clarification response from claimant | |
''' | |
self.claimant_response_extracted_text = "\n\nText Extracted from Claimant clarification to defendant's response:\n" + input_text | |
gpt_prompt = uae_law_stage_prompts.stage_3_prompt | |
prompt = self.claimant_facts_and_evidence + self.defendant_facts_and_evidence + "Claimant clarification to defendant's response: \n" + input_text + "\n\nPrompt:" + "\n\nPrompt:" + gpt_prompt | |
response = llm.llm_function_call(prompt, "gpt-4-0125-preview") | |
# response = uae_law_mock_constants.stage_3_response_given | |
self.temp_bool = False | |
self.stage = 4 | |
self.claimant_response_to_defendant_facts = "\nClaimant clarification to defendant's response: \n" + response | |
self.additional_message = "Defendant may provide a response" | |
utils.write_string_to_file("data//uae_logs.txt", self.claimant_response_to_defendant_facts) | |
return response | |
def stage_4_call(self, input_text): | |
''' | |
Function called to execute stage 4 | |
stage 4 - analysing addendum response from defendant | |
''' | |
self.defendant_response_extracted_text = "\n\nText Extracted from Defendant's response to claimant's clarifications:\n" + input_text | |
gpt_prompt = uae_law_stage_prompts.stage_4_prompt | |
prompt = self.claimant_facts_and_evidence + self.defendant_facts_and_evidence + self.claimant_response_to_defendant_facts + "Defendant's response to claimant's clarifications: \n" + input_text + "\n\nPrompt:" + gpt_prompt | |
response = llm.llm_function_call(prompt, "gpt-4-0125-preview") | |
# response = uae_law_mock_constants.stage_4_response_given | |
self.temp_bool = False | |
self.stage = 4.9 | |
self.defendant_response_to_claimant_clarification_facts = "\nDefendant's response to claimant's clarifications: \n" + response | |
self.additional_message = "Judge is now evaluating evidence submitted by all parties, generating case fables for each party, generating an ideal model and drafting final judgement..." | |
if llm.go_to_stage_4_1(response.lower()): | |
self.temp_bool = False | |
self.stage = 4.1 | |
self.defendant_response_to_claimant_clarification_facts = "\nDefendant's response to claimant's clarifications: \n" + response | |
self.additional_message = "Claimant may provide a response" | |
utils.write_string_to_file("data//uae_logs.txt", self.defendant_response_to_claimant_clarification_facts) | |
return response | |
def stage_4_1_call(self, input_text): | |
''' | |
Function called to execute stage 4 | |
stage 4_1 - Optional Stage | |
''' | |
gpt_prompt = uae_law_stage_prompts.stage_4_1_prompt | |
prompt = self.claimant_facts_and_evidence + self.defendant_facts_and_evidence + self.claimant_response_to_defendant_facts + self.defendant_response_to_claimant_clarification_facts + "Claimant's response to defendant's clarifications: \n" + input_text + "\n\nPrompt:" + gpt_prompt | |
response = llm.llm_function_call(prompt, "gpt-4-0125-preview") | |
# response = uae_law_mock_constants.stage_4_response_given | |
if llm.check_response_given(response.lower()): | |
self.temp_bool = False | |
self.stage = 5 | |
self.defendant_response_to_claimant_clarification_facts = "\nDefendant's response to claimant's clarifications: \n" + response | |
self.additional_message = "Judge is now evaluating evidence submitted by all parties, generating case fables for each party, generating an ideal model and drafting final judgement..." | |
else: | |
self.temp_bool = False | |
self.stage = 5 | |
self.defendant_response_to_claimant_clarification_facts = "\nDefendant's response to claimant's clarifications: \n" + response | |
self.additional_message = "proceeding to stage 5 from stage 4 with no objection" | |
utils.write_string_to_file("data//uae_logs.txt", self.defendant_response_to_claimant_clarification_facts) | |
return response | |
def stage_4_9_call(self): | |
''' | |
Function called to execute stage 4_9 | |
stage 4_9 - analysing evidences | |
''' | |
gpt_prompt = uae_law_stage_prompts.stage_4_9_prompt | |
prompt = self.claim_extracted_text + self.claimant_facts_and_evidence + self.defendant_extracted_text + self.defendant_facts_and_evidence + self.claimant_response_extracted_text + self.claimant_response_to_defendant_facts + self.defendant_response_extracted_text + self.defendant_response_to_claimant_clarification_facts + "\n\nPrompt:" + gpt_prompt | |
response = llm.llm_function_call(prompt, "gpt-4-0125-preview") | |
# response = uae_law_mock_constants.stage_4_9_response | |
self.evidence_analysis = "\nEvidence analysis: \n" + response | |
utils.write_string_to_file("data//uae_logs.txt", response) | |
return response | |
def stage_4_9_5_call(self): | |
''' | |
Function called to execute stage 4_9_5 | |
stage 4_9_5 - Pre stage 5 | |
''' | |
gpt_prompt = uae_law_stage_prompts.stage_4_9_5_prompt | |
prompt = self.claim_extracted_text + self.defendant_extracted_text + self.claimant_response_extracted_text + self.defendant_response_extracted_text + self.evidence_analysis + "\n\nPrompt:" + gpt_prompt | |
response = llm.llm_function_call(prompt, "gpt-4-0125-preview") | |
# response = uae_law_mock_constants.stage_4_9_response | |
self.evidence_analysis_conclusion = "\nEvidence analysis Conclusion: \n" + response | |
utils.write_string_to_file("data//uae_logs.txt", response) | |
return response | |
def stage_5_call(self): | |
''' | |
Function called to execute stage 5 | |
stage 5 - Case Fable | |
''' | |
gpt_prompt = uae_law_stage_prompts.stage_5_prompt | |
prompt = self.claimant_facts_and_evidence + self.defendant_facts_and_evidence + self.claimant_response_to_defendant_facts + self.defendant_response_to_claimant_clarification_facts + self.evidence_analysis + self.evidence_analysis_conclusion + "\n\nPrompt:" + gpt_prompt | |
response = llm.llm_function_call(prompt, "gpt-4-0125-preview") | |
# response = uae_law_mock_constants.stage_5_case_fable | |
self.case_fable = "Case Fable: \n" + response | |
utils.write_string_to_file("data//uae_logs.txt", response) | |
return response | |
def stage_5_9_call(self): | |
''' | |
Function called to execute stage pre 6 | |
pre stage 6 - getting relevant codified laws | |
''' | |
gpt_prompt = uae_law_stage_prompts.stage_5_9_prompt | |
prompt = self.claimant_facts_and_evidence + gpt_prompt | |
self.query = llm.llm_function_call(prompt, "gpt-4-0125-preview") | |
# self.query = uae_law_mock_constants.stage_5_9_query | |
response = self.query | |
return response | |
def stage_5_9_5_call(self): | |
''' | |
Function called to execute stage pre 6 | |
pre stage 6 - getting relevant codified laws | |
''' | |
response = azure_search.azure_search(self.query) | |
self.legal_norms = "\nLegal Norms: \n" + response | |
return response | |
def stage_6_call(self): | |
''' | |
Function called to execute stage 6 | |
stage 6 - Ideal model generation | |
''' | |
gpt_prompt = uae_law_stage_prompts.stage_6_prompt | |
prompt = self.claimant_facts_and_evidence + self.defendant_facts_and_evidence + \ | |
self.claimant_response_to_defendant_facts + self.defendant_response_to_claimant_clarification_facts + \ | |
self.case_fable + self.legal_norms + "\n\nPrompt:" + gpt_prompt | |
response = llm.llm_function_call(prompt, "gpt-4-0125-preview") | |
# response = uae_law_mock_constants.stage_6_ideal_model | |
self.stage_6_ideal_model = "Ideal Behavior Chronology of the Parties: \n " + response | |
utils.write_string_to_file("data//uae_logs.txt", response) | |
return response | |
def stage_7_call(self): | |
''' | |
Function called to execute stage 7 | |
stage 7 - Final Judgement | |
''' | |
judgement_date = date.today() | |
gpt_prompt = uae_law_stage_prompts.stage_7_prompt.format(judgement_date=judgement_date) | |
prompt = self.claimant_facts_and_evidence + self.claim_extracted_text + self.defendant_extracted_text + self.claimant_response_extracted_text + self.defendant_response_extracted_text + self.case_fable + self.stage_6_ideal_model + self.legal_norms + "\n\nPrompt:" + gpt_prompt | |
response = llm.llm_function_call(prompt, "gpt-4-0125-preview") | |
# response = uae_law_mock_constants.stage_7_final_judgement | |
utils.write_string_to_file("data//uae_logs.txt", response) | |
return response | |
def generate_response(self, prompt): | |
if prompt.lower().endswith(".png"): | |
input_text = self.lawsuit_extracted_text | |
else: | |
input_text = prompt | |
# print(self.stage) | |
if self.stage == 1: | |
response = self.stage_1_and_1_1_call(input_text) | |
elif self.stage == 2: | |
response = self.stage_2_and_2_1_call(input_text) | |
elif self.stage == 3: | |
response = self.stage_3_call(input_text) | |
elif self.stage == 4: | |
response = self.stage_4_call(input_text) | |
elif self.stage == 4.1: | |
response = self.stage_4_1_call(input_text) | |
elif self.stage == 4.9: | |
response = self.stage_4_9_call(input_text) | |
# elif self.stage == 5: | |
# response = self.stage_5_call() | |
return response |