Skip to content

Commit

Permalink
Update imports (#26)
Browse files Browse the repository at this point in the history
* Upgrade `inngest` to `3.28.0`

* Switch to `getDefaultRoutingAgent()`

---------

Co-authored-by: Jack Williams <[email protected]>
  • Loading branch information
tonyhb and jpwilliams authored Dec 17, 2024
1 parent f7158e4 commit ded7b31
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 79 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
},
"dependencies": {
"express": "^4.21.1",
"inngest": "3.27.6-pr-776.2",
"inngest": "^3.28.0",
"openai-zod-to-json-schema": "^1.0.3",
"zod": "^3.23.8"
},
Expand Down
10 changes: 5 additions & 5 deletions pnpm-lock.yaml

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

1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
export * from "./agent";
export * from "./model";
export * from "./network";
export * from "./networkRun";
export * from "./state";
export * from "./types";
export * from "./util";
142 changes: 71 additions & 71 deletions src/network.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import { type AiAdapter } from "inngest";
import { z } from "zod";
import {
type Agent,
type RoutingAgent,
createRoutingAgent,
createTool,
} from "./agent";
import { createRoutingAgent, type Agent, type RoutingAgent } from "./agent";
import { NetworkRun } from "./networkRun";
import { type InferenceResult, State } from "./state";
import { State, type InferenceResult } from "./state";
import { type MaybePromise } from "./util";

/**
Expand Down Expand Up @@ -131,76 +126,78 @@ export class Network {
* It is no set model and so relies on the presence of a default model in the
* network or being explicitly given one.
*/
export const defaultRoutingAgent = createRoutingAgent({
name: "Default routing agent",

description:
"Selects which agents to work on based off of the current prompt and input.",

lifecycle: {
onRoute: ({ result }) => {
const tool = result.toolCalls[0];
if (!tool) {
return;
}
if (typeof tool.content === "string") {
return [tool.content];
}
return;
},
},

tools: [
// This tool does nothing but ensure that the model responds with the
// agent name as valid JSON.
createTool({
name: "select_agent",
description:
"select an agent to handle the input, based off of the current conversation",
parameters: z
.object({
name: z
.string()
.describe("The name of the agent that should handle the request"),
})
.strict(),
handler: ({ name }, { network }) => {
if (!network) {
throw new Error(
"The routing agent can only be used within a network of agents",
);
}

if (typeof name !== "string") {
throw new Error("The routing agent requested an invalid agent");
let defaultRoutingAgent: RoutingAgent | undefined;
export const getDefaultRoutingAgent = () => {
defaultRoutingAgent ??= createRoutingAgent({
name: "Default routing agent",

description:
"Selects which agents to work on based off of the current prompt and input.",

lifecycle: {
onRoute: ({ result }) => {
const tool = result.toolCalls[0];
if (!tool) {
return;
}

const agent = network.agents.get(name);
if (agent === undefined) {
throw new Error(
`The routing agent requested an agent that doesn't exist: ${name}`,
);
if (typeof tool.content === "string") {
return [tool.content];
}
return;
},
},

// This returns the agent name to call. The default routing functon
// schedules this agent by inpsecting this name via the tool call output.
return agent.name;
tools: [
// This tool does nothing but ensure that the model responds with the
// agent name as valid JSON.
{
name: "select_agent",
description:
"select an agent to handle the input, based off of the current conversation",
parameters: z
.object({
name: z
.string()
.describe("The name of the agent that should handle the request"),
})
.strict(),
handler: ({ name }, { network }) => {
if (!network) {
throw new Error(
"The routing agent can only be used within a network of agents",
);
}

if (typeof name !== "string") {
throw new Error("The routing agent requested an invalid agent");
}

const agent = network.agents.get(name);
if (agent === undefined) {
throw new Error(
`The routing agent requested an agent that doesn't exist: ${name}`,
);
}

// This returns the agent name to call. The default routing functon
// schedules this agent by inpsecting this name via the tool call output.
return agent.name;
},
},
}),
],
],

tool_choice: "select_agent",
tool_choice: "select_agent",

system: async ({ network }): Promise<string> => {
if (!network) {
throw new Error(
"The routing agent can only be used within a network of agents",
);
}
system: async ({ network }): Promise<string> => {
if (!network) {
throw new Error(
"The routing agent can only be used within a network of agents",
);
}

const agents = await network?.availableAgents();
const agents = await network?.availableAgents();

return `You are the orchestrator between a group of agents. Each agent is suited for a set of specific tasks, and has a name, instructions, and a set of tools.
return `You are the orchestrator between a group of agents. Each agent is suited for a set of specific tasks, and has a name, instructions, and a set of tools.
The following agents are available:
<agents>
Expand All @@ -224,8 +221,11 @@ Follow the set of instructions:
Your aim is to thoroughly complete the request, thinking step by step, choosing the right agent based off of the context.
</instructions>
`;
},
});
},
});

return defaultRoutingAgent;
};

export namespace Network {
export type Constructor = {
Expand Down
4 changes: 2 additions & 2 deletions src/networkRun.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { RoutingAgent, type Agent } from "./agent";
import { defaultRoutingAgent, Network } from "./network";
import { getDefaultRoutingAgent, Network } from "./network";
import { type State } from "./state";

export class NetworkRun extends Network {
Expand Down Expand Up @@ -118,7 +118,7 @@ export class NetworkRun extends Network {
);
}
if (!router) {
router = defaultRoutingAgent;
router = getDefaultRoutingAgent();
}
if (router instanceof RoutingAgent) {
return await this.getNextAgentsViaRoutingAgent(router, input);
Expand Down

0 comments on commit ded7b31

Please sign in to comment.