Vectors: Don't use headers for source-specific fields in requests
| @@ -746,74 +746,65 @@ async function getQueryText(chat, initiator) { | |||
| 746 | } | 746 | } |
| 747 | 747 | ||
| 748 | /** | 748 | /** |
| 749 | * Gets the saved hashes for a collection | 749 | * Gets common body parameters for vector requests. |
| 750 | * @param {string} collectionId | 750 | * @returns {object} |
| 751 | * @returns {Promise<number[]>} Saved hashes | ||
| 752 | */ | 751 | */ |
| 753 | async function getSavedHashes(collectionId) { | 752 | function getVectorsRequestBody() { |
| 754 | const response = await fetch('/api/vector/list', { | 753 | const body = {}; |
| 755 | method: 'POST', | ||
| 756 | headers: getVectorHeaders(), | ||
| 757 | body: JSON.stringify({ | ||
| 758 | collectionId: collectionId, | ||
| 759 | source: settings.source, | ||
| 760 | }), | ||
| 761 | }); | ||
| 762 | |||
| 763 | if (!response.ok) { | ||
| 764 | throw new Error(`Failed to get saved hashes for collection ${collectionId}`); | ||
| 765 | } | ||
| 766 | |||
| 767 | const hashes = await response.json(); | ||
| 768 | return hashes; | ||
| 769 | } | ||
| 770 | |||
| 771 | function getVectorHeaders() { | ||
| 772 | const headers = getRequestHeaders(); | ||
| 773 | switch (settings.source) { | 754 | switch (settings.source) { |
| 774 | case 'extras': | 755 | case 'extras': |
| 775 | Object.assign(headers, { | 756 | body.extrasUrl = extension_settings.apiUrl; |
| 776 | 'X-Extras-Url': extension_settings.apiUrl, | 757 | body.extrasKey = extension_settings.apiKey; |
| 777 | 'X-Extras-Key': extension_settings.apiKey, | ||
| 778 | }); | ||
| 779 | break; | 758 | break; |
| 780 | case 'togetherai': | 759 | case 'togetherai': |
| 781 | Object.assign(headers, { | 760 | body.model = extension_settings.vectors.togetherai_model; |
| 782 | 'X-Togetherai-Model': extension_settings.vectors.togetherai_model, | ||
| 783 | }); | ||
| 784 | break; | 761 | break; |
| 785 | case 'openai': | 762 | case 'openai': |
| 786 | Object.assign(headers, { | 763 | body.model = extension_settings.vectors.openai_model; |
| 787 | 'X-OpenAI-Model': extension_settings.vectors.openai_model, | ||
| 788 | }); | ||
| 789 | break; | 764 | break; |
| 790 | case 'cohere': | 765 | case 'cohere': |
| 791 | Object.assign(headers, { | 766 | body.model = extension_settings.vectors.cohere_model; |
| 792 | 'X-Cohere-Model': extension_settings.vectors.cohere_model, | ||
| 793 | }); | ||
| 794 | break; | 767 | break; |
| 795 | case 'ollama': | 768 | case 'ollama': |
| 796 | Object.assign(headers, { | 769 | body.model = extension_settings.vectors.ollama_model; |
| 797 | 'X-Ollama-Model': extension_settings.vectors.ollama_model, | 770 | body.apiUrl = textgenerationwebui_settings.server_urls[textgen_types.OLLAMA]; |
| 798 | 'X-Ollama-URL': textgenerationwebui_settings.server_urls[textgen_types.OLLAMA], | 771 | body.keep = !!extension_settings.vectors.ollama_keep; |
| 799 | 'X-Ollama-Keep': !!extension_settings.vectors.ollama_keep, | ||
| 800 | }); | ||
| 801 | break; | 772 | break; |
| 802 | case 'llamacpp': | 773 | case 'llamacpp': |
| 803 | Object.assign(headers, { | 774 | body.apiUrl = textgenerationwebui_settings.server_urls[textgen_types.LLAMACPP]; |
| 804 | 'X-LlamaCpp-URL': textgenerationwebui_settings.server_urls[textgen_types.LLAMACPP], | ||
| 805 | }); | ||
| 806 | break; | 775 | break; |
| 807 | case 'vllm': | 776 | case 'vllm': |
| 808 | Object.assign(headers, { | 777 | body.apiUrl = textgenerationwebui_settings.server_urls[textgen_types.VLLM]; |
| 809 | 'X-Vllm-URL': textgenerationwebui_settings.server_urls[textgen_types.VLLM], | 778 | body.model = extension_settings.vectors.vllm_model; |
| 810 | 'X-Vllm-Model': extension_settings.vectors.vllm_model, | ||
| 811 | }); | ||
| 812 | break; | 779 | break; |
| 813 | default: | 780 | default: |
| 814 | break; | 781 | break; |
| 815 | } | 782 | } |
| 816 | return headers; | 783 | return body; |
| 784 | } | ||
| 785 | |||
| 786 | /** | ||
| 787 | * Gets the saved hashes for a collection | ||
| 788 | * @param {string} collectionId | ||
| 789 | * @returns {Promise<number[]>} Saved hashes | ||
| 790 | */ | ||
| 791 | async function getSavedHashes(collectionId) { | ||
| 792 | const response = await fetch('/api/vector/list', { | ||
| 793 | method: 'POST', | ||
| 794 | headers: getRequestHeaders(), | ||
| 795 | body: JSON.stringify({ | ||
| 796 | ...getVectorsRequestBody(), | ||
| 797 | collectionId: collectionId, | ||
| 798 | source: settings.source, | ||
| 799 | }), | ||
| 800 | }); | ||
| 801 | |||
| 802 | if (!response.ok) { | ||
| 803 | throw new Error(`Failed to get saved hashes for collection ${collectionId}`); | ||
| 804 | } | ||
| 805 | |||
| 806 | const hashes = await response.json(); | ||
| 807 | return hashes; | ||
| 817 | } | 808 | } |
| 818 | 809 | ||
| 819 | /** | 810 | /** |
| @@ -825,12 +816,11 @@ function getVectorHeaders() { | |||
| 825 | async function insertVectorItems(collectionId, items) { | 816 | async function insertVectorItems(collectionId, items) { |
| 826 | throwIfSourceInvalid(); | 817 | throwIfSourceInvalid(); |
| 827 | 818 | ||
| 828 | const headers = getVectorHeaders(); | ||
| 829 | |||
| 830 | const response = await fetch('/api/vector/insert', { | 819 | const response = await fetch('/api/vector/insert', { |
| 831 | method: 'POST', | 820 | method: 'POST', |
| 832 | headers: headers, | 821 | headers: getRequestHeaders(), |
| 833 | body: JSON.stringify({ | 822 | body: JSON.stringify({ |
| 823 | ...getVectorsRequestBody(), | ||
| 834 | collectionId: collectionId, | 824 | collectionId: collectionId, |
| 835 | items: items, | 825 | items: items, |
| 836 | source: settings.source, | 826 | source: settings.source, |
| @@ -879,8 +869,9 @@ function throwIfSourceInvalid() { | |||
| 879 | async function deleteVectorItems(collectionId, hashes) { | 869 | async function deleteVectorItems(collectionId, hashes) { |
| 880 | const response = await fetch('/api/vector/delete', { | 870 | const response = await fetch('/api/vector/delete', { |
| 881 | method: 'POST', | 871 | method: 'POST', |
| 882 | headers: getVectorHeaders(), | 872 | headers: getRequestHeaders(), |
| 883 | body: JSON.stringify({ | 873 | body: JSON.stringify({ |
| 874 | ...getVectorsRequestBody(), | ||
| 884 | collectionId: collectionId, | 875 | collectionId: collectionId, |
| 885 | hashes: hashes, | 876 | hashes: hashes, |
| 886 | source: settings.source, | 877 | source: settings.source, |
| @@ -899,12 +890,11 @@ async function deleteVectorItems(collectionId, hashes) { | |||
| 899 | * @returns {Promise<{ hashes: number[], metadata: object[]}>} - Hashes of the results | 890 | * @returns {Promise<{ hashes: number[], metadata: object[]}>} - Hashes of the results |
| 900 | */ | 891 | */ |
| 901 | async function queryCollection(collectionId, searchText, topK) { | 892 | async function queryCollection(collectionId, searchText, topK) { |
| 902 | const headers = getVectorHeaders(); | ||
| 903 | |||
| 904 | const response = await fetch('/api/vector/query', { | 893 | const response = await fetch('/api/vector/query', { |
| 905 | method: 'POST', | 894 | method: 'POST', |
| 906 | headers: headers, | 895 | headers: getRequestHeaders(), |
| 907 | body: JSON.stringify({ | 896 | body: JSON.stringify({ |
| 897 | ...getVectorsRequestBody(), | ||
| 908 | collectionId: collectionId, | 898 | collectionId: collectionId, |
| 909 | searchText: searchText, | 899 | searchText: searchText, |
| 910 | topK: topK, | 900 | topK: topK, |
| @@ -929,12 +919,11 @@ async function queryCollection(collectionId, searchText, topK) { | |||
| 929 | * @returns {Promise<Record<string, { hashes: number[], metadata: object[] }>>} - Results mapped to collection IDs | 919 | * @returns {Promise<Record<string, { hashes: number[], metadata: object[] }>>} - Results mapped to collection IDs |
| 930 | */ | 920 | */ |
| 931 | async function queryMultipleCollections(collectionIds, searchText, topK, threshold) { | 921 | async function queryMultipleCollections(collectionIds, searchText, topK, threshold) { |
| 932 | const headers = getVectorHeaders(); | ||
| 933 | |||
| 934 | const response = await fetch('/api/vector/query-multi', { | 922 | const response = await fetch('/api/vector/query-multi', { |
| 935 | method: 'POST', | 923 | method: 'POST', |
| 936 | headers: headers, | 924 | headers: getRequestHeaders(), |
| 937 | body: JSON.stringify({ | 925 | body: JSON.stringify({ |
| 926 | ...getVectorsRequestBody(), | ||
| 938 | collectionIds: collectionIds, | 927 | collectionIds: collectionIds, |
| 939 | searchText: searchText, | 928 | searchText: searchText, |
| 940 | topK: topK, | 929 | topK: topK, |
| @@ -965,8 +954,9 @@ async function purgeFileVectorIndex(fileUrl) { | |||
| 965 | 954 | ||
| 966 | const response = await fetch('/api/vector/purge', { | 955 | const response = await fetch('/api/vector/purge', { |
| 967 | method: 'POST', | 956 | method: 'POST', |
| 968 | headers: getVectorHeaders(), | 957 | headers: getRequestHeaders(), |
| 969 | body: JSON.stringify({ | 958 | body: JSON.stringify({ |
| 959 | ...getVectorsRequestBody(), | ||
| 970 | collectionId: collectionId, | 960 | collectionId: collectionId, |
| 971 | }), | 961 | }), |
| 972 | }); | 962 | }); |
| @@ -994,8 +984,9 @@ async function purgeVectorIndex(collectionId) { | |||
| 994 | 984 | ||
| 995 | const response = await fetch('/api/vector/purge', { | 985 | const response = await fetch('/api/vector/purge', { |
| 996 | method: 'POST', | 986 | method: 'POST', |
| 997 | headers: getVectorHeaders(), | 987 | headers: getRequestHeaders(), |
| 998 | body: JSON.stringify({ | 988 | body: JSON.stringify({ |
| 989 | ...getVectorsRequestBody(), | ||
| 999 | collectionId: collectionId, | 990 | collectionId: collectionId, |
| 1000 | }), | 991 | }), |
| 1001 | }); | 992 | }); |
| @@ -1019,7 +1010,10 @@ async function purgeAllVectorIndexes() { | |||
| 1019 | try { | 1010 | try { |
| 1020 | const response = await fetch('/api/vector/purge-all', { | 1011 | const response = await fetch('/api/vector/purge-all', { |
| 1021 | method: 'POST', | 1012 | method: 'POST', |
| 1022 | headers: getVectorHeaders(), | 1013 | headers: getRequestHeaders(), |
| 1014 | body: JSON.stringify({ | ||
| 1015 | ...getVectorsRequestBody(), | ||
| 1016 | }), | ||
| 1023 | }); | 1017 | }); |
| 1024 | 1018 | ||
| 1025 | if (!response.ok) { | 1019 | if (!response.ok) { |
| @@ -132,35 +132,35 @@ function getSourceSettings(source, request) { | |||
| 132 | switch (source) { | 132 | switch (source) { |
| 133 | case 'togetherai': | 133 | case 'togetherai': |
| 134 | return { | 134 | return { |
| 135 | model: String(request.headers['x-togetherai-model']), | 135 | model: String(request.body.model), |
| 136 | }; | 136 | }; |
| 137 | case 'openai': | 137 | case 'openai': |
| 138 | return { | 138 | return { |
| 139 | model: String(request.headers['x-openai-model']), | 139 | model: String(request.body.model), |
| 140 | }; | 140 | }; |
| 141 | case 'cohere': | 141 | case 'cohere': |
| 142 | return { | 142 | return { |
| 143 | model: String(request.headers['x-cohere-model']), | 143 | model: String(request.body.model), |
| 144 | }; | 144 | }; |
| 145 | case 'llamacpp': | 145 | case 'llamacpp': |
| 146 | return { | 146 | return { |
| 147 | apiUrl: String(request.headers['x-llamacpp-url']), | 147 | apiUrl: String(request.body.apiUrl), |
| 148 | }; | 148 | }; |
| 149 | case 'vllm': | 149 | case 'vllm': |
| 150 | return { | 150 | return { |
| 151 | apiUrl: String(request.headers['x-vllm-url']), | 151 | apiUrl: String(request.body.apiUrl), |
| 152 | model: String(request.headers['x-vllm-model']), | 152 | model: String(request.body.model), |
| 153 | }; | 153 | }; |
| 154 | case 'ollama': | 154 | case 'ollama': |
| 155 | return { | 155 | return { |
| 156 | apiUrl: String(request.headers['x-ollama-url']), | 156 | apiUrl: String(request.body.apiUrl), |
| 157 | model: String(request.headers['x-ollama-model']), | 157 | model: String(request.body.model), |
| 158 | keep: Boolean(request.headers['x-ollama-keep']), | 158 | keep: Boolean(request.body.keep), |
| 159 | }; | 159 | }; |
| 160 | case 'extras': | 160 | case 'extras': |
| 161 | return { | 161 | return { |
| 162 | extrasUrl: String(request.headers['x-extras-url']), | 162 | extrasUrl: String(request.body.extrasUrl), |
| 163 | extrasKey: String(request.headers['x-extras-key']), | 163 | extrasKey: String(request.body.extrasKey), |
| 164 | }; | 164 | }; |
| 165 | case 'transformers': | 165 | case 'transformers': |
| 166 | return { | 166 | return { |