Skip to main content
Open In ColabOpen on GitHub

ChatAimlapi

This page will help you get started with AI/ML API chat models. For detailed documentation of all ChatAimlapi features and configurations, head to the API reference.

AI/ML API provides access to 300+ models (Deepseek, Gemini, ChatGPT, etc.) via high-uptime and high-rate API.

Overviewโ€‹

Integration detailsโ€‹

ClassPackageLocalSerializableJS supportPackage downloadsPackage latest
ChatAimlapilangchain-aimlapiโœ…betaโŒPyPI - DownloadsPyPI - Version

Model featuresโ€‹

Tool callingStructured outputJSON modeImage inputAudio inputVideo inputToken-level streamingNative asyncToken usageLogprobs
โœ…โœ…โœ…โœ…โœ…โœ…โœ…โœ…โœ…โœ…

Setupโ€‹

To access AI/ML API models, sign up at aimlapi.com, generate an API key, and set the AIMLAPI_API_KEY environment variable:

import getpass
import os

if "AIMLAPI_API_KEY" not in os.environ:
os.environ["AIMLAPI_API_KEY"] = getpass.getpass("Enter your AI/ML API key: ")

Installationโ€‹

Install the langchain-aimlapi package:

%pip install -qU langchain-aimlapi
Note: you may need to restart the kernel to use updated packages.

Instantiationโ€‹

Now we can instantiate the ChatAimlapi model and generate chat completions:

from langchain_aimlapi import ChatAimlapi

llm = ChatAimlapi(
model="meta-llama/Llama-3-70b-chat-hf",
temperature=0.7,
max_tokens=512,
timeout=30,
max_retries=3,
)

Invocationโ€‹

You can invoke the model with a list of messages:

messages = [
("system", "You are a helpful assistant that translates English to French."),
("human", "I love programming."),
]

ai_msg = llm.invoke(messages)
print(ai_msg.content)
J'adore la programmation.

Chainingโ€‹

We can chain the model with a prompt template as follows:

from langchain_core.prompts import ChatPromptTemplate

prompt = ChatPromptTemplate.from_messages(
[
(
"system",
"You are a helpful assistant that translates {input_language} to {output_language}.",
),
("human", "{input}"),
]
)

chain = prompt | llm
response = chain.invoke(
{
"input_language": "English",
"output_language": "German",
"input": "I love programming.",
}
)
print(response.content)
API Reference:ChatPromptTemplate
Ich liebe das Programmieren.

API referenceโ€‹

For detailed documentation of all ChatAimlapi features and configurations, visit the API Reference.