Skip to content

Commit

Permalink
chore: create prompt.json as separate file for ease of alteration
Browse files Browse the repository at this point in the history
  • Loading branch information
sean1832 committed Apr 1, 2023
1 parent 302f7c7 commit a76a948
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 30 deletions.
42 changes: 42 additions & 0 deletions resources/prompt.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
[
{
"type": "recursive",
"legacy": true,
"prompt": "Provide a detailed and comprehensive summary of the following content in flawless [LANGUAGE], ensuring all key points are covered. Create a markdown heading (###) that encapsulates the core information of the content. Make sure it is answered in [LANGUAGE].",
"variables": [
{
"name": "[LANGUAGE]"
}
]
},
{
"type": "recursive",
"legacy": false,
"prompt": "Write a detailed and comprehensive explanation of the following in perfect [LANGUAGE] with no grammar issues, ensuring all key points are covered. Create a markdown heading (###) that encapsulates the core information:\n\n{text}\n\nStructured markdown summary with heading (###) in fluent [LANGUAGE]:",
"variables": [
{
"name": "[LANGUAGE]"
}
]
},
{
"type": "final",
"legacy": true,
"prompt": "identify headings in the transcript and summarise them into five headings. Use #### headings in markdown. Under headings, summarise at least 3 key points and then provide detail explanation of the concept based on the following text in the way that can be read fluently, make sense and avoid repetition. Make sure to include all information. Write it in beautiful and structured markdown in perfect [LANGUAGE]. ",
"variables": [
{
"name": "[LANGUAGE]"
}
]
},
{
"type": "final",
"legacy": false,
"prompt": "Write a detailed summary of the following in [LANGUAGE]:\n\n{text}\n\nIdentify and summarise them into five headings. Use #### headings in markdown. Under headings, summarize a list of key points that best encapsulate the core information. Structured markdown summary with headings in perfect [LANGUAGE] (####): ",
"variables": [
{
"name": "[LANGUAGE]"
}
]
}
]
47 changes: 19 additions & 28 deletions src/Components/sidebar.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import Modules.file_io as file_io
from streamlit_toggle import st_toggle_switch
import Components
from typing import Any, Dict, List, Tuple, Union
import json


Expand Down Expand Up @@ -47,7 +48,13 @@ def _legacy(enable: bool, legacy, experimental):
return experimental
else:
return legacy

def _extract_prompt(json_data: List[Dict[str,Union[bool, str]]], target_type: str, target_legacy: bool, language: str = "English") -> str | None:
for item in json_data:
if item["type"] == target_type and item["legacy"] == target_legacy:
prompt = item["prompt"]
new_prompt = prompt.replace("[LANGUAGE]", language)
return new_prompt
return None

def sidebar():
with st.sidebar:
Expand Down Expand Up @@ -77,41 +84,25 @@ def sidebar():
language_options = ['English', 'Chinese', 'Japanese', 'Korean', 'Spanish', 'French', 'German']
language_index = language_options.index(_set_config(config_file, "LANGUAGE", 'English'))
language = st.selectbox('Language', options=language_options, index=language_index)
default_persona_rec_legacy = 'Provide a detailed and comprehensive summary of the following content in flawless ' \
f'{language}, ensuring all key points are covered. Create a markdown heading (###) that ' \
f'encapsulates the core information of the content. Make sure it is answered in {language}.'
default_persona_rec = f"""Write a detailed and comprehensive explanation of the following in perfect {language} with no grammar issues,
ensuring all key points are covered. Create a markdown heading (###) that encapsulates the core information:""" + \
"""
{text}
""" + \
f"""Structured markdown explanation with heading (###) in perfect {language}: """
_set_language(language)

prompts = file_io.read_json("resources/prompt.json")

persona_rec_legacy = _extract_prompt(prompts, "recursive", True, language)
persona_rec = _extract_prompt(prompts, "recursive", False, language)
persona_rec = st.text_area('Bot Persona Recursive',
value=_set_config(config_file, "OPENAI_PERSONA_REC", _legacy(enable_legacy, default_persona_rec_legacy, default_persona_rec)),
value=_set_config(config_file, "OPENAI_PERSONA_REC", _legacy(enable_legacy, persona_rec_legacy, persona_rec)),
help='System message is a pre-defined message used to instruct the assistant at the '
'beginning of a conversation. iterating and '
'experimenting with potential improvements can help to generate better outputs.'
'Make sure to use casual language.',
height=250)
if enable_final_summary:
default_persona_sum_legacy = 'identify headings in the transcript and summarise them into five ' \
'headings. Use #### headings in markdown. Under headings, summarise at least ' \
'3 key points and then provide detail explanation of the concept based on the ' \
'following text in the way that can be read fluently, make sense and avoid ' \
'repetition. Make sure to include all information. Write it in beautiful and ' \
f'structured markdown in perfect {language}. '
default_persona_sum = f"""Write a detailed summary of the following in {language}:
""" + \
"""
{text}
Identify and summarise them into five headings. Use #### headings in markdown. Under headings, summarize a list of key points that best encapsulate the core information.""" + \
f"""Structured markdown summary with headings in perfect {language} (####): """
persona_sum_legacy = _extract_prompt(prompts, "final", True, language)
persona_sum = _extract_prompt(prompts, "final", False, language)

persona_sum = st.text_area('Bot Persona Total Sum',
value=_set_config(config_file, "OPENAI_PERSONA_SUM", _legacy(enable_legacy, default_persona_sum_legacy, default_persona_sum)),
value=_set_config(config_file, "OPENAI_PERSONA_SUM", _legacy(enable_legacy, persona_sum_legacy, persona_sum)),
help='This is a pre-defined message for total summarization that is used to'
'instruct the assistant at the beginning of a conversation. ',
height=300)
Expand Down Expand Up @@ -187,7 +178,7 @@ def sidebar():

if persona_rec:
set_openai_persona(persona_rec, persona_sum)
_set_language(language)

set_chunk_size(chunk_size)
set_param(param)
set_delay(delay)
Expand Down
10 changes: 8 additions & 2 deletions src/Modules/file_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,18 @@
import streamlit as st



@st.cache_data()
def read_json(file, key: str) -> Any:
def read_json(file, key: str = None) -> Any:
"""Reads a json file and returns the value of a key."""
with open(file, "r") as f:
data = json.load(f)
return data[key]
if key and isinstance(data, dict):
return data[key]
elif key and isinstance(data, list):
return [d[key] for d in data]
else:
return data


@st.cache_data()
Expand Down

0 comments on commit a76a948

Please sign in to comment.