Skip to main content

Installation and Setup

Welcome to the Merlin API installation guide. This page will provide step-by-step instructions on how to integrate Merlin API into your project using your preferred package manager, as well as examples on how to use the provided TypeScript package or to call the endpoints directly.

Package Installation

To begin using the Merlin API with your node.js project, you'll need to first install the merlin-node package. Below are the commands for npm, pnpm, and yarn.

npm

npm install merlin-node --save

pnpm

pnpm add merlin-node

yarn

yarn add merlin-node

Using the TypeScript package

Once you have installed the merlin-node package, you can import it into your TypeScript project as follows:

Example Code

import { Merlin } from "merlin-node";

const apiKey = "<YOUR_MERLIN_API_KEY>"; // Replace with your API key from Merlin
const merlin = new Merlin({ merlinConfig: { apiKey } });

async function createCompletion() {
try {
const completion = await merlin.chat.completions.create({
messages: [{ role: "system", content: "You are a helpful assistant." }],
model: "gpt-3.5-turbo", // Adjust model as needed
});

console.log(completion.choices[0].message.content);
} catch (error) {
console.error('Error creating completion:', error);
}
}

createCompletion();

Make sure to replace <YOUR_MERLIN_API_KEY> with your own API key obtained during the signup process on api.getmerlin.in.

Do node that right now, we only support createCompletion and imageGeneration, so if you want to do other things like finetuning on OpenAI or using the Assistants API, then add your OpenAI key with apiKey parameter when initializing Merlin as follows

const merlin = new Merlin({ apiKey:"<YOUR_OPENAI_KEY>", merlinConfig: {apiKey: "<YOUR_MERLIN_API_KEY>"} });`

In this case, we directly use OpenAI endpoints, hence your existing pipelines stay in place.

Using Endpoints Directly

If you prefer to use the API without a package, you can call the service directly using tools such as curl, axios or the fetch API in JavaScript. The API is accessible via the following URL: https://endpoints.getmerlin.in.

cURL Request Example

Run this cURL command in your terminal to interact with the API directly:

curl --location --request POST 'https://endpoints.getmerlin.in/chat/completions' \
--header 'Content-Type: application/json' \
--header 'Accept: application/json' \
--header 'x-merlin-key: <YOUR_MERLIN_API_KEY>' \
--data-raw '{
"model": "claude-2",
"messages": [
{
"role": "user",
"content": "Who won the world series in 2020?"
},
{
"role": "assistant",
"content": "The Los Angeles Dodgers won the World Series in 2020."
},
{
"role": "user",
"content": "Where was it played?"
}
],
"temperature": 1,
"top_p": 1,
"n": 1,
"stream": false,
"max_tokens": 250,
"presence_penalty": 0,
"frequency_penalty": 0
}'

Axios Example

Alternatively, you can use the axios library in a Node.js environment or in the browser to make an API request. Here's an example:

const axios = require('axios');

const apiKey = '<YOUR_MERLIN_API_KEY>'; // Replace with your API key from Merlin

import axios from 'axios';

const headers = {
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'x-merlin-key': apiKey
}
}

const data = {
'model': 'gpt-3.5-turbo',
'messages': [
{
'role': 'user',
'content': 'Who won the world series in 2020?'
},
{
'role': 'assistant',
'content': 'The Los Angeles Dodgers won the World Series in 2020.'
},
{
'role': 'user',
'content': 'Where was it played?'
}
],
'temperature': 1,
'top_p': 1,
'n': 1,
'stream': false,
'max_tokens': 250,
'presence_penalty': 0,
'frequency_penalty': 0
};


axios.post('https://endpoints.getmerlin.in/chat/completions', data, headers)
.then(response => {
console.log(response.data);
})
.catch(error => {
console.error('There was an error!', error);
});

Once you have set up your preferred method of interaction, you should be able to send requests to Merlin API and receive AI-powered responses. Remember to consult the API documentation for specific parameters and models available.

If there are any further questions or need for clarifications during your installation and setup process, feel free to reach out to Merlin's support for assistance.