Skip to content

Commit

Permalink
Merge pull request activepieces#6353 from kishanprmr/claude
Browse files Browse the repository at this point in the history
feat(utility-ai): add PDF and multiple files support
  • Loading branch information
abuaboud authored Feb 5, 2025
2 parents fbe6e2f + d2b6981 commit 611a295
Show file tree
Hide file tree
Showing 6 changed files with 647 additions and 595 deletions.
2 changes: 1 addition & 1 deletion packages/pieces/community/common/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@activepieces/pieces-common",
"version": "0.3.3",
"version": "0.4.0",
"type": "commonjs"
}
172 changes: 80 additions & 92 deletions packages/pieces/community/common/src/lib/ai/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,144 +2,132 @@ import { ApFile, ServerContext } from '@activepieces/pieces-framework';
import { AI_PROVIDERS, AiProvider } from './providers';

export type AI = {
provider: string;
chat: AIChat;
image?: AIImage;
moderation?: AIModeration;
function?: AIFunction;
provider: string;
chat: AIChat;
image?: AIImage;
moderation?: AIModeration;
function?: AIFunction;
};

export type AIFunction = {
call?: (
params: AIChatCompletionsCreateParams & {
functions: AIFunctionDefinition[];
} & { image: ApFile }
) => Promise<AIChatCompletion & { call: AIFunctionCall | null }>;
call?: (
params: AIChatCompletionsCreateParams & {
functions: AIFunctionDefinition[];
} & { files: ApFile[] },
) => Promise<AIChatCompletion & { call: AIFunctionCall | null }>;
};

export type AIModeration = {
create: (params: AIModerationCreateParams) => Promise<any | null>;
create: (params: AIModerationCreateParams) => Promise<any | null>;
};

export type AIModerationCreateParams = {
model: string;
text?: string;
images?: ApFile[];
maxTokens?: number;
model: string;
text?: string;
images?: ApFile[];
maxTokens?: number;
};

export type AIImage = {
generate: (
params: AIImageGenerateParams
) => Promise<AIImageCompletion | null>;
generate: (params: AIImageGenerateParams) => Promise<AIImageCompletion | null>;
};

export type AIImageGenerateParams = {
prompt: string;
model: string;
size?: string;
advancedOptions?: Record<string, unknown>;
prompt: string;
model: string;
size?: string;
advancedOptions?: Record<string, unknown>;
};

export type AIImageCompletion = {
image: string;
image: string;
};

export type AIChat = {
text: (params: AIChatCompletionsCreateParams) => Promise<AIChatCompletion>;
text: (params: AIChatCompletionsCreateParams) => Promise<AIChatCompletion>;
};

export type AIChatCompletionsCreateParams = {
model: string;
messages: AIChatMessage[];
creativity?: number;
maxTokens?: number;
stop?: string[];
model: string;
messages: AIChatMessage[];
creativity?: number;
maxTokens?: number;
stop?: string[];
};

export type AIChatCompletion = {
choices: AIChatMessage[];
usage?: AIChatCompletionUsage;
choices: AIChatMessage[];
usage?: AIChatCompletionUsage;
};

export type AIChatCompletionUsage = {
promptTokens: number;
completionTokens: number;
totalTokens: number;
promptTokens: number;
completionTokens: number;
totalTokens: number;
};

export type AIChatMessage = {
role: AIChatRole;
content: string;
role: AIChatRole;
content: string;
};

export type AIFunctionCall = {
id: string;
function: {
name: string;
arguments: unknown;
};
id: string;
function: {
name: string;
arguments: unknown;
};
};

export type AIFunctionDefinition = {
name: string;
description: string;
arguments: AIFunctionArgumentDefinition;
name: string;
description: string;
arguments: AIFunctionArgumentDefinition;
};

export type AIFunctionArgumentDefinition = {
type: 'object';
properties?: unknown | null;
required?: string[];
[k: string]: unknown;
}
type: 'object';
properties?: unknown | null;
required?: string[];
[k: string]: unknown;
};

export enum AIChatRole {
SYSTEM = 'system',
USER = 'user',
ASSISTANT = 'assistant',
SYSTEM = 'system',
USER = 'user',
ASSISTANT = 'assistant',
}

export type AIFactory = (params: {
proxyUrl: string;
engineToken: string;
}) => AI;

export const AI = ({
provider,
server,
}: {
provider: AiProvider;
server: ServerContext;
}): AI => {
const proxyUrl = `${server.apiUrl}v1/ai-providers/proxy/${provider}`;
const factory = AI_PROVIDERS.find((p) => p.value === provider)?.factory;
const impl = factory?.({ proxyUrl, engineToken: server.token });

if (!impl) {
throw new Error(`AI provider ${provider} is not registered`);
}


return {
provider,
image: impl.image,
moderation: impl.moderation,
function: impl.function,
chat: {
text: async (params) => {
try {
const response = await impl.chat.text(params);
return response;
} catch (e: any) {
if (e?.error?.error) {
throw e.error.error;
}
throw e;
}
}
},
};
export type AIFactory = (params: { proxyUrl: string; engineToken: string }) => AI;

export const AI = ({ provider, server }: { provider: AiProvider; server: ServerContext }): AI => {
const proxyUrl = `${server.apiUrl}v1/ai-providers/proxy/${provider}`;
const factory = AI_PROVIDERS.find((p) => p.value === provider)?.factory;
const impl = factory?.({ proxyUrl, engineToken: server.token });

if (!impl) {
throw new Error(`AI provider ${provider} is not registered`);
}

return {
provider,
image: impl.image,
moderation: impl.moderation,
function: impl.function,
chat: {
text: async (params) => {
try {
const response = await impl.chat.text(params);
return response;
} catch (e: any) {
if (e?.error?.error) {
throw e.error.error;
}
throw e;
}
},
},
};
};

export * from './providers';
Loading

0 comments on commit 611a295

Please sign in to comment.