DEV Community

Cover image for Integrate AI in 2 lines of code with Heylock
Heylock.dev
Heylock.dev

Posted on • Originally published at heylock.dev

Integrate AI in 2 lines of code with Heylock

Heylock provides a simple SDK that helps you add AI features to JS/TS projects with minimal code.

I’ll guide you through the process of starting to use Heylock and exploring its capabilities.

I don't want to steal your time, so let's begin the tutorial right away. If you need more info, it is available at heylock.dev.

Getting Started in 4 Steps

1. Sign Up

First, create an account.
Don't worry about balance, we'll top you up with free credits at the beginning. And if you need more, use promocode DEV10 for an extra $10.

2. Create an Agent

When your account is ready, click on the "Create Agent" button.

Create an agent with an empty dashboard

3. Get Your Agent Key

Once your new agent is indexed, scroll to the bottom of the page and copy the key from the key card.

Generate the secret key with the

Indexing may take from 15 seconds to 5 minutes, depending on how busy our servers are. You can continue with the tutorial and come back to this step later.

4. Install

Run this command in your console to install the SDK.

npm i heylock
Enter fullscreen mode Exit fullscreen mode

Make sure you have package.json. And if you don't, run this command and then install our package.

npm init -y
Enter fullscreen mode Exit fullscreen mode

Initialize

Paste the following line, replacing 'PASTE-AGENT-KEY-HERE' with your agent key. This creates the object of an agent you will interact with.

import Heylock from 'heylock';

const agent = new Heylock('PASTE-AGENT-KEY-HERE');
Enter fullscreen mode Exit fullscreen mode

It runs initialization automatically. During that time, we check the agent key and your balance.

Learn more about initialization

If you want to learn more about initialization, for example how to set agent's settings, go to initialization docs.

Fix initialization warnings

Use this piece of code to avoid warnings when your agent is not initialized yet.

const agent = new Heylock('PASTE-AGENT-KEY-HERE');

agent.onInitialized(() => {
    // Your code goes here
});
Enter fullscreen mode Exit fullscreen mode

You can use the agent's functionality before initialization, but it is not guaranteed to work.

Message

The easiest prompting function you can ever come up with. And it is in our SDK. What a coincidence 😏
Oh, and by the way, remember the 2 lines promise? Here it is!

// Initialize
const agent = new Heylock(process.env.AGENT_KEY);

// Prompt and print
console.log(await agent.message('Hello!'));
Enter fullscreen mode Exit fullscreen mode

It will automatically save both messages to history.

See messageStream() to work with streaming responses.

Sort/Filter Arrays

Work with arrays in natural language.

// Initialize
const agent = new Heylock(process.env.AGENT_KEY);

// Define an array
const array = ['a', 2, { name: 'helicopter' }];

// Sort
const response = await agent.sort(array, 'Sort by perceived size');

// Print the response
console.log(response.array);

// Print reasoning
console.log(response.reasoning);
Enter fullscreen mode Exit fullscreen mode

Filtering is practically the same. Just prompt it.

// Initialize
const agent = new Heylock(process.env.AGENT_KEY);

// Filter
const response = await agent.sort([1, 2, 3, 4], 'Return only even numbers');

// Print the response
console.log(response.array);
Enter fullscreen mode Exit fullscreen mode

Use this function for recommendations, smart filtering, and everything else. It can sort literally anything. That is the power of AI.

Visit our docs to learn more about the sort() function.

Rewrite text

Want to add an enhance text function to your app? Say less.

// Initialize
const agent = new Heylock(process.env.AGENT_KEY);

// Rewrite
const text = await agent.rewrite('mai english so bed', 'Fix grammar and spelling');

// Print the response
console.log(text);
Enter fullscreen mode Exit fullscreen mode

Learn more in our docs.

Determine when to engage

Want to engage visitors with your AI agent? It knows when to pop up. Just ask.

// Initialize
const agent = new Heylock(process.env.AGENT_KEY);

// Check
const response = await agent.shouldEngage('Engage when visitors seem stuck');

// Print the decision
console.log(response.shouldEngage);

// Print the reasoning
console.log(response.reasoning);
Enter fullscreen mode Exit fullscreen mode

Check out this function in docs.

Manage Context

It’s a good practice to add context on relevant user actions. For example, when a visitor visits a page, clicks a button (even an unimportant one), hovers over something, etc., it is all useful for AI agents. In fact, we use the context in almost all functions, especially in shouldEngage().

// Initialize
const agent = new Heylock(process.env.AGENT_KEY);

// Add a context entry
const id = agent.addContextEntry('User visited the pricing page');

// Read
console.log(agent.getContextString()); // User visited the pricing page now. 
// SDK automatically appends human-readable timestamps

// Modify
agent.modifyContextEntry(id, 'User visited the pricing page twice');

// Remove
agent.removeContextEntry(id);
Enter fullscreen mode Exit fullscreen mode

To learn more, visit the context docs.

That's not all!

This is only a small part of what you can do with Heylock AI agents. Learn more in our official docs and in our other tutorials.

Happy building!

Top comments (0)