DEV Community

Aashish Karki
Aashish Karki

Posted on

Getting Started with the Minds JavaScript SDK

The Minds JavaScript SDK allows you to interact with the Minds AI system seamlessly. With this SDK, you can create and manage minds (artificial intelligences), handle data sources, and generate chat completions. This article will guide you through the essentials of using the SDK in your projects.

Installation

First, install the SDK using npm:

npm install minds_js_sdk
Enter fullscreen mode Exit fullscreen mode

Initializing the Client

To interact with the Minds AI system, you need to initialize the client with your API key. If you're using a custom Minds Cloud instance, you can specify a custom base URL.

const MindsClient = require('minds_js_sdk');

// For default Minds Cloud instance
const client = new MindsClient("YOUR_API_KEY");

// For a custom Minds Cloud instance
const baseUrl = 'https://your_custom_cloud.mdb.ai/';
const customClient = new MindsClient("YOUR_API_KEY", baseUrl);
Enter fullscreen mode Exit fullscreen mode

Replace "YOUR_API_KEY" with your actual API key.

Creating a Data Source

You can connect to various databases, such as PostgreSQL, by configuring your data source. Here's how to set up a PostgreSQL data source:

const postgresConfig = {
    name: 'my_datasource',
    description: 'Description of your data',
    engine: 'postgres',
    connectionData: {
        user: 'your_username',
        password: 'your_password',
        host: 'your_host',
        port: 5432,
        database: 'your_database',
        schema: 'your_schema'
    },
    tables: ['your_table_1', 'your_table_2']
};

client.datasources.create(postgresConfig)
    .then(datasource => console.log('Data source created:', datasource))
    .catch(error => console.error('Error creating data source:', error));
Enter fullscreen mode Exit fullscreen mode

Make sure to replace the placeholder values with your actual database credentials.

Managing Data Sources

Listing All Data Sources

To retrieve a list of all your data sources:

client.datasources.list()
    .then(datasources => console.log('All data sources:', datasources))
    .catch(error => console.error('Error listing data sources:', error));
Enter fullscreen mode Exit fullscreen mode

Getting a Specific Data Source

To fetch details of a specific data source by its name:

client.datasources.get('my_datasource')
    .then(datasource => console.log('Data source details:', datasource))
    .catch(error => console.error('Error getting data source:', error));
Enter fullscreen mode Exit fullscreen mode

Deleting a Data Source

To delete a data source:

client.datasources.drop('my_datasource')
    .then(() => console.log('Data source deleted successfully'))
    .catch(error => console.error('Error deleting data source:', error));
Enter fullscreen mode Exit fullscreen mode

Creating a Mind

A "mind" is an AI model that you can interact with. Here's how to create a mind and associate it with a data source:

client.minds.create({ name: 'my_mind', datasources: [postgresConfig] })
    .then(mind => console.log('Mind created:', mind))
    .catch(error => console.error('Error creating mind:', error));
Enter fullscreen mode Exit fullscreen mode

Alternatively, you can create a mind without a data source and add one later:

// Create a mind without a data source
client.minds.create({ name: 'my_mind' })
    .then(mind => {
        // Add a data source to the mind
        return mind.addDatasource(postgresConfig);
    })
    .then(updatedMind => console.log('Updated mind:', updatedMind))
    .catch(error => console.error('Error:', error));
Enter fullscreen mode Exit fullscreen mode

Managing Minds

Listing All Minds

To list all the minds you've created:

client.minds.list()
    .then(minds => console.log('All minds:', minds))
    .catch(error => console.error('Error listing minds:', error));
Enter fullscreen mode Exit fullscreen mode

Getting a Mind by Name

To retrieve details of a specific mind:

client.minds.get('my_mind')
    .then(mind => console.log('Mind details:', mind))
    .catch(error => console.error('Error getting mind:', error));
Enter fullscreen mode Exit fullscreen mode

Updating a Mind

To update a mind's name or data sources:

client.minds.update('my_mind', {
    name: 'new_mind_name',
    datasources: [postgresConfig]
})
.then(mind => console.log('Mind updated:', mind))
.catch(error => console.error('Error updating mind:', error));
Enter fullscreen mode Exit fullscreen mode

Deleting a Mind

To delete a mind:

client.minds.drop('my_mind')
    .then(() => console.log('Mind deleted successfully'))
    .catch(error => console.error('Error deleting mind:', error));
Enter fullscreen mode Exit fullscreen mode

Chat Completion

You can use a mind to generate chat completions, enabling interactive AI conversations.

Standard Chat Completion

client.minds.get('my_mind')
    .then(mind => mind.completion({ message: "Hello, how are you?" }))
    .then(response => console.log('Chat response:', response))
    .catch(error => console.error('Error in chat completion:', error));
Enter fullscreen mode Exit fullscreen mode

Streaming Chat Completion

For streaming responses:

client.minds.get('my_mind')
    .then(mind => mind.completion({ message: "Tell me a story", stream: true }))
    .then(stream => {
        stream.on('data', (chunk) => console.log(chunk.toString()));
        stream.on('end', () => console.log('Stream ended'));
    })
    .catch(error => console.error('Error in streaming chat completion:', error));
Enter fullscreen mode Exit fullscreen mode

Error Handling

The SDK uses Promise-based error handling. You can handle errors using .catch() or with try...catch in an async function:

try {
    const mind = await client.minds.create({ name: 'my_mind', datasources: [postgresConfig] });
    console.log('Mind created:', mind);
} catch (error) {
    console.error('Error creating mind:', error.message);
}
Enter fullscreen mode Exit fullscreen mode

Conclusion

The Minds JavaScript SDK simplifies the process of interacting with the Minds AI system. Whether you're managing data sources or building intelligent chatbots, this SDK provides the tools you need to get started quickly.

For more information and advanced usage, refer to the official documentation.

Happy coding!

Top comments (0)