跳到主要內容
Open In ColabOpen on GitHub

ChatOCIModelDeployment

這將幫助您開始使用 OCIModelDeployment 聊天模型。如需 ChatOCIModelDeployment 所有功能和配置的詳細文檔,請前往 API 參考

OCI Data Science 是一個完全託管且無伺服器的平台,供數據科學團隊在 Oracle Cloud Infrastructure 中構建、訓練和管理機器學習模型。您可以使用 AI Quick ActionsOCI Data Science Model Deployment Service 上輕鬆部署 LLM。您可以選擇使用流行的推論框架(如 vLLM 或 TGI)部署模型。默認情況下,模型部署端點模仿 OpenAI API 協議。

如需最新的更新、範例和實驗性功能,請參閱 ADS LangChain Integration

概述

整合詳情

類別套件本地可序列化JS 支援套件下載次數套件最新版本
ChatOCIModelDeploymentlangchain-communitybetaPyPI - DownloadsPyPI - Version

模型功能

Tool callingStructured outputJSON modeImage inputAudio inputVideo inputToken-level streamingNative asyncToken usageLogprobs
取決於取決於取決於取決於取決於取決於

某些模型功能,包括工具調用、結構化輸出、JSON 模式和多模態輸入,取決於部署的模型。

設定

要使用 ChatOCIModelDeployment,您需要部署一個具有聊天完成端點的聊天模型,並安裝 langchain-communitylangchain-openaioracle-ads 整合套件。

您可以使用 OCI Data Science Model deployment 上的 AI Quick Actions 輕鬆部署基礎模型。如需其他部署範例,請訪問 Oracle GitHub 範例儲存庫

政策

請確保您擁有訪問 OCI Data Science Model Deployment 端點所需的政策

憑證

您可以通過 Oracle ADS 設置身份驗證。當您在 OCI Data Science Notebook Session 中工作時,可以利用資源主體訪問其他 OCI 資源。

import ads

# Set authentication through ads
# Use resource principal are operating within a
# OCI service that has resource principal based
# authentication configured
ads.set_auth("resource_principal")

或者,您可以使用以下環境變量配置憑證。例如,使用帶有特定配置文件的 API 密鑰

import os

# Set authentication through environment variables
# Use API Key setup when you are working from a local
# workstation or on platform which does not support
# resource principals.
os.environ["OCI_IAM_TYPE"] = "api_key"
os.environ["OCI_CONFIG_PROFILE"] = "default"
os.environ["OCI_CONFIG_LOCATION"] = "~/.oci"

查看 Oracle ADS 文檔以了解更多選項。

安裝

LangChain OCIModelDeployment 整合存在於 langchain-community 套件中。以下命令將安裝 langchain-community 和所需的依賴項。

%pip install -qU langchain-community langchain-openai oracle-ads

實例化

您可以使用通用的 ChatOCIModelDeployment 或框架特定的類(如 ChatOCIModelDeploymentVLLM)來實例化模型。

  • 當您需要用於部署模型的通用入口點時,可以使用 ChatOCIModelDeployment。您可以在實例化此類時通過 model_kwargs 傳遞模型參數。這提供了靈活性和配置的便利性,而無需依賴於框架特定的細節。
from langchain_community.chat_models import ChatOCIModelDeployment

# Create an instance of OCI Model Deployment Endpoint
# Replace the endpoint uri with your own
# Using generic class as entry point, you will be able
# to pass model parameters through model_kwargs during
# instantiation.
chat = ChatOCIModelDeployment(
endpoint="https://modeldeployment.<region>.oci.customer-oci.com/<ocid>/predict",
streaming=True,
max_retries=1,
model_kwargs={
"temperature": 0.2,
"max_tokens": 512,
}, # other model params...
default_headers={
"route": "/v1/chat/completions",
# other request headers ...
},
)
  • 使用框架特定的類,如 ChatOCIModelDeploymentVLLM:當您使用特定框架(例如 vLLM)並需要通過構造函數直接傳遞模型參數時,這非常適合,從而簡化了設置過程。
from langchain_community.chat_models import ChatOCIModelDeploymentVLLM

# Create an instance of OCI Model Deployment Endpoint
# Replace the endpoint uri with your own
# Using framework specific class as entry point, you will
# be able to pass model parameters in constructor.
chat = ChatOCIModelDeploymentVLLM(
endpoint="https://modeldeployment.<region>.oci.customer-oci.com/<md_ocid>/predict",
)

調用

messages = [
(
"system",
"You are a helpful assistant that translates English to French. Translate the user sentence.",
),
("human", "I love programming."),
]

ai_msg = chat.invoke(messages)
ai_msg
AIMessage(content="J'adore programmer.", response_metadata={'token_usage': {'prompt_tokens': 44, 'total_tokens': 52, 'completion_tokens': 8}, 'model_name': 'odsc-llm', 'system_fingerprint': '', 'finish_reason': 'stop'}, id='run-ca145168-efa9-414c-9dd1-21d10766fdd3-0')
print(ai_msg.content)

J'adore programmer.

鏈接

from langchain_core.prompts import ChatPromptTemplate

prompt = ChatPromptTemplate.from_messages(
[
(
"system",
"You are a helpful assistant that translates {input_language} to {output_language}.",
),
("human", "{input}"),
]
)

chain = prompt | chat
chain.invoke(
{
"input_language": "English",
"output_language": "German",
"input": "I love programming.",
}
)
AIMessage(content='Ich liebe Programmierung.', response_metadata={'token_usage': {'prompt_tokens': 38, 'total_tokens': 48, 'completion_tokens': 10}, 'model_name': 'odsc-llm', 'system_fingerprint': '', 'finish_reason': 'stop'}, id='run-5dd936b0-b97e-490e-9869-2ad3dd524234-0')

異步調用

from langchain_community.chat_models import ChatOCIModelDeployment

system = "You are a helpful translator that translates {input_language} to {output_language}."
human = "{text}"
prompt = ChatPromptTemplate.from_messages([("system", system), ("human", human)])

chat = ChatOCIModelDeployment(
endpoint="https://modeldeployment.us-ashburn-1.oci.customer-oci.com/<ocid>/predict"
)
chain = prompt | chat

await chain.ainvoke(
{
"input_language": "English",
"output_language": "Chinese",
"text": "I love programming",
}
)
AIMessage(content='我喜欢编程', response_metadata={'token_usage': {'prompt_tokens': 37, 'total_tokens': 50, 'completion_tokens': 13}, 'model_name': 'odsc-llm', 'system_fingerprint': '', 'finish_reason': 'stop'}, id='run-a2dc9393-f269-41a4-b908-b1d8a92cf827-0')

串流調用

import os
import sys

from langchain_community.chat_models import ChatOCIModelDeployment
from langchain_core.prompts import ChatPromptTemplate

prompt = ChatPromptTemplate.from_messages(
[("human", "List out the 5 states in the United State.")]
)

chat = ChatOCIModelDeployment(
endpoint="https://modeldeployment.us-ashburn-1.oci.customer-oci.com/<ocid>/predict"
)

chain = prompt | chat

for chunk in chain.stream({}):
sys.stdout.write(chunk.content)
sys.stdout.flush()


1. California
2. Texas
3. Florida
4. New York
5. Illinois

結構化輸出

from langchain_community.chat_models import ChatOCIModelDeployment
from pydantic import BaseModel


class Joke(BaseModel):
"""A setup to a joke and the punchline."""

setup: str
punchline: str


chat = ChatOCIModelDeployment(
endpoint="https://modeldeployment.us-ashburn-1.oci.customer-oci.com/<ocid>/predict",
)
structured_llm = chat.with_structured_output(Joke, method="json_mode")
output = structured_llm.invoke(
"Tell me a joke about cats, respond in JSON with `setup` and `punchline` keys"
)

output.dict()
{'setup': 'Why did the cat get stuck in the tree?',
'punchline': 'Because it was chasing its tail!'}

API 參考

有關所有功能和配置的完整詳細信息,請參閱每個類別的 API 參考文檔


此頁面是否有幫助?