ToolJet/marketplace/plugins/openai/lib/index.ts
Arpit 6d2e2046cf
Feature : OpenAI plugin (#5975)
* openai plugin init

* admin private route with admin access only

* update dev mode check
2023-04-11 11:07:27 +05:30

65 lines
1.9 KiB
TypeScript

import { QueryError, QueryResult, QueryService, ConnectionTestResult } from '@tooljet-marketplace/common';
import { SourceOptions, QueryOptions, Operation } from './types';
import { OpenAIApi, Configuration } from 'openai';
import { getCompletion, getChatCompletion } from './query_operations';
export default class Openai implements QueryService {
async run(sourceOptions: SourceOptions, queryOptions: QueryOptions, dataSourceId: string): Promise<QueryResult> {
const operation: Operation = queryOptions.operation;
const openai: OpenAIApi = await this.getConnection(sourceOptions);
let result = {};
try {
switch (operation) {
case Operation.Completion:
result = await getCompletion(openai, queryOptions);
break;
case Operation.Chat:
result = await getChatCompletion(openai, queryOptions);
break;
default:
throw new QueryError('Query could not be completed', 'Invalid operation', {});
}
} catch (error) {
throw new QueryError('Query could not be completed', error?.message, {});
}
return {
status: 'ok',
data: result,
};
}
async testConnection(sourceOptions: SourceOptions): Promise<ConnectionTestResult> {
const openai: OpenAIApi = await this.getConnection(sourceOptions);
try {
const response = await openai.listModels();
if (response.status === 200) {
return {
status: 'ok',
};
}
} catch (error) {
throw new QueryError('Connection could not be established', error?.message, {});
}
}
async getConnection(sourceOptions: SourceOptions): Promise<OpenAIApi> {
const { apiKey, organizationId = null } = sourceOptions;
const creds = {
apiKey: apiKey,
};
if (organizationId) {
creds['organizationId'] = organizationId;
}
const config = new Configuration(creds);
const openai = new OpenAIApi(config);
return openai;
}
}