Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -72,6 +72,13 @@ GENRE_EXAMPLES = {
|
|
72 |
]
|
73 |
}
|
74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
def get_examples_for_genre(genre):
|
76 |
"""Get example prompts specific to the selected genre"""
|
77 |
return GENRE_EXAMPLES.get(genre, GENRE_EXAMPLES["fantasy"])
|
@@ -117,8 +124,20 @@ def format_history_for_gradio(history_tuples):
|
|
117 |
messages.append({"role": "assistant", "content": bot_msg})
|
118 |
return messages
|
119 |
|
120 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
121 |
"""Generate a response based on the current message and conversation history."""
|
|
|
|
|
|
|
|
|
|
|
122 |
system_message = get_enhanced_system_prompt(genre)
|
123 |
|
124 |
# Convert your existing (user, bot) history into a format for the API request
|
@@ -131,17 +150,17 @@ def respond(message, chat_history, genre=None, use_full_memory=True):
|
|
131 |
|
132 |
# Use full memory or partial memory
|
133 |
if use_full_memory and formatted_history:
|
134 |
-
if len(formatted_history) >
|
135 |
summary_instruction = create_story_summary(chat_history[:len(chat_history)-5])
|
136 |
if summary_instruction:
|
137 |
api_messages.append(summary_instruction)
|
138 |
-
for msg in formatted_history[-
|
139 |
api_messages.append(msg)
|
140 |
else:
|
141 |
for msg in formatted_history:
|
142 |
api_messages.append(msg)
|
143 |
else:
|
144 |
-
memory_length =
|
145 |
if formatted_history:
|
146 |
for msg in formatted_history[-memory_length*2:]:
|
147 |
api_messages.append(msg)
|
@@ -160,10 +179,10 @@ def respond(message, chat_history, genre=None, use_full_memory=True):
|
|
160 |
try:
|
161 |
for response_chunk in client.chat_completion(
|
162 |
api_messages,
|
163 |
-
max_tokens=
|
164 |
stream=True,
|
165 |
-
temperature=
|
166 |
-
top_p=
|
167 |
):
|
168 |
delta = response_chunk.choices[0].delta.content
|
169 |
if delta:
|
|
|
72 |
]
|
73 |
}
|
74 |
|
75 |
+
# 2. Add constants at the top for magic numbers
|
76 |
+
MAX_HISTORY_LENGTH = 20
|
77 |
+
MEMORY_WINDOW = 10
|
78 |
+
MAX_TOKENS = 512
|
79 |
+
TEMPERATURE = 0.7
|
80 |
+
TOP_P = 0.95
|
81 |
+
|
82 |
def get_examples_for_genre(genre):
|
83 |
"""Get example prompts specific to the selected genre"""
|
84 |
return GENRE_EXAMPLES.get(genre, GENRE_EXAMPLES["fantasy"])
|
|
|
124 |
messages.append({"role": "assistant", "content": bot_msg})
|
125 |
return messages
|
126 |
|
127 |
+
# 1. Add type hints for better code maintainability
|
128 |
+
# 4. Add input validation
|
129 |
+
def respond(
|
130 |
+
message: str,
|
131 |
+
chat_history: list[tuple[str, str]],
|
132 |
+
genre: str | None = None, # Make optional type more explicit
|
133 |
+
use_full_memory: bool = True
|
134 |
+
) -> Generator[list[dict[str, str]], None, None]: # More specific return type
|
135 |
"""Generate a response based on the current message and conversation history."""
|
136 |
+
if not message.strip():
|
137 |
+
return chat_history
|
138 |
+
if genre and genre not in GENRE_EXAMPLES:
|
139 |
+
genre = "fantasy" # fallback to default
|
140 |
+
|
141 |
system_message = get_enhanced_system_prompt(genre)
|
142 |
|
143 |
# Convert your existing (user, bot) history into a format for the API request
|
|
|
150 |
|
151 |
# Use full memory or partial memory
|
152 |
if use_full_memory and formatted_history:
|
153 |
+
if len(formatted_history) > MAX_HISTORY_LENGTH:
|
154 |
summary_instruction = create_story_summary(chat_history[:len(chat_history)-5])
|
155 |
if summary_instruction:
|
156 |
api_messages.append(summary_instruction)
|
157 |
+
for msg in formatted_history[-MEMORY_WINDOW:]:
|
158 |
api_messages.append(msg)
|
159 |
else:
|
160 |
for msg in formatted_history:
|
161 |
api_messages.append(msg)
|
162 |
else:
|
163 |
+
memory_length = MEMORY_WINDOW
|
164 |
if formatted_history:
|
165 |
for msg in formatted_history[-memory_length*2:]:
|
166 |
api_messages.append(msg)
|
|
|
179 |
try:
|
180 |
for response_chunk in client.chat_completion(
|
181 |
api_messages,
|
182 |
+
max_tokens=MAX_TOKENS,
|
183 |
stream=True,
|
184 |
+
temperature=TEMPERATURE,
|
185 |
+
top_p=TOP_P,
|
186 |
):
|
187 |
delta = response_chunk.choices[0].delta.content
|
188 |
if delta:
|