Skip to content

Commit

Permalink
Simplify agent tools docs (#1012)
Browse files Browse the repository at this point in the history
* Simplify agent tools docs

* Fix input
  • Loading branch information
tonyhb authored Nov 27, 2024
1 parent b6c11a8 commit b11728d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 17 deletions.
59 changes: 43 additions & 16 deletions pages/docs/agent-kit/ai-agents-tools.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,11 @@ Agents themselves are relatively simple. When you call `run` on an agent, there
* The agent’s `onFinish` lifecycle is called with the new result. This lets you inspect the output of tool use
5. The result is returned to the caller

## Agent tools

When you create an agent you can specify any number of tools that the agent can use. Tools follow the standard formats that OpenAI and Anthropic provide: a name and a description, plus typed parameters.

In AgentKit, you also define a `handler` function which is called when the tool is invoked. Because AgentKit runs in the backend (on your own infrastructure) these handlers can run almost any code you define.

## Agent system prompts

### Complex agents with tools
You can define an agent's system prompt as a string or as an async callback which can inspect network state and return custom instructions. This is useful in an agentic workflow: multiple models are called in a loop, impacting network state that can adjust prompts.

A more complex agent used in a network defines a description, lifecycle hooks, tools, and a dynamic set of instructions based off of network state:
Here's an example:

```ts
import { Agent, Network, agenticOpenai as openai } from "@inngest/agent-kit";
Expand All @@ -93,9 +88,7 @@ const agent = createAgent({
// system defines a system prompt. This function is called by the network each time
// the agent runs, and allows you to customize the instructions based off of past state.
system: async ({ network }) => {

// Each time this agent runs, it may produce "file" content. Check if any
// content has already been produced in an agentic workflow.
// Inspect the network state to see if we have any existing code saved as files.
const files: Record<string, string> | undefined = network?.state.kv.get("files")
if (files === undefined) {
return systemPrompt;
Expand All @@ -109,6 +102,45 @@ const agent = createAgent({
}
return prompt;
},
});

```


## Agent tools

<Callout variant="info">
Tools are a vital part of agents, and have two core uses:

* Tools are exceptional at turn unstructured inputs into structured responses
* Tools can call arbitrary code, allowing models to interact with other systems
</Callout>

When you create an agent you can specify any number of tools that the agent can use. Tools follow the standard formats that OpenAI and Anthropic provide: a name and a description, plus typed parameters.

In AgentKit, you also define a `handler` function which is called when the tool is invoked. Because AgentKit runs in the backend (on your own infrastructure) these handlers can run almost any code you define.


### Complex agents with tools

A more complex agent used in a network defines a description, lifecycle hooks, tools, and a dynamic set of instructions based off of network state:

```ts
import { Agent, Network, agenticOpenai as openai } from "@inngest/agent-kit";

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."

const agent = createAgent({
name: "Code writer",

// description helps LLM routers choose the right agents to run.
description: "An expert TypeScript programmer which can write and debug code",

// system defines a system prompt. This function is called by the network each time
// the agent runs, and allows you to customize the instructions based off of past state.
system: systemPrompt;

// tools are provided to the model and are automatically called.
tools: [
Expand Down Expand Up @@ -136,11 +168,6 @@ const agent = createAgent({
],
});

// Use this agent in a network of other agents, or with long-term state.
const network = createNetwork({
agents: [agent]
});

```

Calling `.run` on this agent will pass the tools into your provider, allowing the model to select whether to run the `write_files` tool as a result. Tools are automatically called on your behalf.
Expand Down
2 changes: 1 addition & 1 deletion pages/docs/agent-kit/overview.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export default inngest.createFunction(
const input = `Classify then summarize the latest 10 blog posts
on https://www.deeplearning.ai/blog/`

const result = await network.run(, ({ network }) => {
const result = await network.run(input, ({ network }) => {
// Use an agent which figures out the specific agent to call
// based off of the network's history.
return defaultRoutingAgent;
Expand Down

0 comments on commit b11728d

Please sign in to comment.