File size: 3,157 Bytes
5f17ff4
e36677b
5f17ff4
 
 
 
 
e36677b
5f17ff4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e36677b
5f17ff4
 
 
 
 
 
 
 
 
 
e36677b
5f17ff4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e36677b
5f17ff4
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
import openai
import streamlit as st
import os,time
from dotenv import load_dotenv
load_dotenv()
st.set_page_config(page_title="Pak Case Law ChatBot")
st.header("Wellcome To Pakistani AI Lawyer!")

class OpenAIClient:
    def __init__(self):
        self.client = openai.OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
        self.thread_id = None
        self.file_id = ["file-CwRdM2JgGKEVZP4rhoKSvgpD"]
        self.thread = self.client.beta.threads.create()
      
    def upload_file(self):
        file=self.client.files.create(
        file=open("1964_35_THE_WEST_PAKISTAN_FAMILY_COURTS_ACT_1964.pdf", "rb"),
        purpose="assistants")
        self.file_id.append(file.id)
        print(file.id)
        print("File Uploaded")
      
    def main(self,user_message):
      lawyer = self.client.beta.assistants.create(
        instructions="""You are an Pakistani case law provider, and you have access to files to answer client questions about Pakistan case laws. Always response with info from either of the files.\
Please always make sure to follow these instructions that are delimited in triple backticks:\
```
1 - Behave like a Pakistani lawyer.\
2 - Always remember that you are also suitable for specific legal issues.\
3 - Providing helpful information, drafting documents, reviewing documents, or completing tasks based on user input.
""",
        name="Pakistani Lawyer",
        tools=[{"type": "retrieval"}],
        model="gpt-3.5-turbo-1106",
        file_ids=self.file_id)

      message = self.client.beta.threads.messages.create(
        thread_id= self.thread.id,
        role="user",
        content=user_message,
        file_ids= self.file_id
      )
      _run = self.client.beta.threads.runs.create(
        thread_id= self.thread.id,
        assistant_id= lawyer.id
      )

      while True:
        run = self.client.beta.threads.runs.retrieve(
        thread_id=self.thread.id,
        run_id=_run.id
      ).status
        
        time.sleep(3)
        if run == "completed":
          print(run)
          return self.get_response()
        elif run == "queued":
          continue
        elif run == "failed" or run == "cancelled" or run == "expired":
          print(run)
          break
        elif run == "in_progress":
          continue
        else:
          break
          
    def get_response(self):
      messages = self.client.beta.threads.messages.list(
        thread_id= self.thread.id
      )
      return messages.data[0].content[0].text.value
      
    def file_handler(self):
      return self.client.files.retrieve("file-CwRdM2JgGKEVZP4rhoKSvgpD")
    def delete_file(self):
      return self.client.files.list()
  
try:
  client = OpenAIClient()
  input = st.text_input("Input: ",key='input')
  response=client.main(input)
  submit = st.button("Ask the Question")
  if submit:
    st.subheader("The Response is")
    st.write(response)
except openai.AuthenticationError:
  st.write("Your API key or token was invalid, expired, or revoked.")
except openai.RateLimitError:
  st.write("You have hit your assigned rate limit. Please try again in a few minutes.")
except Exception as e:
  st.write(e)