Skip to content

Instantly share code, notes, and snippets.

@timothycarambat
Created November 4, 2024 16:55
Show Gist options
  • Save timothycarambat/815bdcef8ab078fda8135a73c4848247 to your computer and use it in GitHub Desktop.
Save timothycarambat/815bdcef8ab078fda8135a73c4848247 to your computer and use it in GitHub Desktop.
Agent API Invocation for AnythingLLM

This is a simple API-agent invocation

This plugin simply uses an external API to add two numbers via a GET request to localhost:3001/api/calculate, but is simply an example to demonstrate a custom plugin to reach out to external APIs.

Assume a GET /calculate api like so

 app.get("/calculate", async (request, response) => {
    const { n1, n2 } = request.query;
    const addition = Number(n1) + Number(n2);
    response.status(200).json({ sum: addition });
  });

You can then use a custom agent by:

  1. create a folder in the storage folder of AnythingLLM called agent-skills. This folder should exist already.
  2. create a folder named agent-addition-api-invocation
  3. Copy paste plugin.json and handler.js into this folder.
  4. It should now be visible in Agent Skills in the AnythingLLM UI.
  5. Call is via @agent add x + y and get a response that was calculated externally.
module.exports.runtime = {
API_ENDPOINT: "http://localhost:3001/api/calculate",
handler: async function ({ n1, n2 }) {
const callerId = `${this.config.name}-v${this.config.version}`;
try {
this.introspect(`${callerId}: Adding ${n1} and ${n2}...`);
const url = new URL(this.API_ENDPOINT);
url.searchParams.set("n1", n1);
url.searchParams.set("n2", n2);
const res = await fetch(url, { method: "GET" })
.then((res) => res.json())
.catch((err) => {
this.logger(err);
return null;
});
if (!res) return "Failed to invoke agent API - calculation failed.";
return res; // return the result of the calculation in JSON.
} catch (e) {
this.logger(e)
this.introspect(
`${callerId} failed to execute. Reason: ${e.message}`
);
return `Failed to invoke agent API. Error ${e.message}`;
}
}
};
{
"name": "Agent addition API Invocation",
"version": "1.0.0",
"description": "Invoke an agent API from a prompt",
"author": "Mintplex Labs",
"license": "MIT",
"active": true,
"hubId": "agent-addition-api-invocation",
"setup_args": {},
"examples": [
{
"prompt": "Add 4 + 8",
"call": "{\"n1\": 4, \"n2\": 8}"
},
{
"prompt": "Add two random numbers",
"call": "{\"n1\": 134, \"n2\": 234}"
},
{
"prompt": "What is 5 + 221",
"call": "{\"n1\": 5, \"n2\": 221}"
}
],
"entrypoint": {
"file": "handler.js",
"params": {
"n1": {
"description": "First number",
"type": "number"
},
"n2": {
"description": "Second number",
"type": "number"
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment