Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,143 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import openai
|
3 |
+
import time
|
4 |
+
import config.pagesetup as ps
|
5 |
+
from openai import OpenAI
|
6 |
+
import uuid
|
7 |
+
|
8 |
+
|
9 |
+
#0. Page Config
|
10 |
+
st.set_page_config("FlowGenius", initial_sidebar_state="collapsed", layout="wide")
|
11 |
+
|
12 |
+
#1. Login and Page Setup
|
13 |
+
|
14 |
+
ps.set_title("FlowGenius", "Legal Assistant")
|
15 |
+
ps.set_page_overview("Overview", "**Legal Assistant** provides a way to quickly ask about the law")
|
16 |
+
|
17 |
+
#2. Variable Setup
|
18 |
+
|
19 |
+
openai.api_key = st.secrets.openai.api_key
|
20 |
+
assistant = st.secrets.openai.assistant_key
|
21 |
+
model = "gpt-4-1106-preview"
|
22 |
+
client = OpenAI(api_key=st.secrets.openai.api_key)
|
23 |
+
|
24 |
+
#3. Session State Management
|
25 |
+
if "session_id" not in st.session_state: #used to identify each session
|
26 |
+
st.session_state.session_id = str(uuid.uuid4())
|
27 |
+
|
28 |
+
if "run" not in st.session_state: #stores the run state of the assistant
|
29 |
+
st.session_state.run = {"status": None}
|
30 |
+
|
31 |
+
if "messages" not in st.session_state: #stores messages of the assistant
|
32 |
+
st.session_state.messages = []
|
33 |
+
st.chat_message("assistant").markdown("I am your assistant. How may I help you?")
|
34 |
+
if "retry_error" not in st.session_state: #used for error handling
|
35 |
+
st.session_state.retry_error = 0
|
36 |
+
|
37 |
+
#4. Openai setup
|
38 |
+
if "assistant" not in st.session_state:
|
39 |
+
openai.api_key = st.secrets.openai.api_key
|
40 |
+
|
41 |
+
# Load the previously created assistant
|
42 |
+
st.session_state.assistant = openai.beta.assistants.retrieve(st.secrets.openai.assistant_key)
|
43 |
+
|
44 |
+
# Create a new thread for this session
|
45 |
+
st.session_state.thread = client.beta.threads.create(
|
46 |
+
metadata={
|
47 |
+
'session_id': st.session_state.session_id,
|
48 |
+
}
|
49 |
+
)
|
50 |
+
|
51 |
+
# If the run is completed, display the messages
|
52 |
+
elif hasattr(st.session_state.run, 'status') and st.session_state.run.status == "completed":
|
53 |
+
# Retrieve the list of messages
|
54 |
+
st.session_state.messages = client.beta.threads.messages.list(
|
55 |
+
thread_id=st.session_state.thread.id
|
56 |
+
)
|
57 |
+
|
58 |
+
for thread_message in st.session_state.messages.data:
|
59 |
+
for message_content in thread_message.content:
|
60 |
+
# Access the actual text content
|
61 |
+
message_content = message_content.text
|
62 |
+
annotations = message_content.annotations
|
63 |
+
citations = []
|
64 |
+
|
65 |
+
# Iterate over the annotations and add footnotes
|
66 |
+
for index, annotation in enumerate(annotations):
|
67 |
+
# Replace the text with a footnote
|
68 |
+
message_content.value = message_content.value.replace(annotation.text, f' [{index}]')
|
69 |
+
|
70 |
+
# Gather citations based on annotation attributes
|
71 |
+
if (file_citation := getattr(annotation, 'file_citation', None)):
|
72 |
+
cited_file = client.files.retrieve(file_citation.file_id)
|
73 |
+
citations.append(f'[{index}] {file_citation.quote} from {cited_file.filename}')
|
74 |
+
elif (file_path := getattr(annotation, 'file_path', None)):
|
75 |
+
cited_file = client.files.retrieve(file_path.file_id)
|
76 |
+
citations.append(f'[{index}] Click <here> to download {cited_file.filename}')
|
77 |
+
# Note: File download functionality not implemented above for brevity
|
78 |
+
|
79 |
+
# Add footnotes to the end of the message before displaying to user
|
80 |
+
message_content.value += '\n' + '\n'.join(citations)
|
81 |
+
|
82 |
+
# Display messages
|
83 |
+
for message in reversed(st.session_state.messages.data):
|
84 |
+
if message.role in ["user", "assistant"]:
|
85 |
+
with st.chat_message(message.role):
|
86 |
+
for content_part in message.content:
|
87 |
+
message_text = content_part.text.value
|
88 |
+
st.markdown(message_text)
|
89 |
+
|
90 |
+
if prompt := st.chat_input("How can I help you?"):
|
91 |
+
with st.chat_message('user'):
|
92 |
+
st.write(prompt)
|
93 |
+
|
94 |
+
# Add message to the thread
|
95 |
+
st.session_state.messages = client.beta.threads.messages.create(
|
96 |
+
thread_id=st.session_state.thread.id,
|
97 |
+
role="user",
|
98 |
+
content=prompt
|
99 |
+
)
|
100 |
+
|
101 |
+
# Do a run to process the messages in the thread
|
102 |
+
st.session_state.run = client.beta.threads.runs.create(
|
103 |
+
thread_id=st.session_state.thread.id,
|
104 |
+
assistant_id=st.session_state.assistant.id,
|
105 |
+
)
|
106 |
+
if st.session_state.retry_error < 3:
|
107 |
+
time.sleep(1) # Wait 1 second before checking run status
|
108 |
+
st.rerun()
|
109 |
+
|
110 |
+
# Check if 'run' object has 'status' attribute
|
111 |
+
if hasattr(st.session_state.run, 'status'):
|
112 |
+
# Handle the 'running' status
|
113 |
+
if st.session_state.run.status == "running":
|
114 |
+
with st.chat_message('assistant'):
|
115 |
+
st.write("Thinking ......")
|
116 |
+
if st.session_state.retry_error < 3:
|
117 |
+
time.sleep(1) # Short delay to prevent immediate rerun, adjust as needed
|
118 |
+
st.rerun()
|
119 |
+
|
120 |
+
# Handle the 'failed' status
|
121 |
+
elif st.session_state.run.status == "failed":
|
122 |
+
st.session_state.retry_error += 1
|
123 |
+
with st.chat_message('assistant'):
|
124 |
+
if st.session_state.retry_error < 3:
|
125 |
+
st.write("Run failed, retrying ......")
|
126 |
+
time.sleep(3) # Longer delay before retrying
|
127 |
+
st.rerun()
|
128 |
+
else:
|
129 |
+
st.error("FAILED: The OpenAI API is currently processing too many requests. Please try again later ......")
|
130 |
+
|
131 |
+
# Handle any status that is not 'completed'
|
132 |
+
elif st.session_state.run.status != "completed":
|
133 |
+
# Attempt to retrieve the run again, possibly redundant if there's no other status but 'running' or 'failed'
|
134 |
+
st.session_state.run = client.beta.threads.runs.retrieve(
|
135 |
+
thread_id=st.session_state.thread.id,
|
136 |
+
run_id=st.session_state.run.id,
|
137 |
+
)
|
138 |
+
if st.session_state.retry_error < 3:
|
139 |
+
time.sleep(3)
|
140 |
+
st.rerun()
|
141 |
+
|
142 |
+
#https://medium.com/prompt-engineering/unleashing-the-power-of-openais-new-gpt-assistants-with-streamlit-83779294629f
|
143 |
+
#https://github.com/tractorjuice/STGPT
|