mirror of
https://github.com/hwchase17/langchain.git
synced 2026-02-21 14:43:07 +00:00
**Description:** Introduces documentation notebooks for AI/ML API integration covering the following use cases: - Chat models (`ChatAimlapi`) - Text completion models (`AimlapiLLM`) - Provider usage examples - Text embedding models (`AimlapiEmbeddings`) Additionally, adds the `langchain-aimlapi` package entry to `libs/packages.yml` for package management. This PR aims to provide a comprehensive starting point for developers integrating AI/ML API models with LangChain via the new `langchain-aimlapi` package. **Issue:** N/A **Dependencies:** None **Twitter handle:** @aimlapi --- ### **To-Do Before Submitting PR:** * [x] Run `make format` * [x] Run `make lint` * [x] Confirm all documentation notebooks are in `docs/docs/integrations/` * [x] Double-check `libs/packages.yml` has the correct repo path * [x] Confirm no `pyproject.toml` modifications were made unnecessarily Co-authored-by: Mason Daugherty <mason@langchain.dev>
358 lines
11 KiB
Plaintext
358 lines
11 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"source": [
|
|
"---\n",
|
|
"sidebar_label: AI/ML API\n",
|
|
"---"
|
|
],
|
|
"metadata": {
|
|
"collapsed": false
|
|
},
|
|
"id": "c74887ead73c5eb4"
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"source": [
|
|
"# AimlapiLLM\n",
|
|
"\n",
|
|
"This page will help you get started with AI/ML API [text completion models](/docs/concepts/text_llms). For detailed documentation of all AimlapiLLM features and configurations, head to the [API reference](https://docs.aimlapi.com/?utm_source=langchain&utm_medium=github&utm_campaign=integration).\n",
|
|
"\n",
|
|
"AI/ML API provides access to **300+ models** (Deepseek, Gemini, ChatGPT, etc.) via high-uptime and high-rate API."
|
|
],
|
|
"metadata": {
|
|
"collapsed": false
|
|
},
|
|
"id": "c1895707cde83d90"
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"source": [
|
|
"## Overview\n",
|
|
"### Integration details\n",
|
|
"\n",
|
|
"| Class | Package | Local | Serializable | JS support | Package downloads | Package latest |\n",
|
|
"| :--- | :--- | :---: | :---: | :---: | :---: | :---: |\n",
|
|
"| AimlapiLLM | langchain-aimlapi | ✅ | beta | ❌ |  |  |"
|
|
],
|
|
"metadata": {
|
|
"collapsed": false
|
|
},
|
|
"id": "72b0a510b6eac641"
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"source": [
|
|
"### Model features\n",
|
|
"| Tool calling | Structured output | JSON mode | Image input | Audio input | Video input | Token-level streaming | Native async | Token usage | Logprobs |\n",
|
|
"|:------------:|:-----------------:|:---------:|:-----------:|:-----------:|:-----------:|:---------------------:|:------------:|:-----------:|:--------:|\n",
|
|
"| ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |\n"
|
|
],
|
|
"metadata": {
|
|
"collapsed": false
|
|
},
|
|
"id": "4b87089494d8877d"
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"source": [
|
|
"## Setup\n",
|
|
"To access AI/ML API models, sign up at [aimlapi.com](https://aimlapi.com/app/?utm_source=langchain&utm_medium=github&utm_campaign=integration), generate an API key, and set the `AIMLAPI_API_KEY` environment variable:"
|
|
],
|
|
"metadata": {
|
|
"collapsed": false
|
|
},
|
|
"id": "2c45017efcc36569"
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"outputs": [],
|
|
"source": [
|
|
"import getpass\n",
|
|
"import os\n",
|
|
"\n",
|
|
"if \"AIMLAPI_API_KEY\" not in os.environ:\n",
|
|
" os.environ[\"AIMLAPI_API_KEY\"] = getpass.getpass(\"Enter your AI/ML API key: \")"
|
|
],
|
|
"metadata": {
|
|
"collapsed": false,
|
|
"ExecuteTime": {
|
|
"end_time": "2025-08-07T07:24:48.681319Z",
|
|
"start_time": "2025-08-07T07:24:47.490206Z"
|
|
}
|
|
},
|
|
"id": "86b05af725c45941",
|
|
"execution_count": 1
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"source": [
|
|
"### Installation\n",
|
|
"Install the `langchain-aimlapi` package:"
|
|
],
|
|
"metadata": {
|
|
"collapsed": false
|
|
},
|
|
"id": "51171ba92cb2b382"
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Note: you may need to restart the kernel to use updated packages.\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"%pip install -qU langchain-aimlapi"
|
|
],
|
|
"metadata": {
|
|
"collapsed": false,
|
|
"ExecuteTime": {
|
|
"end_time": "2025-08-07T07:18:08.606708Z",
|
|
"start_time": "2025-08-07T07:17:59.901457Z"
|
|
}
|
|
},
|
|
"id": "2b15cbaf7d5e1560",
|
|
"execution_count": 2
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"source": [
|
|
"## Instantiation\n",
|
|
"Now we can instantiate the `AimlapiLLM` model and generate text completions:"
|
|
],
|
|
"metadata": {
|
|
"collapsed": false
|
|
},
|
|
"id": "e94379f9d37fe6b3"
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"outputs": [],
|
|
"source": [
|
|
"from langchain_aimlapi import AimlapiLLM\n",
|
|
"\n",
|
|
"llm = AimlapiLLM(\n",
|
|
" model=\"gpt-3.5-turbo-instruct\",\n",
|
|
" temperature=0.5,\n",
|
|
" max_tokens=256,\n",
|
|
")"
|
|
],
|
|
"metadata": {
|
|
"collapsed": false,
|
|
"ExecuteTime": {
|
|
"end_time": "2025-08-07T07:46:52.875867Z",
|
|
"start_time": "2025-08-07T07:46:52.869961Z"
|
|
}
|
|
},
|
|
"id": "8a3af681997723b0",
|
|
"execution_count": 23
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"source": [
|
|
"## Invocation\n",
|
|
"You can invoke the model with a prompt:"
|
|
],
|
|
"metadata": {
|
|
"collapsed": false
|
|
},
|
|
"id": "c983ab1d95887e8f"
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"\n",
|
|
"\n",
|
|
"Bubble sort is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order. This process is repeated until the entire list is sorted.\n",
|
|
"\n",
|
|
"The algorithm gets its name from the way smaller elements \"bubble\" to the top of the list. It is commonly used for educational purposes due to its simplicity, but it is not a very efficient sorting algorithm for large data sets.\n",
|
|
"\n",
|
|
"Here is an implementation of the bubble sort algorithm in Python:\n",
|
|
"\n",
|
|
"1. Start by defining a function that takes in a list as its argument.\n",
|
|
"2. Set a variable \"swapped\" to True, indicating that a swap has occurred.\n",
|
|
"3. Create a while loop that runs as long as the \"swapped\" variable is True.\n",
|
|
"4. Inside the loop, set the \"swapped\" variable to False.\n",
|
|
"5. Create a for loop that iterates through the list, starting from the first element and ending at the second to last element.\n",
|
|
"6. Inside the for loop, compare the current element with the next element. If the current element is larger than the next element, swap them and set the \"swapped\" variable to True.\n",
|
|
"7. After the for loop, if the \"swapped\" variable\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"response = llm.invoke(\"Explain the bubble sort algorithm in Python.\")\n",
|
|
"print(response)"
|
|
],
|
|
"metadata": {
|
|
"collapsed": false,
|
|
"ExecuteTime": {
|
|
"end_time": "2025-08-07T07:46:57.209950Z",
|
|
"start_time": "2025-08-07T07:46:53.935975Z"
|
|
}
|
|
},
|
|
"id": "9a193081f431a42a",
|
|
"execution_count": 24
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"source": [
|
|
"## Streaming Invocation\n",
|
|
"You can also stream responses token-by-token:"
|
|
],
|
|
"metadata": {
|
|
"collapsed": false
|
|
},
|
|
"id": "1afedb28f556c7bd"
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
" \n",
|
|
"\n",
|
|
"1. Python\n",
|
|
"Python has been consistently growing in popularity and has become one of the most widely used programming languages in recent years. It is used for a wide range of applications such as web development, data analysis, machine learning, and artificial intelligence. Its simple syntax and readability make it an attractive choice for beginners and experienced programmers alike. With the rise of data-driven technology and automation, Python is projected to be the most in-demand language in 2025.\n",
|
|
"\n",
|
|
"2. JavaScript\n",
|
|
"JavaScript continues to dominate the web development scene and is expected to maintain its position as a top programming language in 2025. With the increasing use of front-end frameworks like React and Angular, JavaScript is crucial for building dynamic and interactive user interfaces. Additionally, the rise of serverless architecture and the popularity of Node.js make JavaScript an essential language for both front-end and back-end development.\n",
|
|
"\n",
|
|
"3. Go\n",
|
|
"Go, also known as Golang, is a relatively new programming language developed by Google. It is designed for"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"llm = AimlapiLLM(\n",
|
|
" model=\"gpt-3.5-turbo-instruct\",\n",
|
|
")\n",
|
|
"\n",
|
|
"for chunk in llm.stream(\"List top 5 programming languages in 2025 with reasons.\"):\n",
|
|
" print(chunk, end=\"\", flush=True)"
|
|
],
|
|
"metadata": {
|
|
"collapsed": false,
|
|
"ExecuteTime": {
|
|
"end_time": "2025-08-07T07:49:25.223233Z",
|
|
"start_time": "2025-08-07T07:49:22.101498Z"
|
|
}
|
|
},
|
|
"id": "a132c9183f648fb4",
|
|
"execution_count": 26
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"source": [
|
|
"## API reference\n",
|
|
"\n",
|
|
"For detailed documentation of all AimlapiLLM features and configurations, visit the [API Reference](https://docs.aimlapi.com/?utm_source=langchain&utm_medium=github&utm_campaign=integration).\n"
|
|
],
|
|
"metadata": {
|
|
"collapsed": false
|
|
},
|
|
"id": "7b4ab33058dc0974"
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"source": [
|
|
"## Chaining\n",
|
|
"\n",
|
|
"You can also easily combine with a prompt template for easy structuring of user input. We can do this using [LCEL](/docs/concepts/lcel)"
|
|
],
|
|
"metadata": {
|
|
"collapsed": false
|
|
},
|
|
"id": "900f36a35477c8ae"
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"outputs": [],
|
|
"source": [
|
|
"from langchain_core.prompts import PromptTemplate\n",
|
|
"\n",
|
|
"prompt = PromptTemplate.from_template(\"Tell me a joke about {topic}\")\n",
|
|
"chain = prompt | llm"
|
|
],
|
|
"metadata": {
|
|
"collapsed": false,
|
|
"ExecuteTime": {
|
|
"end_time": "2025-08-07T07:49:34.857042Z",
|
|
"start_time": "2025-08-07T07:49:34.853032Z"
|
|
}
|
|
},
|
|
"id": "d7f10052eb4ff249",
|
|
"execution_count": 27
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": "\"\\n\\nWhy do bears have fur coats?\\n\\nBecause they'd look silly in sweaters! \""
|
|
},
|
|
"execution_count": 28,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"chain.invoke({\"topic\": \"bears\"})"
|
|
],
|
|
"metadata": {
|
|
"collapsed": false,
|
|
"ExecuteTime": {
|
|
"end_time": "2025-08-07T07:49:48.565804Z",
|
|
"start_time": "2025-08-07T07:49:35.558426Z"
|
|
}
|
|
},
|
|
"id": "184c333c60f94b05",
|
|
"execution_count": 28
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"source": [
|
|
"## API reference\n",
|
|
"\n",
|
|
"For detailed documentation of all `AI/ML API` llm features and configurations head to the API reference: [API Reference](https://docs.aimlapi.com/?utm_source=langchain&utm_medium=github&utm_campaign=integration)"
|
|
],
|
|
"metadata": {
|
|
"collapsed": false
|
|
},
|
|
"id": "804f3a79a8046ec1"
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "Python 3",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 2
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython2",
|
|
"version": "2.7.6"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|