@@ -62,7 +62,11 @@ class FlowtestAI {
6262 } ;
6363 }
6464
65- functions . push ( { name : function_name , description : desc , parameters : schema } ) ;
65+ const f = { name : function_name , description : desc , parameters : schema } ;
66+ // ignore functions with circular dependency
67+ if ( ! this . isCyclic ( f ) ) {
68+ functions . push ( f ) ;
69+ }
6670 } ) ;
6771 } ) ;
6872 // console.log(JSON.stringify(functions));
@@ -78,7 +82,14 @@ class FlowtestAI {
7882 chunks . push ( chunk ) ;
7983 }
8084
81- const documents = chunks . map ( ( chunk ) => JSON . stringify ( chunk ) ) ;
85+ const documents = chunks . map ( ( chunk ) => {
86+ return JSON . stringify (
87+ chunk . map ( ( f ) => {
88+ const { parameters, ...fDescription } = f ;
89+ return fDescription ;
90+ } ) ,
91+ ) ;
92+ } ) ;
8293
8394 const vectorStore = await MemoryVectorStore . fromTexts (
8495 documents ,
@@ -92,7 +103,13 @@ class FlowtestAI {
92103 const retrievedDocuments = await vectorStore . similaritySearch ( instruction , 4 ) ;
93104 var selectedFunctions = [ ] ;
94105 retrievedDocuments . forEach ( ( document ) => {
95- selectedFunctions = selectedFunctions . concat ( JSON . parse ( document . pageContent ) ) ;
106+ const pDocument = JSON . parse ( document . pageContent ) ;
107+ pDocument . forEach ( ( outputF ) => {
108+ const findF = functions . find ( ( f ) => f . name === outputF . name && f . description === outputF . description ) ;
109+ if ( findF ) {
110+ selectedFunctions = selectedFunctions . concat ( findF ) ;
111+ }
112+ } ) ;
96113 } ) ;
97114
98115 return selectedFunctions ;
@@ -152,6 +169,48 @@ class FlowtestAI {
152169
153170 return result ;
154171 }
172+
173+ isCyclic ( obj ) {
174+ var keys = [ ] ;
175+ var stack = [ ] ;
176+ var stackSet = new Set ( ) ;
177+ var detected = false ;
178+
179+ function detect ( obj , key ) {
180+ if ( obj && typeof obj != 'object' ) {
181+ return false ;
182+ }
183+
184+ if ( stackSet . has ( obj ) ) {
185+ // it's cyclic! Print the object and its locations.
186+ var oldindex = stack . indexOf ( obj ) ;
187+ var l1 = keys . join ( '.' ) + '.' + key ;
188+ var l2 = keys . slice ( 0 , oldindex + 1 ) . join ( '.' ) ;
189+ //console.log('CIRCULAR: ' + l1 + ' = ' + l2 + ' = ' + obj);
190+ //console.log(obj);
191+ detected = true ;
192+ return ;
193+ }
194+
195+ keys . push ( key ) ;
196+ stack . push ( obj ) ;
197+ stackSet . add ( obj ) ;
198+ for ( var k in obj ) {
199+ //dive on the object's children
200+ if ( Object . prototype . hasOwnProperty . call ( obj , k ) ) {
201+ detect ( obj [ k ] , k ) ;
202+ }
203+ }
204+
205+ keys . pop ( ) ;
206+ stack . pop ( ) ;
207+ stackSet . delete ( obj ) ;
208+ return ;
209+ }
210+
211+ detect ( obj , 'obj' , keys , stack , stackSet , detected ) ;
212+ return detected ;
213+ }
155214}
156215
157216module . exports = FlowtestAI ;
0 commit comments