@@ -2,11 +2,9 @@ import { ReturnMetadata } from "weaviate-client";
22
33import {
44 QueryAgentResponse ,
5- SearchResult ,
65 PropertyFilter ,
76 AggregationResult ,
87 PropertyAggregation ,
9- Usage ,
108 Source ,
119 StreamedTokens ,
1210 ProgressMessage ,
@@ -16,22 +14,24 @@ import {
1614 SearchModeResponse ,
1715 FilterAndOr ,
1816 AskModeResponse ,
17+ Search ,
18+ ModelUnitUsage ,
1919} from "./response.js" ;
2020
2121import {
2222 ApiQueryAgentResponse ,
23- ApiSearchResult ,
2423 ApiPropertyFilter ,
2524 ApiAggregationResult ,
2625 ApiPropertyAggregation ,
27- ApiUsage ,
2826 ApiSource ,
2927 ApiDateFilterValue ,
3028 ApiSearchModeResponse ,
3129 ApiWeaviateObject ,
3230 ApiWeaviateReturn ,
3331 ApiFilterAndOr ,
3432 ApiAskModeResponse ,
33+ ApiSearch ,
34+ ApiModelUnitUsage ,
3535} from "./api-response.js" ;
3636
3737import { ServerSentEvent } from "./server-sent-events.js" ;
@@ -41,22 +41,14 @@ export const mapAskModeResponse = (
4141) : AskModeResponse => {
4242 const properties : AskModeResponseProperties = {
4343 outputType : "finalState" ,
44- searches : response . searches . map ( ( search ) => ( {
45- query : search . query ,
46- filters : search . filters ? mapFilter ( search . filters ) : undefined ,
47- collection : search . collection ,
48- } ) ) ,
44+ searches : mapSearches ( response . searches ) ,
4945 aggregations : response . aggregations . map ( ( aggregation ) => ( {
5046 groupbyProperty : aggregation . groupby_property ,
5147 aggregation : mapPropertyAggregation ( aggregation . aggregation ) ,
5248 filters : aggregation . filters ? mapFilter ( aggregation . filters ) : undefined ,
5349 collection : aggregation . collection ,
5450 } ) ) ,
55- usage : {
56- modelUnits : response . usage . model_units ,
57- usageInPlan : response . usage . usage_in_plan ,
58- remainingPlanRequests : response . usage . remaining_plan_requests ,
59- } ,
51+ usage : mapUsage ( response . usage ) ,
6052 totalTime : response . total_time ,
6153 isPartialAnswer : response . is_partial_answer ,
6254 missingInformation : response . missing_information ,
@@ -70,16 +62,42 @@ export const mapAskModeResponse = (
7062 } ;
7163} ;
7264
65+ const mapSearches = ( searches : ApiSearch [ ] ) : Search [ ] =>
66+ searches . map ( ( search ) => ( {
67+ query : search . query ,
68+ filters : search . filters ? mapFilter ( search . filters ) : undefined ,
69+ collection : search . collection ,
70+ } ) ) ;
71+
72+ const mapUsage = ( usage : ApiModelUnitUsage ) : ModelUnitUsage => ( {
73+ modelUnits : usage . model_units ,
74+ usageInPlan : usage . usage_in_plan ,
75+ remainingPlanRequests : usage . remaining_plan_requests ,
76+ } ) ;
77+
7378export const mapResponse = (
7479 response : ApiQueryAgentResponse ,
7580) : QueryAgentResponse => {
7681 const properties : ResponseProperties = {
7782 outputType : "finalState" ,
7883 originalQuery : response . original_query ,
7984 collectionNames : response . collection_names ,
80- searches : mapSearches ( response . searches ) ,
85+ searches : response . searches . map ( ( searches ) =>
86+ searches . map ( ( result ) => ( {
87+ collection : result . collection ,
88+ queries : result . queries ,
89+ filters : result . filters . map ( ( filter ) => filter . map ( mapPropertyFilter ) ) ,
90+ filterOperators : result . filter_operators ,
91+ } ) ) ,
92+ ) ,
8193 aggregations : mapAggregations ( response . aggregations ) ,
82- usage : mapUsage ( response . usage ) ,
94+ usage : {
95+ requests : response . usage . requests ,
96+ requestTokens : response . usage . request_tokens ,
97+ responseTokens : response . usage . response_tokens ,
98+ totalTokens : response . usage . total_tokens ,
99+ details : response . usage . details ,
100+ } ,
83101 totalTime : response . total_time ,
84102 isPartialAnswer : response . is_partial_answer ,
85103 missingInformation : response . missing_information ,
@@ -93,17 +111,6 @@ export const mapResponse = (
93111 } ;
94112} ;
95113
96- const mapInnerSearches = ( searches : ApiSearchResult [ ] ) : SearchResult [ ] =>
97- searches . map ( ( result ) => ( {
98- collection : result . collection ,
99- queries : result . queries ,
100- filters : result . filters . map ( ( filter ) => filter . map ( mapPropertyFilter ) ) ,
101- filterOperators : result . filter_operators ,
102- } ) ) ;
103-
104- const mapSearches = ( searches : ApiSearchResult [ ] [ ] ) : SearchResult [ ] [ ] =>
105- searches . map ( ( searchGroup ) => mapInnerSearches ( searchGroup ) ) ;
106-
107114const mapDatePropertyFilter = (
108115 filterValue : ApiDateFilterValue ,
109116) : DateFilterValue | undefined => {
@@ -269,14 +276,6 @@ const mapPropertyAggregation = (
269276 : undefined ,
270277} ) ;
271278
272- const mapUsage = ( usage : ApiUsage ) : Usage => ( {
273- requests : usage . requests ,
274- requestTokens : usage . request_tokens ,
275- responseTokens : usage . response_tokens ,
276- totalTokens : usage . total_tokens ,
277- details : usage . details ,
278- } ) ;
279-
280279const mapSources = ( sources : ApiSource [ ] ) : Source [ ] =>
281280 sources . map ( ( source ) => ( {
282281 objectId : source . object_id ,
@@ -332,31 +331,6 @@ export const mapStreamedTokensFromSSE = (
332331 } ;
333332} ;
334333
335- export const mapResponseFromSSE = (
336- sse : ServerSentEvent ,
337- ) : QueryAgentResponse => {
338- const data : ApiQueryAgentResponse = JSON . parse ( sse . data ) ;
339-
340- const properties : ResponseProperties = {
341- outputType : "finalState" ,
342- originalQuery : data . original_query ,
343- collectionNames : data . collection_names ,
344- searches : mapSearches ( data . searches ) ,
345- aggregations : mapAggregations ( data . aggregations ) ,
346- usage : mapUsage ( data . usage ) ,
347- totalTime : data . total_time ,
348- isPartialAnswer : data . is_partial_answer ,
349- missingInformation : data . missing_information ,
350- finalAnswer : data . final_answer ,
351- sources : mapSources ( data . sources ) ,
352- } ;
353-
354- return {
355- ...properties ,
356- display : ( ) => display ( properties ) ,
357- } ;
358- } ;
359-
360334const mapWeaviateObject = (
361335 object : ApiWeaviateObject ,
362336) : WeaviateObjectWithCollection => {
@@ -410,12 +384,11 @@ export const mapSearchOnlyResponse = (
410384 response : ApiSearchModeResponse ,
411385) : {
412386 mappedResponse : Omit < SearchModeResponse , "next" > ;
413- apiSearches : ApiSearchResult [ ] | undefined ;
387+ apiSearches : ApiSearch [ ] | undefined ;
414388} => {
415389 const apiSearches = response . searches ;
416390 const mappedResponse : Omit < SearchModeResponse , "next" > = {
417- originalQuery : response . original_query ,
418- searches : apiSearches ? mapInnerSearches ( apiSearches ) : undefined ,
391+ searches : apiSearches ? mapSearches ( apiSearches ) : undefined ,
419392 usage : mapUsage ( response . usage ) ,
420393 totalTime : response . total_time ,
421394 searchResults : mapWeviateSearchResults ( response . search_results ) ,
0 commit comments