Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(openai): add optional organization ID for better API usage count #24

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/pieces/community/openai/package.json
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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: [],
});
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Loading