跳到主要内容
您当前查看的页面是关于如何将 AI/ML API 模型用作文本补全模型。许多最新和最流行的 AI/ML API 模型是聊天补全模型您可能正在寻找此页面
本页面帮助您开始使用 AI/ML API 文本补全模型。有关所有 AimlapiLLM 功能和配置的详细文档,请参阅API 参考

概览

集成详情

类别本地可序列化JS 支持下载量版本
AimlapiLLMlangchain-aimlapi测试版PyPI - DownloadsPyPI - Version

模型功能

工具调用结构化输出JSON 模式图像输入音频输入视频输入令牌级流式传输原生异步Token 用量Logprobs

设置

要访问 AI/ML API 模型,您需要创建一个帐户,获取 API 密钥,并安装 langchain-aimlapi 集成包。

凭据

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

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: ")

安装

LangChain AI/ML API 集成位于 langchain-aimlapi 包中。
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.

流式调用

您还可以逐个 token 流式传输响应
llm = AimlapiLLM(
    model="gpt-3.5-turbo-instruct",
)

for chunk in llm.stream("List top 5 programming languages in 2025 with reasons."):
    print(chunk, end="", flush=True)

API 参考

有关所有 AimlapiLLM 功能和配置的详细文档,请参阅API 参考
以编程方式连接这些文档到 Claude、VSCode 等,通过 MCP 获取实时答案。
© . This site is unofficial and not affiliated with LangChain, Inc.