Hello World
This step-by-step guide shows how to create a custom plugin using TypeScript.
This plugin will expose a function that returns a hardcoded string ("hello, world!"). This function can be called by your agent.
To create and deploy the plugin, follow these steps:
- Sign in to asterai
- Create a new agent. You can call it hello-world-agent.
- Install the asterai CLI. Example using
npm
:
npm install -g @asterai/cli
- Generate a new API key in your asterai dashboard, and use it to authenticate your CLI:
asterai auth <your_api_key>
- Initialise a new plugin project called
hello-world-plugin
. This will create a new directory with a template plugin project.
asterai init hello-world-plugin
- Install dependencies on the new project:
cd hello-world-plugin
npm i
- Inspect the contents of
plugin.wit
, the plugin interface file. - Inspect the contents of
plugin.ts
, the plugin implementation. - Modify the contents of
plugin.wit
, removing the examplemul
function and adding a new function for getting a secret phrase. Also rename the plugin name tohello-world-plugin
and ensure to replaceyour-username
inplugin.wit
with your asterai username, as otherwise you will not be able to deploy the plugin.
package your-username:hello-world-plugin@0.1.0;
world plugin {
import asterai:host/api@0.1.0;
export get-secret-phrase: func() -> string;
}
- Run
npm run build
. This will generate the utility types, but building will fail -- this is expected. - Update
plugin.ts
to implement theget-secret-phrase
function. It needs to return a string. The function will also send a message to the agent with that same string, by callingasterai.sendResponseToAgent("string-here")
. Yourplugin.ts
will look like this:
import * as asterai from "asterai:host/api@0.1.0";
export const getSecretPhrase = (): string => {
const phrase = "hello, world!";
// Send the secret phrase to the agent.
asterai.sendResponseToAgent(phrase);
return phrase;
};
- Build the plugin with
npm run build
to check that everything is correct. - Get your agent ID from the cloud console.
- Deploy the plugin:
asterai deploy --agent <your_agent_id>
- Refresh the page in the cloud console. You should see the new plugin that was just deployed.
- In the playground section, ask "what is the phrase?". The agent should reply mentioning "hello, world!".
- You can query the agent programmatically with a library or HTTP REST API.
Hopefully this hello world example illustrates how asterai can be used to connect AI to applications. Within plugins, it is possible to access LLMs, Vector DBs, make HTTP calls and even use WebSockets.