NVIDIA
這將幫助您開始使用 NVIDIA 模型。如需所有 NVIDIA
功能和組態的詳細文件,請前往 API 參考。
概觀
langchain-nvidia-ai-endpoints
套件包含 LangChain 整合,用於建構在 NVIDIA NIM 推論微服務上使用模型的應用程式。這些模型經過 NVIDIA 最佳化,可在 NVIDIA 加速基礎架構上提供最佳效能,並部署為 NIM,這是一種易於使用的預建容器,可使用單一命令在 NVIDIA 加速基礎架構上隨處部署。
NIM 的 NVIDIA 託管部署可在 NVIDIA API 目錄上進行測試。測試後,可以使用 NVIDIA AI Enterprise 授權從 NVIDIA 的 API 目錄匯出 NIM,並在內部部署或雲端中執行,讓企業擁有其 IP 和 AI 應用程式的所有權和完全控制權。
NIM 以每個模型為基礎封裝為容器映像,並透過 NVIDIA NGC Catalog 作為 NGC 容器映像發佈。NIM 的核心是為在 AI 模型上執行推論提供簡單、一致且熟悉的 API。
此範例說明如何使用 LangChain 透過 NVIDIA
類別與 NVIDIA 支援互動。
如需有關透過此 API 存取 llm 模型的更多資訊,請查看 NVIDIA 文件。
整合詳細資訊
類別 | 套件 | 本機 | 可序列化 | JS 支援 | 套件下載 | 套件最新版本 |
---|---|---|---|---|---|---|
NVIDIA | langchain_nvidia_ai_endpoints | ✅ | beta | ❌ |
模型功能
JSON 模式 | 影像輸入 | 音訊輸入 | 視訊輸入 | Token 層級串流 | 原生非同步 | Token 使用量 | Logprobs |
---|---|---|---|---|---|---|---|
❌ | ✅ | ❌ | ❌ | ✅ | ❌ | ❌ | ❌ |
設定
開始使用
-
使用 NVIDIA 建立免費帳戶,其中託管 NVIDIA AI Foundation 模型。
-
按一下您選擇的模型。
-
在
Input
下方,選取Python
標籤,然後按一下Get API Key
。然後按一下Generate Key
。 -
複製產生的金鑰並儲存為
NVIDIA_API_KEY
。從這裡,您應該可以存取端點。
憑證
import getpass
import os
if not os.getenv("NVIDIA_API_KEY"):
# Note: the API key should start with "nvapi-"
os.environ["NVIDIA_API_KEY"] = getpass.getpass("Enter your NVIDIA API key: ")
安裝
LangChain NVIDIA AI Endpoints 整合位於 langchain_nvidia_ai_endpoints
套件中
%pip install --upgrade --quiet langchain-nvidia-ai-endpoints
例項化
請參閱 LLM 以取得完整功能。
from langchain_nvidia_ai_endpoints import NVIDIA
llm = NVIDIA().bind(max_tokens=256)
llm
調用
prompt = "# Function that does quicksort written in Rust without comments:"
print(llm.invoke(prompt))
串流、批次和非同步
這些模型原生支援串流,而且與所有 LangChain LLM 一樣,它們公開批次方法來處理並行請求,以及用於調用、串流和批次的非同步方法。以下是一些範例。
for chunk in llm.stream(prompt):
print(chunk, end="", flush=True)
llm.batch([prompt])
await llm.ainvoke(prompt)
async for chunk in llm.astream(prompt):
print(chunk, end="", flush=True)
await llm.abatch([prompt])
async for chunk in llm.astream_log(prompt):
print(chunk)
response = llm.invoke(
"X_train, y_train, X_test, y_test = train_test_split(X, y, test_size=0.1) #Train a logistic regression model, predict the labels on the test set and compute the accuracy score"
)
print(response)
支援的模型
查詢 available_models
仍會提供您的 API 憑證提供的所有其他模型。
NVIDIA.get_available_models()
# llm.get_available_models()
鏈結
我們可以像這樣使用提示範本鏈結我們的模型
from langchain_core.prompts import ChatPromptTemplate
prompt = ChatPromptTemplate(
[
(
"system",
"You are a helpful assistant that translates {input_language} to {output_language}.",
),
("human", "{input}"),
]
)
chain = prompt | llm
chain.invoke(
{
"input_language": "English",
"output_language": "German",
"input": "I love programming.",
}
)
API 參考
如需所有 NVIDIA
功能和組態的詳細文件,請前往 API 參考:https://langchain-python.dev.org.tw/api_reference/nvidia_ai_endpoints/llms/langchain_nvidia_ai_endpoints.llms.NVIDIA.html