Skip to content

Commit

Permalink
chore(deps)!: upgrade anthropic plugin to genkit RC
Browse files Browse the repository at this point in the history
  • Loading branch information
cabljac committed Oct 24, 2024
1 parent 45c9c54 commit 3b28262
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 136 deletions.
3 changes: 1 addition & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions plugins/anthropic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,13 @@
},
"author": "TheFireCo",
"license": "Apache-2.0",
"peerDependencies": {
"genkit": "^0.9.0-dev.1"
},
"dependencies": {
"@anthropic-ai/sdk": "^0.24.0",
"zod": "^3.23.8"
},
"peerDependencies": {
"@genkit-ai/ai": "^0.5.0",
"@genkit-ai/core": "^0.5.0"
},
"devDependencies": {
"@jest/globals": "^29.7.0",
"@types/node": "^20.12.12",
Expand Down
66 changes: 37 additions & 29 deletions plugins/anthropic/src/claude.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,27 @@
* limitations under the License.
*/

import { describe, it, expect } from '@jest/globals';
import {
type GenerateResponseData,
type Part,
type GenerateRequest,
type MessageData,
ToolDefinition,
CandidateData,
describe,
it,
expect,
beforeEach,
afterEach,
jest,
} from '@jest/globals';
import {
GenerateResponseData,
Part,
GenerateRequest,
MessageData,
Role,
} from '@genkit-ai/ai/model';
import * as GenkitAiModel from '@genkit-ai/ai/model';
Genkit,
} from 'genkit';
import {
type Message,
type MessageParam,
type MessageCreateParams,
type MessageStreamEvent,
Message,
MessageParam,
MessageCreateParams,
MessageStreamEvent,
} from '@anthropic-ai/sdk/resources/messages.mjs';
import {
AnthropicConfigSchema,
Expand All @@ -47,11 +52,7 @@ import {
toAnthropicToolResponseContent,
} from './claude';
import Anthropic from '@anthropic-ai/sdk';

jest.mock('@genkit-ai/ai/model', () => ({
...jest.requireActual('@genkit-ai/ai/model'),
defineModel: jest.fn(),
}));
import { CandidateData, ToolDefinition } from 'genkit/model';

describe('toAnthropicRole', () => {
const testCases: {
Expand Down Expand Up @@ -102,7 +103,7 @@ describe('toAnthropicRole', () => {

describe('toAnthropicToolResponseContent', () => {
it('should throw an error for unknown parts', () => {
const part: Part = { data: 'hi' };
const part: Part = { data: 'hi' } as Part;
expect(() => toAnthropicToolResponseContent(part)).toThrowError(
`Invalid genkit part provided to toAnthropicToolResponseContent: {"data":"hi"}`
);
Expand Down Expand Up @@ -397,6 +398,7 @@ describe('toAnthropicMessages', () => {
},
},
];

for (const test of testCases) {
it(test.should, () => {
const actualOutput = toAnthropicMessages(test.inputMessages);
Expand Down Expand Up @@ -795,7 +797,7 @@ describe('toAnthropicRequestBody', () => {
expect(() =>
toAnthropicRequestBody('fake-model', {
messages: [],
})
} as GenerateRequest<typeof AnthropicConfigSchema>)
).toThrowError('Unsupported model: fake-model');
});

Expand All @@ -805,7 +807,7 @@ describe('toAnthropicRequestBody', () => {
messages: [],
tools: [],
output: { format: 'media' },
})
} as GenerateRequest<typeof AnthropicConfigSchema>)
).toThrowError(
'Only text output format is supported for Claude models currently'
);
Expand Down Expand Up @@ -841,7 +843,7 @@ describe('claudeRunner', () => {
messages: {
stream: jest.fn(
() =>
// Simluate Anthropic SDK request streaming
// Simulate Anthropic SDK request streaming
new (class {
isFirstRequest = true;
[Symbol.asyncIterator]() {
Expand Down Expand Up @@ -892,16 +894,22 @@ describe('claudeRunner', () => {
});

describe('claudeModel', () => {
let ai: Genkit;

beforeEach(() => {
ai = {
defineModel: jest.fn(),
} as unknown as Genkit;
});

afterEach(() => {
jest.clearAllMocks();
});

it('should correctly define supported GPT models', () => {
jest
.spyOn(GenkitAiModel, 'defineModel')
.mockImplementation((() => ({})) as any);
claudeModel('claude-3-haiku', {} as Anthropic);
expect(GenkitAiModel.defineModel).toHaveBeenCalledWith(
it('should correctly define supported Claude models', () => {
jest.spyOn(ai, 'defineModel').mockImplementation((() => ({})) as any);
claudeModel(ai, 'claude-3-haiku', {} as Anthropic);
expect(ai.defineModel).toHaveBeenCalledWith(
{
name: claude3Haiku.name,
...claude3Haiku.info,
Expand All @@ -913,7 +921,7 @@ describe('claudeModel', () => {

it('should throw for unsupported models', () => {
expect(() =>
claudeModel('unsupported-model', {} as Anthropic)
claudeModel(ai, 'unsupported-model', {} as Anthropic)
).toThrowError('Unsupported model: unsupported-model');
});
});
Loading

0 comments on commit 3b28262

Please sign in to comment.