Skip to content

Commit

Permalink
Patrick-promptinghelp (#1)
Browse files Browse the repository at this point in the history
* Added prompting tips in sidebar

* Fixed bugs with prompt help button, moved prompt_tips.py to components

* fix tip button placement

* use existing function to init client
  • Loading branch information
patrickswnsn authored Jul 12, 2024
1 parent 253b989 commit 790db7d
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 18 deletions.
4 changes: 3 additions & 1 deletion components/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ def handle_user_query(placeholder, client, thread_id, assistant_id):
{"role": "user", "content": user_query})
client.beta.threads.messages.create(
thread_id=thread_id, role="user", content=user_query)
# Store the latest user prompt in the session state
st.session_state.latest_user_prompt = user_query
stream_assistant_reply(client, thread_id, assistant_id)


Expand Down Expand Up @@ -70,4 +72,4 @@ def versobot(assistant_id, initial_message, placeholder="Chat with Versobot"):
initialize_chat_history(initial_message)
display_chat_history()
handle_user_query(placeholder, client,
st.session_state.thread_id, assistant_id)
st.session_state.thread_id, assistant_id)
53 changes: 53 additions & 0 deletions components/prompt_tips.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import openai
from utils.config import get_openai_api_key
from components.chat import initialize_client

# Initialize the OpenAI client
client = initialize_client()

def get_prompt_tips(user_prompt, lang):
"""
Generate prompt improvement tips based on the user's input in the specified language.
Args:
user_prompt (str): The prompt submitted by the user.
lang (str): The language to use for generating tips ('English' or 'Deutsch').
Returns:
str: Two tips for improving the prompt in the specified language.
"""
system_messages = {
"English": """You are an AI assistant specializing in helping journalists improve their prompts for AI-based research and writing.
Analyze the given prompt and provide two concise tips for improvement, focusing on aspects such as:
1. Clarity and specificity
2. Use of few-shot examples
3. Implementing chain-of-thought reasoning
4. Providing necessary context
5. Ethical considerations in journalism
Keep each tip brief and actionable.""",

"Deutsch": """Sie sind ein KI-Assistent, der sich darauf spezialisiert hat, Journalisten bei der Verbesserung ihrer Prompts für KI-basierte Recherchen und Schreibarbeiten zu unterstützen.
Analysieren Sie den gegebenen Prompt und geben Sie zwei prägnante Tipps zur Verbesserung, wobei Sie sich auf Aspekte wie die folgenden konzentrieren:
1. Klarheit und Spezifität
2. Verwendung von Few-Shot-Beispielen
3. Implementierung von Chain-of-Thought-Reasoning
4. Bereitstellung des notwendigen Kontexts
5. Ethische Überlegungen im Journalismus
Halten Sie jeden Tipp kurz und umsetzbar."""
}

user_messages = {
"English": f"Please provide two tips to improve this prompt: '{user_prompt}'",
"Deutsch": f"Bitte geben Sie zwei Tipps zur Verbesserung dieses Prompts: '{user_prompt}'"
}

response = client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "system", "content": system_messages[lang]},
{"role": "user", "content": user_messages[lang]}
],
max_tokens=150 # Adjust as needed
)

return response.choices[0].message.content
62 changes: 45 additions & 17 deletions streamlit_app.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import streamlit as st
from components.chat import versobot
from components.prompt_tips import get_prompt_tips
from utils.session import initialize_session_states, chat_button, reset_chat
from utils.config import set_up_page

Expand All @@ -10,14 +11,20 @@
"chat_button_text": "Clear Chat",
"initial_message": "Hi, I'm **Versobot!** How can I help?",
"placeholder": "Chat with Versobot",
"assistant_id": "asst_i5ylsjPx1CWwe0xJFOOQ80yC"
"assistant_id": "asst_i5ylsjPx1CWwe0xJFOOQ80yC",
"prompt_tips_button": "Get Prompt Tips",
"prompt_tips_header": "Prompt Improvement Tips:",
"no_prompt_message": "No prompt available. Please enter a prompt in the chat first."
},
"Deutsch": {
"sidebar_text": "**Versobot** läuft auf OpenAIs GPT-4o. Setzen Sie den Chat mit dem untenstehenden Button zurück.",
"chat_button_text": "Chat löschen",
"initial_message": "Hallo, ich bin **Versobot!** Wie kann ich helfen?",
"placeholder": "Mit Versobot chatten",
"assistant_id": "asst_uXO7y5oCdYpj2RlxWC2tiRP6"
"assistant_id": "asst_uXO7y5oCdYpj2RlxWC2tiRP6",
"prompt_tips_button": "Prompting-Tipps",
"prompt_tips_header": "Tipps zur Verbesserung des Prompts:",
"no_prompt_message": "Kein Prompt verfügbar. Bitte geben Sie zuerst einen Prompt im Chat ein."
}
}

Expand All @@ -26,25 +33,46 @@
initialize_session_states()
st.session_state.chat = True # Start chat by default

# Sidebar with reset button
# Initialize session state for the latest user prompt
if 'latest_user_prompt' not in st.session_state:
st.session_state.latest_user_prompt = ""

# Sidebar with reset button and prompt tips
with st.sidebar:
lang = st.selectbox("Language",
options=list(LANG_STRINGS.keys()),
index=1,
index=0, # Set to English by default
label_visibility="collapsed",
on_change=reset_chat)
LANG_STRINGS[lang]["sidebar_text"]
button_placeholder = st.empty()
st.markdown(LANG_STRINGS[lang]["sidebar_text"])

# Create placeholders for the button and prompt tips
chat_button_placeholder = st.empty()

st.divider()

tips_button_placeholder = st.empty()
tips_placeholder = st.empty()

# Always show the "Clear Chat" button
if chat_button(chat_button_placeholder,
LANG_STRINGS[lang]["chat_button_text"],
primary=True):
reset_chat()
st.rerun()

# Add button for prompt tips
if tips_button_placeholder.button(LANG_STRINGS[lang]["prompt_tips_button"]):
if st.session_state.latest_user_prompt:
tips = get_prompt_tips(st.session_state.latest_user_prompt, lang)
tips_placeholder.markdown(LANG_STRINGS[lang]["prompt_tips_header"])
tips_placeholder.write(tips)
else:
tips_placeholder.write(LANG_STRINGS[lang]["no_prompt_message"])

# Chat interface
if chat_button(button_placeholder,
LANG_STRINGS[lang]["chat_button_text"],
primary=True):
reset_chat()
st.rerun()
else:
versobot(
assistant_id=LANG_STRINGS[lang]["assistant_id"],
initial_message=LANG_STRINGS[lang]["initial_message"],
placeholder=LANG_STRINGS[lang]["placeholder"]
)
versobot(
assistant_id=LANG_STRINGS[lang]["assistant_id"],
initial_message=LANG_STRINGS[lang]["initial_message"],
placeholder=LANG_STRINGS[lang]["placeholder"]
)
1 change: 1 addition & 0 deletions utils/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ def reset_chat():
st.session_state.initial_state = st.session_state.chat_history = []
if 'thread_id' in st.session_state:
del st.session_state.thread_id
st.session_state.latest_user_prompt = ""

0 comments on commit 790db7d

Please sign in to comment.