以编程方式使用文档

This page helps you get started with AI/ML API text completion models.

概述

集成详情

本地可序列化JS 支持下载量版本
AIMLAPILLMlangchain-aimlapibeta!PyPI - 下载量!PyPI - 版本

模型特性

工具调用结构化输出图像输入音频输入视频输入令牌级流式输出原生异步令牌使用量对数概率

设置

To access AI/ML API models you'll need to create an account, get an API key, and install the langchain-aimlapi 集成包。

凭证

前往 aimlapi.com 注册并生成 API 密钥。完成此操作后,设置 AIMLAPI_API_KEY 环境变量:

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

要启用模型调用的自动追踪,请设置您的 LangSmith API 密钥:

os.environ["LANGSMITH_TRACING"] = "true"
os.environ["LANGSMITH_API_KEY"] = getpass.getpass("Enter your LangSmith API key: ")

安装

The LangChain AI/ML API integration lives in the langchain-aimlapi package:

pip install -qU langchain-aimlapi

实例化

现在我们可以实例化模型对象并生成文本补全:

from langchain_aimlapi import AIMLAPILLM

llm = AIMLAPILLM(
    model="gpt-3.5-turbo-instruct",
    temperature=0.5,
    max_tokens=256,
)

调用

response = llm.invoke("Explain the bubble sort algorithm in Python.")
print(response)
Bubble sort is a simple sorting algorithm that repeatedly steps through a list, compares adjacent items, and swaps them when they are out of order. The process repeats until the entire list is sorted. While easy to understand and implement, bubble sort is inefficient on large datasets because it has quadratic time complexity.

流式调用

您也可以逐令牌地流式传输响应:

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

stream = llm.stream_events("List top 5 programming languages in 2025 with reasons.", version="v3")
for token in stream.text:
    print(token, end="", flush=True)