Merge pull request #3858 from pl752/staging Added option to use secondary API URL in vector extension

8e829c900bdd98be2706c46839195a6c3fbd128b

Cohee <18619528+Cohee1207@users.noreply.github.com>

Signed
2 files changed, +40 -9Ignore whitespace
public/scripts/extensions/vectors/index.js+30 -9
@@ -55,6 +55,8 @@ const getBatchSize = () => ['transformers', 'palm', 'ollama'].includes(settings.
5555const settings = {
5656 // For both
5757 source: 'transformers',
58+ alt_endpoint_url: '',
59+ use_alt_endpoint: false,
5860 include_wi: false,
5961 togetherai_model: 'togethercomputer/m2-bert-80M-32k-retrieval',
6062 openai_model: 'text-embedding-ada-002',
@@ -109,6 +111,7 @@ const settings = {
109111const moduleWorker = new ModuleWorkerWrapper(synchronizeChat);
110112const webllmProvider = new WebLlmVectorProvider();
111113const cachedSummaries = new Map();
114+const vectorApiRequiresUrl = ['llamacpp', 'vllm', 'ollama', 'koboldcpp'];
112115
113116/**
114117 * Gets the Collection ID for a file embedded in the chat.
@@ -777,14 +780,14 @@ function getVectorsRequestBody(args = {}) {
777780 break;
778781 case 'ollama':
779782 body.model = extension_settings.vectors.ollama_model;
780783 body.apiUrl = settings.use_alt_endpoint ? settings.alt_endpoint_url : textgenerationwebui_settings.server_urls[textgen_types.OLLAMA];
781784 body.keep = !!extension_settings.vectors.ollama_keep;
782785 break;
783786 case 'llamacpp':
784787 body.apiUrl = settings.use_alt_endpoint ? settings.alt_endpoint_url : textgenerationwebui_settings.server_urls[textgen_types.LLAMACPP];
785788 break;
786789 case 'vllm':
787790 body.apiUrl = settings.use_alt_endpoint ? settings.alt_endpoint_url : textgenerationwebui_settings.server_urls[textgen_types.VLLM];
788791 body.model = extension_settings.vectors.vllm_model;
789792 break;
790793 case 'webllm':
@@ -883,11 +886,18 @@ function throwIfSourceInvalid() {
883886 throw new Error('Vectors: API key missing', { cause: 'api_key_missing' });
884887 }
885888
886- if (settings.source === 'ollama' && !textgenerationwebui_settings.server_urls[textgen_types.OLLAMA] ||
889+ if (vectorApiRequiresUrl.includes(settings.source) && settings.use_alt_endpoint) {
887- settings.source === 'vllm' && !textgenerationwebui_settings.server_urls[textgen_types.VLLM] ||
890+ if (!settings.alt_endpoint_url) {
888- settings.source === 'koboldcpp' && !textgenerationwebui_settings.server_urls[textgen_types.KOBOLDCPP] ||
891+ throw new Error('Vectors: API URL missing', { cause: 'api_url_missing' });
889- settings.source === 'llamacpp' && !textgenerationwebui_settings.server_urls[textgen_types.LLAMACPP]) {
892+ }
890- throw new Error('Vectors: API URL missing', { cause: 'api_url_missing' });
893+ }
894+ else {
895+ if (settings.source === 'ollama' && !textgenerationwebui_settings.server_urls[textgen_types.OLLAMA] ||
896+ settings.source === 'vllm' && !textgenerationwebui_settings.server_urls[textgen_types.VLLM] ||
897+ settings.source === 'koboldcpp' && !textgenerationwebui_settings.server_urls[textgen_types.KOBOLDCPP] ||
898+ settings.source === 'llamacpp' && !textgenerationwebui_settings.server_urls[textgen_types.LLAMACPP]) {
899+ throw new Error('Vectors: API URL missing', { cause: 'api_url_missing' });
900+ }
891901 }
892902
893903 if (settings.source === 'ollama' && !settings.ollama_model || settings.source === 'vllm' && !settings.vllm_model) {
@@ -1087,6 +1097,7 @@ function toggleSettings() {
10871097 $('#webllm_vectorsModel').toggle(settings.source === 'webllm');
10881098 $('#koboldcpp_vectorsModel').toggle(settings.source === 'koboldcpp');
10891099 $('#google_vectorsModel').toggle(settings.source === 'palm');
1100+ $('#vector_altEndpointUrl').toggle(vectorApiRequiresUrl.includes(settings.source));
10901101 if (settings.source === 'webllm') {
10911102 loadWebLlmModels();
10921103 }
@@ -1165,7 +1176,7 @@ async function createKoboldCppEmbeddings(items) {
11651176 headers: getRequestHeaders(),
11661177 body: JSON.stringify({
11671178 items: items,
11681179 server: settings.use_alt_endpoint ? settings.alt_endpoint_url : textgenerationwebui_settings.server_urls[textgen_types.KOBOLDCPP],
11691180 }),
11701181 });
11711182
@@ -1467,6 +1478,16 @@ jQuery(async () => {
14671478 saveSettingsDebounced();
14681479 toggleSettings();
14691480 });
1481+ $('#vector_altEndpointUrl_enabled').prop('checked', settings.use_alt_endpoint).on('input', () => {
1482+ settings.use_alt_endpoint = $('#vector_altEndpointUrl_enabled').prop('checked');
1483+ Object.assign(extension_settings.vectors, settings);
1484+ saveSettingsDebounced();
1485+ });
1486+ $('#vector_altEndpoint_address').val(settings.alt_endpoint_url).on('change', () => {
1487+ settings.alt_endpoint_url = String($('#vector_altEndpoint_address').val());
1488+ Object.assign(extension_settings.vectors, settings);
1489+ saveSettingsDebounced();
1490+ });
14701491 $('#api_key_nomicai').on('click', async () => {
14711492 const popupText = 'NomicAI API Key:';
14721493 const key = await callGenericPopup(popupText, POPUP_TYPE.INPUT, '', {
public/scripts/extensions/vectors/settings.html+10 -0
@@ -25,6 +25,16 @@
2525 <option value="webllm" data-i18n="WebLLM Extension">WebLLM Extension</option>
2626 </select>
2727 </div>
28+ <div class="flex-container flexFlowColumn" id="vector_altEndpointUrl">
29+ <label class="checkbox_label" for="vector_altEndpointUrl_enabled" title="Enable secondary endpoint URL usage, instead of the main one.">
30+ <input id="vector_altEndpointUrl_enabled" type="checkbox" class="checkbox">
31+ <span data-i18n="Use secondary URL">Use secondary URL</span>
32+ </label>
33+ <label for="vector_altEndpoint_address" data-i18n="Secondary Embedding endpoint URL">
34+ Secondary Embedding endpoint URL
35+ </label>
36+ <input id="vector_altEndpoint_address" class="text_pole" type="text" placeholder="e.g. http://localhost:5001" />
37+ </div>
2838 <div class="flex-container flexFlowColumn" id="webllm_vectorsModel">
2939 <label for="vectors_webllm_model" data-i18n="Vectorization Model">
3040 Vectorization Model