Ping-Pong

Let's start with a ping pong

What are we building?

Let's start with a dead simple example to understand how AgentLabs works.

In this recipe, we'll create a basic server with one single agent that listens for an incoming message.

Every time a user sends a message through the Frontend UI, the agent will evaluate it and answer.

If the message is equal to ping the agent will reply with pong.

Otherwise, the agent will say he did not get the message.

The full example is available in our example repository.

Requirements

To follow this tutorial, you must have:

  • some basic programming skills in Python or Typescript

  • created a project and an agent in your console

Let's code

Initializing the SDK

First, we must initialize the SDK with the project ID and the secret key.

You can find this information on your admin console.

import { Project } from 'agentlabs-sdk';

const project = new Project({
    projectId: 'you-project-id',
    secret: 'your-secret',
});

const agent = project.agent('agent-id');

Listen for messages

Now, we can use the onChatMessage method to apply some logic every time the user sends a message in the chat.

The onChatMessage accepts a callback as an argument, this callback will handle our logic.

Then, we'll use the send method exposed on the agent instance to send a message back to the chat on behalf of the agent.

Note the message argument contains some useful data, such as the conversation ID or the message's text.

import { Project } from 'agentlabs-sdk';

const project = new Project({
    projectId: 'you-project-id',
    secret: 'your-secret',
});

const agent = project.agent('agent-id');

project.onChatMessage((message) => {
  if (message.text === 'ping') {
     agent.send({
        conversationId: message.conversationId,
        text: 'pong'
     })
  } else {
     agent.send({
        conversationId: message.conversationId,
        text: 'I did not get that'
     })
  }
})

Init the connection

So far we instantiated our project and agent instances. We also defined the behavior of the agent.

Now, we need to initiate the connection between your server and ours.

No worries, it's the simplest part. We just have to use the project.connect() method.

Note in Python you will have to use the project.wait() in addition to project.connect().

import { Project } from 'agentlabs-sdk';

const project = new Project({
    projectId: 'you-project-id',
    secret: 'your-secret',
});

const agent = project.agent('agent-id');

project.onChatMessage((message) => {
  if (message.text === 'ping') {
     agent.send({
        conversationId: message.conversationId,
        text: 'pong'
     })
  } else {
     agent.send({
        conversationId: message.conversationId,
        text: 'I did not get that'
     })
  }
})

project.connect()

Last updated