|
10 | 10 | rag_pipeline = RagPipeline(chunk_size=500, chunk_overlap=50, use_tools=False) |
11 | 11 |
|
12 | 12 |
|
| 13 | +def get_endpoint_config(): |
| 14 | + config = rag_pipeline.get_endpoint_config() |
| 15 | + return [ |
| 16 | + config.get("llm_model_name", ""), |
| 17 | + config.get("llm_api_key", ""), |
| 18 | + config.get("llm_url", ""), |
| 19 | + config.get("embeddings_model_name", ""), |
| 20 | + config.get("embeddings_api_key", ""), |
| 21 | + config.get("embeddings_url", ""), |
| 22 | + ] |
| 23 | + |
| 24 | + |
| 25 | +def set_endpoint_config( |
| 26 | + llm_model_name, |
| 27 | + llm_api_key, |
| 28 | + llm_url, |
| 29 | + embeddings_model_name, |
| 30 | + embeddings_api_key, |
| 31 | + embeddings_url, |
| 32 | +): |
| 33 | + config = { |
| 34 | + "llm_model_name": llm_model_name, |
| 35 | + "llm_api_key": llm_api_key, |
| 36 | + "llm_url": llm_url, |
| 37 | + "embeddings_model_name": embeddings_model_name, |
| 38 | + "embeddings_api_key": embeddings_api_key, |
| 39 | + "embeddings_url": embeddings_url, |
| 40 | + } |
| 41 | + try: |
| 42 | + rag_pipeline.set_endpoint_config(config) |
| 43 | + error_msg = "" |
| 44 | + error_visible = False |
| 45 | + except Exception as e: |
| 46 | + error_msg = f"<span style='color:red; font-weight:bold;'>Error: {e}</span>" |
| 47 | + error_visible = True |
| 48 | + config_values = get_endpoint_config() |
| 49 | + doc_list, file_table = clear_document_list() |
| 50 | + return ( |
| 51 | + *config_values, |
| 52 | + doc_list, |
| 53 | + file_table, |
| 54 | + gr.update(value=error_msg, visible=error_visible), |
| 55 | + ) |
| 56 | + |
| 57 | + |
| 58 | +def toggle_api_key_visibility(visible, value): |
| 59 | + type = "text" if visible else "password" |
| 60 | + return gr.Textbox(label="API Key", type=type, value=value) |
| 61 | + |
| 62 | + |
13 | 63 | def clear_history(): |
14 | 64 | new_id = uuid4() |
15 | 65 | print(f"New thread_id: {new_id}") |
@@ -95,6 +145,84 @@ def add_document(new_docs, doc_list): |
95 | 145 | with gr.Column(scale=1): |
96 | 146 | clear_doc_button = gr.ClearButton(value="Clear all documents") |
97 | 147 |
|
| 148 | + with gr.Accordion("Endpoint Configuration", open=False): |
| 149 | + llm_model_name = gr.Textbox(label="LLM Model Name") |
| 150 | + llm_url = gr.Textbox(label="LLM URL") |
| 151 | + with gr.Row(): |
| 152 | + llm_api_key = gr.Textbox( |
| 153 | + placeholder="LLM API Key", |
| 154 | + type="password", |
| 155 | + scale=4, |
| 156 | + show_label=False, |
| 157 | + ) |
| 158 | + llm_api_key_visible = gr.Checkbox( |
| 159 | + label="Show LLM API Key", |
| 160 | + value=False, |
| 161 | + scale=1, |
| 162 | + ) |
| 163 | + embeddings_model_name = gr.Textbox(label="Embeddings Model Name") |
| 164 | + embeddings_url = gr.Textbox(label="Embeddings URL") |
| 165 | + with gr.Row(): |
| 166 | + embeddings_api_key = gr.Textbox( |
| 167 | + label="Embeddings API Key", |
| 168 | + type="password", |
| 169 | + scale=4, |
| 170 | + show_label=False, |
| 171 | + ) |
| 172 | + embeddings_api_key_visible = gr.Checkbox( |
| 173 | + label="Show Embeddings API Key", |
| 174 | + value=False, |
| 175 | + scale=1, |
| 176 | + ) |
| 177 | + save_btn = gr.Button("Save") |
| 178 | + config_error = gr.Markdown(value="", visible=False) |
| 179 | + |
| 180 | + # Prefill on load |
| 181 | + demo.load( |
| 182 | + get_endpoint_config, |
| 183 | + inputs=None, |
| 184 | + outputs=[ |
| 185 | + llm_model_name, |
| 186 | + llm_api_key, |
| 187 | + llm_url, |
| 188 | + embeddings_model_name, |
| 189 | + embeddings_api_key, |
| 190 | + embeddings_url, |
| 191 | + ], |
| 192 | + ) |
| 193 | + |
| 194 | + save_btn.click( |
| 195 | + set_endpoint_config, |
| 196 | + inputs=[ |
| 197 | + llm_model_name, |
| 198 | + llm_api_key, |
| 199 | + llm_url, |
| 200 | + embeddings_model_name, |
| 201 | + embeddings_api_key, |
| 202 | + embeddings_url, |
| 203 | + ], |
| 204 | + outputs=[ |
| 205 | + llm_model_name, |
| 206 | + llm_api_key, |
| 207 | + llm_url, |
| 208 | + embeddings_model_name, |
| 209 | + embeddings_api_key, |
| 210 | + embeddings_url, |
| 211 | + doc_list, |
| 212 | + file_table.dataset, |
| 213 | + config_error, |
| 214 | + ], |
| 215 | + ) |
| 216 | + llm_api_key_visible.change( |
| 217 | + toggle_api_key_visibility, |
| 218 | + inputs=[llm_api_key_visible, llm_api_key], |
| 219 | + outputs=llm_api_key, |
| 220 | + ) |
| 221 | + embeddings_api_key_visible.change( |
| 222 | + toggle_api_key_visibility, |
| 223 | + inputs=[embeddings_api_key_visible, embeddings_api_key], |
| 224 | + outputs=embeddings_api_key, |
| 225 | + ) |
98 | 226 | chatbot.clear(clear_history, outputs=[uuid_state, chatbot]) |
99 | 227 |
|
100 | 228 | chat_msg = chat_input.submit( |
|
0 commit comments