切换语言

LangChain

将 Bitdeer AI 接入 LangChain 构建 LLM 应用。

LangChain 的 OpenAI 兼容集成支持自定义 Base URL。将 ChatOpenAI 指向 Bitdeer,即可在链、Agent、RAG 等场景使用 Bitdeer 模型。

安装

pip install langchain-openai

基础用法

from langchain_openai import ChatOpenAI

llm = ChatOpenAI(
    base_url="https://api-inference.bitdeer.ai/v1",
    api_key="YOUR_API_KEY",
    model="deepseek-ai/DeepSeek-V4-Pro",
    max_tokens=256,
)

response = llm.invoke("Explain what RAG is in 3 sentences.")
print(response.content)

视觉(多模态)

from langchain_core.messages import HumanMessage

response = llm.invoke(
    [
        HumanMessage(
            content=[
                {"type": "image_url", "image_url": {"url": "https://example.com/photo.jpg"}},
                {"type": "text", "text": "描述这张图片。"},
            ]
        )
    ],
    model="moonshotai/Kimi-K2.6",
)
print(response.content)

流式

for chunk in llm.stream("Write a haiku about AI."):
    print(chunk.content, end="", flush=True)

构建链

from langchain_core.prompts import ChatPromptTemplate

prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a helpful coding assistant."),
    ("user", "{input}"),
])

chain = prompt | llm
response = chain.invoke({"input": "Write a Python function to compute Fibonacci numbers."})
print(response.content)

最后更新于

本页目录

LangChain · Bitdeer AI