跳到主要內容

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 整合

概觀

整合詳細資訊

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

模型功能

工具呼叫結構化輸出JSON 模式影像輸入音訊輸入視訊輸入Token 等級串流原生非同步Token 使用量Logprobs
dependsdependsdependsdependsdependsdepends

某些模型功能,包括工具呼叫、結構化輸出、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...
)
  • 使用架構特定類別(例如 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.",
}
)
API 參考:ChatPromptTemplate
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 參考文件


此頁面是否有幫助?