本指南提供了快速入门概述,帮助您开始使用 ChatBaseten 聊天模型.
Baseten provides inference designed for production applications. Built on the Baseten Inference Stack, these APIs deliver enterprise-grade performance and reliability for leading open-source or custom models: https://www.baseten.co/library/.
概述
详情
| 类 | 包 | 可序列化 | JS 支持 | 下载量 | 版本 |
|---|---|---|---|---|---|
ChatBaseten | langchain-baseten | beta | ❌ | !PyPI - 下载量 | !PyPI - 版本 |
功能特性
| 工具调用 | 结构化输出 | 图像输入 | 音频输入 | 视频输入 | Token 级流式输出 | 原生异步 | Token 使用量 | 对数概率 |
|---|---|---|---|---|---|---|---|---|
| ✅ | ✅ | ✅ | ✅ | ❌ | ✅ | ✅ | ✅ | ❌ |
Model APIs only support text input, while some dedicated deployments support image and audio input depending on model. Check the Baseten model library for details: https://www.baseten.co/library/
设置
要访问 Baseten 模型,您需要创建一个 Baseten 账户,获取 API 密钥,并安装 langchain-baseten 集成包。
前往 Baseten 网站 创建账户并生成 API 密钥。完成此操作后,设置 BASETEN_API_KEY 环境变量:
凭证
if "BASETEN_API_KEY" not in os.environ:
os.environ["BASETEN_API_KEY"] = getpass.getpass("Enter your Baseten API key: ")
要启用自动化 追踪 您的模型调用,请设置您的 LangSmith API 密钥:
os.environ["LANGSMITH_API_KEY"] = getpass.getpass("Enter your LangSmith API key: ")
os.environ["LANGSMITH_TRACING"] = "true"
安装
LangChain Baseten 集成位于 langchain-baseten package:
pip install -U langchain-baseten
uv add langchain-baseten
实例化
Baseten 提供两种访问聊天模型的方式:
- **模型 API**:用于访问最新、最流行的开源模型。
- **专用 URL**:使用具有专用资源的特定模型部署。
两种方法都支持自动端点规范化。
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
)
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
)
调用
# Use the chat model
response = model.invoke("Hello, how are you?")
print(response.content)
Hello! I'm doing well, thank you for asking! How about you?
您还可以使用消息对象进行更复杂的对话:
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={'token_usage': {'completion_tokens': 26, 'prompt_tokens': 14, 'total_tokens': 40}, 'model_name': 'moonshotai/Kimi-K2-Instruct-0905', 'finish_reason': 'stop', 'model_provider': 'baseten'} id='run--6f7d1db7-daae-4628-a40a-2ab7323e8f15-0'