-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor llm vii #358
Merged
Merged
Refactor llm vii #358
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
559ce85
make link between LLM function definitions and actual definitions mor…
mschwoer 500c60e
use generic perform_dimensionality_reduction
mschwoer 14baaa5
add tests for assistant functions
mschwoer 278a314
add tests for utils functions
mschwoer 457f7b1
add tests for llm_helper and refactor
mschwoer 4f3f919
first bunch of llm_integration unit tests
mschwoer 48b8dd1
next bunch of llm_integration unit tests and refactoring
mschwoer ced836d
next bunch of llm_integration unit tests
mschwoer 3979e07
last bunch of llm_integration unit tests
mschwoer 396a2e0
simplify tests
mschwoer 548ae85
introduce _chat_completion_create
mschwoer d5e9d84
fix imports
mschwoer 721245f
fix some functionality
mschwoer 1386b24
rename api_type -> model_name
mschwoer bc56430
preserve selected model in selectbox
mschwoer e8ff6c2
make connection test more reliable
mschwoer 2e04da8
add option to reset LLM analysis
mschwoer 4d1670c
fix tests
mschwoer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,9 +8,9 @@ | |
display_figure, | ||
) | ||
from alphastats.gui.utils.llm_helper import ( | ||
display_proteins, | ||
get_display_proteins_html, | ||
llm_connection_test, | ||
set_api_key, | ||
test_llm_connection, | ||
) | ||
from alphastats.gui.utils.ui_helper import StateKeys, init_session_state, sidebar_info | ||
from alphastats.llm.llm_integration import LLMIntegration, Models | ||
|
@@ -36,20 +36,24 @@ def llm_config(): | |
"""Show the configuration options for the LLM analysis.""" | ||
c1, _ = st.columns((1, 2)) | ||
with c1: | ||
model_before = st.session_state.get(StateKeys.API_TYPE, None) | ||
current_model = st.session_state.get(StateKeys.MODEL_NAME, None) | ||
|
||
st.session_state[StateKeys.API_TYPE] = st.selectbox( | ||
models = [Models.GPT4O, Models.OLLAMA_31_70B, Models.OLLAMA_31_8B] | ||
st.session_state[StateKeys.MODEL_NAME] = st.selectbox( | ||
"Select LLM", | ||
[Models.GPT4O, Models.OLLAMA_31_70B, Models.OLLAMA_31_8B], | ||
models, | ||
index=models.index(st.session_state.get(StateKeys.MODEL_NAME)) | ||
if current_model is not None | ||
else 0, | ||
Comment on lines
+44
to
+47
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. key = StateKeys.MODEL_NAME? |
||
) | ||
|
||
base_url = None | ||
if st.session_state[StateKeys.API_TYPE] in [Models.GPT4O]: | ||
if st.session_state[StateKeys.MODEL_NAME] in [Models.GPT4O]: | ||
api_key = st.text_input( | ||
"Enter OpenAI API Key and press Enter", type="password" | ||
) | ||
set_api_key(api_key) | ||
elif st.session_state[StateKeys.API_TYPE] in [ | ||
elif st.session_state[StateKeys.MODEL_NAME] in [ | ||
Models.OLLAMA_31_70B, | ||
Models.OLLAMA_31_8B, | ||
]: | ||
|
@@ -58,13 +62,18 @@ def llm_config(): | |
|
||
test_connection = st.button("Test connection") | ||
if test_connection: | ||
test_llm_connection( | ||
api_type=st.session_state[StateKeys.API_TYPE], | ||
api_key=st.session_state[StateKeys.OPENAI_API_KEY], | ||
base_url=base_url, | ||
) | ||
|
||
if model_before != st.session_state[StateKeys.API_TYPE]: | ||
with st.spinner(f"Testing connection to {model_name}.."): | ||
error = llm_connection_test( | ||
model_name=st.session_state[StateKeys.MODEL_NAME], | ||
api_key=st.session_state[StateKeys.OPENAI_API_KEY], | ||
base_url=base_url, | ||
) | ||
if error is None: | ||
st.success(f"Connection to {model_name} successful!") | ||
else: | ||
st.error(f"Connection to {model_name} failed: {str(error)}") | ||
|
||
if current_model != st.session_state[StateKeys.MODEL_NAME]: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this is the intendend behaviour, then why is the config a st.fragment? |
||
st.rerun(scope="app") | ||
|
||
|
||
|
@@ -120,17 +129,24 @@ def llm_config(): | |
c1, c2 = st.columns((1, 2), gap="medium") | ||
with c1: | ||
st.write("Upregulated genes") | ||
display_proteins(upregulated_genes, []) | ||
st.markdown( | ||
get_display_proteins_html(upregulated_genes, True), unsafe_allow_html=True | ||
) | ||
|
||
with c2: | ||
st.write("Downregulated genes") | ||
display_proteins([], downregulated_genes) | ||
st.markdown( | ||
get_display_proteins_html(downregulated_genes, False), | ||
unsafe_allow_html=True, | ||
) | ||
|
||
|
||
st.markdown("##### Prompts generated based on analysis input") | ||
|
||
api_type = st.session_state[StateKeys.API_TYPE] | ||
model_name = st.session_state[StateKeys.MODEL_NAME] | ||
llm_integration_set_for_model = ( | ||
st.session_state.get(StateKeys.LLM_INTEGRATION, {}).get(api_type, None) is not None | ||
st.session_state.get(StateKeys.LLM_INTEGRATION, {}).get(model_name, None) | ||
is not None | ||
) | ||
with st.expander("System message", expanded=False): | ||
system_message = st.text_area( | ||
|
@@ -151,30 +167,39 @@ def llm_config(): | |
) | ||
|
||
|
||
st.markdown(f"##### LLM Analysis with {api_type}") | ||
st.markdown(f"##### LLM Analysis with {model_name}") | ||
|
||
llm_submitted = st.button( | ||
c1, c2, _ = st.columns((0.2, 0.2, 0.6)) | ||
llm_submitted = c1.button( | ||
"Run LLM analysis ...", disabled=llm_integration_set_for_model | ||
) | ||
|
||
if st.session_state[StateKeys.LLM_INTEGRATION].get(api_type) is None: | ||
llm_reset = c2.button( | ||
"Reset LLM analysis ...", disabled=not llm_integration_set_for_model | ||
) | ||
if llm_reset: | ||
del st.session_state[StateKeys.LLM_INTEGRATION] | ||
st.rerun() | ||
|
||
|
||
if st.session_state[StateKeys.LLM_INTEGRATION].get(model_name) is None: | ||
if not llm_submitted: | ||
st.stop() | ||
|
||
try: | ||
llm_integration = LLMIntegration( | ||
api_type=api_type, | ||
model_name=model_name, | ||
system_message=system_message, | ||
api_key=st.session_state[StateKeys.OPENAI_API_KEY], | ||
base_url=OLLAMA_BASE_URL, | ||
dataset=st.session_state[StateKeys.DATASET], | ||
gene_to_prot_id_map=gene_to_prot_id_map, | ||
) | ||
|
||
st.session_state[StateKeys.LLM_INTEGRATION][api_type] = llm_integration | ||
st.session_state[StateKeys.LLM_INTEGRATION][model_name] = llm_integration | ||
|
||
st.success( | ||
f"{st.session_state[StateKeys.API_TYPE]} integration initialized successfully!" | ||
f"{st.session_state[StateKeys.MODEL_NAME]} integration initialized successfully!" | ||
) | ||
|
||
with st.spinner("Processing initial prompt..."): | ||
|
@@ -221,4 +246,4 @@ def llm_chat(llm_integration: LLMIntegration, show_all: bool = False): | |
help="Show all messages in the chat interface.", | ||
) | ||
|
||
llm_chat(st.session_state[StateKeys.LLM_INTEGRATION][api_type], show_all) | ||
llm_chat(st.session_state[StateKeys.LLM_INTEGRATION][model_name], show_all) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be great to have something similarly simple for the differential analysis longterm.