📅 You're Invited: Meet the Socket team at RSAC (April 28 – May 1).RSVP →

genkitx-anthropic

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

genkitx-anthropic - npm Package Compare versions

Comparing version

to
0.10.1

@@ -1,5 +0,6 @@

import { ModelReference, Part, MessageData, ToolDefinition, GenerateResponseData, GenerateRequest, ModelAction } from '@genkit-ai/ai/model';
import { ModelReference, Role, Part, MessageData, ToolDefinition, CandidateData, GenerateResponseData, GenerateRequest, GenerateResponseChunkData, ModelAction } from '@genkit-ai/ai/model';
import Anthropic from '@anthropic-ai/sdk';
import z from 'zod';
import { TextBlockParam, ImageBlockParam, TextBlock, ToolUseBlockParam, ToolResultBlockParam, MessageParam, Tool, Message, MessageCreateParams } from '@anthropic-ai/sdk/resources/messages.mjs';
import { MessageParam, TextBlockParam, ImageBlockParam, TextBlock, ToolUseBlockParam, ToolResultBlockParam, Tool, MessageStreamEvent, Message, MessageCreateParams } from '@anthropic-ai/sdk/resources/messages.mjs';
import { StreamingCallback } from '@genkit-ai/core';

@@ -96,2 +97,76 @@ /**

}>;
declare const claude35Sonnet: ModelReference<z.ZodObject<z.objectUtil.extendShape<{
version: z.ZodOptional<z.ZodString>;
temperature: z.ZodOptional<z.ZodNumber>;
maxOutputTokens: z.ZodOptional<z.ZodNumber>;
topK: z.ZodOptional<z.ZodNumber>;
topP: z.ZodOptional<z.ZodNumber>;
stopSequences: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
}, {
tool_choice: z.ZodOptional<z.ZodUnion<[z.ZodObject<{
type: z.ZodLiteral<"auto">;
}, "strip", z.ZodTypeAny, {
type: "auto";
}, {
type: "auto";
}>, z.ZodObject<{
type: z.ZodLiteral<"any">;
}, "strip", z.ZodTypeAny, {
type: "any";
}, {
type: "any";
}>, z.ZodObject<{
type: z.ZodLiteral<"tool">;
name: z.ZodString;
}, "strip", z.ZodTypeAny, {
type: "tool";
name: string;
}, {
type: "tool";
name: string;
}>]>>;
metadata: z.ZodOptional<z.ZodObject<{
user_id: z.ZodOptional<z.ZodString>;
}, "strip", z.ZodTypeAny, {
user_id?: string | undefined;
}, {
user_id?: string | undefined;
}>>;
}>, "strip", z.ZodTypeAny, {
tool_choice?: {
type: "auto";
} | {
type: "any";
} | {
type: "tool";
name: string;
} | undefined;
metadata?: {
user_id?: string | undefined;
} | undefined;
version?: string | undefined;
temperature?: number | undefined;
maxOutputTokens?: number | undefined;
topK?: number | undefined;
topP?: number | undefined;
stopSequences?: string[] | undefined;
}, {
tool_choice?: {
type: "auto";
} | {
type: "any";
} | {
type: "tool";
name: string;
} | undefined;
metadata?: {
user_id?: string | undefined;
} | undefined;
version?: string | undefined;
temperature?: number | undefined;
maxOutputTokens?: number | undefined;
topK?: number | undefined;
topP?: number | undefined;
stopSequences?: string[] | undefined;
}>>;
declare const claude3Opus: ModelReference<z.ZodObject<z.objectUtil.extendShape<{

@@ -321,2 +396,10 @@ version: z.ZodOptional<z.ZodString>;

/**
* Converts a Genkit role to the corresponding Anthropic role.
* @param role The Genkit role to convert.
* @param toolMessageType In case the message refers to the usage of a tool, the type of tool message (tool_use or tool_result).
* @returns The corresponding Anthropic role.
* @throws Error if the role doesn't map to an Anthropic role.
*/
declare function toAnthropicRole(role: Role, toolMessageType?: 'tool_use' | 'tool_result'): MessageParam['role'];
/**
* Converts a Genkit message Part to the corresponding Anthropic TextBlockParam or ImageBlockParam.

@@ -350,2 +433,10 @@ * @param part The Genkit Part to convert.

declare function toAnthropicTool(tool: ToolDefinition): Tool;
/**
* Converts an Anthropic message stream event to a Genkit Part object.
* @param event The Anthropic message stream event to convert.
* @returns The converted Genkit Part object if the event is a content block
* start or delta, otherwise undefined.
*/
declare function fromAnthropicContentBlockChunk(event: MessageStreamEvent): Part | undefined;
declare function fromAnthropicStopReason(reason: Message['stop_reason']): CandidateData['finishReason'];
declare function fromAnthropicResponse(response: Message): GenerateResponseData;

@@ -362,2 +453,9 @@ /**

/**
* Creates the runner used by Genkit to interact with the Claude model.
* @param name The name of the Claude model.
* @param client The Anthropic client instance.
* @returns The runner that Genkit will call when the model is invoked.
*/
declare function claudeRunner(name: string, client: Anthropic): (request: GenerateRequest<typeof AnthropicConfigSchema>, streamingCallback?: StreamingCallback<GenerateResponseChunkData>) => Promise<GenerateResponseData>;
/**
* Defines a Claude model with the given name and Anthropic client.

@@ -371,2 +469,2 @@ * @param name The name of the Claude model.

export { AnthropicConfigSchema, SUPPORTED_CLAUDE_MODELS, claude3Haiku, claude3Opus, claude3Sonnet, claudeModel, fromAnthropicResponse, toAnthropicMessageContent, toAnthropicMessages, toAnthropicRequestBody, toAnthropicTool, toAnthropicToolResponseContent };
export { AnthropicConfigSchema, SUPPORTED_CLAUDE_MODELS, claude35Sonnet, claude3Haiku, claude3Opus, claude3Sonnet, claudeModel, claudeRunner, fromAnthropicContentBlockChunk, fromAnthropicResponse, fromAnthropicStopReason, toAnthropicMessageContent, toAnthropicMessages, toAnthropicRequestBody, toAnthropicRole, toAnthropicTool, toAnthropicToolResponseContent };

@@ -74,2 +74,3 @@ "use strict";

SUPPORTED_CLAUDE_MODELS: () => SUPPORTED_CLAUDE_MODELS,
claude35Sonnet: () => claude35Sonnet,
claude3Haiku: () => claude3Haiku,

@@ -79,6 +80,10 @@ claude3Opus: () => claude3Opus,

claudeModel: () => claudeModel,
claudeRunner: () => claudeRunner,
fromAnthropicContentBlockChunk: () => fromAnthropicContentBlockChunk,
fromAnthropicResponse: () => fromAnthropicResponse,
fromAnthropicStopReason: () => fromAnthropicStopReason,
toAnthropicMessageContent: () => toAnthropicMessageContent,
toAnthropicMessages: () => toAnthropicMessages,
toAnthropicRequestBody: () => toAnthropicRequestBody,
toAnthropicRole: () => toAnthropicRole,
toAnthropicTool: () => toAnthropicTool,

@@ -108,2 +113,18 @@ toAnthropicToolResponseContent: () => toAnthropicToolResponseContent

});
const claude35Sonnet = (0, import_model.modelRef)({
name: "anthropic/claude-3-5-sonnet",
info: {
versions: ["claude-3-5-sonnet-20240620"],
label: "Anthropic - Claude 3.5 Sonnet",
supports: {
multiturn: true,
tools: true,
media: true,
systemRole: true,
output: ["text"]
}
},
configSchema: AnthropicConfigSchema,
version: "claude-3-5-sonnet-20240620"
});
const claude3Opus = (0, import_model.modelRef)({

@@ -158,2 +179,3 @@ name: "anthropic/claude-3-opus",

const SUPPORTED_CLAUDE_MODELS = {
"claude-3-5-sonnet": claude35Sonnet,
"claude-3-opus": claude3Opus,

@@ -215,3 +237,3 @@ "claude-3-sonnet": claude3Sonnet,

function toAnthropicMessageContent(part) {
var _a;
var _a, _b;
if (part.text) {

@@ -224,6 +246,6 @@ return {

if (part.media) {
const { data, contentType } = extractDataFromBase64Url(part.media.url) || {};
const { data, contentType } = (_a = extractDataFromBase64Url(part.media.url)) != null ? _a : {};
if (!data) {
throw Error(
`Invalid genkit part media provided to toAnthropicMessageContent: ${part.media}.`
`Invalid genkit part media provided to toAnthropicMessageContent: ${JSON.stringify(part.media)}.`
);

@@ -237,3 +259,3 @@ }

// @ts-expect-error TODO: improve these types
media_type: (_a = part.media.contentType) != null ? _a : contentType
media_type: (_b = part.media.contentType) != null ? _b : contentType
}

@@ -258,3 +280,3 @@ };

throw Error(
`Unsupported genkit part fields encountered for current message role: ${part}.`
`Unsupported genkit part fields encountered for current message role: ${JSON.stringify(part)}.`
);

@@ -288,8 +310,2 @@ }

}
const finishReasonMap = {
end_turn: "stop",
max_tokens: "length",
stop_sequence: "stop",
tool_use: "stop"
};
function fromAnthropicContentBlock(contentBlock) {

@@ -309,3 +325,3 @@ return contentBlock.type === "tool_use" ? {

const eventField = event.type === "content_block_start" ? "content_block" : "delta";
return event[eventField].type === "text" ? {
return ["text", "text_delta"].includes(event[eventField].type) ? {
text: event[eventField].text

@@ -354,3 +370,3 @@ } : {

function toAnthropicRequestBody(modelName, request, stream) {
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
const model = SUPPORTED_CLAUDE_MODELS[modelName];

@@ -360,18 +376,18 @@ if (!model)

const { system, messages } = toAnthropicMessages(request.messages);
const mappedModelName = ((_a = request.config) == null ? void 0 : _a.version) || model.version || modelName;
const mappedModelName = (_c = (_b = (_a = request.config) == null ? void 0 : _a.version) != null ? _b : model.version) != null ? _c : modelName;
const body = {
system,
messages,
tools: (_b = request.tools) == null ? void 0 : _b.map(toAnthropicTool),
max_tokens: (_d = (_c = request.config) == null ? void 0 : _c.maxOutputTokens) != null ? _d : 4096,
tools: (_d = request.tools) == null ? void 0 : _d.map(toAnthropicTool),
max_tokens: (_f = (_e = request.config) == null ? void 0 : _e.maxOutputTokens) != null ? _f : 4096,
model: mappedModelName,
top_k: (_e = request.config) == null ? void 0 : _e.topK,
top_p: (_f = request.config) == null ? void 0 : _f.topP,
temperature: (_g = request.config) == null ? void 0 : _g.temperature,
stop_sequences: (_h = request.config) == null ? void 0 : _h.stopSequences,
metadata: (_i = request.config) == null ? void 0 : _i.metadata,
tool_choice: (_j = request.config) == null ? void 0 : _j.tool_choice,
top_k: (_g = request.config) == null ? void 0 : _g.topK,
top_p: (_h = request.config) == null ? void 0 : _h.topP,
temperature: (_i = request.config) == null ? void 0 : _i.temperature,
stop_sequences: (_j = request.config) == null ? void 0 : _j.stopSequences,
metadata: (_k = request.config) == null ? void 0 : _k.metadata,
tool_choice: (_l = request.config) == null ? void 0 : _l.tool_choice,
stream
};
if (((_k = request.output) == null ? void 0 : _k.format) && request.output.format !== "text") {
if (((_m = request.output) == null ? void 0 : _m.format) && request.output.format !== "text") {
throw new Error(

@@ -387,2 +403,36 @@ `Only text output format is supported for Claude models currently`

}
function claudeRunner(name, client) {
return (request, streamingCallback) => __async(this, null, function* () {
let response;
const body = toAnthropicRequestBody(name, request, !!streamingCallback);
if (streamingCallback) {
const stream = client.messages.stream(body);
try {
for (var iter = __forAwait(stream), more, temp, error; more = !(temp = yield iter.next()).done; more = false) {
const chunk = temp.value;
const c = fromAnthropicContentBlockChunk(chunk);
if (c) {
streamingCallback({
index: 0,
content: [c]
});
}
}
} catch (temp) {
error = [temp];
} finally {
try {
more && (temp = iter.return) && (yield temp.call(iter));
} finally {
if (error)
throw error[0];
}
}
response = yield stream.finalMessage();
} else {
response = yield client.messages.create(body);
}
return fromAnthropicResponse(response);
});
}
function claudeModel(name, client) {

@@ -399,34 +449,3 @@ const modelId = `anthropic/${name}`;

}),
(request, streamingCallback) => __async(this, null, function* () {
let response;
const body = toAnthropicRequestBody(name, request, !!streamingCallback);
if (streamingCallback) {
const stream = client.messages.stream(body);
try {
for (var iter = __forAwait(stream), more, temp, error; more = !(temp = yield iter.next()).done; more = false) {
const chunk = temp.value;
const c = fromAnthropicContentBlockChunk(chunk);
if (c) {
streamingCallback({
index: 0,
content: [c]
});
}
}
} catch (temp) {
error = [temp];
} finally {
try {
more && (temp = iter.return) && (yield temp.call(iter));
} finally {
if (error)
throw error[0];
}
}
response = yield stream.finalMessage();
} else {
response = yield client.messages.create(body);
}
return fromAnthropicResponse(response);
})
claudeRunner(name, client)
);

@@ -438,2 +457,3 @@ }

SUPPORTED_CLAUDE_MODELS,
claude35Sonnet,
claude3Haiku,

@@ -443,6 +463,10 @@ claude3Opus,

claudeModel,
claudeRunner,
fromAnthropicContentBlockChunk,
fromAnthropicResponse,
fromAnthropicStopReason,
toAnthropicMessageContent,
toAnthropicMessages,
toAnthropicRequestBody,
toAnthropicRole,
toAnthropicTool,

@@ -449,0 +473,0 @@ toAnthropicToolResponseContent

import { Plugin } from '@genkit-ai/core';
export { claude3Haiku, claude3Opus, claude3Sonnet } from './claude.js';
export { claude35Sonnet, claude3Haiku, claude3Opus, claude3Sonnet } from './claude.js';
import '@genkit-ai/ai/model';

@@ -35,2 +35,3 @@ import '@anthropic-ai/sdk';

* Exports:
* - claude35Sonnet: Reference to the Claude 3.5 Sonnet model.
* - claude3Haiku: Reference to the Claude 3 Haiku model.

@@ -37,0 +38,0 @@ * - claude3Sonnet: Reference to the Claude 3 Sonnet model.

@@ -52,2 +52,3 @@ "use strict";

anthropic: () => anthropic,
claude35Sonnet: () => import_claude.claude35Sonnet,
claude3Haiku: () => import_claude.claude3Haiku,

@@ -84,2 +85,3 @@ claude3Opus: () => import_claude.claude3Opus,

anthropic,
claude35Sonnet,
claude3Haiku,

@@ -86,0 +88,0 @@ claude3Opus,

@@ -18,3 +18,3 @@ {

],
"version": "0.10.0",
"version": "0.10.1",
"type": "commonjs",

@@ -29,3 +29,3 @@ "repository": {

"dependencies": {
"@anthropic-ai/sdk": "^0.22.0",
"@anthropic-ai/sdk": "^0.24.0",
"zod": "^3.23.8"

@@ -38,7 +38,9 @@ },

"devDependencies": {
"@jest/globals": "^29.7.0",
"@types/node": "^20.12.12",
"jest": "^29.7.0",
"npm-run-all": "^4.1.5",
"ts-jest": "^29.1.2",
"tsup": "^8.0.2",
"tsx": "^4.10.5",
"typescript": "^4.9.5"
"typescript": "^5.4.5"
},

@@ -67,5 +69,5 @@ "types": "./lib/index.d.ts",

"build:watch": "tsup-node --watch",
"test": "node --import tsx --test ./tests/*_test.ts"
"test": "jest --coverage"
},
"gitHead": "c3ae62e954d04065bc906fdce6d14513dd743411"
"gitHead": "d90543a476ca42dce912ac8b90b652ad97b3b4d8"
}

@@ -31,3 +31,3 @@ ![Firebase Genkit + Anthropic AI](https://github.com/TheFireCo/genkit-plugins/blob/main/assets/genkit-anthropic.png?raw=true)

The plugin supports the most recent Anthropic models:
**Claude 3 Opus**, **Claude 3 Sonnet**, and **Claude 3 Haiku**.
**Claude 3.5 Sonnet**, **Claude 3 Opus**, **Claude 3 Sonnet**, and **Claude 3 Haiku**.

@@ -34,0 +34,0 @@ ## Installation

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

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