Skip to main content
Open In ColabOpen on GitHub

AI/ML API LLM

AI/ML API provides an API to query 300+ leading AI models (Deepseek, Gemini, ChatGPT, etc.) with enterprise-grade performance.

This example demonstrates how to use LangChain to interact with AI/ML API models.

Installation

%pip install --upgrade langchain-aimlapi
Requirement already satisfied: langchain-aimlapi in c:\users\tuman\appdata\local\programs\python\python312\lib\site-packages (0.1.0)
Requirement already satisfied: langchain-core<0.4.0,>=0.3.15 in c:\users\tuman\appdata\local\programs\python\python312\lib\site-packages (from langchain-aimlapi) (0.3.67)
Requirement already satisfied: langsmith>=0.3.45 in c:\users\tuman\appdata\local\programs\python\python312\lib\site-packages (from langchain-core<0.4.0,>=0.3.15->langchain-aimlapi) (0.4.4)
Requirement already satisfied: tenacity!=8.4.0,<10.0.0,>=8.1.0 in c:\users\tuman\appdata\local\programs\python\python312\lib\site-packages (from langchain-core<0.4.0,>=0.3.15->langchain-aimlapi) (9.1.2)
Requirement already satisfied: jsonpatch<2.0,>=1.33 in c:\users\tuman\appdata\local\programs\python\python312\lib\site-packages (from langchain-core<0.4.0,>=0.3.15->langchain-aimlapi) (1.33)
Requirement already satisfied: PyYAML>=5.3 in c:\users\tuman\appdata\local\programs\python\python312\lib\site-packages (from langchain-core<0.4.0,>=0.3.15->langchain-aimlapi) (6.0.2)
Requirement already satisfied: packaging<25,>=23.2 in c:\users\tuman\appdata\local\programs\python\python312\lib\site-packages (from langchain-core<0.4.0,>=0.3.15->langchain-aimlapi) (24.2)
Requirement already satisfied: typing-extensions>=4.7 in c:\users\tuman\appdata\local\programs\python\python312\lib\site-packages (from langchain-core<0.4.0,>=0.3.15->langchain-aimlapi) (4.14.0)
Requirement already satisfied: pydantic>=2.7.4 in c:\users\tuman\appdata\local\programs\python\python312\lib\site-packages (from langchain-core<0.4.0,>=0.3.15->langchain-aimlapi) (2.11.7)
Requirement already satisfied: jsonpointer>=1.9 in c:\users\tuman\appdata\local\programs\python\python312\lib\site-packages (from jsonpatch<2.0,>=1.33->langchain-core<0.4.0,>=0.3.15->langchain-aimlapi) (3.0.0)
Requirement already satisfied: httpx<1,>=0.23.0 in c:\users\tuman\appdata\local\programs\python\python312\lib\site-packages (from langsmith>=0.3.45->langchain-core<0.4.0,>=0.3.15->langchain-aimlapi) (0.28.1)
Requirement already satisfied: orjson<4.0.0,>=3.9.14 in c:\users\tuman\appdata\local\programs\python\python312\lib\site-packages (from langsmith>=0.3.45->langchain-core<0.4.0,>=0.3.15->langchain-aimlapi) (3.10.18)
Requirement already satisfied: requests<3,>=2 in c:\users\tuman\appdata\local\programs\python\python312\lib\site-packages (from langsmith>=0.3.45->langchain-core<0.4.0,>=0.3.15->langchain-aimlapi) (2.32.4)
Requirement already satisfied: requests-toolbelt<2.0.0,>=1.0.0 in c:\users\tuman\appdata\local\programs\python\python312\lib\site-packages (from langsmith>=0.3.45->langchain-core<0.4.0,>=0.3.15->langchain-aimlapi) (1.0.0)
Requirement already satisfied: zstandard<0.24.0,>=0.23.0 in c:\users\tuman\appdata\roaming\python\python312\site-packages (from langsmith>=0.3.45->langchain-core<0.4.0,>=0.3.15->langchain-aimlapi) (0.23.0)
Requirement already satisfied: annotated-types>=0.6.0 in c:\users\tuman\appdata\local\programs\python\python312\lib\site-packages (from pydantic>=2.7.4->langchain-core<0.4.0,>=0.3.15->langchain-aimlapi) (0.7.0)
Requirement already satisfied: pydantic-core==2.33.2 in c:\users\tuman\appdata\local\programs\python\python312\lib\site-packages (from pydantic>=2.7.4->langchain-core<0.4.0,>=0.3.15->langchain-aimlapi) (2.33.2)
Requirement already satisfied: typing-inspection>=0.4.0 in c:\users\tuman\appdata\local\programs\python\python312\lib\site-packages (from pydantic>=2.7.4->langchain-core<0.4.0,>=0.3.15->langchain-aimlapi) (0.4.1)
Requirement already satisfied: anyio in c:\users\tuman\appdata\local\programs\python\python312\lib\site-packages (from httpx<1,>=0.23.0->langsmith>=0.3.45->langchain-core<0.4.0,>=0.3.15->langchain-aimlapi) (4.9.0)
Requirement already satisfied: certifi in c:\users\tuman\appdata\local\programs\python\python312\lib\site-packages (from httpx<1,>=0.23.0->langsmith>=0.3.45->langchain-core<0.4.0,>=0.3.15->langchain-aimlapi) (2025.6.15)
Requirement already satisfied: httpcore==1.* in c:\users\tuman\appdata\local\programs\python\python312\lib\site-packages (from httpx<1,>=0.23.0->langsmith>=0.3.45->langchain-core<0.4.0,>=0.3.15->langchain-aimlapi) (1.0.9)
Requirement already satisfied: idna in c:\users\tuman\appdata\local\programs\python\python312\lib\site-packages (from httpx<1,>=0.23.0->langsmith>=0.3.45->langchain-core<0.4.0,>=0.3.15->langchain-aimlapi) (3.10)
Requirement already satisfied: h11>=0.16 in c:\users\tuman\appdata\local\programs\python\python312\lib\site-packages (from httpcore==1.*->httpx<1,>=0.23.0->langsmith>=0.3.45->langchain-core<0.4.0,>=0.3.15->langchain-aimlapi) (0.16.0)
Requirement already satisfied: charset_normalizer<4,>=2 in c:\users\tuman\appdata\local\programs\python\python312\lib\site-packages (from requests<3,>=2->langsmith>=0.3.45->langchain-core<0.4.0,>=0.3.15->langchain-aimlapi) (3.4.2)
Requirement already satisfied: urllib3<3,>=1.21.1 in c:\users\tuman\appdata\local\programs\python\python312\lib\site-packages (from requests<3,>=2->langsmith>=0.3.45->langchain-core<0.4.0,>=0.3.15->langchain-aimlapi) (2.5.0)
Requirement already satisfied: sniffio>=1.1 in c:\users\tuman\appdata\local\programs\python\python312\lib\site-packages (from anyio->httpx<1,>=0.23.0->langsmith>=0.3.45->langchain-core<0.4.0,>=0.3.15->langchain-aimlapi) (1.3.1)
Note: you may need to restart the kernel to use updated packages.
``````output

[notice] A new release of pip is available: 25.0.1 -> 25.2
[notice] To update, run: python.exe -m pip install --upgrade pip

Environment

To use AI/ML API, you'll need an API key which you can generate at: https://aimlapi.com/app/

You can pass it via aimlapi_api_key parameter or set as environment variable AIMLAPI_API_KEY.

import os
import getpass

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

Example: Chat Model

from langchain_aimlapi import ChatAimlapi

chat = ChatAimlapi(
model="meta-llama/Llama-3-70b-chat-hf",
)

# Stream response
for chunk in chat.stream("Tell me fun things to do in NYC"):
print(chunk.content, end="", flush=True)

# Or use invoke()
# response = chat.invoke("Tell me fun things to do in NYC")
# print(response)
The city that never sleeps! New York City is a treasure trove of excitement, entertainment, and adventure. Here are some fun things to do in NYC:

**Iconic Attractions:**

1. **Statue of Liberty and Ellis Island**: Take a ferry to Liberty Island to see the iconic statue up close and visit the Ellis Island Immigration Museum.
2. **Central Park**: A tranquil oasis in the middle of Manhattan, perfect for a stroll, picnic, or bike ride.
3. **Empire State Building**: For a panoramic view of the city, head to the observation deck of this iconic skyscraper.
4. **The Metropolitan Museum of Art**: One of the world's largest and most famous museums, with a collection that spans over 5,000 years of human history.

**Neighborhood Explorations:**

1. **SoHo**: Known for its trendy boutiques, art galleries, and cast-iron buildings.
2. **Greenwich Village**: A charming neighborhood with a rich history, known for its bohemian vibe, jazz clubs, and historic brownstones.
3. **Chinatown and Little Italy**: Experience the vibrant cultures of these two iconic neighborhoods, with delicious food, street festivals, and unique shops.
4. **Williamsburg, Brooklyn**: A hip neighborhood with a thriving arts scene, trendy bars, and some of the best restaurants in the city.

**Food and Drink:**

1. **Try a classic NYC slice of pizza**: Visit Lombardi's, Joe's Pizza, or Patsy's Pizzeria for a taste of the city's famous pizza.
2. **Bagels with lox and cream cheese**: A classic NYC breakfast at a Jewish deli like Russ & Daughters Cafe or Ess-a-Bagel.
3. **Food markets**: Visit Smorgasburg in Brooklyn or Chelsea Market for a variety of artisanal foods and drinks.
4. **Rooftop bars**: Enjoy a drink with a view at 230 Fifth, the Top of the Strand, or the Roof at The Viceroy Central Park.
**Performing Arts:**

1. **Broadway shows**: Catch a musical or play on the Great White Way, like Hamilton, The Lion King, or Wicked.
2. **Jazz clubs**: Visit Blue Note Jazz Club, the Village Vanguard, or the Jazz Standard for live music performances.
3. **Lincoln Center**: Home to the New York City Ballet, the Metropolitan Opera, and the Juilliard School.
4. **

Example: Text Completion Model

from langchain_aimlapi import AimlapiLLM

llm = AimlapiLLM(
model="gpt-3.5-turbo-instruct",
)

print(llm.invoke("def bubble_sort(): "))
 # Funkcja ponownie zwraca nową listę, bez zmienienia listy przekazanej jako argument w funkcji
my_list = [16, 12, 16, 3, 2, 6]
new_list = my_list[:]
for x in range(len(new_list)):
for y in range(len(new_list) - 1):
if new_list[y] > new_list[y + 1]:
new_list[y], new_list[y + 1] = new_list[y + 1], new_list[y]
return new_list, my_list


def bubble_sort_lib3(list): # Sortowanie z wykorzystaniem zewnętrznej biblioteki poza pętlą
from itertools import permutations
y = len(list)
perms = []
for a in range(0, y + 1):
for subset in permutations(list, a):