Skip to main content
本指南提供了 Baseten 聊天模型入门的快速概述。有关所有 ChatBaseten 功能、参数和配置的详细列表,请访问 ChatBaseten API 参考 Baseten 提供专为生产应用程序设计的推理服务。基于 Baseten 推理栈构建,这些 API 为领先的开源或自定义模型提供企业级性能和可靠性:https://www.baseten.co/library/。

概述

详情

可序列化JS 支持下载量版本
ChatBasetenlangchain-basetenbetaPyPI - DownloadsPyPI - Version

功能

工具调用结构化输出图像输入音频输入视频输入Token 级流式传输原生异步Token 用量Logprobs
模型 API 仅支持文本输入,而某些专用部署根据模型支持图像和音频输入。详情请查看 Baseten 模型库:https://www.baseten.co/library/

设置

要访问 Baseten 模型,您需要创建 Baseten 账户,获取 API 密钥,并安装 langchain-baseten 集成包。 访问此页面创建 Baseten 账户并生成 API 密钥。完成后,设置 BASETEN_API_KEY 环境变量:

凭据

Set API key
import getpass
import os

if "BASETEN_API_KEY" not in os.environ:
    os.environ["BASETEN_API_KEY"] = getpass.getpass("Enter your Baseten API key: ")
要启用模型调用的自动,请设置您的 LangSmith API 密钥:
Enable tracing
os.environ["LANGSMITH_API_KEY"] = getpass.getpass("Enter your LangSmith API key: ")
os.environ["LANGSMITH_TRACING"] = "true"

安装

LangChain Baseten 集成位于 langchain-baseten 包中:
pip install -U langchain-baseten

实例化

Baseten 提供两种访问聊天模型的方式:
  1. 模型 API:用于访问最新、最流行的开源模型。
  2. 专用 URL:使用具有专用资源的特定模型部署。
两种方式均支持自动端点规范化。
Initialize with model slug
from langchain_baseten import ChatBaseten

# Option 1: Use Model APIs with model slug
model = ChatBaseten(
    model="moonshotai/Kimi-K2-Instruct-0905",  # Choose from available model slugs: https://docs.baseten.co/development/model-apis/overview#supported-models
    api_key="your-api-key",  # Or set BASETEN_API_KEY env var
)
Initialize with model URL
from langchain_baseten import ChatBaseten

# Option 2: Use dedicated deployments with model url
model = ChatBaseten(
    model_url="https://model-<id>.api.baseten.co/environments/production/predict",
    api_key="your-api-key",  # Or set BASETEN_API_KEY env var
)

调用

Basic invocation
# Use the chat model
response = model.invoke("Hello, how are you?")
print(response.content)
content="Hello! I'm doing well, thank you for asking! How about you?" additional_kwargs={} response_metadata={'finish_reason': 'stop'} id='run--908651ec-00d7-4992-a320-864397c14e37-0'
您也可以使用消息对象进行更复杂的对话:
messages = [
    {"role": "system", "content": "You are a poetry expert"},
    {"role": "user", "content": "Write a haiku about spring"},
]
response = model.invoke(messages)
print(response)
content='Buds yawn open wide—  \na robin stitches the hush  \nwith threads of first light.' additional_kwargs={} response_metadata={'finish_reason': 'stop'} id='run--6f7d1db7-daae-4628-a40a-2ab7323e8f15-0'
完整指南可参阅聊天模型调用类型消息类型内容块

API 参考

有关所有 ChatBaseten 功能和配置的详细文档,请访问 API 参考