开始使用 RunPod 聊天模型。
概述
本指南介绍如何使用 LangChain ChatRunPod 类与托管在 RunPod Serverless.
设置
- **安装软件包:**
pip install -qU langchain-runpod
- **部署聊天模型端点:** 请按照 RunPod 提供商指南 在 RunPod Serverless 上部署兼容的聊天模型端点并获取其端点 ID。
- **设置环境变量:** 确保
RUNPOD_API_KEY和RUNPOD_ENDPOINT_ID(或特定的RUNPOD_CHAT_ENDPOINT_ID)已设置。
# Make sure environment variables are set (or pass them directly to ChatRunPod)
if "RUNPOD_API_KEY" not in os.environ:
os.environ["RUNPOD_API_KEY"] = getpass.getpass("Enter your RunPod API Key: ")
if "RUNPOD_ENDPOINT_ID" not in os.environ:
os.environ["RUNPOD_ENDPOINT_ID"] = input(
"Enter your RunPod Endpoint ID (used if RUNPOD_CHAT_ENDPOINT_ID is not set): "
)
# Optionally use a different endpoint ID specifically for chat models
# if "RUNPOD_CHAT_ENDPOINT_ID" not in os.environ:
# os.environ["RUNPOD_CHAT_ENDPOINT_ID"] = input("Enter your RunPod Chat Endpoint ID (Optional): ")
chat_endpoint_id = os.environ.get(
"RUNPOD_CHAT_ENDPOINT_ID", os.environ.get("RUNPOD_ENDPOINT_ID")
)
if not chat_endpoint_id:
raise ValueError(
"No RunPod Endpoint ID found. Please set RUNPOD_ENDPOINT_ID or RUNPOD_CHAT_ENDPOINT_ID."
)
实例化
初始化 ChatRunPod 类。您可以通过 model_kwargs 传递模型特定参数并配置轮询行为。
from langchain_runpod import ChatRunPod
chat = ChatRunPod(
runpod_endpoint_id=chat_endpoint_id, # Specify the correct endpoint ID
model_kwargs={
"max_new_tokens": 512,
"temperature": 0.7,
"top_p": 0.9,
# Add other parameters supported by your endpoint handler
},
# Optional: Adjust polling
# poll_interval=0.2,
# max_polling_attempts=150
)
调用
使用标准的 LangChain .invoke() 和 .ainvoke() 方法调用模型。也支持通过 .stream() 和 .astream() (通过轮询 RunPod 模拟) /stream 端点实现)。
from langchain.messages import HumanMessage, SystemMessage
messages = [
SystemMessage(content="You are a helpful AI assistant."),
HumanMessage(content="What is the RunPod Serverless API flow?"),
]
# Invoke (Sync)
try:
response = chat.invoke(messages)
print("--- Sync Invoke Response ---")
print(response.content)
except Exception as e:
print(
f"Error invoking Chat Model: {e}. Ensure endpoint ID/API key are correct and endpoint is active/compatible."
)
# Stream (Sync, simulated via polling /stream)
print("\n--- Sync Stream Response ---")
try:
stream = chat.stream_events(messages, version="v3")
for token in stream.text:
print(token, end="", flush=True)
print() # Newline
except Exception as e:
print(
f"\nError streaming Chat Model: {e}. Ensure endpoint handler supports streaming output format."
)
### Async Usage
# AInvoke (Async)
try:
async_response = await chat.ainvoke(messages)
print("--- Async Invoke Response ---")
print(async_response.content)
except Exception as e:
print(f"Error invoking Chat Model asynchronously: {e}.")
# AStream (Async)
print("\n--- Async Stream Response ---")
try:
stream = await chat.astream_events(messages, version="v3")
async for token in stream.text:
print(token, end="", flush=True)
print() # Newline
except Exception as e:
print(
f"\nError streaming Chat Model asynchronously: {e}. Ensure endpoint handler supports streaming output format.\n"
)
链式调用
聊天模型可以与 LangChain 表达式语言(LCEL)链无缝集成。
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
prompt = ChatPromptTemplate.from_messages(
[
("system", "You are a helpful assistant."),
("human", "{input}"),
]
)
parser = StrOutputParser()
chain = prompt | chat | parser
try:
chain_response = chain.invoke(
{"input": "Explain the concept of serverless computing in simple terms."}
)
print("--- Chain Response ---")
print(chain_response)
except Exception as e:
print(f"Error running chain: {e}")
# Async chain
try:
async_chain_response = await chain.ainvoke(
{"input": "What are the benefits of using RunPod for AI/ML workloads?"}
)
print("--- Async Chain Response ---")
print(async_chain_response)
except Exception as e:
print(f"Error running async chain: {e}")
模型功能(取决于端点)
高级功能的可用性 **很大程度上** 取决于您的 RunPod 端点处理程序的具体实现。 ChatRunPod 集成提供了基本框架,但处理程序必须支持底层功能。
| 功能 | 集成支持 | 取决于端点? | 备注 |
|---|---|---|---|
| 工具调用 | ❌ | ✅ | 需要处理程序处理工具定义并返回工具调用(例如 OpenAI 格式)。集成需要解析逻辑。 |
| 结构化输出 | ❌ | ✅ | |
| ❌ | ✅ | 需要处理程序接受 json_mode 参数(或类似参数)并保证 JSON 输出。 | |
| ❌ | ✅ | 需要支持多模态的处理程序接受图像数据(例如 base64)。集成不支持多模态消息。 | |
| ❌ | ✅ | 需要处理程序接受音频数据。集成不支持音频消息。 | |
| ❌ | ✅ | 需要处理器接受视频数据。集成不支持视频消息。 | |
| ✅ (模拟) | ✅ | 轮询 /stream。需要处理器填充 stream 列表在状态响应中包含令牌块(例如, [{"output": "token"}])。真正的低延迟流式传输不是内置的。 | |
| ✅ | ✅ | 核心功能 ainvoke/astream 已实现。依赖于端点处理器的性能。 | |
| ❌ | ✅ | 需要处理器返回 prompt_tokens, completion_tokens 在最终响应中。集成目前不解析此内容。 | |
| ❌ | ✅ | 需要处理器返回对数概率。集成目前不解析此内容。 |
关键要点: 如果端点遵循基本的 RunPod API 约定,标准聊天调用和模拟流式传输可以正常工作。高级功能需要特定的处理器实现,并可能需要扩展或自定义此集成包。
API 参考
有关 ChatRunPod 类、参数和方法的详细文档,请参阅源代码或生成的 API 参考(如果有)。
源代码链接: https://github.com/runpod/langchain-runpod/blob/main/langchain_runpod/chat_models.py