Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -43,6 +43,28 @@ def get_ai_response(prompt, chat_history):
|
|
| 43 |
chat_history.insert(0, {"role": "system", "content": system_prompt})
|
| 44 |
chat_history.append({"role": "user", "content": prompt})
|
| 45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
try:
|
| 47 |
with st.chat_message("assistant", avatar=st.session_state.assistant_avatar):
|
| 48 |
stream = client.chat.completions.create(
|
|
@@ -50,7 +72,7 @@ def get_ai_response(prompt, chat_history):
|
|
| 50 |
messages=chat_history,
|
| 51 |
temperature=temperature,
|
| 52 |
top_p=top_p,
|
| 53 |
-
max_tokens=
|
| 54 |
stream=True,
|
| 55 |
)
|
| 56 |
|
|
@@ -215,7 +237,7 @@ with st.sidebar:
|
|
| 215 |
system_prompt = st.text_area("System Prompt", height=200)
|
| 216 |
temperature = st.slider("Temperature", min_value=0.0, max_value=1.0, value=0.3, step=0.1)
|
| 217 |
top_p = st.slider("Top-P", min_value=0.01, max_value=1.00, value=1.00, step=0.01)
|
| 218 |
-
|
| 219 |
st.header("Restore History")
|
| 220 |
history_input = st.text_area("Paste conversation history:", height=200)
|
| 221 |
if st.button("Restore History"):
|
|
|
|
| 43 |
chat_history.insert(0, {"role": "system", "content": system_prompt})
|
| 44 |
chat_history.append({"role": "user", "content": prompt})
|
| 45 |
|
| 46 |
+
system_tokens = num_tokens_from_string(system_message["content"])
|
| 47 |
+
user_tokens = num_tokens_from_string(user_message["content"])
|
| 48 |
+
|
| 49 |
+
# Maximum allowed tokens
|
| 50 |
+
context_length = 4096 - max_tokens
|
| 51 |
+
available_tokens = context_length - system_tokens - user_tokens
|
| 52 |
+
|
| 53 |
+
# Trim chat history if necessary
|
| 54 |
+
trimmed_history = []
|
| 55 |
+
total_tokens = 0
|
| 56 |
+
|
| 57 |
+
for message in reversed(chat_history):
|
| 58 |
+
message_tokens = num_tokens_from_string(message["content"])
|
| 59 |
+
if total_tokens + message_tokens <= available_tokens:
|
| 60 |
+
trimmed_history.insert(0, message)
|
| 61 |
+
total_tokens += message_tokens
|
| 62 |
+
else:
|
| 63 |
+
break
|
| 64 |
+
|
| 65 |
+
# Construct final message list
|
| 66 |
+
final_messages = [system_message] + trimmed_history + [user_message]
|
| 67 |
+
|
| 68 |
try:
|
| 69 |
with st.chat_message("assistant", avatar=st.session_state.assistant_avatar):
|
| 70 |
stream = client.chat.completions.create(
|
|
|
|
| 72 |
messages=chat_history,
|
| 73 |
temperature=temperature,
|
| 74 |
top_p=top_p,
|
| 75 |
+
max_tokens=max_tokens,
|
| 76 |
stream=True,
|
| 77 |
)
|
| 78 |
|
|
|
|
| 237 |
system_prompt = st.text_area("System Prompt", height=200)
|
| 238 |
temperature = st.slider("Temperature", min_value=0.0, max_value=1.0, value=0.3, step=0.1)
|
| 239 |
top_p = st.slider("Top-P", min_value=0.01, max_value=1.00, value=1.00, step=0.01)
|
| 240 |
+
max_tokens = st.slider("Max Tokens (Output)", min_value=1, max_value=1024, value=1024, step=1)
|
| 241 |
st.header("Restore History")
|
| 242 |
history_input = st.text_area("Paste conversation history:", height=200)
|
| 243 |
if st.button("Restore History"):
|