一个专为尽快将代理人部署到应用程序中的 AI 代理人框架。在 MagmaDeploy.com 上部署或自行托管
Magma is a framework that lets you create AI agents without the headache. No complex chains, no confusing abstractions - just write the logic you want your agent to have.
Want to try it out? Chat with Dialog, our user research agent built with Magma!
npm i @pompeii-labs/magmaimport { MagmaAgent } from "@pompeii-labs/magma";
// Magma Agents are class based, so you can extend them with your own methods
class MyAgent extends MagmaAgent {
// Want to give it some personality? Add system prompts:
getSystemPrompts() {
return [{
role: "system",
content: "You are a friendly assistant who loves dad jokes"
}];
}
}
// That's it! You've got a working agent
const myAgent = new MyAgent();
// Run it:
const reply = await myAgent.main();
console.log(reply.content);Tools give your agent the ability to perform actions. Any method decorated with @tool and @toolparam will be available for the agent to use.
Important Notes:
call as a required parameter, which is the MagmaToolCall object…Middleware is a novel concept to Magma. It allows you to add custom logic to your agent before or after a tool is executed.
This is a great way to add custom logging, validation, data sanitization, etc.
Types:
Important Notes:
Error Handling:
import { MagmaAgent } from "@pompeii-labs/magma";
import { middleware } from "@pompeii-labs/magma/decorators";
/**
* Decorate any agent class method with @middleware to add custom logging, validation, etc.
* Types: "preCompletion", "onCompletion", "preToolExecution", "onToolExecution"
*/
class MyAgent extends MagmaAgent {
@middleware("onCompletion")
async logBeforeCompletion(message) {
if (message.content.includes("bad word")) {
throw new Error("You just used a bad word, please try again.");
}
}
}Jobs allow you to schedule functions within your agent. Jobs conform to the standard UNIX cron syntax (https://crontab.guru/).
Important Notes:
import { MagmaAgent } from "@pompeii-labs/magma";
import { job } from "@pompeii-labs/magma/decorators";
class MyAgent extends MagmaAgent {
// Run every day at midnight
@job("0 0 * * *")
static async dailyCleanup() {
await this.cleanDatabase();
}
// Run every hour with timezone
@job("0 * * * *", { timezone: "America/New_York" })
static async hourlySync() {
await this.syncData();
}
}Hooks allow you to expose your agent as an API. Any method decorated with @hook will be exposed as an endpoint.
Important Notes:
/hooks/{hook_name} in the Magma APIexpress.Requestimport { MagmaAgent } from "@pompeii-labs/magma";
import { hook } from "@pompeii-labs/magma/decorators";
import { Request } from "express";
class MyAgent extends MagmaAgent {
@hook('notification')
static async handleNotification(req: Request) {
await this.processNotification(req.body);
}
}You can use any supported provider by setting the providerConfig.
Important Notes:
setProviderConfigclass Agent extends MagmaAgent {
constructor() {
// Use OpenAI (default)
super({
providerConfig: {
provider: "openai",
model: "gpt-4o"
}
});
// Use Anthropic
this.setProviderConfig({
provider: "anthropic",
model: "claude-3.5-sonnet-20240620"
});
// Use Groq
this.setProviderConfig({
provider: "groq",
model: "llama-3.1-70b-versatile"
});
}
}Every Tool, Middleware, Hook, and Job is passed the instance of the agent. This allows you to manipulate agent state and call agent functions in Utility classes
……Event handlers are optional methods that allow you to tack on custom logic to various events in the agent lifecycle.
…Magma is Apache 2.0 licensed.
暂无开放 Issues,或尚未同步最近议题。