Spaces:
Running
Running
Add system prompt functionality and update response handling
Browse files- Modify respond_and_clear to accept a system prompt parameter.
- Implement save_system_prompt to save the current system prompt to user preferences.
- Load saved system prompt during app initialization.
- Add UI element for editing the system prompt in the chat interface.
app.py
CHANGED
|
@@ -568,7 +568,7 @@ def save_chat_history(history, conversation_id):
|
|
| 568 |
logger.error(f"Error saving chat history: {str(e)}")
|
| 569 |
return False
|
| 570 |
|
| 571 |
-
def respond_and_clear(message, history, conversation_id):
|
| 572 |
"""Wrapper function with proper output handling"""
|
| 573 |
try:
|
| 574 |
# Get current model parameters
|
|
@@ -579,7 +579,7 @@ def respond_and_clear(message, history, conversation_id):
|
|
| 579 |
message=message,
|
| 580 |
history=history if history else [],
|
| 581 |
conversation_id=conversation_id,
|
| 582 |
-
system_message=DEFAULT_SYSTEM_MESSAGE
|
| 583 |
max_tokens=params['max_length'],
|
| 584 |
temperature=params['temperature'],
|
| 585 |
top_p=params['top_p']
|
|
@@ -778,6 +778,32 @@ def finetune_from_annotations(epochs=3, batch_size=4, learning_rate=2e-4, min_ra
|
|
| 778 |
|
| 779 |
except Exception as e:
|
| 780 |
return False, f"Error during fine-tuning from annotations: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 781 |
|
| 782 |
def initialize_app():
|
| 783 |
"""Initialize app with user preferences"""
|
|
@@ -804,8 +830,13 @@ def initialize_app():
|
|
| 804 |
token=HF_TOKEN
|
| 805 |
)
|
| 806 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 807 |
logger.info(f"App initialized with model: {ACTIVE_MODEL['name']}")
|
| 808 |
-
return selected_model
|
| 809 |
|
| 810 |
def initialize_chat_evaluator():
|
| 811 |
"""Initialize chat evaluator with proper paths"""
|
|
@@ -830,7 +861,7 @@ def initialize_chat_evaluator():
|
|
| 830 |
raise
|
| 831 |
|
| 832 |
# Initialize HF client with token at startup
|
| 833 |
-
selected_model = initialize_app()
|
| 834 |
|
| 835 |
# Create interface
|
| 836 |
with gr.Blocks() as demo:
|
|
@@ -861,19 +892,34 @@ with gr.Blocks() as demo:
|
|
| 861 |
)
|
| 862 |
submit_btn = gr.Button("Send", variant="primary")
|
| 863 |
clear_btn = gr.Button("Clear")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 864 |
|
| 865 |
# Add event handlers
|
|
|
|
| 866 |
submit_btn.click(
|
| 867 |
respond_and_clear,
|
| 868 |
-
[msg, chatbot, conversation_id],
|
| 869 |
[chatbot, conversation_id, msg]
|
| 870 |
)
|
| 871 |
-
#
|
| 872 |
msg.submit(
|
| 873 |
respond_and_clear,
|
| 874 |
-
[msg, chatbot, conversation_id],
|
| 875 |
[chatbot, conversation_id, msg]
|
| 876 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 877 |
|
| 878 |
clear_btn.click(clear_conversation, None, [chatbot, conversation_id])
|
| 879 |
|
|
|
|
| 568 |
logger.error(f"Error saving chat history: {str(e)}")
|
| 569 |
return False
|
| 570 |
|
| 571 |
+
def respond_and_clear(message, history, conversation_id, system_prompt):
|
| 572 |
"""Wrapper function with proper output handling"""
|
| 573 |
try:
|
| 574 |
# Get current model parameters
|
|
|
|
| 579 |
message=message,
|
| 580 |
history=history if history else [],
|
| 581 |
conversation_id=conversation_id,
|
| 582 |
+
system_message=system_prompt, # Используем переданный промпт вместо DEFAULT_SYSTEM_MESSAGE
|
| 583 |
max_tokens=params['max_length'],
|
| 584 |
temperature=params['temperature'],
|
| 585 |
top_p=params['top_p']
|
|
|
|
| 778 |
|
| 779 |
except Exception as e:
|
| 780 |
return False, f"Error during fine-tuning from annotations: {str(e)}"
|
| 781 |
+
|
| 782 |
+
def save_system_prompt(prompt_text):
|
| 783 |
+
"""Save system prompt to user preferences"""
|
| 784 |
+
try:
|
| 785 |
+
preferences = load_user_preferences()
|
| 786 |
+
|
| 787 |
+
# Добавляем промпт в настройки
|
| 788 |
+
if "system_prompt" not in preferences:
|
| 789 |
+
preferences["system_prompt"] = {}
|
| 790 |
+
|
| 791 |
+
preferences["system_prompt"]["current"] = prompt_text
|
| 792 |
+
|
| 793 |
+
# Сохраняем настройки
|
| 794 |
+
json_content = json.dumps(preferences, indent=2)
|
| 795 |
+
api = HfApi(token=HF_TOKEN)
|
| 796 |
+
api.upload_file(
|
| 797 |
+
path_or_fileobj=io.StringIO(json_content),
|
| 798 |
+
path_in_repo="preferences/user_preferences.json",
|
| 799 |
+
repo_id=DATASET_ID,
|
| 800 |
+
repo_type="dataset"
|
| 801 |
+
)
|
| 802 |
+
|
| 803 |
+
return "Системный промпт сохранен"
|
| 804 |
+
except Exception as e:
|
| 805 |
+
logger.error(f"Error saving system prompt: {str(e)}")
|
| 806 |
+
return f"Ошибка сохранения промпта: {str(e)}"
|
| 807 |
|
| 808 |
def initialize_app():
|
| 809 |
"""Initialize app with user preferences"""
|
|
|
|
| 830 |
token=HF_TOKEN
|
| 831 |
)
|
| 832 |
|
| 833 |
+
# Загружаем сохраненный системный промпт из предпочтений или используем DEFAULT_SYSTEM_MESSAGE
|
| 834 |
+
system_prompt_text = DEFAULT_SYSTEM_MESSAGE
|
| 835 |
+
if "system_prompt" in preferences and "current" in preferences["system_prompt"]:
|
| 836 |
+
system_prompt_text = preferences["system_prompt"]["current"]
|
| 837 |
+
|
| 838 |
logger.info(f"App initialized with model: {ACTIVE_MODEL['name']}")
|
| 839 |
+
return selected_model, system_prompt_text
|
| 840 |
|
| 841 |
def initialize_chat_evaluator():
|
| 842 |
"""Initialize chat evaluator with proper paths"""
|
|
|
|
| 861 |
raise
|
| 862 |
|
| 863 |
# Initialize HF client with token at startup
|
| 864 |
+
selected_model, saved_system_prompt = initialize_app()
|
| 865 |
|
| 866 |
# Create interface
|
| 867 |
with gr.Blocks() as demo:
|
|
|
|
| 892 |
)
|
| 893 |
submit_btn = gr.Button("Send", variant="primary")
|
| 894 |
clear_btn = gr.Button("Clear")
|
| 895 |
+
|
| 896 |
+
with gr.Row():
|
| 897 |
+
system_prompt = gr.TextArea(
|
| 898 |
+
label="System Prompt (редактирование изменит поведение бота)",
|
| 899 |
+
value=DEFAULT_SYSTEM_MESSAGE,
|
| 900 |
+
placeholder="Enter system prompt...",
|
| 901 |
+
lines=5
|
| 902 |
+
)
|
| 903 |
|
| 904 |
# Add event handlers
|
| 905 |
+
# Обновляем обработчики событий
|
| 906 |
submit_btn.click(
|
| 907 |
respond_and_clear,
|
| 908 |
+
[msg, chatbot, conversation_id, system_prompt], # Добавляем system_prompt
|
| 909 |
[chatbot, conversation_id, msg]
|
| 910 |
)
|
| 911 |
+
# Обновляем обработчик нажатия Enter
|
| 912 |
msg.submit(
|
| 913 |
respond_and_clear,
|
| 914 |
+
[msg, chatbot, conversation_id, system_prompt], # Добавляем system_prompt
|
| 915 |
[chatbot, conversation_id, msg]
|
| 916 |
)
|
| 917 |
+
# Добавляем обработчик изменения промпта
|
| 918 |
+
system_prompt.change(
|
| 919 |
+
save_system_prompt,
|
| 920 |
+
inputs=[system_prompt],
|
| 921 |
+
outputs=[]
|
| 922 |
+
)
|
| 923 |
|
| 924 |
clear_btn.click(clear_conversation, None, [chatbot, conversation_id])
|
| 925 |
|