Getting Started with AgentScript
This guide will walk you through the installation process and setting up a basic AgentScript project.
Installation
To begin using AgentScript, install the core package using npm:
npm install agentscript-ai
or with yarn:
yarn add agentscript-ai
Make sure you have Node.js and either npm or yarn installed on your system.
Setting up the Environment
AgentScript relies on environment variables to configure API keys for different services. You'll need to create a .env
file in your project's root directory.
-
Copy
.env.example
: You will find a.env.example
file in the root folder of the project, copy it into a.env
file. -
Fill in API Keys: Open the
.env
file and add your API keys. For example:
ANTHROPIC_API_KEY=your-anthropic-api-key
LINEAR_API_KEY=your-linear-api-key
Make sure you fill the appropriate keys to connect your app with other integrations.
Basic Agent Example
Here's a basic example of how to create and run an agent:
import { AnthropicModel } from 'agentscript-ai/anthropic';
import { defineTool, inferAgent, executeAgent } from 'agentscript-ai/core';
import * as s from 'agentscript-ai/schema';
const add = defineTool({
name: 'add',
description: 'Add two numbers.',
input: {
a: s.number(),
b: s.number(),
},
output: s.number(),
handler: ({input}) => input.a + input.b,
});
const llm = AnthropicModel({
model: 'claude-3-5-sonnet-latest',
apiKey: process.env.ANTHROPIC_API_KEY,
});
const tools = {
add
};
const prompt = 'Add numbers 1 and 2';
const output = s.number();
async function main() {
const agent = await inferAgent({
tools,
output,
llm,
prompt,
});
await executeAgent({agent});
console.log("Agent output:", agent.state?.output)
console.log("Execution variables:", agent.state?.root.variables)
}
main()
This example:
- Defines a Tool: Creates a simple tool named
add
that performs addition - Configures LLM: Sets up the AnthropicModel for code generation
- Define Available Tools: Pass available tools into tools object
- Defines a Task: Sets a natural language prompt for what you want to achieve
- Defines the output: Sets the output type using schema
- Infers the Agent: Uses
inferAgent
to let LLM generate the code for execution - Executes the Agent: Executes the agent's generated code with
executeAgent
- Logs the Results: Outputs the final result, and execution variables
You should see the output Agent output: 3
in the console, with variables that has a
and b
.
This gives a basic overview of the entire AgentScript flow.