|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "fbeb3f1eb129d115", |
| 6 | + "metadata": { |
| 7 | + "collapsed": false |
| 8 | + }, |
| 9 | + "source": [ |
| 10 | + "---\n", |
| 11 | + "sidebar_label: AI/ML API\n", |
| 12 | + "---" |
| 13 | + ] |
| 14 | + }, |
| 15 | + { |
| 16 | + "cell_type": "markdown", |
| 17 | + "id": "6051ba9cfc65a60a", |
| 18 | + "metadata": { |
| 19 | + "collapsed": false |
| 20 | + }, |
| 21 | + "source": [ |
| 22 | + "# ChatAimlapi\n", |
| 23 | + "\n", |
| 24 | + "This page will help you get started with AI/ML API [chat models](../../concepts/chat_models.mdx). For detailed documentation of all ChatAimlapi features and configurations, head to the [API reference](https://docs.aimlapi.com/?utm_source=langchain&utm_medium=github&utm_campaign=integration).\n", |
| 25 | + "\n", |
| 26 | + "AI/ML API provides access to **300+ models** (Deepseek, Gemini, ChatGPT, etc.) via high-uptime and high-rate API." |
| 27 | + ] |
| 28 | + }, |
| 29 | + { |
| 30 | + "cell_type": "markdown", |
| 31 | + "id": "512f94fa4bea2628", |
| 32 | + "metadata": { |
| 33 | + "collapsed": false |
| 34 | + }, |
| 35 | + "source": [ |
| 36 | + "## Overview\n", |
| 37 | + "### Integration details\n", |
| 38 | + "\n", |
| 39 | + "| Class | Package | Local | Serializable | JS support | Package downloads | Package latest |\n", |
| 40 | + "| :--- | :--- | :---: | :---: | :---: | :---: | :---: |\n", |
| 41 | + "| ChatAimlapi | langchain-aimlapi | ✅ | beta | ❌ |  |  |" |
| 42 | + ] |
| 43 | + }, |
| 44 | + { |
| 45 | + "cell_type": "markdown", |
| 46 | + "id": "7163684608502d37", |
| 47 | + "metadata": { |
| 48 | + "collapsed": false |
| 49 | + }, |
| 50 | + "source": [ |
| 51 | + "### Model features\n", |
| 52 | + "| Tool calling | Structured output | JSON mode | Image input | Audio input | Video input | Token-level streaming | Native async | Token usage | Logprobs |\n", |
| 53 | + "|:------------:|:-----------------:|:---------:|:-----------:|:-----------:|:-----------:|:---------------------:|:------------:|:-----------:|:--------:|\n", |
| 54 | + "| ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |\n" |
| 55 | + ] |
| 56 | + }, |
| 57 | + { |
| 58 | + "cell_type": "markdown", |
| 59 | + "id": "bb9345d5b24a7741", |
| 60 | + "metadata": { |
| 61 | + "collapsed": false |
| 62 | + }, |
| 63 | + "source": [ |
| 64 | + "## Setup\n", |
| 65 | + "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:" |
| 66 | + ] |
| 67 | + }, |
| 68 | + { |
| 69 | + "cell_type": "code", |
| 70 | + "execution_count": 1, |
| 71 | + "id": "b26280519672f194", |
| 72 | + "metadata": { |
| 73 | + "collapsed": false, |
| 74 | + "ExecuteTime": { |
| 75 | + "end_time": "2025-08-07T07:16:58.837623Z", |
| 76 | + "start_time": "2025-08-07T07:16:55.346214Z" |
| 77 | + } |
| 78 | + }, |
| 79 | + "outputs": [], |
| 80 | + "source": [ |
| 81 | + "import getpass\n", |
| 82 | + "import os\n", |
| 83 | + "\n", |
| 84 | + "if \"AIMLAPI_API_KEY\" not in os.environ:\n", |
| 85 | + " os.environ[\"AIMLAPI_API_KEY\"] = getpass.getpass(\"Enter your AI/ML API key: \")" |
| 86 | + ] |
| 87 | + }, |
| 88 | + { |
| 89 | + "cell_type": "markdown", |
| 90 | + "id": "fa131229e62dfd47", |
| 91 | + "metadata": { |
| 92 | + "collapsed": false |
| 93 | + }, |
| 94 | + "source": [ |
| 95 | + "### Installation\n", |
| 96 | + "Install the `langchain-aimlapi` package:" |
| 97 | + ] |
| 98 | + }, |
| 99 | + { |
| 100 | + "cell_type": "code", |
| 101 | + "execution_count": 2, |
| 102 | + "id": "3777dc00d768299e", |
| 103 | + "metadata": { |
| 104 | + "collapsed": false, |
| 105 | + "ExecuteTime": { |
| 106 | + "end_time": "2025-08-07T07:17:11.195741Z", |
| 107 | + "start_time": "2025-08-07T07:17:02.288142Z" |
| 108 | + } |
| 109 | + }, |
| 110 | + "outputs": [ |
| 111 | + { |
| 112 | + "name": "stdout", |
| 113 | + "output_type": "stream", |
| 114 | + "text": [ |
| 115 | + "Note: you may need to restart the kernel to use updated packages.\n" |
| 116 | + ] |
| 117 | + } |
| 118 | + ], |
| 119 | + "source": [ |
| 120 | + "%pip install -qU langchain-aimlapi" |
| 121 | + ] |
| 122 | + }, |
| 123 | + { |
| 124 | + "cell_type": "markdown", |
| 125 | + "id": "d168108b0c4f9d7", |
| 126 | + "metadata": { |
| 127 | + "collapsed": false |
| 128 | + }, |
| 129 | + "source": [ |
| 130 | + "## Instantiation\n", |
| 131 | + "Now we can instantiate the `ChatAimlapi` model and generate chat completions:" |
| 132 | + ] |
| 133 | + }, |
| 134 | + { |
| 135 | + "cell_type": "code", |
| 136 | + "execution_count": 3, |
| 137 | + "id": "f29131e65e47bd16", |
| 138 | + "metadata": { |
| 139 | + "collapsed": false, |
| 140 | + "ExecuteTime": { |
| 141 | + "end_time": "2025-08-07T07:17:23.499746Z", |
| 142 | + "start_time": "2025-08-07T07:17:11.196747Z" |
| 143 | + } |
| 144 | + }, |
| 145 | + "outputs": [], |
| 146 | + "source": [ |
| 147 | + "from langchain_aimlapi import ChatAimlapi\n", |
| 148 | + "\n", |
| 149 | + "llm = ChatAimlapi(\n", |
| 150 | + " model=\"meta-llama/Llama-3-70b-chat-hf\",\n", |
| 151 | + " temperature=0.7,\n", |
| 152 | + " max_tokens=512,\n", |
| 153 | + " timeout=30,\n", |
| 154 | + " max_retries=3,\n", |
| 155 | + ")" |
| 156 | + ] |
| 157 | + }, |
| 158 | + { |
| 159 | + "cell_type": "markdown", |
| 160 | + "id": "861b87289f8e146d", |
| 161 | + "metadata": { |
| 162 | + "collapsed": false |
| 163 | + }, |
| 164 | + "source": [ |
| 165 | + "## Invocation\n", |
| 166 | + "You can invoke the model with a list of messages:" |
| 167 | + ] |
| 168 | + }, |
| 169 | + { |
| 170 | + "cell_type": "code", |
| 171 | + "execution_count": 4, |
| 172 | + "id": "430b1cff2e6d77b4", |
| 173 | + "metadata": { |
| 174 | + "collapsed": false, |
| 175 | + "ExecuteTime": { |
| 176 | + "end_time": "2025-08-07T07:17:30.586261Z", |
| 177 | + "start_time": "2025-08-07T07:17:29.074409Z" |
| 178 | + } |
| 179 | + }, |
| 180 | + "outputs": [ |
| 181 | + { |
| 182 | + "name": "stdout", |
| 183 | + "output_type": "stream", |
| 184 | + "text": [ |
| 185 | + "J'adore la programmation.\n" |
| 186 | + ] |
| 187 | + } |
| 188 | + ], |
| 189 | + "source": [ |
| 190 | + "messages = [\n", |
| 191 | + " (\"system\", \"You are a helpful assistant that translates English to French.\"),\n", |
| 192 | + " (\"human\", \"I love programming.\"),\n", |
| 193 | + "]\n", |
| 194 | + "\n", |
| 195 | + "ai_msg = llm.invoke(messages)\n", |
| 196 | + "print(ai_msg.content)" |
| 197 | + ] |
| 198 | + }, |
| 199 | + { |
| 200 | + "cell_type": "markdown", |
| 201 | + "id": "5463797524a19b2e", |
| 202 | + "metadata": { |
| 203 | + "collapsed": false |
| 204 | + }, |
| 205 | + "source": [ |
| 206 | + "## Chaining\n", |
| 207 | + "We can chain the model with a prompt template as follows:" |
| 208 | + ] |
| 209 | + }, |
| 210 | + { |
| 211 | + "cell_type": "code", |
| 212 | + "execution_count": 5, |
| 213 | + "id": "bf6defc12a0c5d78", |
| 214 | + "metadata": { |
| 215 | + "collapsed": false, |
| 216 | + "ExecuteTime": { |
| 217 | + "end_time": "2025-08-07T07:17:36.368436Z", |
| 218 | + "start_time": "2025-08-07T07:17:34.770581Z" |
| 219 | + } |
| 220 | + }, |
| 221 | + "outputs": [ |
| 222 | + { |
| 223 | + "name": "stdout", |
| 224 | + "output_type": "stream", |
| 225 | + "text": [ |
| 226 | + "Ich liebe das Programmieren.\n" |
| 227 | + ] |
| 228 | + } |
| 229 | + ], |
| 230 | + "source": [ |
| 231 | + "from langchain_core.prompts import ChatPromptTemplate\n", |
| 232 | + "\n", |
| 233 | + "prompt = ChatPromptTemplate.from_messages(\n", |
| 234 | + " [\n", |
| 235 | + " (\n", |
| 236 | + " \"system\",\n", |
| 237 | + " \"You are a helpful assistant that translates {input_language} to {output_language}.\",\n", |
| 238 | + " ),\n", |
| 239 | + " (\"human\", \"{input}\"),\n", |
| 240 | + " ]\n", |
| 241 | + ")\n", |
| 242 | + "\n", |
| 243 | + "chain = prompt | llm\n", |
| 244 | + "response = chain.invoke(\n", |
| 245 | + " {\n", |
| 246 | + " \"input_language\": \"English\",\n", |
| 247 | + " \"output_language\": \"German\",\n", |
| 248 | + " \"input\": \"I love programming.\",\n", |
| 249 | + " }\n", |
| 250 | + ")\n", |
| 251 | + "print(response.content)" |
| 252 | + ] |
| 253 | + }, |
| 254 | + { |
| 255 | + "cell_type": "markdown", |
| 256 | + "id": "fcf0bf10a872355c", |
| 257 | + "metadata": { |
| 258 | + "collapsed": false |
| 259 | + }, |
| 260 | + "source": [ |
| 261 | + "## API reference\n", |
| 262 | + "\n", |
| 263 | + "For detailed documentation of all ChatAimlapi features and configurations, visit the [API Reference](https://docs.aimlapi.com/?utm_source=langchain&utm_medium=github&utm_campaign=integration)." |
| 264 | + ] |
| 265 | + } |
| 266 | + ], |
| 267 | + "metadata": { |
| 268 | + "kernelspec": { |
| 269 | + "display_name": "Python 3", |
| 270 | + "language": "python", |
| 271 | + "name": "python3" |
| 272 | + }, |
| 273 | + "language_info": { |
| 274 | + "codemirror_mode": { |
| 275 | + "name": "ipython", |
| 276 | + "version": 2 |
| 277 | + }, |
| 278 | + "file_extension": ".py", |
| 279 | + "mimetype": "text/x-python", |
| 280 | + "name": "python", |
| 281 | + "nbconvert_exporter": "python", |
| 282 | + "pygments_lexer": "ipython2", |
| 283 | + "version": "2.7.6" |
| 284 | + } |
| 285 | + }, |
| 286 | + "nbformat": 4, |
| 287 | + "nbformat_minor": 5 |
| 288 | +} |
0 commit comments