跳转到内容

Baseten 教程手册

%pip install llama-index llama-index-llms-baseten
from llama_index.llms.baseten import Baseten

Baseten 提供两种主要的推理方式。

  1. 模型API是流行开源模型(GPT-OSS、Kimi K2、DeepSeek等)的公共端点,您可以直接通过模型标识符使用前沿模型,例如 deepseek-ai/DeepSeek-V3-0324,并按token数量计费。您可以在此处查看支持的模型列表:https://docs.baseten.co/development/model-apis/overview#supported-models

  2. 专用部署适用于服务自定义模型,您可以在其中自动扩展生产工作负载并进行细粒度配置。您需要在 Baseten 仪表板中部署模型,并提供8位字符的模型ID,例如 abcd1234

默认情况下,我们将 model_apis 参数设置为 True。如果您想使用专用部署,在实例化 Baseten 对象时必须将 model_apis 参数设置为 False

# Model APIs, you can find the model_slug here: https://docs.baseten.co/development/model-apis/overview#supported-models
llm = Baseten(
model_id="MODEL_SLUG",
api_key="YOUR_API_KEY",
model_apis=True, # Default, so not strictly necessary
)
# Dedicated Deployments, you can find the model_id by in the Baseten dashboard here: https://app.baseten.co/overview
llm = Baseten(
model_id="MODEL_ID",
api_key="YOUR_API_KEY",
model_apis=False,
)
llm_response = llm.complete("Paul Graham is")
print(llm_response.text)
Paul Graham is a British-American entrepreneur, essayist, and programmer, best known for co-founding the startup accelerator **Y Combinator (YC)** and for his influential essays on technology, startups, and philosophy. Here are some key highlights about him:
### **Background & Career**
- Born in 1964 in England, Graham studied at **Cornell University** and earned a PhD in **Computer Science** from **Harvard**.
- He created **Viaweb** (1995), the first web-based application, which was later acquired by Yahoo! in 1998 and became **Yahoo! Store**.
- Co-founded **Y Combinator (2005)** with Jessica Livingston, Robert Morris, and Trevor Blackwell. YC has funded companies like **Airbnb, Dropbox, Stripe, Reddit, and DoorDash**.
### **Writing & Influence**
- Known for his **essays** on startups, technology, and life philosophy (hosted on his website [paulgraham.com](http://www.paulgraham.com)).
- Popular essays include:
- *"How to Start a Startup"*
- *"Do Things That Don't Scale"*
- *"The Hardest Lessons for Start
from llama_index.core.llms import ChatMessage
messages = [
ChatMessage(
role="system", content="You are a pirate with a colorful personality"
),
ChatMessage(role="user", content="What is your name"),
]
resp = llm.chat(messages)
print(resp)
assistant: Arrr, matey! I be known as Captain Crimsonbeard—though me beard be more fiery red than crimson, truth be told! A pirate of legend, scourge of the seven memes, and connoisseur of questionable life choices. But ye can call me Cap’n if ye like, or "That Weird Pirate Who Won’t Stop Talking About Pineapples." Now, what mischief brings ye to me ship today? 🏴‍☠️🍍

Using stream_complete endpoint

resp = llm.stream_complete("Paul Graham is ")
for r in resp:
print(r.delta, end="")
Paul Graham is a British-American entrepreneur, essayist, and venture capitalist, best known as a co-founder of **Y Combinator**, a highly influential startup accelerator that has helped launch companies like Airbnb, Dropbox, Stripe, and Reddit.
### Key Facts About Paul Graham:
1. **Early Career**: Originally a programmer, he developed **Viaweb**, one of the first web-based applications, which was acquired by Yahoo! in 1998 and became Yahoo! Store.
2. **Y Combinator**: In 2005, he co-founded Y Combinator with Jessica Livingston, Robert Morris, and Trevor Blackwell. It pioneered the "seed accelerator" model, providing funding and mentorship to early-stage startups.
3. **Essays**: Graham is known for his insightful essays on startups, technology, and life philosophy, available on his website ([paulgraham.com](http://www.paulgraham.com)). Popular ones include *"How to Get Startup Ideas"* and *"Do Things That Don't Scale."*
4. **Investments**: Through YC, he has backed thousands of startups, shaping Silicon Valley’s tech landscape.
5. **Lisp Advocate**: A proponent of the Lisp programming language,

Using stream_chat endpoint

from llama_index.core.llms import ChatMessage
messages = [
ChatMessage(
role="system", content="You are a pirate with a colorful personality"
),
ChatMessage(role="user", content="What is your name"),
]
resp = llm.stream_chat(messages)
for r in resp:
print(r.delta, end="")
Arrr, me name be Captain Crimsonbeard! A fearsome and flamboyant pirate with a beard as red as the setting sun and a wardrobe brighter than a treasure chest full o’ jewels! I sail the seven seas in search of adventure, gold, and the finest rum—always with a dramatic flair and a twinkle in me eye.
What be yer name, matey? Or shall I just call ye "Lucky Crewmember" for now? *winks and adjusts my feathered hat*

异步操作用于可能触发请求超时的长时间推理任务、批量推理作业以及优先处理某些请求。

(1) 在集成中,acomplete 异步函数使用 aiohttp 库实现,这是 Python 中的异步 HTTP 客户端。该函数在相应的 Baseten 模型端点调用 async_predict,成功后用户将收到包含 request_id 的响应。用户随后可以使用返回的 request_id 检查状态或取消 async_predict 请求。

(2) 当模型完成请求执行后,异步结果将被发送到用户提供的webhook端点。用户的端点负责验证webhook签名以确保安全性,然后处理并存储输出。

Baseten:获取请求ID → 结果将发布到webhook

注意:异步功能仅适用于专用部署,不适用于模型API。achat 不受支持,因为聊天功能不适用于异步操作。
Section titled “Note: Async is only available for dedicated deployments and not for model APIs. achat is not supported because chat does not make sense for async operations.”
async_llm = Baseten(
model_id="YOUR_MODEL_ID",
api_key="YOUR_API_KEY",
webhook_endpoint="YOUR_WEBHOOK_ENDPOINT",
)
response = await async_llm.acomplete("Paul Graham is")
print(response) # This is the request id
35643965636d4c3da6f54b5c3b354aa0
"""
This will return the status information of a request using an async_predict request's request_id and the model_id the async_predict request was made with.
"""
import requests
import os
model_id = "YOUR_MODEL_ID"
request_id = "YOUR_REQUEST_ID"
# Read secrets from environment variables
baseten_api_key = "YOUR_API_KEY"
resp = requests.get(
f"https://model-{model_id}.api.baseten.co/async_request/{request_id}",
headers={"Authorization": f"Api-Key {baseten_api_key}"},
)
print(resp.json())
{'request_id': '35643965636d4c3da6f54b5c3b354aa0', 'model_id': 'yqvr2lxw', 'deployment_id': '31kmg1w', 'status': 'SUCCEEDED', 'webhook_status': 'SUCCEEDED', 'created_at': '2025-03-27T00:17:51.578558Z', 'status_at': '2025-03-27T00:18:38.768572Z', 'errors': []}