Spaces:
Running
Running
Add tabs & tab for panel discussion
Browse files
app.py
CHANGED
|
@@ -4,13 +4,15 @@ import streamlit as st
|
|
| 4 |
from ring import LLM_GPT35
|
| 5 |
from ring import LLM_GPT4
|
| 6 |
from ring import LLM_LLAMA2
|
|
|
|
|
|
|
| 7 |
|
| 8 |
from ring import requires_openai_key
|
| 9 |
from ring import get_openai_api_key
|
| 10 |
from ring import generate_response
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
-
__version__ = '0.1.4'
|
| 14 |
|
| 15 |
col1, col2 = st.columns([3, 1])
|
| 16 |
|
|
@@ -19,23 +21,50 @@ with col1:
|
|
| 19 |
st.markdown("**OpenWorm LLM v%s** - work in progress!"%__version__)
|
| 20 |
|
| 21 |
with col2:
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
|
| 25 |
|
|
|
|
| 26 |
|
|
|
|
| 27 |
|
| 28 |
-
with st.form("
|
| 29 |
-
|
| 30 |
-
|
| 31 |
|
| 32 |
-
|
| 33 |
|
| 34 |
-
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
|
|
|
|
| 4 |
from ring import LLM_GPT35
|
| 5 |
from ring import LLM_GPT4
|
| 6 |
from ring import LLM_LLAMA2
|
| 7 |
+
from ring import PREF_ORDER_LLMS
|
| 8 |
+
|
| 9 |
|
| 10 |
from ring import requires_openai_key
|
| 11 |
from ring import get_openai_api_key
|
| 12 |
from ring import generate_response
|
| 13 |
+
from ring import generate_panel_response
|
| 14 |
|
| 15 |
+
__version__ = '0.1.5'
|
|
|
|
| 16 |
|
| 17 |
col1, col2 = st.columns([3, 1])
|
| 18 |
|
|
|
|
| 21 |
st.markdown("**OpenWorm LLM v%s** - work in progress!"%__version__)
|
| 22 |
|
| 23 |
with col2:
|
| 24 |
+
st.image("images/OpenWormLogo.png")
|
| 25 |
+
|
| 26 |
+
tab_free, tab_panel, tab_pubs = st.tabs(["Individual LLMs", "Panel discussion", "Publications"])
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
with tab_free:
|
| 30 |
+
|
| 31 |
+
st.markdown("**Ask individual LLMs questions**")
|
| 32 |
+
|
| 33 |
+
with st.form("form_free"):
|
| 34 |
+
|
| 35 |
+
text = st.text_area("Ask a question related to _C. elegans_:", "What is the primary role of the C. elegans neuron AVBL?")
|
| 36 |
+
|
| 37 |
+
llm_ver = st.selectbox('Which LLM version should I use?', PREF_ORDER_LLMS)
|
| 38 |
+
|
| 39 |
+
temperature = st.text_input("Temperature", value=0.1)
|
| 40 |
+
|
| 41 |
+
submitted = st.form_submit_button("Submit")
|
| 42 |
+
|
| 43 |
+
if requires_openai_key(llm_ver) and not get_openai_api_key():
|
| 44 |
+
st.info("Please add your OpenAI API key to continue.")
|
| 45 |
+
elif submitted:
|
| 46 |
+
response = generate_response(text, llm_ver, temperature)
|
| 47 |
|
| 48 |
+
st.info(response)
|
| 49 |
|
| 50 |
+
with tab_panel:
|
| 51 |
|
| 52 |
+
st.markdown("**Get a consensus answer across multiple LLMs**")
|
| 53 |
|
| 54 |
+
with st.form("form_panel"):
|
| 55 |
+
|
| 56 |
+
text = st.text_area("Ask a question related to _C. elegans_:", "What is the typical length of the worm C. elegans?")
|
| 57 |
|
| 58 |
+
temperature = st.text_input("Temperature", value=0.1)
|
| 59 |
|
| 60 |
+
submitted = st.form_submit_button("Submit")
|
| 61 |
|
| 62 |
+
if requires_openai_key(llm_ver) and not get_openai_api_key():
|
| 63 |
+
st.info("Please add your OpenAI API key to continue.")
|
| 64 |
+
elif submitted:
|
| 65 |
+
response = generate_panel_response(text,
|
| 66 |
+
llm_panelists = [LLM_GPT35, LLM_LLAMA2],
|
| 67 |
+
llm_panel_chair = LLM_GPT4,
|
| 68 |
+
temperature=temperature)
|
| 69 |
|
| 70 |
+
st.info(response)
|
ring.py
CHANGED
|
@@ -13,6 +13,8 @@ LLM_LLAMA2 = 'LLAMA2'
|
|
| 13 |
|
| 14 |
OPENAI_LLMS = [LLM_GPT35, LLM_GPT4]
|
| 15 |
|
|
|
|
|
|
|
| 16 |
def requires_openai_key(llm_ver):
|
| 17 |
|
| 18 |
return llm_ver in OPENAI_LLMS
|
|
@@ -35,21 +37,8 @@ def get_llamaapi_key():
|
|
| 35 |
|
| 36 |
return llamaapi_key
|
| 37 |
|
|
|
|
| 38 |
|
| 39 |
-
def generate_response(input_text, llm_ver, temperature):
|
| 40 |
-
|
| 41 |
-
template = """You are a neuroscientist who is answering questions about the worm C. elegans. Provide succinct, yet scientifically accurate
|
| 42 |
-
answers. If the question is not related to biology, physics or chemistry, then don't answer the question, but instead explain that you
|
| 43 |
-
can currently only answer questions related to C. elegans. Question: {question}
|
| 44 |
-
|
| 45 |
-
Answer: """
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
prompt = PromptTemplate(
|
| 49 |
-
template=template,
|
| 50 |
-
input_variables=['question']
|
| 51 |
-
)
|
| 52 |
-
|
| 53 |
if llm_ver==LLM_GPT35:
|
| 54 |
llm = OpenAI(temperature=temperature, openai_api_key=get_openai_api_key())
|
| 55 |
|
|
@@ -73,6 +62,23 @@ def generate_response(input_text, llm_ver, temperature):
|
|
| 73 |
|
| 74 |
llm = ChatLlamaAPI(client=llama)
|
| 75 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
llm_chain = LLMChain(
|
| 77 |
prompt=prompt,
|
| 78 |
llm=llm
|
|
@@ -81,3 +87,71 @@ def generate_response(input_text, llm_ver, temperature):
|
|
| 81 |
response = llm_chain.invoke(input_text)['text']
|
| 82 |
|
| 83 |
return response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
OPENAI_LLMS = [LLM_GPT35, LLM_GPT4]
|
| 15 |
|
| 16 |
+
PREF_ORDER_LLMS = (LLM_LLAMA2, LLM_GPT35, LLM_GPT4)
|
| 17 |
+
|
| 18 |
def requires_openai_key(llm_ver):
|
| 19 |
|
| 20 |
return llm_ver in OPENAI_LLMS
|
|
|
|
| 37 |
|
| 38 |
return llamaapi_key
|
| 39 |
|
| 40 |
+
def get_llm(llm_ver, temperature):
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
if llm_ver==LLM_GPT35:
|
| 43 |
llm = OpenAI(temperature=temperature, openai_api_key=get_openai_api_key())
|
| 44 |
|
|
|
|
| 62 |
|
| 63 |
llm = ChatLlamaAPI(client=llama)
|
| 64 |
|
| 65 |
+
return llm
|
| 66 |
+
|
| 67 |
+
GENERAL_QUERY_PROMPT_TEMPLATE = """You are a neuroscientist who is answering questions about the worm C. elegans. Provide succinct, yet scientifically accurate
|
| 68 |
+
answers. If the question is not related to biology, physics or chemistry, then don't answer the question, but instead explain that you
|
| 69 |
+
can currently only answer questions related to C. elegans. Question: {question}
|
| 70 |
+
|
| 71 |
+
Answer: """
|
| 72 |
+
|
| 73 |
+
def generate_response(input_text, llm_ver, temperature):
|
| 74 |
+
|
| 75 |
+
prompt = PromptTemplate(
|
| 76 |
+
template=GENERAL_QUERY_PROMPT_TEMPLATE,
|
| 77 |
+
input_variables=['question']
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
llm = get_llm(llm_ver, temperature)
|
| 81 |
+
|
| 82 |
llm_chain = LLMChain(
|
| 83 |
prompt=prompt,
|
| 84 |
llm=llm
|
|
|
|
| 87 |
response = llm_chain.invoke(input_text)['text']
|
| 88 |
|
| 89 |
return response
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
def generate_panel_response(input_text, llm_panelists, llm_panel_chair, temperature):
|
| 94 |
+
|
| 95 |
+
responses = {}
|
| 96 |
+
|
| 97 |
+
for llm_ver in llm_panelists:
|
| 98 |
+
|
| 99 |
+
prompt = PromptTemplate(
|
| 100 |
+
template=GENERAL_QUERY_PROMPT_TEMPLATE,
|
| 101 |
+
input_variables=['question']
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
+
llm = get_llm(llm_ver, temperature)
|
| 105 |
+
|
| 106 |
+
llm_chain = LLMChain(
|
| 107 |
+
prompt=prompt,
|
| 108 |
+
llm=llm
|
| 109 |
+
)
|
| 110 |
+
responses[llm_ver] = llm_chain.invoke(input_text)['text']
|
| 111 |
+
|
| 112 |
+
panel_chair_prompt = """You are a neuroscientist chairing a panel discussion on the nematode C. elegans. A researcher has asked the following question:
|
| 113 |
+
{question}
|
| 114 |
+
and %i experts on the panel have give their answers.
|
| 115 |
+
"""%(len(llm_panelists))
|
| 116 |
+
|
| 117 |
+
for llm_ver in llm_panelists:
|
| 118 |
+
panel_chair_prompt += """
|
| 119 |
+
The panelist named Dr. %s has provided the answer: %s
|
| 120 |
+
""" % (llm_ver, responses[llm_ver])
|
| 121 |
+
|
| 122 |
+
panel_chair_prompt += """
|
| 123 |
+
Please generate a brief answer to the researcher's question based on their responses, pointing out where there is any inconsistency""" +\
|
| 124 |
+
""" in their answers, and using your own knowledge of C. elegans to try to resolve it."""
|
| 125 |
+
|
| 126 |
+
print(panel_chair_prompt)
|
| 127 |
+
|
| 128 |
+
prompt = PromptTemplate(
|
| 129 |
+
template=panel_chair_prompt,
|
| 130 |
+
input_variables=['question']
|
| 131 |
+
)
|
| 132 |
+
|
| 133 |
+
llm = get_llm(llm_panel_chair, temperature)
|
| 134 |
+
|
| 135 |
+
llm_chain = LLMChain(
|
| 136 |
+
prompt=prompt,
|
| 137 |
+
llm=llm
|
| 138 |
+
)
|
| 139 |
+
response_chair = llm_chain.invoke(input_text)['text']
|
| 140 |
+
|
| 141 |
+
response = '''**%s**: %s''' % (llm_panel_chair, response_chair)
|
| 142 |
+
|
| 143 |
+
response += '''
|
| 144 |
+
|
| 145 |
+
-----------------------------------
|
| 146 |
+
_Individual responses:_
|
| 147 |
+
|
| 148 |
+
'''
|
| 149 |
+
for llm_ver in responses:
|
| 150 |
+
response += '''
|
| 151 |
+
_**%s**:_ _%s_
|
| 152 |
+
''' %(llm_ver, responses[llm_ver].strip().replace('\n',' '))
|
| 153 |
+
|
| 154 |
+
|
| 155 |
+
|
| 156 |
+
return response
|
| 157 |
+
|