As in the example from Python, we start from the very beginning too. We assume you already have Node.js installed. If not, here is a guide for beginners.
We need to create a new folder for the example project:
mkdir ./aimlapi-welcome
cd ./aimlapi-welcome
(Optional) If you use IDE then we recommend to open created folder as workspace. On example, in VSCode you can do it with:
code .
Now create a project file:
npm init -y
Install the required dependencies:
npm i openai
Create a file with the source code:
touch ./index.js
And paste the following content:
const { OpenAI } = require("openai");
const baseURL = "<https://api.aimlapi.com/v1>";
const apiKey = "my_key";
const systemPrompt = "You are a travel agent. Be descriptive and helpful";
const userPrompt = "Tell me about San Francisco";
const api = new OpenAI({
apiKey,
baseURL,
});
const main = async () => {
const completion = await api.chat.completions.create({
model: "mistralai/Mistral-7B-Instruct-v0.2",
messages: [
{
role: "system",
content: systemPrompt,
},
{
role: "user",
content: userPrompt,
},
],
temperature: 0.7,
max_tokens: 256,
});
const response = completion.choices[0].message.content;
console.log("User:", userPrompt);
console.log("AI:", response);
};
main();
You will see a response that looks like this:
User: Tell me about San Francisco
AI: San Francisco, located in the northern part of California, USA, is a vibrant and culturally rich city known for its iconic landmarks, beautiful scenery, and diverse neighborhoods.
The city is famous for its iconic Golden Gate Bridge, an engineering marvel and on
Was this article helpful?
That’s Great!
Thank you for your feedback
Sorry! We couldn't be helpful
Thank you for your feedback
Feedback sent
We appreciate your effort and will try to fix the article