Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use @inngest/ai and optional step tooling #43

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@
}
},
"dependencies": {
"@inngest/ai": "^0.0.0",
"express": "^4.21.1",
"inngest": "^3.29.0",
"inngest": "3.29.4-pr-802.0",
"openai-zod-to-json-schema": "^1.0.3",
"zod": "^3.23.8"
},
Expand Down
44 changes: 35 additions & 9 deletions pnpm-lock.yaml

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

14 changes: 7 additions & 7 deletions src/adapters/anthropic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
type AiAdapter,
type Anthropic,
type AnthropicAiAdapter,
} from "inngest";
} from "@inngest/ai";
import { zodToJsonSchema } from "openai-zod-to-json-schema";
import { z } from "zod";
import { type AgenticModel } from "../model";
Expand All @@ -21,12 +21,12 @@ export const requestParser: AgenticModel.RequestParser<Anthropic.AiModel> = (
model,
messages,
tools,
tool_choice = "auto",
tool_choice = "auto"
) => {
// 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" && m.type === "text",
(m) => m.role === "system" && m.type === "text"
) as TextMessage;
const system =
typeof systemMessage?.content === "string" ? systemMessage.content : "";
Expand Down Expand Up @@ -79,7 +79,7 @@ export const requestParser: AgenticModel.RequestParser<Anthropic.AiModel> = (
];
}
},
[] as AiAdapter.Input<Anthropic.AiModel>["messages"],
[] as AiAdapter.Input<Anthropic.AiModel>["messages"]
);

const request: AiAdapter.Input<Anthropic.AiModel> = {
Expand All @@ -97,7 +97,7 @@ export const requestParser: AgenticModel.RequestParser<Anthropic.AiModel> = (
input_schema: (t.parameters
? zodToJsonSchema(t.parameters)
: zodToJsonSchema(
z.object({}),
z.object({})
)) as AnthropicAiAdapter.Tool.InputSchema,
};
});
Expand All @@ -111,7 +111,7 @@ export const requestParser: AgenticModel.RequestParser<Anthropic.AiModel> = (
* Parse a response from Anthropic output to internal network messages.
*/
export const responseParser: AgenticModel.ResponseParser<Anthropic.AiModel> = (
input,
input
) => {
return (input?.content ?? []).reduce<Message[]>((acc, item) => {
if (!item.type) {
Expand Down Expand Up @@ -165,7 +165,7 @@ export const responseParser: AgenticModel.ResponseParser<Anthropic.AiModel> = (
};

const toolChoice = (
choice: Tool.Choice,
choice: Tool.Choice
): AiAdapter.Input<Anthropic.AiModel>["tool_choice"] => {
switch (choice) {
case "auto":
Expand Down
2 changes: 1 addition & 1 deletion src/adapters/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type AiAdapter, type AiAdapters } from "inngest";
import { type AiAdapter, type AiAdapters } from "@inngest/ai";
import { type AgenticModel } from "../model";
import * as anthropic from "./anthropic";
import * as openai from "./openai";
Expand Down
10 changes: 5 additions & 5 deletions src/adapters/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @module
*/

import { type AiAdapter, type OpenAi } from "inngest";
import { type AiAdapter, type OpenAi } from "@inngest/ai";
import { zodToJsonSchema } from "openai-zod-to-json-schema";
import { type AgenticModel } from "../model";
import {
Expand All @@ -23,7 +23,7 @@ export const requestParser: AgenticModel.RequestParser<OpenAi.AiModel> = (
model,
messages,
tools,
tool_choice = "auto",
tool_choice = "auto"
) => {
const request: AiAdapter.Input<OpenAi.AiModel> = {
messages: messages.map((m) => {
Expand Down Expand Up @@ -82,7 +82,7 @@ export const requestParser: AgenticModel.RequestParser<OpenAi.AiModel> = (
* Parse a response from OpenAI output to internal network messages.
*/
export const responseParser: AgenticModel.ResponseParser<OpenAi.AiModel> = (
input,
input
) => {
return (input?.choices ?? []).reduce<Message[]>((acc, choice) => {
const { message, finish_reason } = choice;
Expand Down Expand Up @@ -149,12 +149,12 @@ const safeParseOpenAIJson = (str: string): unknown => {
// Replace backtick strings with regular JSON strings
// Match content between backticks, preserving newlines
const withQuotes = trimmed.replace(/`([\s\S]*?)`/g, (_, content) =>
JSON.stringify(content),
JSON.stringify(content)
);
return JSON.parse(withQuotes);
} catch (e) {
throw new Error(
`Failed to parse JSON with backticks: ${stringifyError(e)}`,
`Failed to parse JSON with backticks: ${stringifyError(e)}`
);
}
}
Expand Down
26 changes: 13 additions & 13 deletions src/agent.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type AiAdapter } from "inngest";
import { type AiAdapter } from "@inngest/ai";
import { createAgenticModelFromAiAdapter, type AgenticModel } from "./model";
import { NetworkRun } from "./networkRun";
import {
Expand All @@ -8,7 +8,7 @@ import {
type ToolResultMessage,
} from "./state";
import { type Tool } from "./types";
import { getStepTools, type AnyZodType, type MaybePromise } from "./util";
import { type AnyZodType, type MaybePromise } from "./util";

/**
* createTool is a helper that properly types the input argument for a handler
Expand Down Expand Up @@ -108,7 +108,7 @@ export class Agent {
*/
async run(
input: string,
{ model, network, state, maxIter = 0 }: Agent.RunOptions | undefined = {},
{ model, network, state, maxIter = 0 }: Agent.RunOptions | undefined = {}
): Promise<InferenceResult> {
const rawModel = model || this.model || network?.defaultModel;
if (!rawModel) {
Expand Down Expand Up @@ -152,7 +152,7 @@ export class Agent {
p,
prompt,
history,
run,
run
);

hasMoreActions =
Expand Down Expand Up @@ -183,13 +183,13 @@ export class Agent {
p: AgenticModel.Any,
prompt: Message[],
history: Message[],
network?: NetworkRun,
network?: NetworkRun
): Promise<InferenceResult> {
const { output, raw } = await p.infer(
this.name,
prompt.concat(history),
Array.from(this.tools.values()),
this.tool_choice || "auto",
this.tool_choice || "auto"
);

// Now that we've made the call, we instantiate a new InferenceResult for
Expand All @@ -201,7 +201,7 @@ export class Agent {
history,
output,
[],
typeof raw === "string" ? raw : JSON.stringify(raw),
typeof raw === "string" ? raw : JSON.stringify(raw)
);
if (this.lifecycles?.onResponse) {
result = await this.lifecycles.onResponse({
Expand All @@ -223,7 +223,7 @@ export class Agent {
private async invokeTools(
msgs: Message[],
p: AgenticModel.Any,
network?: NetworkRun,
network?: NetworkRun
): Promise<ToolResultMessage[]> {
const output: ToolResultMessage[] = [];

Expand All @@ -240,7 +240,7 @@ export class Agent {
const found = this.tools.get(tool.name);
if (!found) {
throw new Error(
`Inference requested a non-existent tool: ${tool.name}`,
`Inference requested a non-existent tool: ${tool.name}`
);
}

Expand All @@ -254,7 +254,7 @@ export class Agent {
const result = await found.handler(tool.input, {
agent: this,
network,
step: await getStepTools(),
// step: await getStepTools(),
});

// TODO: handle error and send them back to the LLM
Expand All @@ -280,7 +280,7 @@ export class Agent {

private async agentPrompt(
input: string,
network?: NetworkRun,
network?: NetworkRun
): Promise<Message[]> {
// Prompt returns the full prompt for the current agent. This does NOT
// include the existing network's state as part of the prompt.
Expand Down Expand Up @@ -401,7 +401,7 @@ export namespace Agent {
* running tools.
*/
onResponse?: (
args: Agent.LifecycleArgs.Result,
args: Agent.LifecycleArgs.Result
) => MaybePromise<InferenceResult>;

/**
Expand All @@ -411,7 +411,7 @@ export namespace Agent {
*
*/
onFinish?: (
args: Agent.LifecycleArgs.Result,
args: Agent.LifecycleArgs.Result
) => MaybePromise<InferenceResult>;
}

Expand Down
Loading
Loading