|
2 | 2 | import React, { createContext, useContext, useRef, useState } from "react"; |
3 | 3 | import { |
4 | 4 | defaultPriceToolCallOptions, |
5 | | - defaultNotFilterToolCallOptions, |
6 | 5 | defaultRelevanceToolCallOptions, |
7 | 6 | defaultSearchToolCallOptions, |
8 | 7 | useModalState, |
@@ -491,7 +490,6 @@ function ChatProvider({ children }: { children: React.ReactNode }) { |
491 | 490 | } |
492 | 491 |
|
493 | 492 | let skipSearch = false; |
494 | | - let notFilter = false; |
495 | 493 |
|
496 | 494 | if ( |
497 | 495 | props.recommendOptions?.filter && |
@@ -622,55 +620,7 @@ function ChatProvider({ children }: { children: React.ReactNode }) { |
622 | 620 | }, |
623 | 621 | }); |
624 | 622 | } |
625 | | - }); |
626 | | - |
627 | | - const notFilterPromise = retryOperation(async () => { |
628 | | - if (!curGroup && messages.length > 1) { |
629 | | - return await trieveSDK.getToolCallFunctionParams({ |
630 | | - user_message_text: `Here's the previous message thread so far: ${messages.map( |
631 | | - (message) => { |
632 | | - if ( |
633 | | - message.type === "system" && |
634 | | - message.additional?.length && |
635 | | - props.type === "ecommerce" |
636 | | - ) { |
637 | | - const chunks = message.additional |
638 | | - .map((chunk) => { |
639 | | - return JSON.stringify({ |
640 | | - title: chunk.metadata?.title || "", |
641 | | - description: chunk.chunk_html || "", |
642 | | - price: chunk.num_value |
643 | | - ? `${props.defaultCurrency || ""} ${chunk.num_value}` |
644 | | - : "", |
645 | | - link: chunk.link || "", |
646 | | - }); |
647 | | - }) |
648 | | - .join("\n\n"); |
649 | | - return `\n\n${chunks}${message.text}`; |
650 | | - } else { |
651 | | - return `\n\n${message.text}`; |
652 | | - } |
653 | | - }, |
654 | | - )} \n\n${props.notFilterToolCallOptions?.userMessageTextPrefix ?? defaultNotFilterToolCallOptions.userMessageTextPrefix}: ${questionProp || currentQuestion}.`, |
655 | | - image_url: localImageUrl ? localImageUrl : null, |
656 | | - audio_input: curAudioBase64 ? curAudioBase64 : null, |
657 | | - tool_function: { |
658 | | - name: "not_filter", |
659 | | - description: |
660 | | - props.notFilterToolCallOptions?.toolDescription ?? |
661 | | - defaultNotFilterToolCallOptions.toolDescription, |
662 | | - parameters: [ |
663 | | - { |
664 | | - name: "not_filter", |
665 | | - parameter_type: "boolean", |
666 | | - description: |
667 | | - "Whether or not the user is interested in the products previously shown to them. Set this to true if the user is not interested in the products they were shown or want something different.", |
668 | | - }, |
669 | | - ], |
670 | | - }, |
671 | | - }); |
672 | | - } |
673 | | - }); |
| 623 | + }) |
674 | 624 |
|
675 | 625 | const imageFiltersPromise = retryOperation(async () => { |
676 | 626 | if (localImageUrl) { |
@@ -709,13 +659,13 @@ function ChatProvider({ children }: { children: React.ReactNode }) { |
709 | 659 | user_message_text: |
710 | 660 | questionProp || currentQuestion |
711 | 661 | ? `Get filters from the following messages: ${messages |
712 | | - .slice(0, -1) |
713 | | - .filter((message) => { |
714 | | - return message.type == "user"; |
715 | | - }) |
716 | | - .map( |
717 | | - (message) => `\n\n${message.text}`, |
718 | | - )} \n\n ${questionProp || currentQuestion}` |
| 662 | + .slice(0, -1) |
| 663 | + .filter((message) => { |
| 664 | + return message.type == "user"; |
| 665 | + }) |
| 666 | + .map( |
| 667 | + (message) => `\n\n${message.text}`, |
| 668 | + )} \n\n ${questionProp || currentQuestion}` |
719 | 669 | : null, |
720 | 670 | image_url: localImageUrl ? localImageUrl : null, |
721 | 671 | audio_input: curAudioBase64 ? curAudioBase64 : null, |
@@ -747,19 +697,13 @@ function ChatProvider({ children }: { children: React.ReactNode }) { |
747 | 697 | } |
748 | 698 | }); |
749 | 699 |
|
750 | | - const [ |
751 | | - priceFiltersResp, |
752 | | - imageFiltersResp, |
753 | | - tagFiltersResp, |
754 | | - skipSearchResp, |
755 | | - notFilterResp, |
756 | | - ] = await Promise.all([ |
757 | | - priceFiltersPromise, |
758 | | - imageFiltersPromise, |
759 | | - tagFiltersPromise, |
760 | | - skipSearchPromise, |
761 | | - notFilterPromise, |
762 | | - ]); |
| 700 | + const [priceFiltersResp, imageFiltersResp, tagFiltersResp, skipSearchResp] = |
| 701 | + await Promise.all([ |
| 702 | + priceFiltersPromise, |
| 703 | + imageFiltersPromise, |
| 704 | + tagFiltersPromise, |
| 705 | + skipSearchPromise, |
| 706 | + ]); |
763 | 707 |
|
764 | 708 | if (transcribedQuery && curAudioBase64) { |
765 | 709 | questionProp = transcribedQuery; |
@@ -847,15 +791,6 @@ function ChatProvider({ children }: { children: React.ReactNode }) { |
847 | 791 | } |
848 | 792 | } |
849 | 793 |
|
850 | | - if (notFilterResp?.parameters) { |
851 | | - const notFilterParam = (notFilterResp.parameters as any)[ |
852 | | - "not_filter" |
853 | | - ]; |
854 | | - if (typeof notFilterParam === "boolean" && notFilterParam) { |
855 | | - notFilter = true; |
856 | | - } |
857 | | - } |
858 | | - |
859 | 794 | clearTimeout(toolCallTimeout); |
860 | 795 | } catch (e) { |
861 | 796 | console.error("error getting getToolCallFunctionParams", e); |
@@ -889,19 +824,6 @@ function ChatProvider({ children }: { children: React.ReactNode }) { |
889 | 824 | filters.should == null |
890 | 825 | ) { |
891 | 826 | filters = null; |
892 | | - |
893 | | - if (notFilter) { |
894 | | - if (filters == null) { |
895 | | - filters = { must_not: [] }; |
896 | | - } |
897 | | - |
898 | | - if (filters?.must_not) { |
899 | | - filters.must_not.push({ |
900 | | - field: "group_ids", |
901 | | - match_any: groupIdsInChat, |
902 | | - }); |
903 | | - } |
904 | | - } |
905 | 827 | } |
906 | 828 |
|
907 | 829 | searchAbortController.current = new AbortController(); |
@@ -996,13 +918,13 @@ function ChatProvider({ children }: { children: React.ReactNode }) { |
996 | 918 | : null; |
997 | 919 | const imageUrls = props.relevanceToolCallOptions?.includeImages |
998 | 920 | ? ( |
999 | | - (firstChunk?.image_urls?.filter( |
1000 | | - (stringOrNull): stringOrNull is string => |
1001 | | - Boolean(stringOrNull), |
1002 | | - ) || |
1003 | | - []) ?? |
1004 | | - [] |
1005 | | - ).splice(0, 1) |
| 921 | + (firstChunk?.image_urls?.filter( |
| 922 | + (stringOrNull): stringOrNull is string => |
| 923 | + Boolean(stringOrNull), |
| 924 | + ) || |
| 925 | + []) ?? |
| 926 | + [] |
| 927 | + ).splice(0, 1) |
1006 | 928 | : undefined; |
1007 | 929 | const jsonOfFirstChunk = { |
1008 | 930 | title: (firstChunk?.metadata as any)?.title ?? "", |
@@ -1194,47 +1116,47 @@ function ChatProvider({ children }: { children: React.ReactNode }) { |
1194 | 1116 |
|
1195 | 1117 | if (referenceImageUrls.length > 0 || curGroup) { |
1196 | 1118 | if (referenceImageUrls.length == 0 && curGroup) { |
1197 | | - const fulltextSearchPromise = trieveSDK.searchInGroup( |
1198 | | - { |
1199 | | - query: questionProp || currentQuestion, |
1200 | | - search_type: "fulltext", |
1201 | | - page_size: 10, |
1202 | | - group_id: curGroup.id, |
1203 | | - user_id: fingerprint, |
1204 | | - }, |
1205 | | - searchAbortController.current.signal, |
1206 | | - ); |
1207 | | - |
1208 | | - const chunksInGroupPromise = trieveSDK.getChunksInGroup( |
1209 | | - { |
1210 | | - groupId: curGroup.id, |
1211 | | - page: 1, |
1212 | | - }, |
1213 | | - searchAbortController.current.signal, |
1214 | | - ); |
1215 | | - |
1216 | | - const [fulltextSearchResp, chunksInGroupResp] = await Promise.all([ |
1217 | | - fulltextSearchPromise, |
1218 | | - chunksInGroupPromise, |
1219 | | - ]); |
1220 | | - |
1221 | | - const chunkIds = fulltextSearchResp.chunks.map( |
1222 | | - (score_chunk) => score_chunk.chunk.id, |
1223 | | - ); |
| 1119 | + const fulltextSearchPromise = trieveSDK.searchInGroup( |
| 1120 | + { |
| 1121 | + query: questionProp || currentQuestion, |
| 1122 | + search_type: "fulltext", |
| 1123 | + page_size: 10, |
| 1124 | + group_id: curGroup.id, |
| 1125 | + user_id: fingerprint, |
| 1126 | + }, |
| 1127 | + searchAbortController.current.signal, |
| 1128 | + ); |
1224 | 1129 |
|
1225 | | - chunksInGroupResp.chunks.filter((chunk) => chunkIds.includes(chunk.id)); |
| 1130 | + const chunksInGroupPromise = trieveSDK.getChunksInGroup( |
| 1131 | + { |
| 1132 | + groupId: curGroup.id, |
| 1133 | + page: 1, |
| 1134 | + }, |
| 1135 | + searchAbortController.current.signal, |
| 1136 | + ); |
1226 | 1137 |
|
1227 | | - const topChunk = chunksInGroupResp.chunks[0]; |
| 1138 | + const [fulltextSearchResp, chunksInGroupResp] = await Promise.all([ |
| 1139 | + fulltextSearchPromise, |
| 1140 | + chunksInGroupPromise, |
| 1141 | + ]); |
1228 | 1142 |
|
1229 | | - if (topChunk) { |
1230 | | - topChunk.image_urls?.forEach((url) => { |
1231 | | - if (url) { |
1232 | | - referenceImageUrls.push(url); |
1233 | | - } |
1234 | | - }); |
1235 | | - } |
| 1143 | + const chunkIds = fulltextSearchResp.chunks.map( |
| 1144 | + (score_chunk) => score_chunk.chunk.id, |
| 1145 | + ); |
| 1146 | + |
| 1147 | + chunksInGroupResp.chunks.filter((chunk) => chunkIds.includes(chunk.id)); |
| 1148 | + |
| 1149 | + const topChunk = chunksInGroupResp.chunks[0]; |
| 1150 | + |
| 1151 | + if (topChunk) { |
| 1152 | + topChunk.image_urls?.forEach((url) => { |
| 1153 | + if (url) { |
| 1154 | + referenceImageUrls.push(url); |
| 1155 | + } |
| 1156 | + }); |
| 1157 | + } |
1236 | 1158 |
|
1237 | | - referenceImageUrls.slice(0, 3); |
| 1159 | + referenceImageUrls.slice(0, 3); |
1238 | 1160 | } |
1239 | 1161 |
|
1240 | 1162 | if (await handleImageEdit()) { |
@@ -1278,23 +1200,23 @@ function ChatProvider({ children }: { children: React.ReactNode }) { |
1278 | 1200 | if (skipSearch) { |
1279 | 1201 | createMessageFilters = props.useGroupSearch |
1280 | 1202 | ? { |
1281 | | - must: [ |
1282 | | - { |
1283 | | - field: "group_ids", |
1284 | | - match_any: groupIdsInChat, |
1285 | | - }, |
1286 | | - ], |
1287 | | - } |
| 1203 | + must: [ |
| 1204 | + { |
| 1205 | + field: "group_ids", |
| 1206 | + match_any: groupIdsInChat, |
| 1207 | + }, |
| 1208 | + ], |
| 1209 | + } |
1288 | 1210 | : { |
1289 | | - must: [ |
1290 | | - { |
1291 | | - field: "ids", |
1292 | | - match_any: messages |
1293 | | - .flatMap((m) => m.additional ?? []) |
1294 | | - .map((chunk) => chunk.id), |
1295 | | - }, |
1296 | | - ], |
1297 | | - }; |
| 1211 | + must: [ |
| 1212 | + { |
| 1213 | + field: "ids", |
| 1214 | + match_any: messages |
| 1215 | + .flatMap((m) => m.additional ?? []) |
| 1216 | + .map((chunk) => chunk.id), |
| 1217 | + }, |
| 1218 | + ], |
| 1219 | + }; |
1298 | 1220 | } |
1299 | 1221 | const systemPromptToUse = |
1300 | 1222 | props.systemPrompt && props.systemPrompt !== "" |
|
0 commit comments