Skip to content

Commit

Permalink
Agent() tool calling loop + Network() ReAct impl. (#17)
Browse files Browse the repository at this point in the history
* 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.

* fix(adapters): `tool_choice = "auto"` + safely parse JSON generated by OpenAI

* fix(agent): implement tool calling loop in `Agent()`

* Fix linting, switch compile error

* Further fixes

* Fix up parsing to be a touch safer

* fix(network): `select_agent` fix + disable iterations on Agent when ran with a network

* feat(demo): restore demo, working demo

* Fix invalid `step` typing being too specific

Also fixes this not being compatible with prev/next versions.

* Use `AiAdapter.Any` for easier subtype checking

* Shift back to stable `inngest` version

* Update agent, state, and tool types

* Allow passing state into `agent.run()` and `new Network()`

* comments

* Comments

---------

Co-authored-by: Tony Holdstock-Brown <[email protected]>
Co-authored-by: Jack Williams <[email protected]>
  • Loading branch information
3 people authored Dec 6, 2024
1 parent a2135c1 commit 56cf929
Show file tree
Hide file tree
Showing 15 changed files with 1,356 additions and 234 deletions.
3 changes: 0 additions & 3 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npx lint-staged
25 changes: 12 additions & 13 deletions demo/inngest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import {
anthropic,
createAgent,
createNetwork,
createTypedTool,
createTool,
defaultRoutingAgent,
openai,
} from "../src/index";
import { EventSchemas, Inngest } from "inngest";
import { z } from "zod";
Expand All @@ -24,17 +25,16 @@ export const fn = inngest.createFunction(
{ id: "agent" },
{ event: "agent/run" },
async ({ event, step }) => {
const model = anthropic({ model: "claude-3-5-haiku-latest", max_tokens: 1024, step });
const model = openai({ model: "gpt-4", step });

// 1. Single agents
//
// 1. Single agent

// Run a single agent as a prompt without a network.
const { output, raw } = await codeWritingAgent.run(event.data.input, {
await codeWritingAgent.run(event.data.input, {
model,
});

// 2. A network of agents that works together

// 2. A network of agents that works together
const network = createNetwork({
agents: [
codeWritingAgent.withModel(model),
Expand All @@ -61,8 +61,7 @@ export const fn = inngest.createFunction(
);

const systemPrompt =
"You are an expert TypeScript programmer. Given a set of asks, think step-by-step to plan clean, " +
"idiomatic TypeScript code, with comments and tests as necessary.";
"You are an expert TypeScript programmer. You can create files with idiomatic TypeScript code, with comments and associated tests.";

const codeWritingAgent = createAgent({
name: "Code writer",
Expand Down Expand Up @@ -99,9 +98,9 @@ const codeWritingAgent = createAgent({
// "Do not respond with anything else other than the following XML tags:" +
// "- If you would like to write code, add all code within the following tags (replace $filename and $contents appropriately):" +
// " <file name='$filename.ts'>$contents</file>";
createTypedTool({
name: "write_files",
description: "Write code with the given filenames",
createTool({
name: "create_files",
description: "Create files with the given filenames and contents",
parameters: z
.object({
files: z.array(
Expand Down Expand Up @@ -141,7 +140,7 @@ const executingAgent = createAgent({
},
},

system: `You are an export TypeScript engineer that can execute commands, run tests, debug the output, and make modifications to code.
system: `You are an expert TypeScript engineer that can execute commands, run tests, debug the output, and make modifications to code.
Think carefully about the request that the user is asking for. Do not respond with anything else other than the following XML tags:
Expand Down
2 changes: 1 addition & 1 deletion demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"dependencies": {
"@inngest/agent-kit": "~0.0.1",
"express": "^4.21.1",
"inngest": "^3.26.2"
"inngest": "^3.27.3"
},
"devDependencies": {
"@types/express": "^5.0.0",
Expand Down
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"build": "rm -rf dist && tsc",
"lint": "eslint .",
"release": "pnpm run build && changeset publish",
"prepare": "husky install"
"prepare": "husky install",
"dev": "nodemon --watch demo -e ts --exec \"ts-node -P tsconfig-demo.json demo/index.ts\""
},
"files": [
"dist"
Expand Down Expand Up @@ -38,14 +39,16 @@
}
},
"dependencies": {
"inngest": "3.27.1",
"inngest": "^3.27.4",
"express": "^4.21.1",
"openai-zod-to-json-schema": "^1.0.3",
"zod": "^3.23.8"
},
"packageManager": "[email protected]",
"devDependencies": {
"@changesets/cli": "^2.27.10",
"@eslint/js": "^9.15.0",
"@types/express": "^5.0.0",
"@types/node": "^22.9.1",
"@typescript-eslint/eslint-plugin": "^8.15.0",
"@typescript-eslint/parser": "^8.15.0",
Expand All @@ -54,7 +57,9 @@
"eslint-plugin-prettier": "^5.2.1",
"husky": ">=7",
"lint-staged": ">=10",
"nodemon": "^3.0.2",
"prettier": "^3.3.3",
"ts-node": "^10.9.2",
"typescript": "^5.6.3",
"typescript-eslint": "^8.15.0"
},
Expand Down
Loading

0 comments on commit 56cf929

Please sign in to comment.