Skip to content

Commit

Permalink
Update llm api key (#268)
Browse files Browse the repository at this point in the history
  • Loading branch information
moria97 authored Nov 13, 2024
1 parent bf5072b commit c1ee4c0
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 16 deletions.
3 changes: 2 additions & 1 deletion src/pai_rag/app/web/event_listeners.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,13 @@ def change_llm(value):
eas_visible = value.lower() == "paieas"
api_visible = value.lower() != "paieas"
model_options = LLM_MODEL_KEY_DICT.get(value, [])

cur_model = model_options[0] if model_options else ""
return [
gr.update(visible=eas_visible),
gr.update(visible=eas_visible),
gr.update(visible=eas_visible),
gr.update(choices=model_options, value=cur_model, visible=api_visible),
gr.update(visible=api_visible),
]


Expand All @@ -133,6 +133,7 @@ def change_mllm(value):
gr.update(visible=eas_visible),
gr.update(visible=api_visible),
gr.update(choices=model_options, value=cur_model),
gr.update(visible=api_visible),
]


Expand Down
3 changes: 1 addition & 2 deletions src/pai_rag/app/web/tabs/data_analysis_tab.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import os
from typing import Dict, Any, List
import gradio as gr
import pandas as pd
Expand All @@ -10,7 +9,7 @@
)


DEFAULT_IS_INTERACTIVE = os.environ.get("PAIRAG_RAG__SETTING__interactive", "true")
DEFAULT_IS_INTERACTIVE = True


def upload_file_fn(input_file):
Expand Down
22 changes: 18 additions & 4 deletions src/pai_rag/app/web/tabs/settings_tab.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
from pai_rag.app.web.utils import components_to_dict
from pai_rag.app.web.index_utils import index_related_component_keys
from pai_rag.app.web.tabs.vector_db_panel import create_vector_db_panel
import os
import pai_rag.app.web.event_listeners as ev_listeners

DEFAULT_IS_INTERACTIVE = os.environ.get("PAIRAG_RAG__SETTING__interactive", "true")
DEFAULT_IS_INTERACTIVE = True


def create_setting_tab() -> Dict[str, Any]:
Expand Down Expand Up @@ -153,7 +152,7 @@ def create_setting_tab() -> Dict[str, Any]:
with gr.Row():
_ = gr.Markdown(value="\N{WHITE MEDIUM STAR} **Large Language Model**")
llm = gr.Radio(
["paieas", "dashscope", "openai"],
["paieas", "dashscope"],
label="LLM Model Source",
elem_id="llm",
interactive=DEFAULT_IS_INTERACTIVE.lower() != "false",
Expand All @@ -179,6 +178,12 @@ def create_setting_tab() -> Dict[str, Any]:
label="LLM Model Name",
elem_id="llm_api_model_name",
)
llm_api_key = gr.Textbox(
label="LLM API Key",
elem_id="llm_api_key",
type="password",
interactive=True,
)
with gr.Column(variant="panel"):
_ = gr.Markdown(
value="\N{WHITE MEDIUM STAR} **(Optional) Multi-Modal Large Language Model**"
Expand Down Expand Up @@ -222,6 +227,12 @@ def create_setting_tab() -> Dict[str, Any]:
label="LLM Model Name",
elem_id="mllm_api_model_name",
)
mllm_api_key = gr.Textbox(
label="MultiModal-LLM API Key",
elem_id="mllm_api_key",
type="password",
interactive=True,
)

with gr.Column(scale=5, variant="panel"):
_ = gr.Markdown(
Expand Down Expand Up @@ -264,12 +275,14 @@ def create_setting_tab() -> Dict[str, Any]:
llm_eas_token,
llm_eas_model_name,
llm_api_model_name,
llm_api_key,
use_mllm,
mllm,
mllm_eas_url,
mllm_eas_token,
mllm_eas_model_name,
mllm_api_model_name,
mllm_api_key,
use_oss,
oss_ak,
oss_sk,
Expand All @@ -293,13 +306,14 @@ def create_setting_tab() -> Dict[str, Any]:
llm_eas_token,
llm_eas_model_name,
llm_api_model_name,
llm_api_key,
],
)

mllm.input(
fn=ev_listeners.change_mllm,
inputs=mllm,
outputs=[m_eas_col, api_mllm_col, mllm_api_model_name],
outputs=[m_eas_col, api_mllm_col, mllm_api_model_name, mllm_api_key],
)

save_btn = gr.Button("Save Llm Setting", variant="primary")
Expand Down
3 changes: 1 addition & 2 deletions src/pai_rag/app/web/tabs/vector_db_panel.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import gradio as gr
from typing import Any, Dict
from pai_rag.app.web.utils import components_to_dict
import os
import pai_rag.app.web.event_listeners as ev_listeners

DEFAULT_IS_INTERACTIVE = os.environ.get("PAIRAG_RAG__SETTING__interactive", "true")
DEFAULT_IS_INTERACTIVE = True


def create_vector_db_panel() -> Dict[str, Any]:
Expand Down
18 changes: 13 additions & 5 deletions src/pai_rag/app/web/view_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
PandasAnalysisConfig,
SqliteAnalysisConfig,
)
from pai_rag.integrations.llms.pai.llm_config import PaiEasLlmConfig
from pai_rag.integrations.llms.pai.llm_config import DashScopeLlmConfig, PaiEasLlmConfig
from pai_rag.integrations.postprocessor.pai.pai_postprocessor import (
SimilarityPostProcessorConfig,
)
Expand All @@ -43,7 +43,7 @@ class ViewModel(BaseModel):
llm: str = "PaiEas"
llm_eas_url: str = None
llm_eas_token: str = None
llm_eas_model_name: str = "model"
llm_eas_model_name: str = "default"
llm_api_key: str = None
llm_api_model_name: str = None
llm_temperature: float = 0.1
Expand All @@ -53,7 +53,7 @@ class ViewModel(BaseModel):
mllm: str = None
mllm_eas_url: str = None
mllm_eas_token: str = None
mllm_eas_model_name: str = "model"
mllm_eas_model_name: str = "default"
mllm_api_key: str = None
mllm_api_model_name: str = None

Expand Down Expand Up @@ -145,7 +145,7 @@ def from_app_config(config: RagConfig):
view_model.llm_eas_model_name = config.llm.model
view_model.llm_eas_url = config.llm.endpoint
view_model.llm_eas_token = config.llm.token
else:
elif isinstance(config.llm, DashScopeLlmConfig):
view_model.llm_api_key = config.llm.api_key
view_model.llm_api_model_name = config.llm.model

Expand Down Expand Up @@ -434,8 +434,12 @@ def to_component_settings(self) -> Dict[str, Dict[str, Any]]:
"value": self.llm_eas_token,
"visible": self.llm.lower() == "paieas",
}
settings["llm_api_key"] = {
"value": self.llm_api_key,
"visible": self.llm.lower() != "paieas",
}
if self.llm.lower() == "paieas" and not self.llm_eas_model_name:
self.llm_eas_model_name = "model"
self.llm_eas_model_name = "default"

settings["llm_eas_model_name"] = {
"value": self.llm_eas_model_name,
Expand All @@ -459,6 +463,10 @@ def to_component_settings(self) -> Dict[str, Dict[str, Any]]:
"choices": MLLM_MODEL_KEY_DICT.get(self.mllm, []),
"visible": self.mllm.lower() != "paieas",
}
settings["mllm_api_key"] = {
"value": self.mllm_api_key,
"visible": self.mllm.lower() != "paieas",
}
settings["m_eas_col"] = {"visible": self.mllm == "paieas"}
settings["api_mllm_col"] = {"visible": self.mllm == "dashscope"}

Expand Down
3 changes: 1 addition & 2 deletions src/pai_rag/app/web/webui.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from fastapi import FastAPI
import gradio as gr
import os
from pai_rag.app.web import event_listeners
from pai_rag.app.web.index_utils import index_to_components_settings
from pai_rag.app.web.tabs.agent_tab import create_agent_tab
Expand All @@ -22,7 +21,7 @@

from loguru import logger

DEFAULT_IS_INTERACTIVE = os.environ.get("PAIRAG_RAG__SETTING__interactive", "true")
DEFAULT_IS_INTERACTIVE = True


def resume_ui():
Expand Down

0 comments on commit c1ee4c0

Please sign in to comment.