From c975a562452172db401ca791e7977ba1dd8e02c7 Mon Sep 17 00:00:00 2001 From: Ali BARIN Date: Wed, 20 Dec 2023 14:46:31 +0000 Subject: [PATCH] feat(azure-openai): add send prompt action --- .../src/apps/azure-openai/actions/index.ts | 3 + .../azure-openai/actions/send-prompt/index.ts | 87 +++++++++++++++++++ .../src/apps/azure-openai/assets/favicon.svg | 6 ++ .../src/apps/azure-openai/auth/index.ts | 58 +++++++++++++ .../azure-openai/auth/is-still-verified.ts | 8 ++ .../azure-openai/auth/verify-credentials.ts | 7 ++ .../azure-openai/common/add-auth-header.ts | 15 ++++ .../apps/azure-openai/common/set-base-url.ts | 13 +++ .../backend/src/apps/azure-openai/index.d.ts | 0 .../backend/src/apps/azure-openai/index.ts | 19 ++++ .../openai/actions/send-chat-prompt/index.ts | 2 +- .../apps/openai/actions/send-prompt/index.ts | 2 +- 12 files changed, 218 insertions(+), 2 deletions(-) create mode 100644 packages/backend/src/apps/azure-openai/actions/index.ts create mode 100644 packages/backend/src/apps/azure-openai/actions/send-prompt/index.ts create mode 100644 packages/backend/src/apps/azure-openai/assets/favicon.svg create mode 100644 packages/backend/src/apps/azure-openai/auth/index.ts create mode 100644 packages/backend/src/apps/azure-openai/auth/is-still-verified.ts create mode 100644 packages/backend/src/apps/azure-openai/auth/verify-credentials.ts create mode 100644 packages/backend/src/apps/azure-openai/common/add-auth-header.ts create mode 100644 packages/backend/src/apps/azure-openai/common/set-base-url.ts create mode 100644 packages/backend/src/apps/azure-openai/index.d.ts create mode 100644 packages/backend/src/apps/azure-openai/index.ts diff --git a/packages/backend/src/apps/azure-openai/actions/index.ts b/packages/backend/src/apps/azure-openai/actions/index.ts new file mode 100644 index 00000000..b91a5287 --- /dev/null +++ b/packages/backend/src/apps/azure-openai/actions/index.ts @@ -0,0 +1,3 @@ +import sendPrompt from './send-prompt'; + +export default [sendPrompt]; diff --git a/packages/backend/src/apps/azure-openai/actions/send-prompt/index.ts b/packages/backend/src/apps/azure-openai/actions/send-prompt/index.ts new file mode 100644 index 00000000..302a4b9d --- /dev/null +++ b/packages/backend/src/apps/azure-openai/actions/send-prompt/index.ts @@ -0,0 +1,87 @@ +import defineAction from '../../../../helpers/define-action'; + +const castFloatOrUndefined = (value: string | null) => { + return value === '' ? undefined : parseFloat(value); +} + +export default defineAction({ + name: 'Send prompt', + key: 'sendPrompt', + description: 'Creates a completion for the provided prompt and parameters.', + arguments: [ + { + label: 'Prompt', + key: 'prompt', + type: 'string' as const, + required: true, + variables: true, + description: 'The text to analyze.' + }, + { + label: 'Temperature', + key: 'temperature', + type: 'string' as const, + required: false, + variables: true, + description: 'What sampling temperature to use, between 0 and 2. Higher values means the model will take more risks. Try 0.9 for more creative applications, and 0 (argmax sampling) for ones with a well-defined answer. We generally recommend altering this or Top P but not both.' + }, + { + label: 'Maximum tokens', + key: 'maxTokens', + type: 'string' as const, + required: false, + variables: true, + description: 'The maximum number of tokens to generate in the completion.' + }, + { + label: 'Stop Sequence', + key: 'stopSequence', + type: 'string' as const, + required: false, + variables: true, + description: 'Single stop sequence where the API will stop generating further tokens. The returned text will not contain the stop sequence.' + }, + { + label: 'Top P', + key: 'topP', + type: 'string' as const, + required: false, + variables: true, + description: 'An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered. We generally recommend altering this or temperature but not both.' + }, + { + label: 'Frequency Penalty', + key: 'frequencyPenalty', + type: 'string' as const, + required: false, + variables: true, + description: `Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model's likelihood to repeat the same line verbatim.` + }, + { + label: 'Presence Penalty', + key: 'presencePenalty', + type: 'string' as const, + required: false, + variables: true, + description: `Number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far, increasing the model's likelihood to talk about new topics.` + }, + ], + + async run($) { + const payload = { + model: $.step.parameters.model as string, + prompt: $.step.parameters.prompt as string, + temperature: castFloatOrUndefined($.step.parameters.temperature as string), + max_tokens: castFloatOrUndefined($.step.parameters.maxTokens as string), + stop: ($.step.parameters.stopSequence as string || null), + top_p: castFloatOrUndefined($.step.parameters.topP as string), + frequency_penalty: castFloatOrUndefined($.step.parameters.frequencyPenalty as string), + presence_penalty: castFloatOrUndefined($.step.parameters.presencePenalty as string), + }; + const { data } = await $.http.post(`/deployments/${$.auth.data.deploymentId}/completions`, payload); + + $.setActionItem({ + raw: data, + }); + }, +}); diff --git a/packages/backend/src/apps/azure-openai/assets/favicon.svg b/packages/backend/src/apps/azure-openai/assets/favicon.svg new file mode 100644 index 00000000..b62b84eb --- /dev/null +++ b/packages/backend/src/apps/azure-openai/assets/favicon.svg @@ -0,0 +1,6 @@ + + OpenAI + + + + \ No newline at end of file diff --git a/packages/backend/src/apps/azure-openai/auth/index.ts b/packages/backend/src/apps/azure-openai/auth/index.ts new file mode 100644 index 00000000..716b4f10 --- /dev/null +++ b/packages/backend/src/apps/azure-openai/auth/index.ts @@ -0,0 +1,58 @@ +import verifyCredentials from './verify-credentials'; +import isStillVerified from './is-still-verified'; + +export default { + fields: [ + { + key: 'screenName', + label: 'Screen Name', + type: 'string' as const, + required: true, + readOnly: false, + value: null, + placeholder: null, + description: + 'Screen name of your connection to be used on Automatisch UI.', + clickToCopy: false, + }, + { + key: 'yourResourceName', + label: 'Your Resource Name', + type: 'string' as const, + required: true, + readOnly: false, + value: null, + placeholder: null, + description: 'The name of your Azure OpenAI Resource.', + docUrl: 'https://automatisch.io/docs/azure-openai#your-resource-name', + clickToCopy: false, + }, + { + key: 'deploymentId', + label: 'Deployment ID', + type: 'string' as const, + required: true, + readOnly: false, + value: null, + placeholder: null, + description: 'The deployment name you chose when you deployed the model.', + docUrl: 'https://automatisch.io/docs/azure-openai#deployment-id', + clickToCopy: false, + }, + { + key: 'apiKey', + label: 'API Key', + type: 'string' as const, + required: true, + readOnly: false, + value: null, + placeholder: null, + description: 'Azure OpenAI API key of your account.', + docUrl: 'https://automatisch.io/docs/azure-openai#api-key', + clickToCopy: false, + }, + ], + + verifyCredentials, + isStillVerified, +}; diff --git a/packages/backend/src/apps/azure-openai/auth/is-still-verified.ts b/packages/backend/src/apps/azure-openai/auth/is-still-verified.ts new file mode 100644 index 00000000..4c990b92 --- /dev/null +++ b/packages/backend/src/apps/azure-openai/auth/is-still-verified.ts @@ -0,0 +1,8 @@ +import { IGlobalVariable } from '@automatisch/types'; + +const isStillVerified = async ($: IGlobalVariable) => { + await $.http.get('/fine_tuning/jobs'); + return true; +}; + +export default isStillVerified; diff --git a/packages/backend/src/apps/azure-openai/auth/verify-credentials.ts b/packages/backend/src/apps/azure-openai/auth/verify-credentials.ts new file mode 100644 index 00000000..946257d0 --- /dev/null +++ b/packages/backend/src/apps/azure-openai/auth/verify-credentials.ts @@ -0,0 +1,7 @@ +import { IGlobalVariable } from '@automatisch/types'; + +const verifyCredentials = async ($: IGlobalVariable) => { + await $.http.get('/fine_tuning/jobs'); +}; + +export default verifyCredentials; diff --git a/packages/backend/src/apps/azure-openai/common/add-auth-header.ts b/packages/backend/src/apps/azure-openai/common/add-auth-header.ts new file mode 100644 index 00000000..277fad8e --- /dev/null +++ b/packages/backend/src/apps/azure-openai/common/add-auth-header.ts @@ -0,0 +1,15 @@ +import { TBeforeRequest } from '@automatisch/types'; + +const addAuthHeader: TBeforeRequest = ($, requestConfig) => { + if ($.auth.data?.apiKey) { + requestConfig.headers['api-key'] = $.auth.data.apiKey as string; + } + + requestConfig.params = { + 'api-version': '2023-10-01-preview' + } + + return requestConfig; +}; + +export default addAuthHeader; diff --git a/packages/backend/src/apps/azure-openai/common/set-base-url.ts b/packages/backend/src/apps/azure-openai/common/set-base-url.ts new file mode 100644 index 00000000..7e90dcfd --- /dev/null +++ b/packages/backend/src/apps/azure-openai/common/set-base-url.ts @@ -0,0 +1,13 @@ +import { TBeforeRequest } from '@automatisch/types'; + +const setBaseUrl: TBeforeRequest = ($, requestConfig) => { + const yourResourceName = $.auth.data.yourResourceName as string; + + if (yourResourceName) { + requestConfig.baseURL = `https://${yourResourceName}.openai.azure.com/openai`; + } + + return requestConfig; +}; + +export default setBaseUrl; diff --git a/packages/backend/src/apps/azure-openai/index.d.ts b/packages/backend/src/apps/azure-openai/index.d.ts new file mode 100644 index 00000000..e69de29b diff --git a/packages/backend/src/apps/azure-openai/index.ts b/packages/backend/src/apps/azure-openai/index.ts new file mode 100644 index 00000000..77f570c3 --- /dev/null +++ b/packages/backend/src/apps/azure-openai/index.ts @@ -0,0 +1,19 @@ +import defineApp from '../../helpers/define-app'; +import setBaseUrl from './common/set-base-url'; +import addAuthHeader from './common/add-auth-header'; +import auth from './auth'; +import actions from './actions'; + +export default defineApp({ + name: 'Azure OpenAI', + key: 'azure-openai', + baseUrl: 'https://azure.microsoft.com/en-us/products/ai-services/openai-service', + apiBaseUrl: '', + iconUrl: '{BASE_URL}/apps/azure-openai/assets/favicon.svg', + authDocUrl: 'https://automatisch.io/docs/apps/azure-openai/connection', + primaryColor: '000000', + supportsConnections: true, + beforeRequest: [setBaseUrl, addAuthHeader], + auth, + actions, +}); diff --git a/packages/backend/src/apps/openai/actions/send-chat-prompt/index.ts b/packages/backend/src/apps/openai/actions/send-chat-prompt/index.ts index 0e5a7dec..ea239036 100644 --- a/packages/backend/src/apps/openai/actions/send-chat-prompt/index.ts +++ b/packages/backend/src/apps/openai/actions/send-chat-prompt/index.ts @@ -105,7 +105,7 @@ export default defineAction({ description: `Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model's likelihood to repeat the same line verbatim.` }, { - label: 'presencePenalty', + label: 'Presence Penalty', key: 'presencePenalty', type: 'string' as const, required: false, diff --git a/packages/backend/src/apps/openai/actions/send-prompt/index.ts b/packages/backend/src/apps/openai/actions/send-prompt/index.ts index 7eef1b62..cff0ae8f 100644 --- a/packages/backend/src/apps/openai/actions/send-prompt/index.ts +++ b/packages/backend/src/apps/openai/actions/send-prompt/index.ts @@ -75,7 +75,7 @@ export default defineAction({ description: `Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model's likelihood to repeat the same line verbatim.` }, { - label: 'presencePenalty', + label: 'Presence Penalty', key: 'presencePenalty', type: 'string' as const, required: false,