@activepieces/pieces-common
Advanced tools
Comparing version 0.2.17 to 0.2.18
{ | ||
"name": "@activepieces/pieces-common", | ||
"version": "0.2.17", | ||
"version": "0.2.18", | ||
"type": "commonjs", | ||
@@ -5,0 +5,0 @@ "dependencies": { |
import { ServerContext } from '@activepieces/pieces-framework'; | ||
import { AiProvider } from './providers'; | ||
export type AI<SDK> = { | ||
export type AI = { | ||
provider: string; | ||
chat: AIChat; | ||
image?: AIImage; | ||
}; | ||
export type AIImage = { | ||
generate: (params: AIImageGenerateParams) => Promise<AIImageCompletion | null>; | ||
}; | ||
export type AIImageGenerateParams = { | ||
prompt: string; | ||
model: string; | ||
numImages: number; | ||
size: string; | ||
}; | ||
export type AIImageCompletion = { | ||
url: string; | ||
}; | ||
export type AIChat = { | ||
text: (params: AIChatCompletionsCreateParams) => Promise<AIChatCompletion>; | ||
extractStructuredData: (params: AIExtractStructuredDataParams) => Promise<AIExtractStructuredDataResponse>; | ||
function: (params: AIChatCompletionsCreateParams & { | ||
functions: AIFunctionDefinition[]; | ||
}) => Promise<AIChatCompletion & { | ||
call: AIFunctionCall | null; | ||
}>; | ||
}; | ||
@@ -20,4 +37,2 @@ export type AIChatCompletionsCreateParams = { | ||
id: string; | ||
created: number; | ||
model: string; | ||
choices: AIChatMessage[]; | ||
@@ -31,5 +46,8 @@ usage?: AIChatCompletionUsage; | ||
}; | ||
export type AIChatCompletionMessageToolCall = { | ||
export type AIChatMessage = { | ||
role: AIChatRole; | ||
content: string; | ||
}; | ||
export type AIFunctionCall = { | ||
id: string; | ||
type: string; | ||
function: { | ||
@@ -40,23 +58,13 @@ name: string; | ||
}; | ||
export type AIChatMessage = { | ||
role: AIChatRole; | ||
content: string; | ||
export type AIFunctionDefinition = { | ||
name: string; | ||
description: string; | ||
arguments: AIFunctionArgumentDefinition[]; | ||
}; | ||
export type AIFunctionCallingPropDefinition = { | ||
export type AIFunctionArgumentDefinition = { | ||
name: string; | ||
type: string; | ||
type: "string" | "number" | "boolean"; | ||
description?: string; | ||
isRequired: boolean; | ||
}; | ||
export type AIExtractStructuredDataParams = AIChatCompletionsCreateParams & { | ||
functionCallingProps: AIFunctionCallingPropDefinition[]; | ||
}; | ||
export type AIExtractStructuredDataResponse = { | ||
id: string; | ||
created: number; | ||
model: string; | ||
choices: AIChatMessage[]; | ||
toolCall: AIChatCompletionMessageToolCall; | ||
usage?: AIChatCompletionUsage; | ||
}; | ||
export declare enum AIChatRole { | ||
@@ -70,7 +78,7 @@ SYSTEM = "system", | ||
engineToken: string; | ||
}) => AI<unknown>; | ||
}) => AI; | ||
export declare const AI: ({ provider, server, }: { | ||
provider: AiProvider; | ||
server: ServerContext; | ||
}) => AI<unknown>; | ||
}) => AI; | ||
export * from './providers'; |
@@ -22,2 +22,3 @@ "use strict"; | ||
provider, | ||
image: impl.image, | ||
chat: { | ||
@@ -37,6 +38,6 @@ text: (params) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { | ||
}), | ||
extractStructuredData: (params) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { | ||
function: (params) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { | ||
var _c; | ||
try { | ||
const response = yield impl.chat.extractStructuredData(params); | ||
const response = yield impl.chat.function(params); | ||
return response; | ||
@@ -43,0 +44,0 @@ } |
@@ -5,4 +5,4 @@ "use strict"; | ||
const tslib_1 = require("tslib"); | ||
const sdk_1 = tslib_1.__importDefault(require("@anthropic-ai/sdk")); | ||
const __1 = require("../.."); | ||
const sdk_1 = tslib_1.__importDefault(require("@anthropic-ai/sdk")); | ||
const anthropic = ({ proxyUrl, engineToken, }) => { | ||
@@ -19,2 +19,3 @@ const sdk = new sdk_1.default({ | ||
provider: 'ANTHROPIC', | ||
image: undefined, | ||
chat: { | ||
@@ -56,4 +57,4 @@ text: (params) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { | ||
}), | ||
extractStructuredData: (params) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { | ||
var _c, _d; | ||
function: (params) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { | ||
var _c; | ||
const completion = yield sdk.messages.create({ | ||
@@ -66,20 +67,19 @@ model: params.model, | ||
max_tokens: (_c = params.maxTokens) !== null && _c !== void 0 ? _c : 2000, | ||
tools: [ | ||
{ | ||
name: 'extract_structured_data', | ||
description: 'Extract the following data from the provided text.', | ||
input_schema: { | ||
type: 'object', | ||
properties: params.functionCallingProps.reduce((acc, { name, type, description }) => { | ||
acc[name] = { type, description }; | ||
return acc; | ||
}, {}), | ||
required: params.functionCallingProps | ||
.filter((prop) => prop.isRequired) | ||
.map((prop) => prop.name), | ||
}, | ||
tools: params.functions.map((functionDefinition) => ({ | ||
name: functionDefinition.name, | ||
description: functionDefinition.description, | ||
input_schema: { | ||
type: 'object', | ||
properties: functionDefinition.arguments.reduce((acc, { name, type, description }) => { | ||
acc[name] = { type, description }; | ||
return acc; | ||
}, {}), | ||
required: functionDefinition.arguments | ||
.filter((prop) => prop.isRequired) | ||
.map((prop) => prop.name), | ||
}, | ||
], | ||
})), | ||
}); | ||
const toolCallsResponse = completion.content.filter((choice) => choice.type === 'tool_use'); | ||
const toolCall = toolCallsResponse[0]; | ||
return { | ||
@@ -92,10 +92,9 @@ choices: completion.content | ||
})), | ||
toolCall: { | ||
id: toolCallsResponse[0].id, | ||
type: 'function', | ||
call: toolCall ? { | ||
id: toolCall.id, | ||
function: { | ||
name: (_d = toolCallsResponse[0].name) !== null && _d !== void 0 ? _d : 'extract_structured_data', | ||
arguments: toolCallsResponse[0].input, | ||
name: toolCall.name, | ||
arguments: toolCall.input, | ||
}, | ||
}, | ||
} : null, | ||
id: completion.id, | ||
@@ -102,0 +101,0 @@ model: completion.model, |
@@ -10,2 +10,3 @@ import { Static } from "@sinclair/typebox"; | ||
value: string; | ||
types: string[]; | ||
}[]; | ||
@@ -22,2 +23,3 @@ auth: import("./utils").AuthHeader; | ||
value: string; | ||
types: string[]; | ||
}[]; | ||
@@ -27,3 +29,3 @@ auth: import("./utils").AuthHeader; | ||
})[]; | ||
export declare const aiProps: { | ||
export declare const aiProps: (type: 'text' | 'image') => { | ||
provider: import("@activepieces/pieces-framework").DropdownProperty<"openai" | "anthropic", true>; | ||
@@ -30,0 +32,0 @@ model: import("@activepieces/pieces-framework").DropdownProperty<string, true>; |
@@ -19,6 +19,8 @@ "use strict"; | ||
models: [ | ||
{ label: 'gpt-4o', value: 'gpt-4o' }, | ||
{ label: 'gpt-4o-mini', value: 'gpt-4o-mini' }, | ||
{ label: 'gpt-4-turbo', value: 'gpt-4-turbo' }, | ||
{ label: 'gpt-3.5-turbo', value: 'gpt-3.5-turbo' }, | ||
{ label: 'gpt-4o', value: 'gpt-4o', types: ['text'] }, | ||
{ label: 'gpt-4o-mini', value: 'gpt-4o-mini', types: ['text'] }, | ||
{ label: 'gpt-4-turbo', value: 'gpt-4-turbo', types: ['text'] }, | ||
{ label: 'gpt-3.5-turbo', value: 'gpt-3.5-turbo', types: ['text'] }, | ||
{ label: 'dall-e-3', value: 'dall-e-3', types: ['image'] }, | ||
{ label: 'dall-e-2', value: 'dall-e-2', types: ['image'] }, | ||
], | ||
@@ -36,3 +38,4 @@ auth: (0, utils_1.authHeader)({ bearer: true }), | ||
label: 'claude-3-5-sonnet', | ||
value: 'claude-3-5-sonnet-20240620' | ||
value: 'claude-3-5-sonnet-20240620', | ||
types: ['text'] | ||
}, | ||
@@ -42,2 +45,3 @@ { | ||
value: 'claude-3-opus-20240229', | ||
types: ['text'] | ||
}, | ||
@@ -47,2 +51,3 @@ { | ||
value: 'claude-3-sonnet-20240229', | ||
types: ['text'] | ||
}, | ||
@@ -52,2 +57,3 @@ { | ||
value: 'claude-3-haiku-20240307', | ||
types: ['text'] | ||
}, | ||
@@ -59,3 +65,3 @@ ], | ||
]; | ||
exports.aiProps = { | ||
const aiProps = (type) => ({ | ||
provider: pieces_framework_1.Property.Dropdown({ | ||
@@ -80,12 +86,17 @@ displayName: 'Provider', | ||
} | ||
const providersWithMetadata = providers.body.data.flatMap((p) => { | ||
const providerMetadata = exports.AI_PROVIDERS.find((meta) => meta.value === p.provider && meta.models.some(m => m.types.includes(type))); | ||
if ((0, shared_1.isNil)(providerMetadata)) { | ||
return []; | ||
} | ||
return { | ||
value: providerMetadata.value, | ||
label: providerMetadata.label, | ||
models: providerMetadata.models, | ||
}; | ||
}); | ||
return { | ||
placeholder: 'Select AI Provider', | ||
disabled: false, | ||
options: providers.body.data.map((p) => { | ||
var _a, _b; | ||
return ({ | ||
label: (_b = (_a = exports.AI_PROVIDERS.find((f) => f.value === p.provider)) === null || _a === void 0 ? void 0 : _a.label) !== null && _b !== void 0 ? _b : 'Unknown Label', | ||
value: p.provider, | ||
}); | ||
}), | ||
options: providersWithMetadata, | ||
}; | ||
@@ -107,3 +118,3 @@ }) | ||
} | ||
const models = (_b = exports.AI_PROVIDERS.find((p) => p.value === provider)) === null || _b === void 0 ? void 0 : _b.models; | ||
const models = (_b = exports.AI_PROVIDERS.find((p) => p.value === provider)) === null || _b === void 0 ? void 0 : _b.models.filter(m => m.types.includes(type)); | ||
return { | ||
@@ -115,5 +126,6 @@ disabled: (0, shared_1.isNil)(models), | ||
}), | ||
}; | ||
}); | ||
exports.aiProps = aiProps; | ||
exports.AiProvider = typebox_1.Type.Union(exports.AI_PROVIDERS.map(p => typebox_1.Type.Literal(p.value))); | ||
tslib_1.__exportStar(require("./utils"), exports); | ||
//# sourceMappingURL=index.js.map |
@@ -18,2 +18,13 @@ "use strict"; | ||
provider: 'OPENAI', | ||
image: { | ||
generate: (params) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { | ||
const response = yield sdk.images.generate({ | ||
model: params.model, | ||
prompt: params.prompt, | ||
n: params.numImages, | ||
}); | ||
const url = response.data[0].url; | ||
return url ? { url } : null; | ||
}), | ||
}, | ||
chat: { | ||
@@ -50,4 +61,4 @@ text: (params) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { | ||
}), | ||
extractStructuredData: (params) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { | ||
var _b, _c, _d, _e, _f; | ||
function: (params) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { | ||
var _b; | ||
const completion = yield sdk.chat.completions.create({ | ||
@@ -60,24 +71,24 @@ model: params.model, | ||
max_tokens: params.maxTokens, | ||
tools: [ | ||
{ | ||
type: 'function', | ||
function: { | ||
name: 'extract_structured_data', | ||
description: 'Extract the following data from the provided text.', | ||
parameters: { | ||
type: 'object', | ||
properties: params.functionCallingProps.reduce((acc, { name, type, description }) => { | ||
acc[name] = { type, description }; | ||
return acc; | ||
}, {}), | ||
required: params.functionCallingProps | ||
.filter((prop) => prop.isRequired) | ||
.map((prop) => prop.name), | ||
}, | ||
tools: params.functions.map((functionDefinition) => ({ | ||
type: 'function', | ||
function: { | ||
name: functionDefinition.name, | ||
description: functionDefinition.description, | ||
parameters: { | ||
type: 'object', | ||
properties: functionDefinition.arguments.reduce((acc, { name, type, description }) => { | ||
acc[name] = { type, description }; | ||
return acc; | ||
}, {}), | ||
required: functionDefinition.arguments | ||
.filter((prop) => prop.isRequired) | ||
.map((prop) => prop.name), | ||
}, | ||
}, | ||
], | ||
})), | ||
}); | ||
const toolCall = (_b = completion.choices[0].message.tool_calls) === null || _b === void 0 ? void 0 : _b[0]; | ||
return { | ||
choices: completion.choices.map((choice) => { | ||
choices: completion.choices | ||
.map((choice) => { | ||
var _a; | ||
@@ -89,10 +100,9 @@ return ({ | ||
}), | ||
toolCall: { | ||
id: (_c = (_b = completion.choices[0].message.tool_calls) === null || _b === void 0 ? void 0 : _b[0].id) !== null && _c !== void 0 ? _c : '', | ||
type: 'function', | ||
call: toolCall ? { | ||
id: toolCall.id, | ||
function: { | ||
name: (_e = (_d = completion.choices[0].message.tool_calls) === null || _d === void 0 ? void 0 : _d[0].function.name) !== null && _e !== void 0 ? _e : 'extract_structured_data', | ||
arguments: JSON.parse((_f = completion.choices[0].message.tool_calls) === null || _f === void 0 ? void 0 : _f[0].function.arguments), | ||
name: toolCall.function.name, | ||
arguments: JSON.parse(toolCall.function.arguments), | ||
}, | ||
}, | ||
} : null, | ||
created: completion.created, | ||
@@ -99,0 +109,0 @@ id: completion.id, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
84890
1262