11const OpenAI = require ( 'openai' )
22
3- class CoCreateOpenAi {
4- constructor ( crud ) {
5- this . name = "openAi"
6- this . wsManager = crud . wsManager
7- this . crud = crud
8- this . organizations = { }
9- this . init ( )
10- }
11-
12- init ( ) {
13- if ( this . wsManager ) {
14- this . wsManager . on ( this . name , ( data ) => this . send ( data ) ) ;
15- this . wsManager . on ( 'openai.chat' , ( data ) => this . send ( data ) ) ;
16- }
17- }
18-
19- async send ( data ) {
20- try {
21- let organization = await this . organizations [ data . organization_id ]
22- if ( ! organization ) {
23- if ( data . chat . apiKey ) {
24- organization = new OpenAI ( { apiKey : data . chat . apiKey } ) ;
25- this . organizations [ data . organization_id ] = organization
26- } else {
27- let apiKey = await this . getApiKey ( data . organization_id , this . name )
28- organization = new OpenAI ( { apiKey } ) ;
29- this . organizations [ data . organization_id ] = organization
30- }
31- }
32-
33- const openai = organization
34- // let response
35- switch ( data . method ) {
36- case 'chat' :
37- case 'chat.completions' :
38- case 'chat.completions.create' :
39- case 'openai.chat' :
40- delete data . chat . apiKey
41- data . chat = await openai . chat . completions . create ( data . chat ) ;
42- break ;
43- }
44-
45- if ( data . socket ) {
46- let socket = data . socket
47- delete data . socket
48- socket . send ( JSON . stringify ( data ) )
49- } else
50- return data
51- } catch ( error ) {
52- console . error ( 'OpenAi Error:' , error ) ;
53- data . error = error . message
54- let socket = data . socket
55- delete data . socket
56- socket . send ( JSON . stringify ( data ) )
57- }
58- }
59-
60- async getApiKey ( organization_id , name ) {
61- this . organizations [ organization_id ] = this . getOrganization ( organization_id , name )
62- this . organizations [ organization_id ] = await this . organizations [ organization_id ]
63- return this . organizations [ organization_id ]
64- }
3+ const name = 'openai'
654
66- async getOrganization ( organization_id , name ) {
67- let organization = await this . crud . send ( {
68- method : 'object.read' ,
69- database : organization_id ,
70- array : 'organizations' ,
71- object : [ { _id : organization_id } ] ,
72- organization_id
73- } )
5+ async function send ( data ) {
6+ let openai = false ;
747
75- if ( organization
76- && organization . object
77- && organization . object [ 0 ] ) {
78- if ( organization . object [ 0 ] . apis && organization . object [ 0 ] . apis [ name ] ) {
79- return organization . object [ 0 ] . apis && organization . object [ 0 ] . apis [ name ]
80- } else
81- return { [ this . name ] : false , error : 'An apikey could not be found' }
8+ try {
9+ // let organization = await this.organizations[data.organization_id]
10+ // if (!organization) {
11+ if ( data . chat . apiKey ) {
12+ openai = new OpenAI ( { apiKey : data . chat . apiKey } ) ;
8213 } else {
83- return { serverOrganization : false , error : 'An organization could not be found' }
14+ let apiKey = data . apis
15+ openai = new OpenAI ( { apiKey } ) ;
16+ }
17+ // }
18+
19+ // let response
20+ switch ( data . method ) {
21+ case 'chat' :
22+ case 'chat.completions' :
23+ case 'chat.completions.create' :
24+ case 'openai.chat' :
25+ delete data . chat . apiKey
26+ data . chat = await openai . chat . completions . create ( data . chat ) ;
27+ break ;
8428 }
8529
30+ return data
31+ } catch ( error ) {
32+ console . error ( 'OpenAi Error:' , error ) ;
33+ data . error = error . message
34+ return data
8635 }
87-
88-
8936}
9037
91- module . exports = CoCreateOpenAi ;
38+ module . exports = { send } ;
0 commit comments