@activepieces/pieces-common
Advanced tools
Comparing version 0.2.26 to 0.2.27
{ | ||
"name": "@activepieces/pieces-common", | ||
"version": "0.2.26", | ||
"version": "0.2.27", | ||
"type": "commonjs", | ||
"dependencies": { | ||
"@anthropic-ai/sdk": "0.27.3", | ||
"@sinclair/typebox": "0.32.35", | ||
"@sinclair/typebox": "0.33.18", | ||
"axios": "1.7.7", | ||
@@ -17,4 +17,4 @@ "axios-retry": "4.4.1", | ||
"semver": "7.6.0", | ||
"@activepieces/pieces-framework": "0.7.37", | ||
"@activepieces/shared": "0.10.121", | ||
"@activepieces/pieces-framework": "0.7.39", | ||
"@activepieces/shared": "0.10.123", | ||
"tslib": "2.6.2" | ||
@@ -21,0 +21,0 @@ }, |
@@ -8,3 +8,13 @@ import { ApFile, ServerContext } from '@activepieces/pieces-framework'; | ||
moderation?: AIModeration; | ||
function?: AIFunction; | ||
}; | ||
export type AIFunction = { | ||
call?: (params: AIChatCompletionsCreateParams & { | ||
functions: AIFunctionDefinition[]; | ||
} & { | ||
image: ApFile; | ||
}) => Promise<AIChatCompletion & { | ||
call: AIFunctionCall | null; | ||
}>; | ||
}; | ||
export type AIModeration = { | ||
@@ -21,9 +31,2 @@ create: (params: AIModerationCreateParams) => Promise<any | null>; | ||
generate: (params: AIImageGenerateParams) => Promise<AIImageCompletion | null>; | ||
function?: (params: AIChatCompletionsCreateParams & { | ||
functions: AIFunctionDefinition[]; | ||
} & { | ||
image: ApFile; | ||
}) => Promise<AIChatCompletion & { | ||
call: AIFunctionCall | null; | ||
}>; | ||
}; | ||
@@ -41,7 +44,2 @@ export type AIImageGenerateParams = { | ||
text: (params: AIChatCompletionsCreateParams) => Promise<AIChatCompletion>; | ||
function?: (params: AIChatCompletionsCreateParams & { | ||
functions: AIFunctionDefinition[]; | ||
}) => Promise<AIChatCompletion & { | ||
call: AIFunctionCall | null; | ||
}>; | ||
}; | ||
@@ -78,9 +76,9 @@ export type AIChatCompletionsCreateParams = { | ||
description: string; | ||
arguments: AIFunctionArgumentDefinition[]; | ||
arguments: AIFunctionArgumentDefinition; | ||
}; | ||
export type AIFunctionArgumentDefinition = { | ||
name: string; | ||
type: 'string' | 'number' | 'boolean'; | ||
description?: string; | ||
isRequired: boolean; | ||
type: 'object'; | ||
properties?: unknown | null; | ||
required?: string[]; | ||
[k: string]: unknown; | ||
}; | ||
@@ -87,0 +85,0 @@ export declare enum AIChatRole { |
@@ -20,3 +20,2 @@ "use strict"; | ||
} | ||
const functionCalling = impl.chat.function; | ||
return { | ||
@@ -26,2 +25,3 @@ provider, | ||
moderation: impl.moderation, | ||
function: impl.function, | ||
chat: { | ||
@@ -40,18 +40,3 @@ text: (params) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { | ||
} | ||
}), | ||
function: functionCalling | ||
? (params) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { | ||
var _a; | ||
try { | ||
const response = yield functionCalling(params); | ||
return response; | ||
} | ||
catch (e) { | ||
if ((_a = e === null || e === void 0 ? void 0 : e.error) === null || _a === void 0 ? void 0 : _a.error) { | ||
throw e.error.error; | ||
} | ||
throw e; | ||
} | ||
}) | ||
: undefined, | ||
}) | ||
}, | ||
@@ -58,0 +43,0 @@ }; |
@@ -18,45 +18,30 @@ "use strict"; | ||
provider: 'ANTHROPIC', | ||
chat: { | ||
text: (params) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { | ||
var _a, _b; | ||
const concatenatedSystemMessage = params.messages | ||
.filter((message) => message.role === 'system') | ||
.map((message) => message.content) | ||
.join('\n'); | ||
const completion = yield sdk.messages.create({ | ||
model: params.model, | ||
messages: params.messages.map((message) => ({ | ||
role: message.role === 'user' ? 'user' : 'assistant', | ||
content: message.content, | ||
})), | ||
temperature: Math.tanh((_a = params.creativity) !== null && _a !== void 0 ? _a : 100), | ||
stop_sequences: params.stop, | ||
system: concatenatedSystemMessage, | ||
stream: false, | ||
max_tokens: (_b = params.maxTokens) !== null && _b !== void 0 ? _b : 2000, | ||
}); | ||
return { | ||
choices: completion.content | ||
.filter((choice) => choice.type === 'text') | ||
.map((choice) => ({ | ||
content: choice.text, | ||
role: __1.AIChatRole.ASSISTANT, | ||
})), | ||
created: new Date().getTime(), | ||
model: completion.model, | ||
usage: { | ||
completionTokens: completion.usage.output_tokens, | ||
promptTokens: completion.usage.input_tokens, | ||
totalTokens: completion.usage.output_tokens + completion.usage.input_tokens, | ||
}, | ||
}; | ||
}), | ||
function: (params) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { | ||
function: { | ||
call: (params) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { | ||
var _a; | ||
const messages = params.messages.map((message) => ({ | ||
role: 'user', | ||
content: [ | ||
{ type: 'text', text: message.content }, | ||
], | ||
})); | ||
if (params.image) { | ||
messages.push({ | ||
role: 'user', | ||
content: [ | ||
{ | ||
type: 'image', | ||
source: { | ||
type: 'base64', | ||
media_type: (params.image.extension && | ||
mime_types_1.default.lookup(params.image.extension)) || 'image/jpeg', | ||
data: params.image.base64, | ||
}, | ||
}, | ||
], | ||
}); | ||
} | ||
const completion = yield sdk.messages.create({ | ||
model: params.model, | ||
messages: params.messages.map((message) => ({ | ||
role: message.role === 'user' ? 'user' : 'assistant', | ||
content: message.content, | ||
})), | ||
messages: messages, | ||
max_tokens: (_a = params.maxTokens) !== null && _a !== void 0 ? _a : 2000, | ||
@@ -66,16 +51,7 @@ tools: params.functions.map((functionDefinition) => ({ | ||
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), | ||
}, | ||
input_schema: functionDefinition.arguments, | ||
})), | ||
}); | ||
const toolCallsResponse = completion.content.filter((choice) => choice.type === 'tool_use'); | ||
const toolCall = toolCallsResponse === null || toolCallsResponse === void 0 ? void 0 : toolCallsResponse[0]; | ||
const toolCall = toolCallsResponse[0]; | ||
return { | ||
@@ -107,43 +83,21 @@ choices: completion.content | ||
}, | ||
image: { | ||
function: (params) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { | ||
var _a; | ||
chat: { | ||
text: (params) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { | ||
var _a, _b; | ||
const concatenatedSystemMessage = params.messages | ||
.filter((message) => message.role === 'system') | ||
.map((message) => message.content) | ||
.join('\n'); | ||
const completion = yield sdk.messages.create({ | ||
model: params.model, | ||
messages: params.messages.map((message) => ({ | ||
role: 'user', | ||
content: [ | ||
{ | ||
type: 'image', | ||
source: { | ||
type: 'base64', | ||
media_type: (params.image.extension && | ||
mime_types_1.default.lookup(params.image.extension)) || 'image/jpeg', | ||
data: params.image.base64, | ||
}, | ||
}, | ||
{ | ||
type: 'text', | ||
text: message.content, | ||
}, | ||
], | ||
role: message.role === 'user' ? 'user' : 'assistant', | ||
content: message.content, | ||
})), | ||
max_tokens: (_a = params.maxTokens) !== null && _a !== void 0 ? _a : 2000, | ||
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), | ||
}, | ||
})), | ||
temperature: Math.tanh((_a = params.creativity) !== null && _a !== void 0 ? _a : 100), | ||
stop_sequences: params.stop, | ||
system: concatenatedSystemMessage, | ||
stream: false, | ||
max_tokens: (_b = params.maxTokens) !== null && _b !== void 0 ? _b : 2000, | ||
}); | ||
const toolCallsResponse = completion.content.filter((choice) => choice.type === 'tool_use'); | ||
const toolCall = toolCallsResponse[0]; | ||
return { | ||
@@ -156,13 +110,4 @@ choices: completion.content | ||
})), | ||
call: toolCall | ||
? { | ||
id: toolCall.id, | ||
function: { | ||
name: toolCall.name, | ||
arguments: toolCall.input, | ||
}, | ||
} | ||
: null, | ||
created: new Date().getTime(), | ||
model: completion.model, | ||
created: new Date().getTime(), | ||
usage: { | ||
@@ -174,3 +119,5 @@ completionTokens: completion.usage.output_tokens, | ||
}; | ||
}), | ||
}) | ||
}, | ||
image: { | ||
generate: (parmas) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { return null; }), | ||
@@ -177,0 +124,0 @@ }, |
@@ -18,25 +18,22 @@ "use strict"; | ||
provider: 'OPENAI', | ||
image: { | ||
generate: (params) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { | ||
const mapper = findImageMapper(params.model); | ||
const input = yield mapper.encodeInput(params); | ||
const response = yield sdk.images.generate(input); | ||
return mapper.decodeOutput(response); | ||
}), | ||
function: (params) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { | ||
function: { | ||
call: (params) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { | ||
var _a; | ||
const completion = yield sdk.chat.completions.create({ | ||
model: params.model, | ||
messages: params.messages.map((message) => ({ | ||
const messages = params.messages.map((message) => ({ | ||
role: 'user', | ||
content: [ | ||
{ type: 'text', text: message.content }, | ||
], | ||
})); | ||
if (params.image) { | ||
messages.push({ | ||
role: 'user', | ||
content: [ | ||
{ type: 'text', text: message.content }, | ||
{ | ||
type: 'image_url', | ||
image_url: { | ||
url: `data:image/${params.image.extension};base64,${params.image.base64}`, | ||
}, | ||
}, | ||
{ type: 'image_url', image_url: { url: `data:image/${params.image.extension};base64,${params.image.base64}` } }, | ||
], | ||
})), | ||
}); | ||
} | ||
const completion = yield sdk.chat.completions.create({ | ||
model: params.model, | ||
messages: messages, | ||
max_tokens: params.maxTokens, | ||
@@ -48,12 +45,3 @@ tools: params.functions.map((functionDefinition) => ({ | ||
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), | ||
}, | ||
parameters: functionDefinition.arguments, | ||
}, | ||
@@ -90,2 +78,10 @@ })), | ||
}, | ||
image: { | ||
generate: (params) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { | ||
const mapper = findImageMapper(params.model); | ||
const input = yield mapper.encodeInput(params); | ||
const response = yield sdk.images.generate(input); | ||
return mapper.decodeOutput(response); | ||
}), | ||
}, | ||
chat: { | ||
@@ -120,57 +116,3 @@ text: (params) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { | ||
}; | ||
}), | ||
function: (params) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { | ||
var _a; | ||
const completion = yield sdk.chat.completions.create({ | ||
model: params.model, | ||
messages: params.messages.map((message) => ({ | ||
role: message.role === 'user' ? 'user' : 'assistant', | ||
content: message.content, | ||
})), | ||
max_tokens: params.maxTokens, | ||
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 = (_a = completion.choices[0].message.tool_calls) === null || _a === void 0 ? void 0 : _a[0]; | ||
return { | ||
choices: completion.choices.map((choice) => { | ||
var _a; | ||
return ({ | ||
role: __1.AIChatRole.ASSISTANT, | ||
content: (_a = choice.message.content) !== null && _a !== void 0 ? _a : '', | ||
}); | ||
}), | ||
call: toolCall | ||
? { | ||
id: toolCall.id, | ||
function: { | ||
name: toolCall.function.name, | ||
arguments: JSON.parse(toolCall.function.arguments), | ||
}, | ||
} | ||
: null, | ||
created: completion.created, | ||
model: completion.model, | ||
usage: completion.usage && { | ||
completionTokens: completion.usage.completion_tokens, | ||
promptTokens: completion.usage.prompt_tokens, | ||
totalTokens: completion.usage.total_tokens, | ||
}, | ||
}; | ||
}), | ||
}) | ||
}, | ||
@@ -177,0 +119,0 @@ moderation: { |
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
117186
1754
+ Added@activepieces/pieces-framework@0.7.39(transitive)
+ Added@activepieces/shared@0.10.123(transitive)
+ Added@sinclair/typebox@0.33.18(transitive)
- Removed@activepieces/pieces-framework@0.7.37(transitive)
- Removed@activepieces/shared@0.10.1190.10.121(transitive)
- Removed@sinclair/typebox@0.32.35(transitive)
Updated@sinclair/typebox@0.33.18