Anne31415 commited on
Commit
c8e6d98
·
1 Parent(s): cdb0cfc

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +117 -0
app.py ADDED
@@ -0,0 +1,117 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ # Importing required packages
3
+ import streamlit as st
4
+ import openai
5
+ import uuid
6
+ import time
7
+
8
+ from openai import OpenAI
9
+
10
+ api_key = os.getenv("OPENAI_API_KEY")
11
+
12
+ client = OpenAI(api_key="Hier deienen OPENAI-API-KEY einfügen")
13
+
14
+
15
+ MODEL = "gpt-4-1106-preview"
16
+
17
+
18
+ if "session_id" not in st.session_state:
19
+ st.session_state.session_id = str(uuid.uuid4())
20
+
21
+ if "run" not in st.session_state:
22
+ st.session_state.run = {"status": None}
23
+
24
+ if "messages" not in st.session_state:
25
+ st.session_state.messages = []
26
+
27
+ if "retry_error" not in st.session_state:
28
+ st.session_state.retry_error = 0
29
+
30
+ st.set_page_config(page_title="OpenAI - Assistent API")
31
+ st.sidebar.title("OpenAI - Assistant API🤖")
32
+ st.sidebar.markdown("**Model:** gpt-4-1106-preview API")
33
+ st.sidebar.markdown("**Version:** 1.1")
34
+ st.sidebar.markdown("**Session-ID:**")
35
+ st.sidebar.markdown(st.session_state.session_id)
36
+ st.sidebar.divider()
37
+ st.title("Assitent-Name")
38
+ st.text("Beschreibung")
39
+
40
+ if "assistant" not in st.session_state:
41
+ openai.api_key = "Hier deinen OPENAI-API-KEY einfügen"
42
+ # Load the previously created assistant
43
+ st.session_state.assistant = openai.beta.assistants.retrieve("Hier deine ASSISTENT-ID Einfügen")
44
+
45
+ # Create a new thread for this session
46
+ st.session_state.thread = client.beta.threads.create(
47
+ metadata={
48
+ 'session_id': st.session_state.session_id,
49
+ }
50
+ )
51
+
52
+ # If the run is completed, display the messages
53
+ elif hasattr(st.session_state.run, 'status') and st.session_state.run.status == "completed":
54
+ # Retrieve the list of messages
55
+ st.session_state.messages = client.beta.threads.messages.list(
56
+ thread_id=st.session_state.thread.id
57
+ )
58
+
59
+ # Display messages
60
+ for message in reversed(st.session_state.messages.data):
61
+ if message.role in ["user", "assistant"]:
62
+ with st.chat_message(message.role):
63
+ for content_part in message.content:
64
+ message_text = content_part.text.value
65
+ st.markdown(message_text)
66
+
67
+ if prompt := st.chat_input("Wie kann ich dir helfen?"):
68
+ with st.chat_message('user'):
69
+ st.write(prompt)
70
+
71
+ # Add message to the thread
72
+ st.session_state.messages = client.beta.threads.messages.create(
73
+ thread_id=st.session_state.thread.id,
74
+ role="user",
75
+ content=prompt
76
+ )
77
+
78
+ # Do a run to process the messages in the thread
79
+ st.session_state.run = client.beta.threads.runs.create(
80
+ thread_id=st.session_state.thread.id,
81
+ assistant_id=st.session_state.assistant.id,
82
+ )
83
+ if st.session_state.retry_error < 3:
84
+ time.sleep(1) # Wait 1 second before checking run status
85
+ st.rerun()
86
+
87
+ # Check if 'run' object has 'status' attribute
88
+ if hasattr(st.session_state.run, 'status'):
89
+ # Handle the 'running' status
90
+ if st.session_state.run.status == "running":
91
+ with st.chat_message('assistant'):
92
+ st.write("Thinking ......")
93
+ if st.session_state.retry_error < 3:
94
+ time.sleep(1) # Short delay to prevent immediate rerun, adjust as needed
95
+ st.rerun()
96
+
97
+ # Handle the 'failed' status
98
+ elif st.session_state.run.status == "failed":
99
+ st.session_state.retry_error += 1
100
+ with st.chat_message('assistant'):
101
+ if st.session_state.retry_error < 3:
102
+ st.write("Run failed, retrying ......")
103
+ time.sleep(3) # Longer delay before retrying
104
+ st.rerun()
105
+ else:
106
+ st.error("FAILED: The OpenAI API is currently processing too many requests. Please try again later ......")
107
+
108
+ # Handle any status that is not 'completed'
109
+ elif st.session_state.run.status != "completed":
110
+ # Attempt to retrieve the run again, possibly redundant if there's no other status but 'running' or 'failed'
111
+ st.session_state.run = client.beta.threads.runs.retrieve(
112
+ thread_id=st.session_state.thread.id,
113
+ run_id=st.session_state.run.id,
114
+ )
115
+ if st.session_state.retry_error < 3:
116
+ time.sleep(3)
117
+ st.rerun()