@@ -29,6 +29,8 @@ export class DataSourceLookup extends Component {
2929 // Need to reserve 30 characters for the prefixed unique id
3030 'string.max' : `The length of the 'namespace' name must be 50 characters or fewer.` ,
3131 } ) ,
32+ scoreThreshold : Joi . number ( ) . optional ( ) . label ( 'Score Threshold' ) ,
33+ includeScore : Joi . boolean ( ) . optional ( ) . label ( 'Include Score' ) ,
3234 } ) ;
3335 constructor ( ) {
3436 super ( ) ;
@@ -53,6 +55,9 @@ export class DataSourceLookup extends Component {
5355 const postprocess = config . data ?. postprocess || false ;
5456 const includeMetadata = config . data ?. includeMetadata || false ;
5557
58+ const scoreThreshold = config . data ?. scoreThreshold || 0.001 ; // Use low score (0.001) to return most results for backward compatibility
59+ const includeScore = config . data ?. includeScore || false ;
60+
5661 const _input = typeof input . Query === 'string' ? input . Query : JSON . stringify ( input . Query ) ;
5762
5863 const topK = Math . max ( config . data ?. topK || 50 , 50 ) ;
@@ -64,27 +69,40 @@ export class DataSourceLookup extends Component {
6469 throw new Error ( `Namespace ${ namespace } does not exist` ) ;
6570 }
6671
67- let results : string [ ] | { content : string ; metadata : any } [ ] ;
72+ let results : string [ ] | { content : string ; metadata : any ; score ?: number } [ ] ;
6873 let _error ;
6974 try {
7075 const response = await vectorDbConnector
7176 . requester ( AccessCandidate . team ( teamId ) )
7277 . search ( namespace , _input , { topK, includeMetadata : true } ) ;
78+
7379 results = response . slice ( 0 , config . data . topK ) . map ( ( result ) => ( {
7480 content : result . text ,
7581 metadata : result . metadata ,
82+ score : result . score , // use a very low score to return
7683 } ) ) ;
7784
78- if ( includeMetadata ) {
79- // only show user-level metadata
80- results = results . map ( ( result ) => ( {
85+ results = results . filter ( ( result ) => result . score >= scoreThreshold ) ;
86+
87+ // Transform results based on inclusion flags
88+ results = results . map ( ( result ) => {
89+ const transformedResult : any = {
8190 content : result . content ,
82- //* legacy user-specific metadata key [result.metadata?.metadata]),
83- metadata : this . parseMetadata ( result . metadata || result . metadata ?. metadata ) ,
84- } ) ) ;
85- } else {
86- results = results . map ( ( result ) => result . content ) ;
87- }
91+ } ;
92+
93+ if ( includeMetadata ) {
94+ // legacy user-specific metadata key [result.metadata?.metadata]
95+ transformedResult . metadata = this . parseMetadata ( result . metadata || result . metadata ?. metadata ) ;
96+ }
97+
98+ if ( includeScore ) {
99+ transformedResult . score = result . score ;
100+ }
101+
102+ // If neither metadata nor score is included, return just the content string
103+ return includeMetadata || includeScore ? transformedResult : result . content ;
104+ } ) ;
105+
88106 debugOutput += `[Results] \nLoaded ${ results . length } results from namespace: ${ namespace } \n\n` ;
89107 } catch ( error ) {
90108 _error = error . toString ( ) ;
@@ -112,6 +130,7 @@ export class DataSourceLookup extends Component {
112130
113131 const totalLength = JSON . stringify ( results ) . length ;
114132 debugOutput += `[Total Length] \n${ totalLength } \n\n` ;
133+
115134 return {
116135 Results : results ,
117136 _error,
0 commit comments