Skip to main content
Open In ColabOpen on GitHub

ChatOCI 模型部署

这将帮助您开始使用OCIModelDeployment 聊天模型。要查看所有ChatOCIModelDeployment功能和配置的详细文档,请访问API参考

OCI数据科学是Oracle云基础设施上的一个完全托管且无服务器平台,用于数据科学团队构建、训练和管理机器学习模型。您可以使用AI快速操作轻松将LLM部署到OCI数据科学模型部署服务中。您可以选择使用vLLM或TGI等流行的推理框架进行部署。默认情况下,模型部署端点模仿OpenAI API协议。

对于最新更新、示例和实验功能,请参见ADS LangChain 维护集成

概览

集成细节

Class本地序列化JS支持Package downloadsPackage 最新版本
ChatOCIModelDeploymentlangchain-communitybetaPyPI - DownloadsPyPI - Version

模型特性

工具调用结构化输出JSON 模式图像输入音频输入视频输入Token级流式传输原生异步Token 使用对数概率
dependsdependsdependsdependsdependsdepends

有些模型功能,包括工具调用、结构化输出、JSON模式和多模态输入,取决于已部署的模型。

设置

要使用ChatOCIModelDeployment,您需要部署一个带有聊天完成端点的聊天模型,并安装langchain-communitylangchain-openaioracle-ads集成包。

您可以使用OCI数据科学模型部署中的AI快速操作轻松部署基础模型。如需查看其他部署示例,请访问Oracle GitHub代码库

Policies

确保拥有访问OCI数据科学模型部署端点所需的策略

Credentials

您可以使用Oracle ADS进行身份验证。当您在OCI数据科学笔记本会话中工作时,可以利用资源主原则访问其他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")

Alternatively, you can configure the credentials using the following environment variables. For example, to use API key with specific profile:

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 文档 以获取更多选项。

安装

The LangChain OCIModelDeployment 整合部分位于 langchain-community 包中。以下命令将安装 langchain-community 及其所需的依赖项。

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

Instantiation

您可以使用通用的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",
)

Invocation

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')

Streaming calls

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参考文档: