-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add anthropic as a provider This depends on inngest/inngest-js#764, which exposes model options in Inngest's model provider. This makes types happy. * Add lint CI * Fix lint CI ignored files * Fix linting, switch compile error * Update package.json * Update lockfile --------- Co-authored-by: Jack Williams <[email protected]> Co-authored-by: Jack Williams <[email protected]>
- Loading branch information
1 parent
f9dd9f6
commit 485afd1
Showing
10 changed files
with
196 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/** | ||
* Adapters for Anthropic I/O to transform to/from internal network messages. | ||
* | ||
* @module | ||
*/ | ||
|
||
import { | ||
type AnthropicAiAdapter, | ||
type AiAdapter, | ||
type Anthropic, | ||
} from "inngest"; | ||
import { zodToJsonSchema } from "openai-zod-to-json-schema"; | ||
import { type AgenticModel } from "../model"; | ||
import { type InternalNetworkMessage } from "../state"; | ||
|
||
/** | ||
* Parse a request from internal network messages to an Anthropic input. | ||
*/ | ||
export const requestParser: AgenticModel.RequestParser<Anthropic.AiModel> = ( | ||
model, | ||
messages, | ||
tools, | ||
) => { | ||
// Note that Anthropic has a top-level system prompt, then a series of prompts | ||
// for assistants and users. | ||
const systemMessage = messages.find((m) => m.role === "system"); | ||
const system = | ||
typeof systemMessage?.content === "string" ? systemMessage.content : ""; | ||
|
||
const request: AiAdapter.Input<Anthropic.AiModel> = { | ||
system, | ||
model: model.options.model, | ||
max_tokens: model.options.max_tokens, | ||
messages: messages | ||
.filter((m) => m.role !== "system") | ||
.map((m) => { | ||
return { | ||
role: m.role, | ||
content: m.content, | ||
}; | ||
}) as AiAdapter.Input<Anthropic.AiModel>["messages"], | ||
}; | ||
|
||
if (tools?.length) { | ||
request.tools = tools.map((t) => { | ||
return { | ||
name: t.name, | ||
description: t.description, | ||
input_schema: zodToJsonSchema( | ||
t.parameters, | ||
) as AnthropicAiAdapter.Tool.InputSchema, | ||
}; | ||
}); | ||
} | ||
|
||
return request; | ||
}; | ||
|
||
/** | ||
* Parse a response from Anthropic output to internal network messages. | ||
*/ | ||
export const responseParser: AgenticModel.ResponseParser<Anthropic.AiModel> = ( | ||
input, | ||
) => { | ||
return (input?.content ?? []).reduce<InternalNetworkMessage[]>( | ||
(acc, item) => { | ||
if (!item.type) { | ||
return acc; | ||
} | ||
|
||
switch (item.type) { | ||
case "text": | ||
return [ | ||
...acc, | ||
{ | ||
role: input.role, | ||
content: item.text, | ||
}, | ||
]; | ||
case "tool_use": { | ||
let args; | ||
try { | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment | ||
args = | ||
typeof item.input === "string" | ||
? JSON.parse(item.input) | ||
: item.input; | ||
} catch { | ||
args = item.input; | ||
} | ||
|
||
return [ | ||
...acc, | ||
{ | ||
role: input.role, | ||
content: "", | ||
tools: [ | ||
{ | ||
type: "tool", | ||
id: item.id, | ||
name: item.name, | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment | ||
input: args, | ||
}, | ||
], | ||
}, | ||
]; | ||
} | ||
} | ||
|
||
return acc; | ||
}, | ||
[], | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { | ||
anthropic as ianthropic, | ||
type GetStepTools, | ||
type Inngest, | ||
type Anthropic, | ||
} from "inngest"; | ||
import { requestParser, responseParser } from "../adapters/anthropic"; | ||
import { AgenticModel } from "../model"; | ||
|
||
export namespace AnthropicModel { | ||
export interface Options<TAiAdapter extends Anthropic.AiModel> | ||
extends Omit<Anthropic.AiModelOptions, "model"> { | ||
/** | ||
* The Anthropic model to use. | ||
*/ | ||
model: Anthropic.AiModelOptions["model"] | TAiAdapter; | ||
|
||
/** | ||
* The step tools to use internally within this model. | ||
*/ | ||
step: GetStepTools<Inngest.Any>; | ||
} | ||
} | ||
|
||
/** | ||
* Create an agentic Anthropic model using the Anthropic chat format. | ||
*/ | ||
export const anthropic = <TAiAdapter extends Anthropic.AiModel>({ | ||
step, | ||
...modelOptions | ||
}: AnthropicModel.Options<TAiAdapter>) => { | ||
const model = | ||
typeof modelOptions.model === "string" | ||
? ianthropic({ ...modelOptions, model: modelOptions.model }) | ||
: modelOptions.model; | ||
|
||
return new AgenticModel({ | ||
model, | ||
step, | ||
requestParser, | ||
responseParser, | ||
}); | ||
}; |
Oops, something went wrong.