vsouza commited on
Commit ·
cf36c54
1
Parent(s): 145c745
init
Browse files- .gitignore +3 -0
- Dockerfile +18 -0
- app.py +89 -0
- requirements.txt +2 -0
.gitignore
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
.streamlit/
|
| 2 |
+
.vscode/
|
| 3 |
+
*.pyc
|
Dockerfile
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.11-slim
|
| 2 |
+
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
|
| 5 |
+
ENV PYTHONUNBUFFERED=1
|
| 6 |
+
|
| 7 |
+
ENV VIRTUAL_ENV=/app/venv
|
| 8 |
+
ENV PATH="$VIRTUAL_ENV/bin:$PATH"
|
| 9 |
+
RUN python -m venv ${VIRTUAL_ENV}
|
| 10 |
+
|
| 11 |
+
COPY requirements.txt .
|
| 12 |
+
RUN pip install -r requirements.txt
|
| 13 |
+
|
| 14 |
+
COPY . .
|
| 15 |
+
|
| 16 |
+
EXPOSE 8501
|
| 17 |
+
|
| 18 |
+
CMD ["streamlit", "run", "--server.port", "8501", "main.py"]
|
app.py
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import openai
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import uuid
|
| 4 |
+
import time
|
| 5 |
+
|
| 6 |
+
openai.api_key = st.secrets["OPENAI_API_KEY"]
|
| 7 |
+
assistant_id = st.secrets["OPENAI_ASSISTANT_ID"]
|
| 8 |
+
client = openai
|
| 9 |
+
|
| 10 |
+
if "start_chat" not in st.session_state:
|
| 11 |
+
st.session_state.start_chat = False
|
| 12 |
+
if "session_id" not in st.session_state:
|
| 13 |
+
st.session_state.session_id = str(uuid.uuid4())
|
| 14 |
+
if "thread_id" not in st.session_state:
|
| 15 |
+
st.session_state.thread_id = None
|
| 16 |
+
if "messages" not in st.session_state:
|
| 17 |
+
st.session_state.messages = []
|
| 18 |
+
|
| 19 |
+
st.set_page_config(page_title="Assistant API Chat", page_icon=":speech_balloon:")
|
| 20 |
+
st.title(":speech_balloon: Assistant API Chat")
|
| 21 |
+
|
| 22 |
+
st.sidebar.caption(f"**Session ID**: \n {st.session_state.session_id}")
|
| 23 |
+
st.sidebar.header("Configuration")
|
| 24 |
+
thread_id = st.sidebar.text_input("Enter your Thread ID:")
|
| 25 |
+
|
| 26 |
+
if st.sidebar.button("Start Chat"):
|
| 27 |
+
|
| 28 |
+
st.session_state.start_chat = True
|
| 29 |
+
if thread_id:
|
| 30 |
+
st.session_state.thread_id = thread_id
|
| 31 |
+
else:
|
| 32 |
+
thread = client.beta.threads.create(
|
| 33 |
+
metadata={
|
| 34 |
+
'session_id': st.session_state.session_id,
|
| 35 |
+
}
|
| 36 |
+
)
|
| 37 |
+
st.session_state.thread_id = thread.id
|
| 38 |
+
|
| 39 |
+
st.caption(f"**Thread ID**: {st.session_state.thread_id}")
|
| 40 |
+
st.session_state.messages = client.beta.threads.messages.list(
|
| 41 |
+
thread_id=st.session_state.thread_id
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
#def process_message(message):
|
| 45 |
+
# message_content = message.content[0].text
|
| 46 |
+
# annotations = message_content.annotations if hasattr(message_content, 'annotations') else []
|
| 47 |
+
# citations = []
|
| 48 |
+
# full_response = message_content.value + '\n\n' + '\n'.join(citations)
|
| 49 |
+
# return full_response
|
| 50 |
+
|
| 51 |
+
if st.session_state.start_chat:
|
| 52 |
+
|
| 53 |
+
for message in reversed(st.session_state.messages.data):
|
| 54 |
+
with st.chat_message(message.role):
|
| 55 |
+
st.markdown(message.content[0].text.value)
|
| 56 |
+
|
| 57 |
+
if prompt := st.chat_input():
|
| 58 |
+
|
| 59 |
+
with st.chat_message("user"):
|
| 60 |
+
st.markdown(prompt)
|
| 61 |
+
|
| 62 |
+
client.beta.threads.messages.create(
|
| 63 |
+
thread_id=st.session_state.thread_id,
|
| 64 |
+
role="user",
|
| 65 |
+
content=prompt
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
run = client.beta.threads.runs.create(
|
| 69 |
+
thread_id=st.session_state.thread_id,
|
| 70 |
+
assistant_id=assistant_id,
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
while run.status != 'completed':
|
| 74 |
+
time.sleep(1)
|
| 75 |
+
run = client.beta.threads.runs.retrieve(
|
| 76 |
+
thread_id=st.session_state.thread_id,
|
| 77 |
+
run_id=run.id
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
st.session_state.messages = client.beta.threads.messages.list(
|
| 81 |
+
thread_id=st.session_state.thread_id
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
for message in reversed(st.session_state.messages.data):
|
| 85 |
+
if message.run_id == run.id and message.role == "assistant":
|
| 86 |
+
with st.chat_message("assistant"):
|
| 87 |
+
st.markdown(message.content[0].text.value)
|
| 88 |
+
else:
|
| 89 |
+
st.write("Click on 'Start Chat' to start/continue a thread.")
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit==1.28.2
|
| 2 |
+
openai==1.2.4
|