@@ -3,11 +3,7 @@ const dotenv = require('dotenv');
33const { MemoryVectorStore } = require ( 'langchain/vectorstores/memory' ) ;
44const { OpenAIEmbeddings } = require ( '@langchain/openai' ) ;
55
6- dotenv . config ( ) ;
7-
8- const openai = new OpenAI ( {
9- apiKey : process . env . OPENAI_API_KEY ,
10- } ) ;
6+ //dotenv.config();
117
128const SYSTEM_MESSAGE = `You are a helpful assistant. \
139 Respond to the following prompt by using function_call and then summarize actions. \
@@ -17,10 +13,14 @@ const SYSTEM_MESSAGE = `You are a helpful assistant. \
1713const MAX_CALLS = 10 ;
1814
1915class FlowtestAI {
20- async generate ( collection , user_instruction ) {
21- const available_functions = await this . get_available_functions ( collection ) ;
22- const functions = await this . filter_functions ( available_functions , user_instruction ) ;
23- return await this . process_user_instruction ( functions , user_instruction ) ;
16+ async generate ( collection , user_instruction , model ) {
17+ if ( model . name === 'OPENAI' ) {
18+ const available_functions = await this . get_available_functions ( collection ) ;
19+ const functions = await this . filter_functions ( available_functions , user_instruction , model . apiKey ) ;
20+ return await this . process_user_instruction ( functions , user_instruction , model . apiKey ) ;
21+ } else {
22+ throw Error ( `Model ${ model . name } not supported` ) ;
23+ }
2424 }
2525
2626 async get_available_functions ( collection ) {
@@ -69,7 +69,7 @@ class FlowtestAI {
6969 return functions ;
7070 }
7171
72- async filter_functions ( functions , instruction ) {
72+ async filter_functions ( functions , instruction , apiKey ) {
7373 const chunkSize = 32 ;
7474 const chunks = [ ] ;
7575
@@ -84,7 +84,7 @@ class FlowtestAI {
8484 documents ,
8585 [ ] ,
8686 new OpenAIEmbeddings ( {
87- openAIApiKey : process . env . OPENAI_API_KEY ,
87+ openAIApiKey : apiKey ,
8888 } ) ,
8989 ) ;
9090
@@ -98,7 +98,11 @@ class FlowtestAI {
9898 return selectedFunctions ;
9999 }
100100
101- async get_openai_response ( functions , messages ) {
101+ async get_openai_response ( functions , messages , apiKey ) {
102+ const openai = new OpenAI ( {
103+ apiKey,
104+ } ) ;
105+
102106 return await openai . chat . completions . create ( {
103107 model : 'gpt-3.5-turbo-16k-0613' ,
104108 functions : functions ,
@@ -108,7 +112,7 @@ class FlowtestAI {
108112 } ) ;
109113 }
110114
111- async process_user_instruction ( functions , instruction ) {
115+ async process_user_instruction ( functions , instruction , apiKey ) {
112116 let result = [ ] ;
113117 let num_calls = 0 ;
114118 const messages = [
@@ -117,7 +121,7 @@ class FlowtestAI {
117121 ] ;
118122
119123 while ( num_calls < MAX_CALLS ) {
120- const response = await this . get_openai_response ( functions , messages ) ;
124+ const response = await this . get_openai_response ( functions , messages , apiKey ) ;
121125 // console.log(response)
122126 const message = response [ 'choices' ] [ 0 ] [ 'message' ] ;
123127
0 commit comments