Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -136,39 +136,48 @@ def respond(
|
|
136 |
chat_history: List[Tuple[str, str]],
|
137 |
genre: Optional[str] = None,
|
138 |
use_full_memory: bool = True
|
139 |
-
) -> List[Tuple[str, str]]:
|
140 |
"""Generate a response based on the current message and conversation history."""
|
141 |
if not message.strip():
|
142 |
return chat_history
|
143 |
|
144 |
-
#
|
145 |
formatted_messages = [{"role": "system", "content": get_enhanced_system_prompt(genre)}]
|
146 |
|
147 |
-
#
|
148 |
if chat_history and use_full_memory:
|
149 |
for user_msg, bot_msg in chat_history[-MEMORY_WINDOW:]:
|
150 |
-
formatted_messages.
|
151 |
-
|
|
|
|
|
152 |
|
153 |
-
#
|
154 |
-
formatted_messages.append({"role": "user", "content":
|
155 |
|
156 |
try:
|
157 |
-
# Make API call
|
158 |
response = client.chat_completion(
|
159 |
messages=formatted_messages,
|
160 |
max_tokens=MAX_TOKENS,
|
161 |
temperature=TEMPERATURE,
|
162 |
-
top_p=TOP_P
|
|
|
163 |
)
|
164 |
-
|
165 |
-
# Extract
|
166 |
-
|
167 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
168 |
|
169 |
except Exception as e:
|
170 |
error_message = f"Story magic temporarily interrupted. Please try again. (Error: {str(e)})"
|
171 |
-
return chat_history + [(
|
172 |
|
173 |
def save_story(chat_history):
|
174 |
"""Convert chat history to markdown for download"""
|
|
|
136 |
chat_history: List[Tuple[str, str]],
|
137 |
genre: Optional[str] = None,
|
138 |
use_full_memory: bool = True
|
139 |
+
) -> List[Tuple[str, str]]:
|
140 |
"""Generate a response based on the current message and conversation history."""
|
141 |
if not message.strip():
|
142 |
return chat_history
|
143 |
|
144 |
+
# Format messages for API
|
145 |
formatted_messages = [{"role": "system", "content": get_enhanced_system_prompt(genre)}]
|
146 |
|
147 |
+
# Format chat history
|
148 |
if chat_history and use_full_memory:
|
149 |
for user_msg, bot_msg in chat_history[-MEMORY_WINDOW:]:
|
150 |
+
formatted_messages.extend([
|
151 |
+
{"role": "user", "content": user_msg},
|
152 |
+
{"role": "assistant", "content": bot_msg}
|
153 |
+
])
|
154 |
|
155 |
+
# Add current message
|
156 |
+
formatted_messages.append({"role": "user", "content": message})
|
157 |
|
158 |
try:
|
159 |
+
# Make API call
|
160 |
response = client.chat_completion(
|
161 |
messages=formatted_messages,
|
162 |
max_tokens=MAX_TOKENS,
|
163 |
temperature=TEMPERATURE,
|
164 |
+
top_p=TOP_P,
|
165 |
+
stream=False # Ensure non-streaming response
|
166 |
)
|
167 |
+
|
168 |
+
# Extract response safely using proper attribute access
|
169 |
+
if hasattr(response.choices[0], 'message'):
|
170 |
+
bot_message = response.choices[0].message.content
|
171 |
+
else:
|
172 |
+
bot_message = str(response.choices[0].delta.content)
|
173 |
+
|
174 |
+
new_history = list(chat_history)
|
175 |
+
new_history.append((message, bot_message))
|
176 |
+
return new_history
|
177 |
|
178 |
except Exception as e:
|
179 |
error_message = f"Story magic temporarily interrupted. Please try again. (Error: {str(e)})"
|
180 |
+
return list(chat_history) + [(message, error_message)]
|
181 |
|
182 |
def save_story(chat_history):
|
183 |
"""Convert chat history to markdown for download"""
|