File size: 3,359 Bytes
cf36c54
 
ce4ef7a
cf36c54
 
 
 
 
 
 
ce4ef7a
 
 
 
 
 
 
 
 
cf36c54
 
 
 
 
 
 
ce4ef7a
 
 
0165aee
 
ce4ef7a
7dcb88a
ce4ef7a
 
 
 
 
 
 
 
 
 
 
cf36c54
ce4ef7a
 
a6a7eef
cf36c54
7dcb88a
cf36c54
 
 
 
 
 
ce4ef7a
 
 
 
d35c922
cf36c54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ffbcb46
 
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
94
95
96
97
98
import openai 
import streamlit as st
import extra_streamlit_components as stx
import uuid
import time

openai.api_key = st.secrets["OPENAI_API_KEY"]
assistant_id = st.secrets["OPENAI_ASSISTANT_ID"]
client = openai

st.set_page_config(page_title="Assistant API Chat", page_icon=":speech_balloon:")

@st.cache_resource(experimental_allow_widgets=True)
def get_manager():
    return stx.CookieManager()
cookie_manager = get_manager()

st.session_state.thread_id = cookie_manager.get('thread_id')

if "session_id" not in st.session_state:
    st.session_state.session_id = str(uuid.uuid4())
if "messages" not in st.session_state:
    st.session_state.messages = []

st.title(":speech_balloon: Assistant API Chat")

with st.sidebar:
    st.caption(f"**Session ID**:  \n  {st.session_state.session_id}")
    st.header("Configuration")
    thread_id = st.text_input("Enter your Thread ID:", 
                              placeholder="Leave empty to start a new thread")
    c1, c2 = st.columns(2)

    if c1.button("Start Chat", use_container_width=True):
        if thread_id:
            st.session_state.thread_id = thread_id
        else:
            thread = client.beta.threads.create(
                metadata={
                    'session_id': st.session_state.session_id,
                }
            )
            st.session_state.thread_id = thread.id
        cookie_manager.set('thread_id', st.session_state.thread_id)

    if c2.button("Clear Chat", use_container_width=True):
        cookie_manager.delete('thread_id')
        st.session_state.thread_id = None

#def process_citations(message):
#    message_content = message.content[0].text
#    annotations = message_content.annotations if hasattr(message_content, 'annotations') else []
#    citations = []
#    full_response = message_content.value + '\n\n' + '\n'.join(citations)
#    return full_response

if st.session_state.thread_id:
    st.session_state.messages = client.beta.threads.messages.list(
        thread_id=st.session_state.thread_id
    )
    st.caption(f"**Thread ID**: {st.session_state.thread_id}")
    for message in reversed(st.session_state.messages.data):
        with st.chat_message(message.role):
            st.markdown(message.content[0].text.value)

    if prompt := st.chat_input():
        with st.chat_message("user"):
            st.markdown(prompt)

        client.beta.threads.messages.create(
            thread_id=st.session_state.thread_id,
            role="user",
            content=prompt
        )

        run = client.beta.threads.runs.create(
            thread_id=st.session_state.thread_id,
            assistant_id=assistant_id,
        )

        while run.status != 'completed':
            time.sleep(1)
            run = client.beta.threads.runs.retrieve(
                thread_id=st.session_state.thread_id,
                run_id=run.id
            )

        st.session_state.messages = client.beta.threads.messages.list(
            thread_id=st.session_state.thread_id
        )

        for message in reversed(st.session_state.messages.data):
            if message.run_id == run.id and message.role == "assistant":
                with st.chat_message("assistant"):
                    st.markdown(message.content[0].text.value)
else:
    st.write("\n")
    st.info("Click on 'Start Chat' to start/continue a thread.", icon='⚠')