1- import OpenAI from " openai" ;
1+ const OpenAI = require ( ' openai' )
22
33class CoCreateOpenAi {
44 constructor ( crud ) {
@@ -11,31 +11,30 @@ class CoCreateOpenAi {
1111
1212 init ( ) {
1313 if ( this . wsManager ) {
14- this . wsManager . on ( 'openAi' , ( data ) => this . send ( data ) ) ;
14+ this . wsManager . on ( this . name , ( data ) => this . send ( data ) ) ;
15+ this . wsManager . on ( 'openai.chat' , ( data ) => this . send ( data ) ) ;
1516 }
1617 }
1718
1819 async send ( data ) {
1920 try {
20- let organization = this . organizations [ data . organization_id ]
21+ let organization = await this . organizations [ data . organization_id ]
2122 if ( ! organization ) {
22- organization = { apikey : await this . getApiKey ( organization_id , this . name ) }
23- organization . instance = new OpenAI ( organization . apikey ) ;
23+ let apiKey = await this . getApiKey ( data . organization_id , this . name )
24+ organization = new OpenAI ( { apiKey } ) ;
25+ this . organizations [ data . organization_id ] = organization
2426 }
2527
26- const openai = organization . instance
27- const completion = await openai . chat . completions . create ( {
28- messages : [ { role : "system" , content : "You are a helpful assistant." } ] ,
29- model : "gpt-3.5-turbo" ,
30- } ) ;
31- data . completion = completion
32- console . log ( completion . choices [ 0 ] ) ;
33- // messages: data.messages,
34- // max_tokens: data.maxtokens,
35- // temperature: data.temperature,
36- // n,
37- // stop,
38- // model
28+ const openai = organization
29+ // let response
30+ switch ( data . method ) {
31+ case 'chat' :
32+ case 'chat.completions' :
33+ case 'chat.completions.create' :
34+ case 'openai.chat' :
35+ data . chat = await openai . chat . completions . create ( data . chat ) ;
36+ break ;
37+ }
3938
4039 if ( data . socket ) {
4140 let socket = data . socket
@@ -49,14 +48,9 @@ class CoCreateOpenAi {
4948 }
5049
5150 async getApiKey ( organization_id , name ) {
52- if ( this . organizations [ organization_id ] ) {
53- return await this . organizations [ organization_id ]
54- } else {
55- this . organizations [ organization_id ] = this . getOrganization ( organization_id , name )
56- this . organizations [ organization_id ] = await this . organizations [ organization_id ]
57- return this . organizations [ organization_id ]
58- }
59-
51+ this . organizations [ organization_id ] = this . getOrganization ( organization_id , name )
52+ this . organizations [ organization_id ] = await this . organizations [ organization_id ]
53+ return this . organizations [ organization_id ]
6054 }
6155
6256 async getOrganization ( organization_id , name ) {
0 commit comments