Merge pull request #3495 from SillyTavern/vectors-requests Vectors: Don't use headers for source-specific fields in requests

1a5b1f77d7b930e71ea031045395df8d703c5bee

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

Signed
2 files changed, +67 -73Ignore whitespace
public/scripts/extensions/vectors/index.js+56 -62
@@ -746,6 +746,44 @@ async function getQueryText(chat, initiator) {
746746}
747747
748748/**
749+ * Gets common body parameters for vector requests.
750+ * @returns {object}
751+ */
752+function getVectorsRequestBody() {
753+ const body = {};
754+ switch (settings.source) {
755+ case 'extras':
756+ body.extrasUrl = extension_settings.apiUrl;
757+ body.extrasKey = extension_settings.apiKey;
758+ break;
759+ case 'togetherai':
760+ body.model = extension_settings.vectors.togetherai_model;
761+ break;
762+ case 'openai':
763+ body.model = extension_settings.vectors.openai_model;
764+ break;
765+ case 'cohere':
766+ body.model = extension_settings.vectors.cohere_model;
767+ break;
768+ case 'ollama':
769+ body.model = extension_settings.vectors.ollama_model;
770+ body.apiUrl = textgenerationwebui_settings.server_urls[textgen_types.OLLAMA];
771+ body.keep = !!extension_settings.vectors.ollama_keep;
772+ break;
773+ case 'llamacpp':
774+ body.apiUrl = textgenerationwebui_settings.server_urls[textgen_types.LLAMACPP];
775+ break;
776+ case 'vllm':
777+ body.apiUrl = textgenerationwebui_settings.server_urls[textgen_types.VLLM];
778+ body.model = extension_settings.vectors.vllm_model;
779+ break;
780+ default:
781+ break;
782+ }
783+ return body;
784+}
785+
786+/**
749787 * Gets the saved hashes for a collection
750788* @param {string} collectionId
751789* @returns {Promise<number[]>} Saved hashes
@@ -753,8 +791,9 @@ async function getQueryText(chat, initiator) {
753791async function getSavedHashes(collectionId) {
754792 const response = await fetch('/api/vector/list', {
755793 method: 'POST',
756794 headers: getVectorHeadersgetRequestHeaders(),
757795 body: JSON.stringify({
796+ ...getVectorsRequestBody(),
758797 collectionId: collectionId,
759798 source: settings.source,
760799 }),
@@ -768,54 +807,6 @@ async function getSavedHashes(collectionId) {
768807 return hashes;
769808}
770809
771-function getVectorHeaders() {
772- const headers = getRequestHeaders();
773- switch (settings.source) {
774- case 'extras':
775- Object.assign(headers, {
776- 'X-Extras-Url': extension_settings.apiUrl,
777- 'X-Extras-Key': extension_settings.apiKey,
778- });
779- break;
780- case 'togetherai':
781- Object.assign(headers, {
782- 'X-Togetherai-Model': extension_settings.vectors.togetherai_model,
783- });
784- break;
785- case 'openai':
786- Object.assign(headers, {
787- 'X-OpenAI-Model': extension_settings.vectors.openai_model,
788- });
789- break;
790- case 'cohere':
791- Object.assign(headers, {
792- 'X-Cohere-Model': extension_settings.vectors.cohere_model,
793- });
794- break;
795- case 'ollama':
796- Object.assign(headers, {
797- 'X-Ollama-Model': extension_settings.vectors.ollama_model,
798- 'X-Ollama-URL': textgenerationwebui_settings.server_urls[textgen_types.OLLAMA],
799- 'X-Ollama-Keep': !!extension_settings.vectors.ollama_keep,
800- });
801- break;
802- case 'llamacpp':
803- Object.assign(headers, {
804- 'X-LlamaCpp-URL': textgenerationwebui_settings.server_urls[textgen_types.LLAMACPP],
805- });
806- break;
807- case 'vllm':
808- Object.assign(headers, {
809- 'X-Vllm-URL': textgenerationwebui_settings.server_urls[textgen_types.VLLM],
810- 'X-Vllm-Model': extension_settings.vectors.vllm_model,
811- });
812- break;
813- default:
814- break;
815- }
816- return headers;
817-}
818-
819810/**
820811 * Inserts vector items into a collection
821812 * @param {string} collectionId - The collection to insert into
@@ -825,12 +816,11 @@ function getVectorHeaders() {
825816async function insertVectorItems(collectionId, items) {
826817 throwIfSourceInvalid();
827818
828- const headers = getVectorHeaders();
829-
830819 const response = await fetch('/api/vector/insert', {
831820 method: 'POST',
832821 headers: headersgetRequestHeaders(),
833822 body: JSON.stringify({
823+ ...getVectorsRequestBody(),
834824 collectionId: collectionId,
835825 items: items,
836826 source: settings.source,
@@ -879,8 +869,9 @@ function throwIfSourceInvalid() {
879869async function deleteVectorItems(collectionId, hashes) {
880870 const response = await fetch('/api/vector/delete', {
881871 method: 'POST',
882872 headers: getVectorHeadersgetRequestHeaders(),
883873 body: JSON.stringify({
874+ ...getVectorsRequestBody(),
884875 collectionId: collectionId,
885876 hashes: hashes,
886877 source: settings.source,
@@ -899,12 +890,11 @@ async function deleteVectorItems(collectionId, hashes) {
899890 * @returns {Promise<{ hashes: number[], metadata: object[]}>} - Hashes of the results
900891 */
901892async function queryCollection(collectionId, searchText, topK) {
902- const headers = getVectorHeaders();
903-
904893 const response = await fetch('/api/vector/query', {
905894 method: 'POST',
906895 headers: headersgetRequestHeaders(),
907896 body: JSON.stringify({
897+ ...getVectorsRequestBody(),
908898 collectionId: collectionId,
909899 searchText: searchText,
910900 topK: topK,
@@ -929,12 +919,11 @@ async function queryCollection(collectionId, searchText, topK) {
929919 * @returns {Promise<Record<string, { hashes: number[], metadata: object[] }>>} - Results mapped to collection IDs
930920 */
931921async function queryMultipleCollections(collectionIds, searchText, topK, threshold) {
932- const headers = getVectorHeaders();
933-
934922 const response = await fetch('/api/vector/query-multi', {
935923 method: 'POST',
936924 headers: headersgetRequestHeaders(),
937925 body: JSON.stringify({
926+ ...getVectorsRequestBody(),
938927 collectionIds: collectionIds,
939928 searchText: searchText,
940929 topK: topK,
@@ -965,8 +954,9 @@ async function purgeFileVectorIndex(fileUrl) {
965954
966955 const response = await fetch('/api/vector/purge', {
967956 method: 'POST',
968957 headers: getVectorHeadersgetRequestHeaders(),
969958 body: JSON.stringify({
959+ ...getVectorsRequestBody(),
970960 collectionId: collectionId,
971961 }),
972962 });
@@ -994,8 +984,9 @@ async function purgeVectorIndex(collectionId) {
994984
995985 const response = await fetch('/api/vector/purge', {
996986 method: 'POST',
997987 headers: getVectorHeadersgetRequestHeaders(),
998988 body: JSON.stringify({
989+ ...getVectorsRequestBody(),
999990 collectionId: collectionId,
1000991 }),
1001992 });
@@ -1019,7 +1010,10 @@ async function purgeAllVectorIndexes() {
10191010 try {
10201011 const response = await fetch('/api/vector/purge-all', {
10211012 method: 'POST',
10221013 headers: getVectorHeadersgetRequestHeaders(),
1014+ body: JSON.stringify({
1015+ ...getVectorsRequestBody(),
1016+ }),
10231017 });
10241018
10251019 if (!response.ok) {
src/endpoints/vectors.js+11 -11
@@ -132,35 +132,35 @@ function getSourceSettings(source, request) {
132132 switch (source) {
133133 case 'togetherai':
134134 return {
135135 model: String(request.headers['x-togetherai-body.model']),
136136 };
137137 case 'openai':
138138 return {
139139 model: String(request.headers['x-openai-body.model']),
140140 };
141141 case 'cohere':
142142 return {
143143 model: String(request.headers['x-cohere-body.model']),
144144 };
145145 case 'llamacpp':
146146 return {
147147 apiUrl: String(request.headers['x-llamacpp-url']body.apiUrl),
148148 };
149149 case 'vllm':
150150 return {
151151 apiUrl: String(request.headers['x-vllm-url']body.apiUrl),
152152 model: String(request.headers['x-vllm-body.model']),
153153 };
154154 case 'ollama':
155155 return {
156156 apiUrl: String(request.headers['x-ollama-url']body.apiUrl),
157157 model: String(request.headers['x-ollama-body.model']),
158158 keep: Boolean(request.headers['x-ollama-body.keep']),
159159 };
160160 case 'extras':
161161 return {
162162 extrasUrl: String(request.headers['x-extras-url']body.extrasUrl),
163163 extrasKey: String(request.headers['x-extras-key']body.extrasKey),
164164 };
165165 case 'transformers':
166166 return {