Skip to content

Commit

Permalink
added configurable prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
ruoccofabrizio committed Feb 13, 2023
1 parent c5c08df commit 9b48314
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 19 deletions.
11 changes: 7 additions & 4 deletions code/OpenAI_Queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def get_languages():
if 'question' not in st.session_state:
st.session_state['question'] = default_question
if 'prompt' not in st.session_state:
st.session_state['prompt'] = default_prompt
st.session_state['prompt'] = os.getenv("QUESTION_PROMPT", "Please reply to the question using only the information present in the text above. If you can't find it, reply 'Not in the text'.\nQuestion: _QUESTION_\nAnswer:").replace(r'\n', '\n')
if 'response' not in st.session_state:
st.session_state['response'] = {
"choices" :[{
Expand All @@ -31,6 +31,8 @@ def get_languages():
}
if 'limit_response' not in st.session_state:
st.session_state['limit_response'] = True
if 'full_prompt' not in st.session_state:
st.session_state['full_prompt'] = ""

# Set page layout to wide screen and menu item
menu_items = {
Expand All @@ -57,6 +59,7 @@ def get_languages():
"OpenAI GPT-3 Model",
(os.environ['OPENAI_ENGINES'].split(','))
)
st.text_area("Prompt",height=100, key='prompt')
st.tokens_response = st.slider("Tokens response length", 100, 500, 400)
st.temperature = st.slider("Temperature", 0.0, 1.0, 0.1)
st.selectbox("Language", [None] + list(available_languages.keys()), key='translation_language')
Expand All @@ -67,16 +70,16 @@ def get_languages():
if question != '':
if question != st.session_state['question']:
st.session_state['question'] = question
st.session_state['prompt'], st.session_state['response'] = utils.get_semantic_answer(df, question, model=model, engine='davinci', limit_response=st.session_state['limit_response'], tokens_response=st.tokens_response, temperature=st.temperature)
st.session_state['full_prompt'], st.session_state['response'] = utils.get_semantic_answer(df, question, st.session_state['prompt'] ,model=model, engine='davinci', limit_response=st.session_state['limit_response'], tokens_response=st.tokens_response, temperature=st.temperature)
st.write(f"Q: {question}")
st.write(st.session_state['response']['choices'][0]['text'])
with st.expander("Question and Answer Context"):
st.text(st.session_state['prompt'])
st.text(st.session_state['full_prompt'])
else:
st.write(f"Q: {st.session_state['question']}")
st.write(f"{st.session_state['response']['choices'][0]['text']}")
with st.expander("Question and Answer Context"):
st.text(st.session_state['prompt'].encode().decode())
st.text(st.session_state['full_prompt'].encode().decode())

if st.session_state['translation_language'] is not None:
st.write(f"Translation to other languages, 翻译成其他语言, النص باللغة العربية")
Expand Down
2 changes: 1 addition & 1 deletion code/pages/01_Add_Document.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def token_count():
st.session_state['doc_text'] = st.text_area(" or Add a new text content and the click on 'Compute Embeddings'", height=600)

with col2:
st.session_state['embeddings_model'] = st.selectbox('Embeddings models', (utils.get_embeddings_model()['doc']), disabled=True)
st.session_state['embeddings_model'] = st.selectbox('Embeddings models', [utils.get_embeddings_model()['doc']], disabled=True)
st.button("Compute Embeddings", on_click=embeddings)

with st.expander("View documents in the knowledge base", expanded=False):
Expand Down
25 changes: 11 additions & 14 deletions code/utilities/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,18 @@ def get_semantic_answer(df, question, explicit_prompt="", model="DaVinci-text",
restart_sequence = "\n\n"
question += "\n"

if explicit_prompt == "":
res = search_semantic_redis(df, question, n=3, pprint=False, engine=engine)

if len(res) == 0:
prompt = f"{question}"
elif limit_response:
res_text = "\n".join(res['text'][0:int(os.getenv("NUMBER_OF_EMBEDDINGS_FOR_QNA",1))])
question_prompt = os.getenv("QUESTION_PROMPT", "Please reply to the question using only the information present in the text above. If you can't find it, reply 'Not in the text'.\nQuestion: _QUESTION_\nAnswer:").replace(r'\n', '\n')
question_prompt = question_prompt.replace("_QUESTION_", question)
prompt = f"{res_text}{restart_sequence}{question_prompt}"
else:
prompt = f"{res_text}{restart_sequence}{question}"

res = search_semantic_redis(df, question, n=3, pprint=False, engine=engine)

if len(res) == 0:
prompt = f"{question}"
elif limit_response:
res_text = "\n".join(res['text'][0:int(os.getenv("NUMBER_OF_EMBEDDINGS_FOR_QNA",1))])
question_prompt = explicit_prompt.replace(r'\n', '\n')
question_prompt = question_prompt.replace("_QUESTION_", question)
prompt = f"{res_text}{restart_sequence}{question_prompt}"
else:
prompt = f"{explicit_prompt}{restart_sequence}{question}"
prompt = f"{res_text}{restart_sequence}{question}"


response = openai.Completion.create(
engine=model,
Expand Down

0 comments on commit 9b48314

Please sign in to comment.