Skip to content

Commit

Permalink
feat(openai): add optional organization ID for better API usage count
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamSelene committed Apr 22, 2024
1 parent d03452f commit b751bc6
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 18 deletions.
4 changes: 2 additions & 2 deletions packages/pieces/community/openai/package.json
@@ -1,4 +1,4 @@
{
"name": "@activepieces/piece-openai",
"version": "0.3.22"
}
"version": "0.4.0"
}
37 changes: 32 additions & 5 deletions packages/pieces/community/openai/src/index.ts
Expand Up @@ -7,6 +7,7 @@ import {
import {
PieceAuth,
createPiece,
Property,
} from '@activepieces/pieces-framework';
import { PieceCategory } from '@activepieces/shared';
import { askAssistant } from './lib/actions/ask-assistant';
Expand All @@ -27,10 +28,23 @@ Follow these instructions to get your OpenAI API Key:
It is strongly recommended that you add your credit card information to your OpenAI account and upgrade to the paid plan **before** generating the API Key. This will help you prevent 429 errors.
`;

export const openaiAuth = PieceAuth.SecretText({
export type OpenAIAuth = {
apiKey: string;
organizationId?: string;
};
export const openaiAuth = PieceAuth.CustomAuth({
description: markdownDescription,
displayName: 'API Key',
required: true,
props: {
apiKey: PieceAuth.SecretText({
displayName: 'API Key',
required: true,
}),
organizationId: Property.ShortText({
displayName: 'Organization ID',
required: false,
}),
},
validate: async (auth) => {
try {
await httpClient.sendRequest<{
Expand All @@ -40,7 +54,10 @@ export const openaiAuth = PieceAuth.SecretText({
method: HttpMethod.GET,
authentication: {
type: AuthenticationType.BEARER_TOKEN,
token: auth.auth as string,
token: auth.auth.apiKey,
},
headers: {
'OpenAI-Organization': auth.auth.organizationId,
},
});
return {
Expand All @@ -49,7 +66,7 @@ export const openaiAuth = PieceAuth.SecretText({
} catch (e) {
return {
valid: false,
error: 'Invalid API key',
error: 'Invalid API key or organization ID',
};
}
},
Expand Down Expand Up @@ -80,6 +97,16 @@ export const openai = createPiece({
},
}),
],
authors: ["aboudzein","astorozhevsky","Willianwg","Nilesh","Salem-Alaa","kishanprmr","MoShizzle","khaledmashaly","abuaboud"],
authors: [
'aboudzein',
'astorozhevsky',
'Willianwg',
'Nilesh',
'Salem-Alaa',
'kishanprmr',
'MoShizzle',
'khaledmashaly',
'abuaboud',
],
triggers: [],
});
Expand Up @@ -5,7 +5,7 @@ import {
Validators,
} from '@activepieces/pieces-framework';
import OpenAI from 'openai';
import { openaiAuth } from '../..';
import { OpenAIAuth, openaiAuth } from '../..';
import { sleep } from '../common/common';

export const askAssistant = createAction({
Expand All @@ -29,7 +29,8 @@ export const askAssistant = createAction({
}
try {
const openai = new OpenAI({
apiKey: auth as string,
apiKey: (auth as OpenAIAuth).apiKey,
organization: (auth as OpenAIAuth).organizationId,
});
const assistants = await openai.beta.assistants.list();

Expand Down Expand Up @@ -65,7 +66,8 @@ export const askAssistant = createAction({
},
async run({ auth, propsValue, store }) {
const openai = new OpenAI({
apiKey: auth,
apiKey: auth.apiKey,
organization: auth.organizationId,
});
const { assistant, prompt, memoryKey } = propsValue;
const runCheckDelay = 1000;
Expand Down
Expand Up @@ -99,7 +99,8 @@ export const generateImage = createAction({
},
async run({ auth, propsValue }) {
const openai = new OpenAI({
apiKey: auth,
apiKey: auth.apiKey,
organization: auth.organizationId,
});

const { quality, resolution, model, prompt } = propsValue;
Expand Down
Expand Up @@ -5,7 +5,7 @@ import {
Validators,
} from '@activepieces/pieces-framework';
import OpenAI from 'openai';
import { openaiAuth } from '../..';
import { OpenAIAuth, openaiAuth } from '../..';
import {
calculateMessagesTokenSize,
exceedsHistoryLimit,
Expand Down Expand Up @@ -36,7 +36,8 @@ export const askOpenAI = createAction({
}
try {
const openai = new OpenAI({
apiKey: auth as string,
apiKey: (auth as OpenAIAuth).apiKey,
organization: (auth as OpenAIAuth).organizationId,
});
const response = await openai.models.list();
// We need to get only LLM models
Expand Down Expand Up @@ -119,7 +120,8 @@ export const askOpenAI = createAction({
},
async run({ auth, propsValue, store }) {
const openai = new OpenAI({
apiKey: auth,
apiKey: auth.apiKey,
organization: auth.organizationId,
});
const {
model,
Expand Down
Expand Up @@ -111,7 +111,8 @@ export const textToSpeech = createAction({
},
async run({ auth, propsValue, files }) {
const openai = new OpenAI({
apiKey: auth,
apiKey: auth.apiKey,
organization: auth.organizationId,
});
const { voice, format, model, text, speed } = propsValue;

Expand Down
Expand Up @@ -48,7 +48,8 @@ export const transcribeAction = createAction({
form.append('language', language);

const headers = {
Authorization: `Bearer ${context.auth}`,
Authorization: `Bearer ${context.auth.apiKey}`,
'OpenAI-Organization': context.auth.organizationId,
};

const request: HttpRequest = {
Expand Down
Expand Up @@ -32,7 +32,8 @@ export const translateAction = createAction({
form.append('model', 'whisper-1');

const headers = {
Authorization: `Bearer ${context.auth}`,
Authorization: `Bearer ${context.auth.apiKey}`,
'OpenAI-Organization': context.auth.organizationId,
};

const request: HttpRequest = {
Expand Down
Expand Up @@ -95,7 +95,8 @@ export const visionPrompt = createAction({
},
async run({ auth, propsValue }) {
const openai = new OpenAI({
apiKey: auth,
apiKey: auth.apiKey,
organization: auth.organizationId,
});
const { temperature, maxTokens, topP, frequencyPenalty, presencePenalty } =
propsValue;
Expand Down

0 comments on commit b751bc6

Please sign in to comment.