Runpod
RunPod 提供了GPU云基础设施,包括针对部署和扩展AI模型优化的无服务器端点。
此指南介绍了如何使用langchain-runpod集成包将LangChain应用程序连接到托管在RunPod Serverless上的模型。
The integration offers interfaces for both standard Language Models (LLMs) and Chat Models.
安装
安装专用的合作伙伴包:
%pip install -qU langchain-runpod
设置
1. 在RunPod上部署一个端点
- 导航到您的RunPod 无服务器控制台。
- 创建一个"新端点",选择与您的模型和预期输入/输出格式兼容的适当GPU和模板(例如,vLLM、TGI、text-generation-webui)。请参阅组件指南或包README。
- 配置设置并部署。
- 关键是在部署后复制端点ID。
2. 设置API凭据
The integration needs your RunPod API Key and the Endpoint ID. Set them as environment variables for secure access:
import getpass
import os
os.environ["RUNPOD_API_KEY"] = getpass.getpass("Enter your RunPod API Key: ")
os.environ["RUNPOD_ENDPOINT_ID"] = input("Enter your RunPod Endpoint ID: ")
(可选) 如果使用不同的端点用于LLM和Chat模型,在初始化时可能需要设置 RUNPOD_CHAT_ENDPOINT_ID 或直接传递ID。
组件
此包提供了两个主要组件:
1. LLM
使用标准文本完成模型进行交互。
见RunPod LLM集成指南以获取详细的用法说明
from langchain_runpod import RunPod
# Example initialization (uses environment variables)
llm = RunPod(model_kwargs={"max_new_tokens": 100}) # Add generation params here
# Example Invocation
try:
response = llm.invoke("Write a short poem about the cloud.")
print(response)
except Exception as e:
print(
f"Error invoking LLM: {e}. Ensure endpoint ID and API key are correct and endpoint is active."
)
2. 聊天模型
与对话模型进行交互。
请参阅RunPod 聊天模型集成指南以获取详细用法和功能支持。
from langchain_core.messages import HumanMessage
from langchain_runpod import ChatRunPod
# Example initialization (uses environment variables)
chat = ChatRunPod(model_kwargs={"temperature": 0.8}) # Add generation params here
# Example Invocation
try:
response = chat.invoke(
[HumanMessage(content="Explain RunPod Serverless in one sentence.")]
)
print(response.content)
except Exception as e:
print(
f"Error invoking Chat Model: {e}. Ensure endpoint ID and API key are correct and endpoint is active."
)
API 参考:人类消息