Setting Up
Last updated
Last updated
To use the AI/ML API, you need to create an account and generate an API key. Follow these steps:
Create an Account: Visit the AI/ML API website and create an account.
Generate an API Key: After logging in, navigate to your account dashboard and generate your API key. Ensure that key is enabled on UI.
Depending on your environment and application, you will set the base URL differently. Below is a universal string that you can use to access our API. Copy it or return here later when you are ready with your environment or app.
The AI/ML API supports both versioned and non-versioned URLs, providing flexibility in your API requests. You can use either of the following formats:
https://api.aimlapi.com
https://api.aimlapi.com/v1
Using versioned URLs can help ensure compatibility with future updates and changes to the API. It is recommended to use versioned URLs for long-term projects to maintain stability.
Based on your environment, you will call our API differently. Below are two common ways to call our API using two popular programming languages: Python and NodeJS.
In the examples below, we use the OpenAI SDK. This is possible due to our compatibility with most OpenAI APIs, but this is just one approach. You can use our API without this SDK with raw HTTP queries.
Both examples are written in different programming languages, but despite that, they look very similar. Let's break down the code step by step and see what's going on.
In the examples above, we are using the OpenAI SDK. The OpenAI SDK is a nice module that allows us to use the AI/ML API without dealing with repetitive boilerplate code for handling HTTP requests. Before we can use the OpenAI SDK, it needs to be imported. The import happens in the following places:
Simple as it is. The next step is to initialize variables that our code will use. The two main ones are: the base URL and the API key. We already discussed them at the beginning of the article.
To communicate with LLM models, users use texts. These texts are usually called "Prompts." Inside our code, we have prompts with two roles: the system and the user. The system prompt is designed to be the main source of instruction for LLM generation, while the user prompt is designed to be user input, the subject of the system prompt. Despite that many models can operate differently, this behavior usually applies to chat LLM models, currently one of the most useful and popular ones.
Inside the code, the prompts are called in variables systemPrompt
, userPrompt
in JS, and system_prompt
, user_prompt
in Python.
Before we use the API, we need to create an instance of the OpenAI SDK class. It allows us to use all their methods. The instance is created with our imported package, and here we forward two main parameters: the base URL and the API key.
Because of notation, these two parameters are called slightly differently in these different languages (camel case in JS and snake case in Python), but their functionality is the same.
All preparation steps are done. Now we need to write our functionality and create something great. In the examples above, we make the simplest travel agent. Let's break down the steps of how we send a request to the model.
The best practice is to split the code blocks into complete parts with their own logic and not place executable code inside global module code. This rule applies in both languages we discuss. So we create a main function with all our logic. In JS, this function needs to be async, due to Promises and simplicity. In Python, requests run synchronously.
The OpenAI SDK provides us with methods to communicate with chat models. It is placed inside the chat.completions.create
function. This function accepts multiple parameters but requires only two: model
and messages
.
model
is a string, the name of the model that you want to use. For the best results, use a model designed for chat, or you can get unpredictable results if the model is not fine-tuned for that purpose. A list of supported models can be found here.
messages
is an array of objects with a content
field as prompt and a role
string that can be one of system
, user
, tool
, assistant
. With the role, the model can understand what to do with this prompt: Is this an instruction? Is this a user message? Is this an example of how to answer? Is this the result of code execution? The tool role is used for more complex behavior and will be discussed in another article.
In our example, we also use max_tokens
and temperature
. How to use these parameters is discussed in the parameters article.
With that knowledge, we can now send our request like the following:
The response from the function chat.completions.create
contains a completion. Completion is a fundamental part of LLM models' logic. Every LLM model is some sort of word autocomplete engine, trained by huge amounts of data. The chat models are designed to autocomplete large chunks of messages with prompts and certain roles, but other models can have their own custom logic without even roles.
Inside this completion, we are interested in the text of the generation. We can get it by getting the result from the completion variable:
In certain cases, completion can have multiple results. These results are called choices. Every choice has a message, the product of generation. The string content is placed inside the content
variable, which we placed inside our response variable above.
In the next steps, we can finally see the results. In both examples, we print the user prompt and response like it was a conversation:
Voila! Using AI/ML API models is the simplest and most productive way to get into the world of Machine Learning and Artificial Intelligence.