PopNowAPI 文档
集成
正文

LangChain 与 LangGraph 工具

为 LangChain 与 LangGraph 智能体提供工具,用 PopNow 生成 AI 图片、视频、音乐和语音。每笔付费调用先以美元报价,确认后才执行。

langchain-popnow 为 LangChain 与 LangGraph 智能体提供工具:可用 PopNow 模型生成图片、视频、音乐和语音,也可以列出模型、领取任务结果、查询 API 余额。一把 API 密钥覆盖所有模型,从 Nano Banana、Seedream 到 MiniMax H3、Kling、Suno 和 Gemini TTS。

每笔付费调用都先以美元报价。付费工具如果没有传入 max_usd,只会返回价格:不会创建任何内容,也不会扣费。这个包以 MIT 许可证开源(GitHub),基于官方的 Python SDK 构建。

概览

包PyPI 上的 langchain-popnow
安装pip install -U langchain-popnow
要求Python 3.10 或更高版本,langchain-core 0.3.15 或更高版本
密钥POPNOW_API_KEY,即在 API 控制台创建的 sk-popnow- 密钥
计费从你单独的 API 余额按美元扣费。付费工具先报价,只有带上 max_usd 才会执行
工具图片、视频、音乐和语音生成,可按任意模型 ID 调用,另有模型列表、任务查询和余额查询(完整列表)
源码github.com/popnow-ai/langchain-popnow

设置

安装包

pip install -U langchain-popnow

创建 API 密钥

在 API 控制台创建一个密钥。为智能体单独配一个每日消费上限较低的密钥是个好习惯:这样撤销它时,不会影响你的其他集成。API 余额初始为 $0,请在 API 账单中充值。

把密钥放进环境变量

工具从 POPNOW_API_KEY 读取密钥:

export POPNOW_API_KEY="sk-popnow-..."

你也可以把 api_key="sk-popnow-..." 传给任意工具或 PopNowToolkit。POPNOW_BASE_URL 可以覆盖 API 地址(默认 https://api.popnow.ai)。

先报价,再执行

先不带 max_usd 调用一次付费工具获取价格,然后把 max_usd 设为该价格,再调用一次:

Python
from langchain_popnow import PopNowImageGeneration

tool = PopNowImageGeneration()

# 1. Quote: returns the price, starts nothing.
print(tool.invoke({"prompt": "A small red fox curled up in a teacup, soft morning light"}))
# {"status": "needs_confirmation", "model": "google/nano-banana-2.1/text-to-image", "cost_usd": "0.048", ...}

# 2. Approved: creates the task and waits for it.
print(tool.invoke({"prompt": "A small red fox curled up in a teacup, soft morning light", "max_usd": "0.048"}))
# {"status": "succeeded", "task_id": "...", "cost_usd": "0.048", "outputs": [{"url": "https://...", "uri": "popnow://g/.../0"}], ...}

智能体用 ToolCall 调用工具时,工具返回一个 ToolMessage:它的 content 是上面的 JSON 摘要,它的 artifact 是完整的 PopNow 任务或报价记录。

工具

工具类工具名功能默认模型计费
PopNowImageGenerationpopnow_image_generation文生图,或编辑 image_urls 中传入的图片google/nano-banana-2.1/text-to-image(编辑:google/nano-banana-2.1/edit)付费,先报价
PopNowVideoGenerationpopnow_video_generation文生视频,或让 image_url 中的图片动起来minimax/h3/text-to-video(图生视频:minimax/h3/image-to-video)付费,先报价
PopNowMusicGenerationpopnow_music_generation歌曲与纯音乐,可附歌词suno/v6/text-to-audio付费,先报价
PopNowSpeechpopnow_text_to_speech多种音色的文本转语音google/gemini-3.8-flash-tts/text-to-speech付费,先报价
PopNowCreateTaskpopnow_create_task按模型 ID 调用任意 PopNow 模型,使用该模型自己的 input由智能体选择付费,先报价
PopNowListModelspopnow_list_models模型 ID 与美元费率;指定 model 时返回该模型的输入字段免费
PopNowGetTaskpopnow_get_task领取仍在运行中的任务结果免费
PopNowCheckBalancepopnow_check_balance可用与冻结的 API 余额(美元)免费

PopNowToolkit().get_tools() 会用同一份配置返回全部八个工具。

生成类工具接受通用字段(prompt、aspect_ratio、resolution、duration_seconds、voice_id 等)、可选的 model(用来选择其他 PopNow 模型),以及 extra_input(用来传入 popnow_list_models 为该模型列出的任意字段)。模型 ID 遵循 模型中描述的 <maker>/<model>/<task> 格式。

审批如何运作

PopNow 任务是异步的:先报价,按确认的价格创建,然后等待结果。这些工具遵循与面向开发者的 MCP 服务器相同的流程:

  1. 报价。 智能体不带 max_usd 调用付费工具。工具向 PopNow 请求报价,并返回 status: "needs_confirmation" 和 cost_usd。此时不会创建任务,也不会冻结任何金额。
  2. 确认。 智能体把价格展示给用户。用户确认后,它用相同的参数再次调用工具,并把 max_usd 设为报价中的 cost_usd。
  3. 执行。 如果当前报价不超过 max_usd,任务会以该报价锁定价格创建,工具随后等待结果。如果价格上涨,工具会返回新的 needs_confirmation,并且不收取任何费用。

结果中包含签名下载链接(会过期),以及一个不会过期的 popnow://g/<task>/<n> URI,它可以传给另一个 PopNow 工具,例如让生成的图片动起来。长视频可能在 wait_timeout(默认 600 秒)结束时仍在运行:此时工具返回 status: "running" 和 task_id,之后可以用 popnow_get_task 领取结果。失败或过期的任务会释放冻结的金额,不收费。

消费防护

在任意付费工具或 PopNowToolkit 上都可以设置以下参数:

设置作用
max_usd_per_call="1.00"硬性上限。报价超过它时会返回 status: "over_limit",即使智能体传入了更高的 max_usd
auto_approve_usd="0.05"用于无人值守的流水线:不超过此金额的报价无需 max_usd 即可执行。默认值 None 表示每次付费调用都需要确认
allow_model_override=False智能体不能切换到工具配置的 model 之外

在 API 控制台中为 API 密钥设置的消费上限和模型白名单同样生效。

在 LangChain 智能体中使用

Python
from langchain.agents import create_agent
from langchain_popnow import PopNowToolkit

tools = PopNowToolkit(max_usd_per_call="1.00").get_tools()

agent = create_agent(
    model="anthropic:claude-sonnet-4-6",
    tools=tools,
    system_prompt=(
        "You create media with PopNow tools. Paid tools return a price first: "
        "show it to the user and only call again with max_usd after they approve."
    ),
)

result = agent.invoke({"messages": [{"role": "user", "content": "Make a 9:16 clip of a vintage car on a coastal road"}]})
print(result["messages"][-1].content)

该智能体可以运行在 LangChain 支持的任意对话模型上,包括通过 OpenAI 兼容端点 https://api.popnow.ai/v1 调用的 PopNow 对话模型。

在 LangGraph 中使用

Python
from langchain.chat_models import init_chat_model
from langgraph.prebuilt import create_react_agent
from langchain_popnow import PopNowToolkit

model = init_chat_model("anthropic:claude-sonnet-4-6")
agent = create_react_agent(model, PopNowToolkit(max_usd_per_call="1.00").get_tools())

for step in agent.stream(
    {"messages": [{"role": "user", "content": "Write a short lo-fi track for a cooking video and tell me the price first"}]},
    stream_mode="values",
):
    step["messages"][-1].pretty_print()

create_react_agent 在 LangGraph 1.x 中仍然可用;新项目可以使用如上所示的 langchain.agents.create_agent。如果需要一个严格的人工确认步骤,可以把这些工具与 LangChain 的 HumanInTheLoopMiddleware,或 LangGraph 的 interrupt 结合使用。

更多示例

查找模型及其输入

列出模型是免费的。传入 model 时,工具会返回该模型的输入字段和价格:

Python
from langchain_popnow import PopNowListModels

PopNowListModels().invoke({"modality": "video", "search": "kling"})
PopNowListModels().invoke({"model": "kling/3.0/image-to-video"})

用选定的模型让图片动起来

Python
from langchain_popnow import PopNowVideoGeneration

PopNowVideoGeneration().invoke({
    "prompt": "Gentle waves, slow push-in",
    "image_url": "https://example.com/beach.jpg",
    "model": "kling/3.0/image-to-video",
    "duration_seconds": 5,
    "max_usd": "0.50",
})

无需为每一行单独确认的配音

配音的费用只有几分之一美分,因此流水线可以自动确认小额报价:

Python
from langchain_popnow import PopNowSpeech

speech = PopNowSpeech(auto_approve_usd="0.02")
speech.invoke({"text": "Welcome back to the channel!", "voice_id": "Kore"})

任意模型 ID

PopNowCreateTask 发送模型自己的 input,写法与任务中完全一致:

Python
from langchain_popnow import PopNowCreateTask

PopNowCreateTask().invoke({
    "model": "alibaba/z-image-turbo/text-to-image",
    "input": {"prompt": "A paper boat on a puddle, overcast street, photo", "aspect_ratio": "1:1"},
    "max_usd": "0.02",
})

每个工具都可以异步运行:await tool.ainvoke(...)。

价格

列出模型、读取任务和查询余额都是免费的。生成类工具按模型的美元价格从你的 API 余额扣费:任务被接受时会冻结其报价金额,完成时按实际费用结算。失败或过期的任务不收费。

popnow_list_models 会返回每个模型的美元费率。每个模型的价格都列在模型页及其各自的模型页面上;AI 价格指数也列出了这些价格。报价、冻结与退款的运作方式见计费与报价。

常见错误

你看到的原因解决方法
每次调用都返回 needs_confirmation智能体从不传入 max_usd在系统提示中告诉它先展示价格,用户确认后再带上 max_usd 调用;或对小额调用设置 auto_approve_usd
over_limit报价高于 max_usd_per_call提高上限,或选择更便宜的模型或设置
401密钥缺失或已被撤销检查 POPNOW_API_KEY,或传入 api_key
40201API 余额不足以覆盖任务的冻结金额在 API 账单中充值
40202该密钥的消费上限将被超过在 API 控制台中提高这个密钥的上限
40301该密钥不能调用此模型把该模型加入密钥的白名单
status: "running"任务超过了 wait_timeout用 task_id 调用 popnow_get_task

完整列表见错误。

能和 LangGraph 一起用吗?

能。这些工具是标准的 LangChain 工具,因此 create_react_agent、ToolNode 以及自定义的 LangGraph 图都可以调用它们。如果每次价格都需要人工确认,可以搭配 LangGraph 的 interrupt 使用。

智能体会在我不知情的情况下动用我的余额吗?

默认不会。付费工具不带 max_usd 调用时只返回报价。只有设置了 auto_approve_usd,才会在未经确认的情况下花费,而且 max_usd_per_call 和密钥的消费上限依然生效。

哪些模型可以用于这些工具?

目录中的任意图片、视频或音频模型都可以。生成类工具都有合理的默认值;传入 model 即可选择其他模型,或用 PopNowCreateTask 调用任意模型 ID。popnow_list_models 可以在智能体内列出这些 ID。

有 JavaScript 版本吗?

TypeScript 与 JavaScript 请使用 Vercel AI SDK 提供方或 JavaScript SDK。

本页内容